B u `sã@sddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z ddlmZmZmZmZmZmZmZmZddlmZmZddlmZddlZddlmZm Z ddl!m"Z"e #d ¡Z$ej% &e'¡Z(d Z)d Z*iZ+e j,e j-e j.d fe+d <e j/e j0e j1dfe+d<e j2e j0e j2dfe+d<e j/e j0e j3dfe+d<e j2e j0e j4dfe+d<e j2e j0e j4dfe+d<e j,e j5e j6dfe+d<e j7e j5e j7dfe+d<e j8e j5e j8dfe+d<dd„Z9Gd d!„d!e"e ƒZ:d*d"d#„Z;d$d%„Zd(e:¡e?d)kre @e=ƒ¡dS),éN)ÚTerminationConditionÚ SolverStatusÚSolutionStatus)ÚPySPConfiguredObject)ÚPySPConfigValueÚPySPConfigBlockÚsafe_register_common_optionÚsafe_register_unique_optionÚsafe_declare_common_optionÚsafe_declare_unique_optionÚ_domain_tuple_of_str_or_dictÚ_domain_must_be_str)Úparse_command_lineÚlaunch_command)ÚScenarioTreeManagerFactory)ÚSPSolverResultsÚSPSolverFactory)ÚSPSolverShellCommandz pyomo.pyspz DDSIP OptionsÚ__DDSIP_FIRSTSTAGEzTermination signal received.éÿÿÿÿz+Node limit reached (total number of nodes).éz Gap reached.ézTime limit reached.ézMaximal dispersion equals zero.éz€The whole branching tree was backtracked. Probably due to MIP gaps (see below) the specified gap tolerance could not be reached.ézNo valid lower bound.ézProblem infeasible.ézProblem unbounded.éc Csi}t|ƒ,}x$|D]} |  ¡ ¡\} } | || <qWWdQRXi} t|ƒ,}x$|D]} |  ¡ ¡\} } | | | <qRWWdQRX|j|jdj} |jd|j ¡ks¦t‚|j|jdj}t|dƒH}|  ¡} x|  ¡dkræ|  ¡} qÐW|  ¡} |   d¡sþt‚|  ¡} |   d¡st‚|  ¡ ¡} x8| dkrZ|  ¡\}}t |ƒ| ||<|  ¡ ¡} q$Wx| dkrx|  ¡ ¡} q^Wd |}|  ¡ ¡} x| |krª|  ¡ ¡} qW|  ¡ ¡} xN| dkr|   d ¡s|  ¡\}}|d kröt |ƒ|| |<|  ¡ ¡} qºWWdQRXt S) NrrÚrz1. Best Solutionz"Variable name Valuez#-----------------------------------Úz4. Second-stage solutionsz Scenario %d:z Scenario ZONE_VAR_CONSTANT) ÚopenÚstripÚsplitÚ_xZ node_listÚnameÚ scenario_treeÚ findRootNodeÚAssertionErrorÚreadlineÚ startswithÚfloatÚx)ÚmanagerZscenarioÚsolution_filenameÚ info_filenameZfirststage_symbols_filenameZsecondstage_symbols_filenameZ scenario_idZfirststage_symbol_mapÚfÚlineÚ lp_symbolÚscenario_tree_idZsecondstage_symbol_mapZ x_firststageZ x_secondstageÚvarlabelÚvarsolZscenario_label©r5ú) rr8rHrrr rr rA)rCr5r5r6Úrunddsip_register_optionsus@  rªc CsFt ¡}t|ƒ}| ¡tdƒtdƒtƒ}|j}t|ƒdkr¬t|ƒtkrŒxZ|D]0}d|ksft ‚|  d¡\}}|  ¡|j |  ¡<qVWn x|  ¡D]\}}||j |<q–W|j|dd} |j|| |j|j|jd} | j} | `tdƒt| ƒ|jrtdƒ|j ¡|j ¡|j ¡WdQRXtdƒtd t ¡|ƒdS) zR Construct a senario tree manager and solve it with the DDSIP solver. rz8Running DDSIP solver for stochastic programming problemsrú=T)Úsparse)rCrnr¦r§Nz!Total execution time=%.2f seconds)rerZ initializer_r8r¨rVÚtypeÚtupler'r"r!rCÚitemsZextract_user_options_to_dictZsolvernr¦r§rlr¥r%ZsnapshotSolutionFromScenariosZpprintSolutionZ pprintCosts) rCÚ start_timermrEr¨Zname_valr$rrpZ ddsip_optionsrurlr5r5r6ÚrunddsipŸsH        r±c Cs\ddl}yt|tddd}Wn$tk r@}z|jSd}~XYnXtt|d|j|j|j dS)Nrr±z5Optimize a stochastic program using the DDSIP solver.)Úprogr;z runddsip: )Z error_labelr£Z profile_countr“) Z pyomo.environrrªÚ SystemExitÚcoderr±r£r¤r“)Úargsr`rCZ_excr5r5r6ÚmainÒs r¶rEÚ__main__)N)N)ArƒrXÚsysreÚloggingr“Zpyutilib.subprocessrfZ pyomo.optrrrZ!pyomo.pysp.util.configured_objectrZpyomo.pysp.util.configrrrr r r r r Zpyomo.pysp.util.miscrrZpyomo.pysp.scenariotree.managerrZpyomo.pysp.convert.ddsipr`Zpyomo.pysp.solvers.spsolverrrZ'pyomo.pysp.solvers.spsolvershellcommandrÚ getLoggerr‘rYÚabspathÚ__file__ZthisfilerAZ_firststage_var_suffixr™r›ZabortedZ userInterruptZstoppedByLimitÚokZmaxEvaluationsZoptimalZ maxTimeLimitZ minStepLengthÚwarningZinvalidProblemZ infeasibleZ unboundedr7r8rªr±r¶Zregister_solverrÚexitr5r5r5r6Ús„ (             4Y *3 "