B u `@sddlZddlmZddlmZddlmZddlmZm Z ddl m Z m Z m Z mZmZmZmZmZmZmZmZddlmZedZd d Zd d Ze d de ddGdddeZdS)N) iteritems)PauseGC) ProblemFormat)AbstractProblemWriter WriterFactory) SymbolMap TextLabelerNumericLabeler ConstraintSortComponentsVarvalue SOSConstraint Objective ComponentMapis_fixed)generate_standard_repnz pyomo.corecCs|dkr dS|S)z6Make sure -0 is never output. Makes diff tests easier.r)valrr= %z z<= %%z <= z <= %)r__init__rr_referenced_variable_idsZ_precision_stringlinear_coef_string_templatequad_coef_string_templateobj_string_templatesos_template_stringeq_string_templategeq_string_templateleq_string_templatelb_string_templateub_string_template)selfrrrr!2s zProblemWriter_cpxlp.__init__cCsBt|}|dd}|dd}|dd}|dd}|dd} |dd} |d d} |d d} |d d} t|rtd d ddt|D|r| dk rtd|rt} n| dkrtd} |j |dkr|j d}t <}t |d&}|j |||| || | | || |d }WdQRXWdQRX|j ||fS)Nskip_trivial_constraintsFsymbolic_solver_labelsoutput_fixed_variable_boundsinclude_all_variable_boundslabelerfile_determinism row_order column_orderforce_objective_constantz5ProblemWriter_cpxlp passed unrecognized io_options: z css|]\}}d||fVqdS)z%s = %sNr).0kvrrr sz/ProblemWriter_cpxlp.__call__..zcProblemWriter_cpxlp: Using both the 'symbolic_solver_labels' and 'labeler' I/O options is forbiddenxz.lpw)r/r2r4r5r-r6r0)dictpoplenrjoinrrr r"clearnameropen_print_model_LP)r,modelZoutput_filenamesolver_capabilityZ io_optionsr-r.r/r0r1r2r4r5r6Zpgc output_file symbol_maprrr__call__QsN              zProblemWriter_cpxlp.__call__Fcs6|r |s t|j}|j} d} t|jdkrd} x|jD]} | |jt| <q6Wdkrfdd|jD} xtt| ddd D] \} }| ||j | |fq~WnLxJtt|jfd dd D],\} }t|}| ||j | |fqWt|j dkrd} x0|j D]&\}}||jt|<||jt|<q W| d dkrrt }g} d} x|j D]v\}}t|}t|}||kr| ||fn2||kr| ||fn| | | ||f| d 7} q`Wxtt| d dd D]z\} }|rd|j| }| | |n| | |j| | |krN| d|dn| d|d|d fqWnPt }g}d} x|j D]\}}|}|}||kr| ||ft|t|fnT||kr| ||ft|t|fn$| | | ||ft|f| d 7} qWxtt|ddd D]v\} }|rp| | d|j| n| | |j| | |kr| d|d n| d|d |dfqHW| d|r| dn | d| r|s| |d|r0|s|jdkr0| |j|jdf|jS)aW Return a expression as a string in LP format. Note that this function does not handle any differences in LP format interpretation by the solvers (e.g. CPlex vs GLPK). That decision is left up to the caller. required arguments: x: A Pyomo canonical expression to write in LP format TrFNcsg|]}t|qSr)id)r7var)variable_symbol_dictionaryrr sz=ProblemWriter_cpxlp._print_expr_canonical..cSs|dS)Nr3r)r;rrrz;ProblemWriter_cpxlp._print_expr_canonical..)keycs |dS)Nr3r)r;)r5rrrNrOz+ [ r3cSs|dS)Nr3r)r;rrrrNrOz%s ^ 2 z%s * %s cSs |ddS)Nr3rr)r;rrrrN rO]z / 2 r)rONE_VAR_CONSTANTgrS)AssertionErrorr#r$r?Z linear_varsr"rJsorted enumerateappendZ linear_coefsZquadratic_varssetaddZquadratic_coefsconstantr%)r,r;outputobject_symbol_dictionaryrLZ is_objectiver5r6r#r$rZvardatanamesirBrKZvar1Zvar2ZquadZname1Zname2Znames_tmpcolsZcol1Zcol2Zcols_r)r5rLr_print_expr_canonicals  $         $ & &       z)ProblemWriter_cpxlp._print_expr_canonicalc Cs|j}t|drt|}n t|}t|dkr:dS|j}|d||||fxv|D]n\} } t | } | dkrt d| | j |j f| j rt d|j | j f| |jt| <|||| | fq`WdS)zY Prints the SOS constraint associated with the _SOSConstraintData object get_itemsrNz %s: S%s:: zHCannot use negative weight %f for variable %s is special ordered set %s zSOSConstraint '%s' includes a fixed variable '%s'. This is currently not supported. Deactive this constraint in order to proceed.)r&hasattrlistrcitemsr?levelrWZ getSymbolrrrBfixed RuntimeErrorr"rJ) r,rHr1variable_symbol_map soscondatar[r&Z sos_itemsrgr]ZweightrrrprintSOSPs.    zProblemWriter_cpxlp.printSOSr3Nc @ s|j} |j} |j}|j}|j}tt}tjtj}tj}g}t j |dkrjt j B|dkrjt j Bgg}t |jddt |jtd}t fdd|D}||j}|j}|j}g}|d|jf|d }d }g}xlD]b}t|d d}t|d s t|_|j}x4|jtdd dD]} |d7}|| j|dkrxtd|jd|f| | d| r|dn |d|rt| j}!|!|| <n|| }!|! }"|"d krt!"dd} n8|"dkr|s,t#d| jn|"dkr,t#d| j||t$| d||!|||d|| d}#qt!"d-d,},|d.|,|d/|,|dg}-|d0}.|d1}/d }0xD]}x|jt1dd dD]x}1|1|1j2}2|2dkr|.r|2dkr|/r|2dkrtd2|2|0d kr|-d3d}0|3||1|-qWqW|d4g}3g}4x|D]~}5| sZt$|5|j4krZq:|t$|5}6|6d5krxtd6|55r|4|6n.|56r|3|6n|57st8d7|5j|5j9r@|std8|5j|jf|5j'dkrtd9t'|5j'}7t'|5j'}8|d:||t,|7||6||t,|8nxt+|5j:}7t+|5j;}8|d:|5-r|||t,|7n |d;||6|5.r||t,|8n |d<q:Wt/|3d kr|d=x|3D]}9|d>|9qWt/|4d kr*|d?x|4D]}9|d>|9qW|d,|-|d@|0d,|t<|j=t<|j4=}:j};j>}<|j}=x"|:D]}>|=|>}?|;|>=|<|?=qW~S)ANr3rQT)activesort)rnc3s|]}||fVqdS)Nr)r7r])create_symbol_funcr1rHrrr:sz6ProblemWriter_cpxlp._print_model_LP..z"\* Source Pyomo model name=%s *\ Zquadratic_objectiverZ _gen_obj_repn_repnF)rmrn descend_intozfMore than one active objective defined for input model '%s'; Cannot write legal LP file Objectives: %srZ__default_objective__zmin zmax zTConstant objective detected, replacing with a placeholder to prevent solver failure.zeSelected solver is unable to handle objective functions with quadratic terms. Objective at issue: %s.zWCannot write legal LP file. Objective '%s' has nonlinear terms that are not quadratic.z: )r6zIERROR: No objectives defined for input model. Cannot write legal LP file.rzs.t. Zquadratic_constraintc3sxD]}t|dd}t|ds(t|_|j}xp|jtdddD]Z}|sb|sb|jrBt qB|j rr| }n |rt |j }|||<n||}||fVqBWqWdS)NZ _gen_con_repnTrpF)rmrnrq)getattrrdrrpcomponent_data_objectsr has_lbhas_ubequalityrTZ_linear_canonical_formZcanonical_formrbody)blockZ gen_con_repn block_repnconstraint_datarepn) all_blocks sortOrderrrconstraint_generator9s*        zAProblemWriter_cpxlp._print_model_LP..constraint_generatorcs |dS)Nrr)r;)r4rrrNZrOz5ProblemWriter_cpxlp._print_model_LP..)rPc3s xD]\}}||fVqWdS)Nr)datar{)sorted_constraint_listrryield_all_constraints[szBProblemWriter_cpxlp._print_model_LP..yield_all_constraintszHSolver unable to handle quadratic expressions. Constraint at issue: '%s'zMCannot write legal LP file. Constraint '%s' has a body with nonlinear terms.zc_e_%s_zr_l_%s_zc_l_%s_zr_u_%s_zc_u_%s_iz>Empty constraint block written in LP format - solver may errorz%sc_e_ONE_VAR_CONSTANT: z%sONE_VAR_CONSTANT = 1.0 sos1sos2z0Solver does not support SOS level %s constraintszSOS zbounds ezAttempting to write variable with name 'e' in a CPLEX LP formatted file will cause a parse failure due to confusion with numeric values expressed in scientific notationz`Invalid domain type for variable with name '%s'. Variable is not continuous, integer, or binary.a$Encountered a fixed variable (%s) inside an active objective or constraint expression on model %s, which is usually indicative of a preprocessing error. Use the IO-option 'output_fixed_variable_bounds=True' to suppress this error and fix the variable by overwriting its bounds in the LP file.z,Variable cannot be fixed to a value of None.z z -inf <= z <= +inf zgeneral z %s zbinary zend )?r'r)r(r+r*rZ createSymbolZ createSymbolsaliasr ZunsortedindicesZ alphabeticalreZblock_data_objectsrsr Z addSymbolsZbyObjectrbrWrBrrrdrrprrr@Z is_minimizingrexprZpolynomial_degreeloggerwarningrirJrnrvr lowerupperrTrrrtrur?writerrgrlr"Z is_binary is_integerZ is_continuous TypeErrorrhZlbZubrXkeysZbySymbol)@r,rErGrFr1r/r2r4r5r-r6r0r'r)r(r+r*rjZcreate_symbols_funcZalias_symbol_funcZvariable_label_pairsZ variable_listr\rLZprint_expr_canonicalr[Zsupports_quadratic_objectiveZnumObjZonamesrxZ gen_obj_repnryZobjective_datar{ZdegreeoffsetZhave_nontrivialZsupports_quadratic_constraintr~rrzZ con_symbollabelboundprefixZSOSlinesrrZ writtenSOSrkrgZ integer_varsZ binary_varsr]Zname_to_outputZ vardata_lbZ vardata_ubvar_nameZvars_to_deleteZ sm_byObjectZ sm_bySymbolZvar_sm_byObjectZvaridsymbolr)r|ror1r4r}rrHrrDys                                                                                               z#ProblemWriter_cpxlp._print_model_LP)F)Fr3NNFFF)__name__ __module__ __qualname__r!rIrbrlrDrrrrr.sh .r)loggingsixrZpyomo.common.gc_managerrZ pyomo.optrZpyomo.opt.baserrZpyomo.core.baserrr r r r r rrrrZ pyomo.repnr getLoggerrrrregisterrrrrrs   4