B _փ@sddlZddlZddlZddlmZmZmZddlmZddl m Z ddl m Z ddl mZmZddlmZmZmZmZmZmZmZmZmZmZmZddlmZed Zd d Z d d Z!e"ddGdddeZ#dS)N) iteritemsiterkeysStringIO)xrange)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:/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/repn/plugins/mps.py_no_negative_zero#srcCs0|dkr dSt|rt|Stdt|dS)Nznon-fixed bound or weight: )rr ValueErrorstr)exprrr _get_bound)s rmpsz#Generate the corresponding MPS filec@s6eZdZddZddZddZddZdd d Zd S)ProblemWriter_mpscCst|tji|_d|_dS)Nz.17g)r__init__rr_referenced_variable_ids_precision_string)selfrrrr 4s zProblemWriter_mps.__init__cCsPt|}|dd}|dd}|dd}|dd}|dd} |dd} |d d} |d d} |d d} |d d}t|rtd dddt|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_constantskip_objective_sensez3ProblemWriter_mps passed unrecognized io_options: z css|]\}}d||fVqdS)z%s = %sNr).0kvrrr sz-ProblemWriter_mps.__call__..zaProblemWriter_mps: Using both the 'symbolic_solver_labels' and 'labeler' I/O options is forbiddenxz.mpsw)r&r)r+r,r$r-r'r.)dictpoplenrjoinrr r r!clearnameropen_print_model_MPS)r#modelZoutput_filenamesolver_capabilityZ io_optionsr$r%r&r'r(r)r+r,r-r.Zpgc output_file symbol_maprrr__call__LsR               zProblemWriter_mps.__call__c Cst|jdkrNx>t|j|jD],\}}||jt|<|||||fqWt|jdkrg}xTt|j|jD]B\}}|d|jt|d<|d|jt|d<|||fqpW|||f|j S)Nrr*) r7Z linear_coefszipZ linear_varsr!idappendZquadratic_coefsZquadratic_varsconstant) r# row_labelrepn column_dataZquadratic_datavariable_to_columnvardatacoef quad_termsrrr_extract_variable_coefficientss z0ProblemWriter_mps._extract_variable_coefficientsc Cst|drt|}n t|}t|dkr4dS|d|j}|d||||fd|jd}xv|D]n\} } t | } | dkrt d| | j |j f| j rt d|j | j f| |jt| <|||| | fqrWdS) N get_itemsrzSOS z S%d %s z %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.)hasattrlistrNitemsr7writelevelZ getSymbolr"rrr:fixed RuntimeErrorr!rC) r#r@r(variable_symbol_map soscondatar?Z sos_itemsrTZsos_template_stringrJZweightrrr _printSOSs2    zProblemWriter_mps._printSOSFr*Nc M s t} t}|j}tj}tj}tj}g}tj|dkrRtjB|dkrRtjBgg}x\|j ddD]J} |x:|j t dddD]$}| || ||| ||fqWqjW| || j}|j}dk r|jfddd td d t|Dd d tt|dD}g}g}g}|d|d|d|d|jfd}d}g}x|D]r}t|dd} t|dst|_|j}!xB|j tdddD]*}"|d7}| |"j|dkrtd|jd|f|| |"|}| |"d| s0|d|"r&|dn |d|d|d|| rbt|"j}#|#|!|"<n|!|"}#|#}$|$dkrt !dd} n|$dkrt"d|"j|||#||}%| s|%dkr|d  ||%fqWqjW|dkrtd!t#||dk s t$fd"d#}&dk rNt%|&jfd$dd fd%d&}'n|&}'x|'D]\}(}#|#}$|$dkr| rq\n|$dkrt"d'|(j|| |(|})|(j&r(t'|(j(t'|(j)kst$d(|)d)}*|| |(|*|d*|*||*|#||}+|(j(},t*|,|+},| |*t+|,fq\|(,r|(-rJd+|)d)}*n d,|)d)}*|| |(|*|d-|*||*|#||}+|(j(},t*|,|+},| |*t+|,fn|(-st$|(-r2|(,rd.|)d)}*n d/|)d)}*|| |(|*|d0|*||*|#||}+|(j)},t*|,|+},| |*t+|,fn|(,s\t$q\Wt|d dkrz|d1|d  d2| d2d3|j.d4}-|d5d}.x|D]}||}/|.d7}.t|/dkr|t/|}0xZt|/D]&\}1\}2}3||-|0|2t+|3fqWn&| r|t/|}0||-|0|dfqW|.t|dksFt$t|d dkr|d }/d6}0x2t|/D]&\}1\}2}3||-|0|2t+|3fqnWd7|j.d4}4|d8x,t|D] \}1\}2}5||4|2|5fqWt0}6|d9}7|d:}8xD]~}xv|j t1dddD]`}9|| |9||9j2}:|:dkr8|7rR|:dkrH|8rR|:dkr^td;|:|3| |||9|6qWqWd<|j.d4};|d=x|D]}| st/||j4kr|t/|}0|j5r|std>|j|jf|j'dkrtd?|d@|;|0t+t'|j'fqt+t*|j6}|- }?d}@|8r|s|dB|;|0| r|? r|dF|0nH|> s:|dG|;|0|t=|j4>}G| j}H| j?}I|j}Jx"|GD]}K|J|K}L|H|K=|I|L= qW~| S)RNr*T)activesortF)r[r\ descend_intocs|S)Nr)_x)r,rr>z4ProblemWriter_mps._print_model_MPS..)keycss|]\}}||fVqdS)Nr)r/irJrrrr2Bsz5ProblemWriter_mps._print_model_MPS..cSsg|]}gqSrr)r/rbrrr Dsz6ProblemWriter_mps._print_model_MPS..z* Source: Pyomo MPS Writer z* Format: Free MPS z* zNAME %s rZ _gen_obj_repn_repnzgMore than one active objective defined for input model '%s'; Cannot write legal MPS file Objectives: %s Z__default_objective__z OBJSENSE z MIN z MAX zROWS z N %s zTConstant objective detected, replacing with a placeholder to prevent solver failure.zWCannot write legal MPS file. Objective '%s' has nonlinear terms that are not quadratic.gzGCannot write legal MPS file: No objective defined for input model '%s'.c3sxD]}t|dd}t|ds(t|_|j}xp|jtdddD]Z}|sb|sb|jrBt qB|j rr| }n |rt |j }|||<n||}||fVqBWqWdS)NZ _gen_con_repnTrdF)r[r\r])getattrrPrrdcomponent_data_objectsr has_lbhas_ubequalityAssertionErrorZ_linear_canonical_formZcanonical_formrbody)blockZ gen_con_repn block_repnconstraint_datarG) all_blocks sortOrderrrconstraint_generators*        z@ProblemWriter_mps._print_model_MPS..constraint_generatorcs |dS)Nrr)r3)r+rrr_r`c3s xD]\}}||fVqWdS)Nr)rprG)sorted_constraint_listrryield_all_constraintsszAProblemWriter_mps._print_model_MPS..yield_all_constraintszXCannot write legal MPS file. Constraint '%s' has nonlinear terms that are not quadratic.Zc_e__z E %s Zr_l_Zc_l_z G %s Zr_u_Zc_u_z L %s z E c_e_ONE_VAR_CONSTANT )Zc_e_ONE_VAR_CONSTANTr*z %s %s %rOzCOLUMNS ZONE_VAR_CONSTANTz RHS %s %zRHS sos1sos2z0Solver does not support SOS level %s constraintsz%s %zBOUNDS 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 MPS file.z,Variable cannot be fixed to a value of None.z FX BOUND z BV BOUND %s z LI BOUND z LI BOUND %s -10E20 z UI BOUND z UI BOUND %s 10E20 z FR BOUND %s z LO BOUND z MI BOUND %s z UP BOUND zQUADOBJ cs$t|dd|ddfS)Nrr*)sorted)r^)rIrrr_scs|S)Nr)r^)rIrrr_r`zQCMATRIX %s cs$t|dd|ddfS)Nrr*)ry)r^)rIrrr_scs|S)Nr)r^)rIrrr_r`g?zENDATA )@r rMZ createSymbolZ createSymbolsaliasrZunsortedindicesZ alphabeticalZblock_data_objectsrDrhrZ addSymbolsZbyObjectr\r enumeraterr7rSr:rgrPrdrrr8Z is_minimizingrexprZpolynomial_degreeloggerwarningrVrrlrQrkrlowerupperrrrirjr"rCrrrTrYr!rUZlbZubZ is_binary is_integerZ is_continuousgetvaluerysetkeysZbySymbol)Mr#r=r?r>r(r&r)r+r,r$r-r'r.r@rWZextract_variable_coefficientsZcreate_symbol_funcZcreate_symbols_funcZalias_symbol_funcZvariable_label_pairsZ variable_listrnrJZobject_symbol_dictionaryZvariable_symbol_dictionaryrHZ quadobj_dataZquadmatrix_dataZrhs_dataZobjective_labelZnumObjZonamesZ gen_obj_repnroZobjective_datarGZdegreerErsrurpZ con_symbollabeloffsetboundZcolumn_templateZcntZ col_entriesZ var_labelrbrFrKZ rhs_templaterhsZSOSlinesrwrxrXrTZentry_templateZ vardata_lbZ vardata_ubZ unbounded_lbZ unbounded_ubZtreat_as_integerrLZtermZvar1Zvar2Z var1_labelZ var2_labelZvars_to_deleteZ sm_byObjectZ sm_bySymbolZvar_sm_byObjectZvaridsymbolr)rqr,r+rrrtrIrr<s                                                                                   z"ProblemWriter_mps._print_model_MPS)Fr*NNFFFF)__name__ __module__ __qualname__r rArMrYr<rrrrr1sg 1r)$loggingmathoperatorsixrrrZ six.movesrZ pyutilib.miscrZ pyomo.optrZpyomo.opt.baserr Zpyomo.core.baser r r r rrrrrrrZ pyomo.repnr getLoggerr~rrregisterrrrrrs   4