B u ` @sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZmZddlmZddlmZddlmZmZdd lmZmZdd lmZdd lmZdd 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-m.Z.m/Z/m0Z0ddl m1Z1m2Z2ddl3m4Z4ddl5m6Z6m7Z7ej89e:Z;eee>Z?ddZ@ddZAe dddddddd d!gZBd"d#ZCd=d%d&ZDd'd(ZEd)d*ZFd+d,ZGd>d.d/ZHd?d0d1ZId2d3ZJd@d4d5ZKd6d7ZLdAd8d9ZMeNd:kreMejOd;dd<dS)BN) namedtuple) ComponentMap) WriterFactory)value as_numeric)SortComponents) 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-n62dbgi3/pyomo/pysp/convert/smps.py_safe_remove_file@sr'cCs|dkr dS|S)z6Make sure -0 is never output. Makes diff tests easier.rr%)valr%r%r&_no_negative_zeroGsr) 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& wsz(map_constraint_stages..T)active descend_intosortF)r7r8zGSOSConstraints are not allowed with this format. Invalid constraint: %s_repnz-Unable to find _repn ComponentMap on block %s)key)" _instancelenstagesAssertionError findRootNodenamerindicesZ alphabeticalbyObjectdictbySymbolraliasesidappendr r9block_data_objectscomponent_data_objectsr TypeErrorgetattr ValueErrorr rZ_collect_variablesbodyvaluesfixedsortedkeysoperator itemgetter)scenario scenario_tree symbol_mapstochastic_constraint_idsfirststage_variable_idssecondstage_variable_idsreference_model firststage secondstagerootnodeStageToConstraintMapZ sortOrderrCZ reverse_aliasalias obj_weakref_aliasesblockconZ block_repnr5rFZconstraint_stagevarr;r%r%r&map_constraint_stagesWsn               rfFcsV|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) Nr2rr3T)r8Fcs&g|]}||d|dfqS)rr3r%)r4r5)FirstStageVarsr%r& sz'map_variable_stages..cs&g|]}||d|dfqS)rr3r%)r4r5)SecondStageVarsr%r&ris)r<r@r=r>r?Z node_listZstageZ_standard_variable_idsZ_derived_variable_ids _variable_ids_ScenarioTreeSymbolMaprCrJr rGgetrQrA)rUrVrW!enforce_derived_nonanticipativityr[r^r\r]Zsecondstage_nodeZfirststage_standard_variablesZfirststage_derived_variablesZsecondstage_variablesZscenariotree_byObjectZsymbolmap_byObjectrer5scenario_tree_idStageToVariableMapr%)rhrjr&map_variable_stagessJ          rqcCsi}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)r7r8F)compute_values)rI _gen_obj_repn _gen_con_repnrrGr:rJrrexprr Z_linear_canonical_formZcanonical_formrN)model repn_cachercobjective_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)r7r8rsrtr:z;Failed to complete partial SMPS conversion for scenario: %s) r<rIhasattrrsrtr:rH'_convert_external_setup_without_cleanuploggererrorrAsetattr) workerrUargskwdsr[ cached_attrsrcblock_cached_attrsrAr%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)r7r8)Z max_allowedFcs|ddS)NrT)getname)x)constraint_name_bufferr%r&wz9_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)r4rd)stochastic_rhsr%r&r6scs|ddS)NrT)r)r)rr%r&rrc3s|]}|jfVqdS)N)r)r4rd)stochastic_matrixr%r&r6scss|]\}}t|VqdS)N)rG)r4rd_r%r%r&r6scss|]\}}t|VqdS)N)rG)r4rdrr%r%r&r6szbThe 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)r4cr%r%r&risz;_convert_external_setup_without_cleanup..cSsg|] }t|qSr%)r)r4rr%r%r&risz.setup.. column_order row_ordercSsdS)NTr%)rr%r%r&rr)rncss|]\}}}t|VqdS)N)rG)r4r5reror%r%r&r6scss|]\}}}t|VqdS)N)rG)r4r5reror%r%r&r6scss|]\}}t|VqdS)N)rG)r4symbolsrdr%r%r&r6sr2RHSzCongratulations! 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&r@rwz%s %s c3s(|] }|D]\}}||fVqqdS)Nr%)r4Z stage_namerrd)r_r%r&r6bsz.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!pathexistsr?rDrVr<r@r>listrJr rrr=Zhas_declarationsZexpand_entriesr9 RuntimeError__name__tuplersetupdaterrrMr|r{rsrtrOrconstant linear_coefsquadratic_coefsrjoinrArqrfZ_instance_objectiveZ parent_blockrGr enumerateopenrlrQrkrEZis_expression_typerCrH writelineswrite startswithZ probabilityr) isinstancer is_linearrzlower_deterministic_check_constantZ_lowerupperZ_upper linear_varsr rPzip_deterministic_check_valuer_namerr)errUoutput_directorybasenameZ file_formatrn io_optionspyomorVr[r^r\r]objective_name_buffervariable_name_bufferZall_constraintsZstochastic_rhs_entriesZempty_rhs_annotationZstochastic_matrix_entriesZempty_matrix_annotationrXZstochastic_objectiveZstochastic_varboundsrw block_repnsrywriteroutput_filename output_fnamerWrprYrZZsecondstage_constraint_idsrxZobjective_blockobjective_repnZ column_indexr5rerorZ row_indexrrdsymbols_filenamefZ st_symbol_maplinesid_lp_labelconstraint_symbolsr+r,Zf_colrr-r.Zf_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_ubr0r1r/Zf_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%)r_rrrrr&r}Is                                                                                                                   r}rc - 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)BNrr2z;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!rrmakedirsrVr=r>rMZcontains_bundlesrmkdirZinvoke_functionthisfilerZ PerScenarioZ scenariosrAr'shutilcopy2rrr? copyfileobjprintrelpathsystemfilecmpcmplistdirrmtree)-rrscenario_tree_manager core_formatrnrdisable_consistency_checkskeep_scenario_fileskeep_auxiliary_filesverboserrVZscenario_directorycountsZreference_scenarioZreference_scenario_namer+r,r-r.r/r0r1 input_filesZ core_filenamer tim_filenameZcore_row_filenameZcore_col_filenameZsto_struct_filenameZcore_det_filename sto_filenamefdstrUZscenario_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)rrr2z;SMPS conversion does not yet handle more than 2 time-stageszcProblems with stochastic variables bounds can not be converted into an embedded SMPS representationT)r7r8rsrtr:z+Failed to complete embedded SMPS conversion)rr!rrrr?rDr= time_stagesrMZhas_stochastic_variable_boundsr[rIr|rsrtr:rH_convert_embeddedr~rr) rrsprrrnrr[rrcrrAr%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&r rT)r7r8)Zderived_last_stagecs |dS)NT)r)r)rr%r&r.rcs |dS)NT)r)r)rr%r&r0rr:cSsg|] }t|qSr%)r)r4rr%r%r&ri`sz%_convert_embedded..cSsg|] }t|qSr%)r)r4rr%r%r&riasz.corZcorz .cor.symbolsrrrrcSsdS)NTr%)rr%r%r&rurrz%s %s css|]}|gfVqdS)Nr%)r4r5r%r%r&r6sz$_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)rrzCannot 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)r4p)rr%r&ri 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%)rA)r4rr%r%r&ritscs|S)Nr%)r)rr%r&r{rcs |dS)Nrr%)r)rr%r&rrcs$g|]\}}|jt||fqSr%)rCrG)r4revarcoef)rWr%r&risaRCannot 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)r4param)rr%r&riscSsg|] }|jqSr%)rA)r4rr%r%r&ri s)Fr[rr=rr?rQZstage_to_variables_maprHaddrGr9rJr Zcompute_time_stagerrr_valuerr|r{rsrtrOrrrr!rrrrZvariable_symbolsrCritemsrDrErrFr itertoolschainrZ objectiverZ objective_to_stochastic_data_maprrRrrurrMrArrZ_collect_mutable_parametersstrtyper __class__rrr weightsr)Z!constraint_to_stochastic_data_maprNrr)Jrrrrrrnr[Zfirst_stage_variablesZfirst_stage_variable_idsZsecond_stage_variablesZsecond_stage_variable_idsr\r]reZderivedZfirst_stage_constraintsZfirst_stage_constraint_idsZsecond_stage_constraintsZsecond_stage_constraint_idsrdZconstageZ column_cntrZrow_cntrZparam_vals_origZ paramdatarwZsymbolic_repn_datarryrrrrrrrrrrZorig_valrZ_reverse_aliasr`rarbr5rFrrrrrZvaridrZstochastic_data_seenrrrrrZobjective_varsrrparamsZstochastic_paramsr distributionZvarlabelZprobr(Zstochastic_constraintsrZconstraint_varsZstochastic_constraint_labelr%)rrrrrWr&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 **rnzAdds 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)rrnrrrrrz!Total execution time=%.2f seconds)rrrMr!rrrrrrcompile_scenario_instancesr?rr~warnrrrZ initializerrrnrrrrr)rr start_timerZ manager_classrZend_timer%r%r&run_convertsmpssB       rc 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)rrrrmZ _argparseargparseArgumentParserZinitialize_argparseZimport_argparse parse_args SystemExitcoderrrrr)rrrZ argparse_valZapZ_excr%r%r&mains$     r&__main__r3)r)F)rFNFFFF)rNF)N)N)Pr!r!sysrrSrrloggingr collectionsrZpyomo.common.collectionsrZ pyomo.optrZpyomo.core.base.numvaluerrZpyomo.core.base.blockrZpyomo.core.base.objectiverZpyomo.core.base.varr r Zpyomo.core.base.constraintr r Zpyomo.core.base.sosr Z pyomo.repnrZpyomo.pysp.scenariotree.managerrZpyomo.pysp.embeddedsprrZpyomo.pysp.annotationsrrrrrZpyomo.pysp.util.configrrrrrrrZpyomo.pysp.util.miscrsixrr rabspath__file__r getLoggerr~rrr'r)r*rfrqr{rr}rrrrrr&rargvr%r%r%r& s           l 7"-X  F- ~5 -