B _J @s$ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZmZddlmZmZmZddlmZddlmZmZdd lmZmZdd lmZdd l m!Z!dd l"m#Z#dd l$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+m,Z,ddl-m.Z.m/Z/m0Z0m1Z1m2Z2ddl"m3Z3m4Z4ddl5m6Z6ddl7m8Z8m9Z9ej:;e<Z=e>dZ?dZ@ee@ZAddZBddZCe dddddddd d!gZDd"d#ZEd=d%d&ZFd'd(ZGd)d*ZHd+d,ZId>d.d/ZJd?d0d1ZKd2d3ZLd@d4d5ZMd6d7ZNdAd8d9ZOePd:kr eOejQd;dd<dS)BN) namedtuple) ComponentMap) WriterFactory)value as_numeric)Block _BlockDataSortComponents) Objective)Var_VarData) Constraint_ConstraintData) SOSConstraint)generate_standard_repn)InvocationType) EmbeddedSPTableDistribution)locate_annotations$StochasticConstraintBoundsAnnotation"StochasticConstraintBodyAnnotationStochasticObjectiveAnnotation"StochasticVariableBoundsAnnotation)PySPConfigValuePySPConfigBlocksafe_register_common_optionsafe_register_unique_option_domain_must_be_str)ScenarioTreeManagerClientSerialScenarioTreeManagerClientPyro)launch_command) iteritems itervaluesz pyomo.pyspl` cCs(yt|Wntk r"YnXdS)z'Try to remove a file, ignoring failure.N)osremoveOSError)filenamer';/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/convert/smps.py_safe_remove_fileBsr)cCs|dkr dS|S)z6Make sure -0 is never output. Makes diff tests easier.rr')valr'r'r(_no_negative_zeroIsr+ ProblemStatsfirststage_variable_countsecondstage_variable_countfirststage_constraint_countsecondstage_constraint_countstochastic_cost_countstochastic_rhs_countstochastic_matrix_countZscenario_countcCs@|j}t|jdkst|jd}|jd}|} t|jdksFti} g| |j<g| |j<tjtjB} |j } t dd|j D} x.t |j D] \}}| | t||qWxt| D] }|qWx"|jdd| dD] }x&|jtddd D]}td |jqWt|d d}|dkr4td |jx|jtdd| dD]}| t|}| |}t|dksrtt||kr|}nP|}xJt|jD]6}|jst||kr|}Pnt||kstqW| |j||fqHWqWt| t|j|jgkstx$| D]}| |jt !dd qW| S)Nrcss|]}|gfVqdS)Nr').0symbolr'r'r( ysz(map_constraint_stages..T)active descend_intosortF)r9r:zGSOSConstraints are not allowed with this format. Invalid constraint: %s_repnz-Unable to find _repn ComponentMap on block %s)key)" _instancelenstagesAssertionError findRootNodenamer indicesZ alphabeticalbyObjectdictbySymbolr!aliasesidappendr"r;block_data_objectscomponent_data_objectsr TypeErrorgetattr ValueErrorr rZ_collect_variablesbodyvaluesfixedsortedkeysoperator itemgetter)scenario scenario_tree symbol_mapstochastic_constraint_idsfirststage_variable_idssecondstage_variable_idsreference_model firststage secondstagerootnodeStageToConstraintMapZ sortOrderrEZ reverse_aliasalias obj_weakref_aliasesblockconZ block_repnr7rHZconstraint_stagevarr=r'r'r(map_constraint_stagesYsn               rhFcsV|j}ii|}t|jdks(t|jd}|jd}|jd}|j|ksTt|j} |j} |j } |j j } |j } x|j t ddD]}t|| krq| t|}| t|d}|| kr||f|<q|r|| kr||f|<q|| ks|| ks|dkr ||f|<qdstqWi}fdd tD||j<fd d tD||j<|S) Nr4rr5T)r:Fcs&g|]}||d|dfqS)rr5r')r6r7)FirstStageVarsr'r( sz'map_variable_stages..cs&g|]}||d|dfqS)rr5r')r6r7)SecondStageVarsr'r(rks)r>rBr?r@rAZ node_listZstageZ_standard_variable_idsZ_derived_variable_ids _variable_ids_ScenarioTreeSymbolMaprErLr rIgetrSrC)rWrXrY!enforce_derived_nonanticipativityr]r`r^r_Zsecondstage_nodeZfirststage_standard_variablesZfirststage_derived_variablesZsecondstage_variablesZscenariotree_byObjectZsymbolmap_byObjectrgr7scenario_tree_idStageToVariableMapr')rjrlr(map_variable_stagessJ          rscCsi}x|jdddD]}d|_d|_t|t|<|_x0|jtdddD]}t|j dd}||j|<qJWxD|jt dddD]0}|j r|j dd}nt|j dd}||j|<q|WqW|S)zCompiles expressions in a way that reduces the chance of throwing out 0*var terms. Also activate flags that disable regeneration by a solver plugin.T)r9r:F)compute_values)rK _gen_obj_repn _gen_con_repnrrIr<rLr rexprr Z_linear_canonical_formZcanonical_formrP)model repn_cachereobjective_objectrepnconstraint_datar'r'r( build_repnss0   r}c Os|j}g}xp|jdddD]^}i}t|dr:|j|d<|`t|drR|j|d<|`t|drj|j|d<|`|||fqWz6yt||f||St d|j YnXWdxX|D]P\}}t|dr|`t|dr|`t|dr|`x|D]}t ||||qWqWXdS)NT)r9r:rurvr<z;Failed to complete partial SMPS conversion for scenario: %s) r>rKhasattrrurvr<rJ'_convert_external_setup_without_cleanuploggererrorrCsetattr) workerrWargskwdsr] cached_attrsreblock_cached_attrsrCr'r'r(_convert_external_setups@             rce sddl}tj|st|dks$tt|}|j}|j} |} |j d} |j d} ii} i}t dd| j t dddD}t | tddtdkrdi}d }n|tdkstddjrd }}|jfd d d t|dkr"td tjnd}tfdd|D}t | tddtdkrLdi}d }n~tdks^tddjrd }}|jfdd d t|dkrtd tjnd}tfdd|D}t}|dd|D|dd|Dt | tdd}t|dkrd}nt|dks,t|dd}t | t}t|dkr^tdtjdkrdkr|dkrtdtjtjtjft| drtt| }t| dst| jrt| jrtxV| D]J}xB| D]6}t!|j"|_"dd|j#D|_#dd|j$D|_$qWqWt%|f}tj&||d|d|j'}d|ksdtd|ksrt|| |dd |\}}||kstWdQRXt(||||d}tdd|| j'D}td d|| j'D} t)|||||| td!d| j'D}!t|j d"kst|j d} |j d} |j*}"|"dk sFt|"+}#|t,|#|"}$t-x| j'D]0\}+}7x$|+D]}&|:8|&d,|8d7}8qWqWx>| j'D]0\}+}7x$|+D]}&|:8|&d,|9d7}9qWqW|:8d/|9d7}9WdQRXt/tj&||d0|j'd(};|;8d1||d2k rp|;8d3|;8d4|| j'dd|j5t,|"f| j'dd}+t|+dkr|+d9d5s|+d9d6s|+d9d7st|+d}|;8d?|;8d@dA}=|;8dB|;8|=|j5t,|"dCfx:| j'D],\}+}7x |+D]}&|;8|=|&dCf qW qWx:| j'D],\}+}7x |+D]}&|;8|=|&dDf qW qW|;8|=dE|;8dFx,|| j'D]\}&}7}7|;8|=|&dCf qbWx,|| j'D]\}&}7}7|;8|=|&dDf qW|;8|=dG|;8dHWdQRXt}>d}?d}@d}Ad}Bd}Cd}Dt-}Et-}Fd}Gd}Hd}It/tj&||dI|j'd(}Jt/tj&||dJ|j'd(}K|j:}L|K8dKt;|LdL}Mdk r4x|D]\},}Nt<|,t= st| st,|,|!k rtdM|,j'tjf|t,|,+|,}O|O> stdNt?j'|Oj"}Pd|O_"|Pdk rdO}P|3|,}+t|+dk stx |+D]}Q|Q9d5 sD|Q9d6 r|Ndk s`|Nddk s`t|Gd7}G|K8|M|Qt;t!|,j@t!|Pf|J8dP|Q|,j@|E|,<tA|,_B|Q9d5r(|,jC|F|,<tA|,_D q&|Q9d8 r:|Ndk s|Nddkr(|Gd7}G|K8|M|Qt;t!|,j@t!|Pf|J8dP|Q|,j@|E|,<tA|,_Bn|Q9d7 r|Ndk sb|Nddk sbt|Gd7}G|K8|M|Qt;t!|,jCt!|Pf|J8dP|Q|,jC|F|,<tA|,_Dnz|Q9d9r|Ndk s|Nddkr(|Gd7}G|K8|M|Qt;t!|,jCt!|Pf|J8dP|Q|,jC|F|,<tA|,_Dn d s&t q&W q|WdQ}Rdk rx|D]\},}St<|,t=sdt|st,|,|!krtdM|,j'tjf|t,|,+|,}O|O>stdNt?j't|OjEdkst|Sdkr|OjE}St|Sdkst|3|,}+t |S}S|SjfdRd d t |Oj#}Tx|SD]}'t<|'tFs6t|'jGrBtd}UxstdU|"j'|]dkr|$jE}]|j5t,|"}_t |]}]|]jfdVd d t|]dks|^stt |$j#}Tx|]D]}'t<|'tFstd}Ux.T)r9r:)Z max_allowedFcs|ddS)NrT)getname)x)constraint_name_bufferr'r(yz9_convert_external_setup_without_cleanup..)r=zxThe %s annotation was declared with explicit entries but no active Constraint objects were recovered from those entries.c3s|]}|jfVqdS)N)default)r6rf)stochastic_rhsr'r(r8scs|ddS)NrT)r)r)rr'r(rrc3s|]}|jfVqdS)N)r)r6rf)stochastic_matrixr'r(r8scss|]\}}t|VqdS)N)rI)r6rf_r'r'r(r8scss|]\}}t|VqdS)N)rI)r6rfrr'r'r(r8szbThe SMPS writer does not currently support stochastic variable bounds. Invalid annotation type: %sz{No stochastic annotations found. SMPS conversion requires at least one of the following annotation types: - %s - %s - %sr<cSsg|] }t|qSr')r)r6cr'r'r(rksz;_convert_external_setup_without_cleanup..cSsg|] }t|qSr')r)r6rr'r'r(rksz.setup.. column_order row_ordercSsdS)NTr')rr'r'r(rr)rpcss|]\}}}t|VqdS)N)rI)r6r7rgrqr'r'r(r8scss|]\}}}t|VqdS)N)rI)r6r7rgrqr'r'r(r8scss|]\}}t|VqdS)N)rI)r6symbolsrfr'r'r(r8sr4RHSzCongratulations! You have hit an edge case. The SMPS input format forbids variables from using the symbol 'RHS'. Please rename it or use a different symbol in the output file.z .symbols.force_objective_constantcSsdS)NTr')rr'r'r(rBrwz%s %s c3s(|] }|D]\}}||fVqqdS)Nr')r6Z stage_namerrf)rar'r(r8dsz.col. zONE_VAR_CONSTANT z.row.c_e_ONE_VAR_CONSTANTz.tim.zTIME %s rzPERIODS IMPLICIT z %s %s TIME1 c_e_c_l_c_u_r_l_r_u_z %s z ONE_VAR_CONSTANT z %s TIME2 rzPERIODS EXPLICIT z TIME1 z TIME2 z %s %s zROWS TIME1TIME2)rrzCOLS )ONE_VAR_CONSTANTrzENDATA z .sto.struct.z.sto.z BL BLOCK1 PERIOD2 %.17g z RHS %s %.17g zThe constraint %s has been declared in the %s annotation but it was not identified as a second-stage constraint. To correct this issue, remove the constraint from this annotation.z`Only linear constraints are accepted for conversion to SMPS format. Constraint %s is not linear.gzRHS %s z %s %s %.17g cs|S)Nr')_v)rr'r(rrzThe coefficient for variable %s has been marked as stochastic in constraint %s using the %s annotation, but the variable does not appear in the canonical constraint expression.zwThe %s annotation was declared with explicit entries but no active Objective objects were recovered from those entries.ziOnly linear stochastic objectives are accepted for conversion to SMPS format. Objective %s is not linear.cs|S)Nr')r)rr'r(rrzThe coefficient for variable %s has been marked as stochastic in objective %s using the %s annotation, but the variable does not appear in the canonical objective expression.rZZeroStochasticDataz.det.cSsdS)NTr')rr'r'r(rr)N pyomo.environr#pathexistsrArFrXr>rBr@listrLr rrr?Zhas_declarationsZexpand_entriesr; RuntimeError__name__tuplersetupdaterrrOr~r}rurvrQrconstant linear_coefsquadratic_coefsrjoinrCrsrhZ_instance_objectiveZ parent_blockrIr enumerateopenrnrSrmrGZis_expression_typerErJ writelineswrite startswithZ probabilityr+ isinstancer is_linearr|lower_deterministic_check_constantZ_lowerupperZ_upper linear_varsr rRzip_deterministic_check_valuer_namer!r)errWoutput_directorybasenameZ file_formatrp io_optionspyomorXr]r`r^r_objective_name_buffervariable_name_bufferZall_constraintsZstochastic_rhs_entriesZempty_rhs_annotationZstochastic_matrix_entriesZempty_matrix_annotationrZZstochastic_objectiveZstochastic_varboundsry block_repnsr{writeroutput_filename output_fnamerYrrr[r\Zsecondstage_constraint_idsrzZobjective_blockobjective_repnZ column_indexr7rgrqrZ row_indexrrfsymbols_filenamefZ st_symbol_maplinesid_lp_labelconstraint_symbolsr-r.Zf_colrr/r0Zf_rowf_timstage2_row_start line_templateZstochastic_lp_labelsZstochastic_constraint_countZ stochastic_secondstage_rhs_countZ)stochastic_firststagevar_constraint_countZ*stochastic_secondstagevar_constraint_countZ(stochastic_firststagevar_objective_countZ)stochastic_secondstagevar_objective_countZmodified_constraint_lbZmodified_constraint_ubr2r3r1Zf_coordsf_stoZscenario_probabilityZ rhs_templateZ include_boundconstraint_repnZ body_constantZ con_labelZmatrix_templateZvar_listZ new_coefsZvar_coefiZ_varZcoefZ var_labelZ obj_templateZ sorted_valuesobjZobjective_variablesZinclude_constantstochastic_objective_labelZ obj_constantZreference_model_nameZdet_output_filenamerrr')rarrrrr(rKs                                                                                                                   rrc - Cs ddl} ddl} |dkri}tj|s2t||j} t| jdkrNt d| r^t dtj |d} tj| st | |j dttj| ||||fd} | jd}|j}| |\}}}}}}}i}tj ||d}t|ttj | |d |d ||||d <tj ||d }t|ttj | |d |d ||||d <tj ||d}t|ttj | |d||||d<tj ||d}t|ttj | |d||tj ||d}t|ttj | |d||tj ||d}t|ttj | |d||tj ||d |d}t|ttj | |d |d||tj ||d}t|t|d} | d|d| dx\| jD]R}!tj | |d|!j}"tj|"stt|"d}#t|#| WdQRXqW| d WdQRX||d!<| rtd"td#tj|td$td%td&|td'|td(td&|td'|td)|td*|td+td,||s| rtd-| td.d/}$ytd0sd1}$nd/}$Wnd/}$YnX| r:td2x| jD]}!tj | |d|!j}%|$r|td3|%d4|}&ntj|%|d/d5 }&|&rt d6||!j|%ftj | |d|!j}'|$rtd3|'d4|}&ntj|'|d/d5 }&|&rBt d7||!j|'fqBW| rtd8xr| jD]h}!tj | |d|!j}(|$r`td3|(d4|}&ntj|(|d/d5 }&|&r&t d9||!j|(fq&W| rtd:xr| jD]h}!tj | |d|!j})|$rtd3|)d4|}&ntj|)|d/d5 }&|&rt d;||!j|)fqW| r"td<xz| jD]p}!tj | |d |d|!j}*|$rltd3|*d4|}&ntj|*|d/d5 }&|&r*t d=||!j|*fq*W|st|t|t|t||s| rtd>x| jD]}!tj | |d|!j}%tj|%stt|%tj | |d|!j}'tj|'sDtt|'tj | |d|!j}(tj|(svtt|(tj | |d|!j})tj|)stt|)tj | |d|!j}"tj|"stt|"tj | |d |d|!j}*tj|*stt|*tj | |d?|d |!j}+tj|+sNtt|+tj | |d |d |!j},tj|,stt|,tj | |d |d |!j},tj|,stt|,qWtt| dk rtj | d1d@n| rtdA|S)BNrr4z;SMPS conversion does not yet handle more than 2 time-stagesz+SMPS conversion does not yet handle bundlesZscenario_filesr)Zinvocation_typeZ function_argsz.corrcorez .cor.symbolsz .symbols.rz.timz.tim.timez.rowz.row.z.colz.col.z .sto.structz .sto.struct.z.detz.det.z.storzSTOCH rzBLOCKS DISCRETE REPLACE z.sto.rzENDATA stoz SMPS Conversion CompletezOutput Saved To: zBasic Problem Information:z - Variables:z - First Stage: %dz - Second Stage: %dz - Constraints:z - Stoch. RHS Entries: %dz - Stoch. Matrix Entries: %dz - Objective:z - Stoch. Cost Entries: %dzS Starting scenario structure consistency checks across scenario files stored in %s.zThis may take some time. If this test is prohibitively slow or can not be executed on your system, disable it by activating the disable_consistency_check option.Fzdiff --help > /dev/nullTz& - Checking row and column ordering...zdiff -q  )ZshallowaThe row ordering indicated in file '%s' does not match that for scenario %s indicated in file '%s'. This suggests that one or more locations of stochastic data have not been annotated. If you feel this message is in error, please report this issue to the PySP developers.a5The column ordering indicated in file '%s' does not match that for scenario %s indicated in file '%s'. This suggests that the set of variables on the model changes across scenarios. This is not allowed by the SMPS format. If you feel this is a developer error, please report this issue to the PySP developers.z) - Checking time-stage classifications...akMain .tim file '%s' does not match .tim file for scenario %s located at '%s'. This indicates there was a problem translating the reference model to SMPS format. Please make sure the problem structure is identical over all scenarios (e.g., no. of variables, no. of constraints), or report this issue to the PySP developers if you feel that it is a developer error.z6 - Checking sparse locations of stochastic elements...aHThe structure of stochastic entries indicated in file '%s' does not match that for scenario %s indicated in file '%s'. This suggests that the set of variables appearing in some expression declared as stochastic is changing across scenarios. If you feel this is a developer error, please report this issue to the PySP developers.z> - Checking deterministic sections in the core problem file...aYOne or more deterministic parts of the problem found in file '%s' do not match those for scenario %s found in file %s. This suggests that one or more locations of stochastic data have not been been annotated on the reference Pyomo model. If this seems like a tolerance issue or a developer error, please report this issue to the PySP developers.z%Cleaning temporary per-scenario filesz.setup.) ignore_errorszHTemporary per-scenario files are retained in scenario_files subdirectory)!rZ%pyomo.solvers.plugins.smanager.phpyror#rrmakedirsrXr?r@rOZcontains_bundlesrmkdirZinvoke_functionthisfilerZ PerScenarioZ scenariosrCr)shutilcopy2rrrA copyfileobjprintrelpathsystemfilecmpcmplistdirrmtree)-rrscenario_tree_manager core_formatrprdisable_consistency_checkskeep_scenario_fileskeep_auxiliary_filesverboserrXZscenario_directorycountsZreference_scenarioZreference_scenario_namer-r.r/r0r1r2r3 input_filesZ core_filenamer tim_filenameZcore_row_filenameZcore_col_filenameZsto_struct_filenameZcore_det_filename sto_filenamefdstrWZscenario_sto_filenamefsrcZhas_diffZscenario_core_row_filenamercZscenario_core_col_filenameZscenario_tim_filenameZscenario_sto_struct_filenameZscenario_core_det_filenameZscenario_core_setup_filenameZscenario_core_filenamer'r'r(convert_externals                                          rc Csz|dkr i}ddl}tj|s*t||dks6tt|}t|jdkrTt d|j rbt d|j }g}xp|j dddD]^} i} t | dr| j| d<| `t | d r| j| d <| `t | d r| j| d <| `|| | fq|Wz0yt||||||Std YnXWdxb|D]Z\} } t | dr.| `t | d r>| `t | d rN| `x| D]} t| | | | qTWqWXdS) Nr)rrr4z;SMPS conversion does not yet handle more than 2 time-stageszcProblems with stochastic variables bounds can not be converted into an embedded SMPS representationT)r9r:rurvr<z+Failed to complete embedded SMPS conversion)rr#rrrrArFr? time_stagesrOZhas_stochastic_variable_boundsr]rKr~rurvr<rJ_convert_embeddedrrr) rrsprrrprr]rrerrCr'r'r(convert_embeddeds\                 rcJ sj}g}t}g} t} tjdks,ttjtjksDtjd} jd} xPj| D]B\} }|rt|r|| |t | qd| | | t | qdWx.j| D] \} }| | | t | qWi|j fdddi| j fdddt|t|ks tt| t| ks6tg}t}g}t}xt|j t ddd D]`}j || d }|| kr|||t |n&|| kst|||t |q\Wi|j fd ddi|j fd ddtxt|D]\}} || <qWx$t| tD]\}} || <q&Wtxt|D]\}} || <qLWx$t|tD]\}} || <qrWt}x jD]}|j||<d|_qWt|d rtt|}t|d st|jrt|jrti}xj|D]^}xV|D]J}|j|j|jf||<t|j|_dd|jD|_dd|jD|_q WqWi}tj||d}||d<tj||d}||d<t|} d|kstd|kst|d<|d<d|d<| ||dd|\}!|!|kstt|d\}"g}#xBj| D]4\} }$j | }%j!t | }&|#d|&|%fqW|""t|#WdQRXWdQRXx"|D]}||\|_|_|_qvW~x|#D]\}}'|'|_qW~t}(t$ddj%D})x2t&j'D]$\}*}+|)j!t |+|*qWxt(|)D]},|, qWx4t)*||D]$}j!t |}-|)|-}.|.|(|<q.Wtj||d}/|/|d<t|/d}0|0+d||d kr|0+d!|0+d"j!t |dj!t j,f|(|d}1t|1dkr$|1d-d#s|1d-d$s|1d-d%st|1d}2n4t|1}1|1d-d&sP|1d-d'sPt|1d}2t | dj!kr|0+d(j!t | dn |0+d)|0+d*|2nb|d+kst|0+d,|0+d-|0+d.d/}3|0+d0|0+|3j!t j,d1fx4|D],}x$|(|D]}-|0+|3|-d1fqWqWx4|D],}x$|(|D]}-|0+|3|-d2fqHWq:W|0+|3d3|0+d4x:|D]2} t | }4|4j!kr|0+|3j!|4d1fqWx:| D]2} t | }4|4j!kr|0+|3j!|4d2fqW|0+|3d5|0+d6WdQRXtj||d7}5|5|d8<t}6d9}3t|5d}7|7+d:|d;|7+d<i}8i}9i}:tj.dk rtj.dkstt/j.0dj,kstt1j,j2d=d>};|;3st4d?j,j5j,j2ft/t6|;j7|;j}<|t/8|>}?fdBd|?D}@xH|@D]@}A|A|6k rt4dCj,j5|Aj5j,j5|6|Aj5fn j,|6|A< qxWt|@dk r|@d}|>|k rt4dDj,j5t9| |j5|>fj|}Bt:|Bt;k r(t4dE|Bj| t? sH| dAk sBt| }Cnj!t | }Ct|Bjdk sjtt|Bj@t|Bjk stxlt6|Bj@|BjD](\}D}E|7+|3|C|=tA|EtA|Df qWn0t|@dk rDt4dFj,j5| j5dGd|@Df qDWt/jB0}F|Fj fdHddx|FD]}t1|jCd=d>}G|G3 sTt4d?|j5|jCft/t6|Gj7|Gj}H|Hj fdIddfdJd|HD}H|Gjdk rx:8|GjD]&}A|Ajk rt4dK|j5|Aj5f qW|(|}1t|1dk rt4dL|j5t|1dk s t|1d}I|I-d# s*|I-d$ r>|HdMdM|jDfn|I-d% r\|HdMdM|jEfx|HD]x\} }C}>t/8|>}?fdNd|?D}@xB|@D]:}A|A|6k rt4dC|j5|Aj5|j5|6|Aj5fn||6|A< qWt|@dk r|@d}|>|k rt4dD|j5t9| |j5|>fj|}Bt:|Bt;k rBt4dE|Bj.)r=cs |dS)NT)r)r)rr'r(rrT)r9r:)Zderived_last_stagecs |dS)NT)r)r)rr'r(r0rcs |dS)NT)r)r)rr'r(r2rr<cSsg|] }t|qSr')r)r6rr'r'r(rkbsz%_convert_embedded..cSsg|] }t|qSr')r)r6rr'r'r(rkcsz.corZcorz .cor.symbolsrrrrcSsdS)NTr')rr'r'r(rwrrz%s %s css|]}|gfVqdS)Nr')r6r7r'r'r(r8sz$_convert_embedded..z.timZtimzTIME %s rzPERIODS IMPLICIT z %s %s TIME1 rrrrrz %s z ONE_VAR_CONSTANT z %s TIME2 rzPERIODS EXPLICIT z TIME1 z TIME2 z %s %s zROWS rr)rrzCOLS )rrzENDATA z.storz %s %s %.17g %.17g zSTOCH rzINDEP DISCRETE F)rtzCannot output embedded SP representation for component '%s'. The embedded SMPS writer does not yet handle stochastic nonlinear expressions. Invalid expression: %scs |dS)Nrr')r)rr'r(rrrcsg|]}|jkr|qSr')stochastic_data)r6p)rr'r(rk"saCannot output embedded SP representation for component '%s'. The embedded SMPS writer does not yet handle the case where a stochastic data component appears in multiple expressions or locations within a single expression (e.g., multiple constraints, or multiple variable coefficients within a constraint). The parameter '%s' appearing in component '%s' was previously encountered in another location in component '%s'.aMCannot output embedded SP representation for component '%s'. The embedded SMPS writer does not yet handle the case where a stochastic data component appears in an expression that defines a single variable's coefficient. The coefficient for variable '%s' must be exactly set to parameter '%s' in the expression. Invalid expression: %szInvalid distribution type '%s' for stochastic parameter '%s'. The embedded SMPS writer currently only supports discrete table distributions of type pyomo.pysp.embeddedsp.TableDistribution.a'Cannot output embedded SP representation for component '%s'. The embedded SMPS writer does not yet handle the case where multiple stochastic data components appear in an expression that defines a single variable's coefficient. The coefficient for variable '%s' involves stochastic parameters: %scSsg|] }|jqSr')rC)r6rr'r'r(rkvscs|S)Nr')r)rr'r(r}rcs |dS)Nrr')r)rr'r(rrcs$g|]\}}|jt||fqSr')rErI)r6rgvarcoef)rYr'r(rksaRCannot output embedded SP representation for component '%s'. The embedded SMPS writer does not yet handle the case where a stochastic data appears in the body of a constraint expression that must be moved to the bounds. The constraint must be written so that the stochastic element '%s' is a simple bound or a simple variable coefficient.zCannot output embedded SP representation for component '%s'. The embedded SMPS writer does not yet handle range constraints that have stochastic data.rcsg|]}|jkr|qSr')r)r6param)rr'r(rkscSsg|] }|jqSr')rC)r6rr'r'r(rk s)Fr]rr?rrArSZstage_to_variables_maprJaddrIr;rLr Zcompute_time_stagerrr_valuerr~r}rurvrQrrrr#rrrrZvariable_symbolsrEritemsrFrGr!rHr" itertoolschainrZ objectiverZ objective_to_stochastic_data_maprrTrrwrrOrCrrZ_collect_mutable_parametersstrtyper __class__rrr weightsr+Z!constraint_to_stochastic_data_maprPrr)Jrrrrrrpr]Zfirst_stage_variablesZfirst_stage_variable_idsZsecond_stage_variablesZsecond_stage_variable_idsr^r_rgZderivedZfirst_stage_constraintsZfirst_stage_constraint_idsZsecond_stage_constraintsZsecond_stage_constraint_idsrfZconstageZ column_cntrZrow_cntrZparam_vals_origZ paramdataryZsymbolic_repn_datarr{rrrrrrrrrrZorig_valrZ_reverse_aliasrbrcrdr7rHrrrrrZvaridrZstochastic_data_seenrrrrrZobjective_varsrrparamsZstochastic_paramsr distributionZvarlabelZprobr*Zstochastic_constraintsrZconstraint_varsZstochastic_constraint_labelr')rrrrrYr(rs                       "                                                     rc Cs>|dkrt}t|dt|dt|dt|dt|dt|dtdtddd d t|d td td dd d dd dgidt|dtdtddd d t|dtdtddd d t|dtdtddd d t|dtdtddd d t|dtdtddd d t|dtdtddd d t|dt|t||S)N disable_gcprofile tracebackrsymbolic_solver_labelsexplicitFzxGenerate SMPS files using explicit scenarios (or bundles). ** This option is deprecated. It is the default behavior. ** r)domain descriptiondocZ visibilityrrzjThe format used to generate the core SMPS problem file. Choices are: [mps, lp]. The default format is MPS.choicesr)Zap_kwdsrrzTThe directory in which all SMPS related output files will be stored. Default is '.'.rzEThe basename to use for all SMPS related output files. ** Required **rpzAdds nonanticipativity constraints for variables flagged as derived within their respective time stage (except for the final time stage). The default behavior behavior is to treat derived variables as belonging to the final time stage.raKDisables consistency checks that attempt to find issues with the SMPS conversion. By default, these checks are run after conversion takes place and leave behind a temporary directory with per-scenario output files if the checks fail. This option is not recommended, but can be used if the consistency checks are prohibitively slow.rzKeeps around the per-scenario SMPS files created for testing whether a conversion is valid (whether or not the validation checks are performed). These files can be useful for debugging purposes.rzKeep auxiliary files for the template scenario that are normally used for testing the validity of the SMPS conversion. These include the .row, .col, .sto.struct, and .[mps,lp].det files.r) rrrrboolrrZregister_optionsr)optionsr'r'r(convertsmps_register_optionss               rc Csddl}|jdkrtdtj|js4t|jt}d|j i}|j rPt |j r`t dd}|jdkrtt}n|jdkrt}||:}|t|j|j||j|j||j|j|j|jd WdQRXt}tdtd ||dS) NrzCOutput basename is required. Use the --basename command-line optionrz_DEPRECATED: The use of the --explicit option is no longer necessary. It is the default behaviorserialZpyro)rrprrrrrz!Total execution time=%.2f seconds)rrrOr#rrrrrrcompile_scenario_instancesrArrwarnrrrZ initializerrrprrrrr)rr start_timerZ manager_classrZend_timer'r'r(run_convertsmpssB       r!c Csddl}t}t||dj}d|d_y8tjdd}||||d_||j |dWn$t k r}z|j Sd}~XYnXt t |d|j|j|jdS)Nrrzpyomo.pysp.convert.smps)prog)rzpyomo.pysp.convert.smps: )Z error_labelrZ profile_countr)rrrroZ _argparseargparseArgumentParserZinitialize_argparseZimport_argparse parse_args SystemExitcoder r!rrr)rrrZ argparse_valZapZ_excr'r'r(mains$     r(__main__r5)r)F)rFNFFFF)rNF)N)N)Rr#r#sysrrUrrloggingr collectionsrZpyomo.common.collectionsrZ pyomo.optrZpyomo.core.base.numvaluerrZpyomo.core.base.blockrrr Zpyomo.core.base.objectiver Zpyomo.core.base.varr r Zpyomo.core.base.constraintr rZpyomo.core.base.sosrZ pyomo.repnrZpyomo.pysp.scenariotree.managerrZpyomo.pysp.embeddedsprrZpyomo.pysp.annotationsrrrrrZpyomo.pysp.util.configrrrrrrrZpyomo.pysp.util.miscr sixr!r"rabspath__file__r getLoggerrrrr)r+r,rhrsr}rrrrrrr!r(rargvr'r'r'r( s          l 7"-X  F- ~5 -