B _#c@s.dZddlZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZmZmZddlmZddlm Z mZmZmZdd lmZdd lmZdd lmZmZdd lmZerdd l m!Z"n dd l#m!Z"e$dZ%ddZ&dddZ'GdddeZ(Gddde(Z)e*dGddde"eZ+dS))_LinearConstraintDataMatrixConstraint compile_block_linear_constraintsN)ref)Any)SortComponentsVar Constraint)is_fixedvalue ZeroConstant)ModelComponentFactory)r IndexedConstraintSimpleConstraint_ConstraintData)native_numeric_types)generate_standard_repn) iteritemsPY3)xrange)Mappingz pyomo.corecCsT|dkrt|dS|dkr,t|ddS|dkrDt|ddSt|ddS)Ng@@z Bg.Az KBgeAz MBz GB)str)xr:/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/repn/beta/matrix.py _label_bytes-s rFTc) s|rtdtd|j|s0td|jg}g}g}g} g} dd} t} |r`tdtjtjB} dd|jd | |d D}t}|rtd || t} |rtd gx"|D]} |j t | d dqWt ddt Dt}|rtd|| t} |r(tdg}g}g}t}t}d}d}dg}x|D]x}t|drl|`x^|jtd | d d D]F}t|trtt|dkr|||fqt|t}xtt|D]\}}|jjtks|jdkr|r|||fn|||f|||ft |j}|j!dksFtg}g}t|j"dkrj|rqn4fdd|j"D}|#||j$dk st|j$}||dt|| || ||t|7}|d7}| |j%}| |j&} t'|j(}!| |dk r||!nd| | dk r,| |!nd|dk rb| dk rb|j)sb| tj*tj+BnV|j)rx| tj,n@|dk r| dkst| tj*n| dk st| tj+tj-||<qWqWqVWt|}"t}|rtd|| t} |rtdd}#d}$x$|D]\}}|.||$d7}$qWx|D]\}}|#d7}#qDWx,|D]$\}}|.||#d7}#|$d7}$q`Wx2|D]*\}}t|dkr|.||$d7}$qWt}|rtd|#|$ftd|| t} |rtdt/|}%t ddt |%Dfdd|D}fdd|%D}&t}|rbtd || t} |rtd!td"|rd#nd$|rd%nd&}'t00d'|}t00d'|}t00|'|}t00|'| } t00d(| } t}|r|1d|j2|1d|j2|1d|j2| 1d| j2| 1d| j2}(td)td*t3|td+t3|"td,t3|td-t3t4|(td.|| |5|t||"||||| | |& dS)/Nz3Compiling linear constraints on block with name: %sz@Attempting to compile block '%s' with unconstructed component(s)cSs0|dkr dSt|rt|Stdt|dS)Nznon-fixed bound: )r r ValueErrorr)exprrr _get_boundTs z4compile_block_linear_constraints.._get_boundzSorting active blocks...cSsg|]}|qSrr).0_brrr `sz4compile_block_linear_constraints..T)activesort descend_intoz(Time to sort active blocks: %.2f secondsz(Collecting variables on active blocks...F)r$r%css|]\}}t||fVqdS)N)id)r indexvardatarrr ysz3compile_block_linear_constraints..z8Time to collect variables on active blocks: %.2f secondsz&Compiling active linear constraints...r_repncsg|]}t|qSr)r&)r r()VarIDToVarSymbolrrr"sz7Time to compile active linear constraints: %.2f secondsz'Removing compiled constraint objects...z=Eliminated %s constraints and %s Constraint container objectsz8Time to remove compiled constraint objects: %.2f secondsz$Assigning variable column indices...css|]\}}||fVqdS)Nr)r columnsymbolrrrr)scsg|] }|qSrr)r r/)VarSymbolToColumnIndexrrr"scsg|] }|qSrr)r Z var_symbol)VarSymbolToVarObjectrrr"sz4Time to assign variable column indices: %.2f secondsz7Converting compiled constraint data to array storage...z) - Using %s precision for numeric valuesZsingledoublefdLBzSparse Matrix Dimension:z - Rows: z - Cols: z - Nonzeros: zCompiled Data Storage: zGTime to convert compiled constraint data to array storage: %.2f seconds)6printnameZis_constructed RuntimeErrortimerindicesZ alphabeticalZblock_data_objectsextendZcomponent_data_objectsrdict enumeratesethasattrr*Zcomponent_objectsr isinstancerAssertionErrorlenappendrlistrbody __class__rZpolynomial_degreeaddrZnonlinear_exprZ linear_varsupdateZ linear_coefslowerupperr constantequality LowerBound UpperBoundEqualityZSkipZ del_componentsortedarray buffer_infoitemsizerrZ add_component))Z parent_blockZconstraint_nameZskip_trivial_constraintsZsingle_precision_storageverboser%ZSparseMat_pRowsZSparseMat_jColsZSparseMat_ValsZRangesZ RangeTypesr start_timeZ sortOrderZ all_blocksZ stop_timeblockZconstraint_data_to_removeZ%empty_constraint_containers_to_removeZconstraint_containers_to_removeZconstraint_containers_to_checkZreferenced_variable_symbolsnnznrows constraintZ singletonr'Zconstraint_dataZrepnZrow_variable_symbolsZrow_coefficientsr5UrLncolsZconstraints_removedZconstraint_containers_removedZColumnIndexToVarSymbolZColumnIndexToVarObjectZnumber_storageZ storage_bytesr)r,r0r1rr:sf                                        F rc@seZdZdZdZdddZdS)ra This class defines the data for a single linear constraint in canonical form. Constructor arguments: component The Constraint object that owns this data. Public class attributes: active A boolean that is true if this constraint is active in the model. body The Pyomo expression for this constraint lower The Pyomo expression for the lower bound upper The Pyomo expression for the upper bound equality A boolean that indicates whether this is an equality constraint strict_lower A boolean that indicates whether this constraint uses a strict lower bound strict_upper A boolean that indicates whether this constraint uses a strict upper bound variables A tuple of variables comprising the body of this constraint coefficients A tuple of coefficients matching the order of variables that comprise the body of this constraint constant A number representing the aggregation of any constant/fixed items found in the body of this constraint Private class attributes: _component The objective component. _active A boolean that indicates whether this data is active rNcCs |dk rt|nd|_d|_dS)NT) weakref_ref _component_active)selfr' componentrrr__init__rsz_LinearConstraintData.__init__)N)__name__ __module__ __qualname____doc__ __slots__rbrrrrrNs rcseZdZdZdZd)ddZfddZd*d d Zd d Zd dZ ddZ ddZ ddZ e ddZe ddZeZe ddZe ddZe ddZe dd Ze d!d"Ze d#d$Ze d%d&Zd'd(ZZS)+_LinearMatrixConstraintDataa  This class defines the data for a single linear constraint derived from a canonical form Ax=b constraint. Constructor arguments: component The Constraint object that owns this data. Public class attributes: active A boolean that is true if this constraint is active in the model. body The Pyomo expression for this constraint lower The Pyomo expression for the lower bound upper The Pyomo expression for the upper bound equality A boolean that indicates whether this is an equality constraint strict_lower A boolean that indicates whether this constraint uses a strict lower bound strict_upper A boolean that indicates whether this constraint uses a strict upper bound variables A tuple of variables comprising the body of this constraint coefficients A tuple of coefficients matching the order of variables that comprise the body of this constraint constant A number representing the aggregate of any constants found in the body of this constraint Private class attributes: _component The objective component. _active A boolean that indicates whether this data is active _indexNcCs2|dk rt|nd|_d|_|dks(t||_dS)NTr)r]r^r_rBri)r`r'rarrrrbs  z$_LinearMatrixConstraintData.__init__cstt|}|j|d<|S)zL This method must be defined because this class uses slots. ri)superrh __getstate__ri)r`result)rGrrrks z(_LinearMatrixConstraintData.__getstate__Tc sx|}|}|j}|j|j|jy,tfddt||||dDStt fk rr|rndSXdS)zC Compute the value of the body of this constraint. c3s$|]}||VqdS)Nr)r p)jcolsvalsvarmaprrr)sz7_LinearMatrixConstraintData.__call__..r+N) parent_componentr'_prows_jcols_varmap_valssumrr TypeError)r` exceptioncompr'prowsr)rnrorpr__call__sz$_LinearMatrixConstraintData.__call__cCs|j}|dk o|tdkS)zYReturns :const:`False` when the lower bound is :const:`None` or negative infinityNz-inf)rJfloat)r`Zlbrrrhas_lbsz"_LinearMatrixConstraintData.has_lbcCs|j}|dk o|tdkS)zYReturns :const:`False` when the upper bound is :const:`None` or positive infinityNinf)rKr|)r`Zubrrrhas_ubsz"_LinearMatrixConstraintData.has_ubcCs|j|dS)z Returns the value of L-f(x) for constraints of the form: L <= f(x) (<= U) (U >=) f(x) >= L N)rJ)r`rrrlslacksz"_LinearMatrixConstraintData.lslackcCs|j|dS)z Returns the value of U-f(x) for constraints of the form: (L <=) f(x) <= U U >= f(x) (>= L) N)rK)r`rrruslacksz"_LinearMatrixConstraintData.uslackcCs|jS)N)ri)r`rrrr'sz!_LinearMatrixConstraintData.indexcsp|}|}|j}|j|j||j||jdkr>dStfddt||j||jdD}|S)z4A tuple of variables comprising the constraint body.r+rc3s(|] }|js|VqdS)N)fixed)r rm)rnrprrr)sz8_LinearMatrixConstraintData.variables..)rqr'rrrsrtritupler)r`ryr'rz variablesr)rnrprrs z%_LinearMatrixConstraintData.variablescsx|}|}|j}|j|j|j||j||jdkrDdStfddt||j||jdD}|S)z6A tuple of coefficients associated with the variables.r+rc3s$|]}|js|VqdS)N)r)r rm)rnrorprrr)sz;_LinearMatrixConstraintData.coefficients..) rqr'rrrsrurtrirr)r`ryr'rzZcoefsr)rnrorpr coefficientssz(_LinearMatrixConstraintData.coefficientscs||}|}|j}|j|j|j||j||jdkrDdStfddt||j||jdD}t |S)z7The constant value associated with the constraint body.r+rc3s2|]*}|jr||VqdS)N)r)r rm)rnrorprrr).sz7_LinearMatrixConstraintData.constant..) rqr'rrrsrurtrirrrv)r`ryr'rzZtermsr)rnrorprrL#s z$_LinearMatrixConstraintData.constantcsp|}|}|j}|j|j|j||j||jdkrDtStfddt ||||dDS)z+Access the body of a constraint expression.r+c3s"|]}||VqdS)Nr)r rm)rnrorprrr)Dsz3_LinearMatrixConstraintData.body..) rqr'rrrsrtrurir rvr)r`ryr'rzr)rnrorprrF9sz _LinearMatrixConstraintData.bodycCs2|}|}|j|tj@r.|jd|SdS)z2Access the lower bound of a constraint expression.N)rqr' _range_typesrrN_ranges)r`ryr'rrrrJHs z!_LinearMatrixConstraintData.lowercCs6|}|}|j|tj@r2|jd|dSdS)z2Access the upper bound of a constraint expression.rr+N)rqr'rrrOr)r`ryr'rrrrKQs z!_LinearMatrixConstraintData.uppercCs|j|tj@tjkS)z.) logger isEnabledForloggingDEBUGdebugr8Z _constructedrhrrrCr_data)r`datar)rr`r constructs  zMatrixConstraint.constructcCs |j|S)N)r)r`keyrrr __getitem__szMatrixConstraint.__getitem__cCs |jS)N)r__len__)r`rrrrszMatrixConstraint.__len__cCstddtt|DS)Ncss|] }|VqdS)Nr)r rrrrr)sz,MatrixConstraint.__iter__..)iterrrC)r`rrr__iter__szMatrixConstraint.__iter__cCstdS)N)r)r`r'rrrrrHszMatrixConstraint.addcCstdS)N)r)r`rrr __delitem__szMatrixConstraint.__delitem__)N)rcrdrerrOrPrNrZNoBoundrbrrrrrHrrrrrrss  r)FFFT),__all__r:rrRweakrefrr]Zpyomo.core.base.set_typesrZpyomo.core.baserrr Zpyomo.core.base.numvaluer r r Zpyomo.core.base.pluginr Zpyomo.core.base.constraintrrrZpyomo.core.expr.numvaluerZ pyomo.repnrsixrrZ six.movesrcollections.abcrZcollections_Mapping collections getLoggerrrrrrhregisterrrrrr s>         /w