B _@sddlZddlZddlZddlZddlZddlZddlZddlm Z ddl Z ddl m Z ddlmZddlmZmZmZmZmZmZddlmZddlmZddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,dd l-m.Z.m/Z/m0Z0dd l1m2Z2dd l3m4Z4m5Z5dd l6m7Z7m8Z8dd l9m:Z:ddl;mm?Z?m@Z@mAZAddlBmCZCddl3mDZDeEdZFdZGGdddeZHGddde?eZId!ddZJd"ddZKd#ddZLeAMdeIeNd kreOeLdS)$N)shutdown_pyro_components)yaml) ComponentUID) SolverFactoryTerminationCondition UndefinedData ProblemFormat UnknownSolverSolutionStatus)SolverManagerFactory)PySPConfiguredObject)PySPConfigValuePySPConfigBlocksafe_register_common_optionsafe_register_unique_optionsafe_declare_common_optionsafe_declare_unique_option_domain_percent_domain_nonnegative_domain_nonnegative_integer_domain_positive_integer_domain_must_be_str_domain_unit_interval_domain_tuple_of_str_domain_tuple_of_str_or_dict_output_options_group_title_extension_options_group_title_deprecated_options_group_title)parse_command_linelaunch_commandsort_extensions_by_precedence)find_active_objective)ScenarioTreeManagerScenarioTreeManagerClientSerial)IPySPSolutionSaverExtensionIPySPSolutionLoaderExtension)ISolutionWriterExtension)write_efcreate_ef_instance)SPSolverSPSolverResultsSPSolverFactory)ScenarioTreeManagerClientPyro)#ScenarioTreeManagerSolverClientPyroz pyomo.pyspz EF OptionscsdeZdZedddZddZddZdd Zfd d Zd d Z ddZ ddZ dddZ Z S)ExtensiveFormAlgorithmNc Cs"|dkrt}t|dtdtddddtdt|dtdtd dddtdt|d td td dddtdt|d tdtddddtdt|dtdtddddtdt|dt|dt|dt|dt|dt|dt|dt|dt|dtdt|dtdt|dtd|S)N cvar_weightg?zThe weight associated with the CVaR term in the risk-weighted objective formulation. If the weight is 0, then *only* a non-weighted CVaR cost will appear in the EF objective - the expected cost component will be dropped. Default is 1.0.r)domain descriptiondoc visibility)ap_groupgenerate_weighted_cvarFzDAdd a weighted CVaR term to the primary objective. Default is False. risk_alphagffffff?zrThe probability threshold associated with CVaR (or any future) risk-oriented performance metrics. Default is 0.95.cc_alphagzrThe probability threshold associated with a chance constraint. The RHS will be one minus this value. Default is 0.cc_indicator_varzThe name of the binary variable to be used to construct a chance constraint. Default is None, which indicates no chance constraint.solver solver_iosolver_managersolver_optionsdisable_warmstartsolver_manager_pyro_hostsolver_manager_pyro_portZsolver_manager_pyro_shutdownverbose output_timesoutput_solver_results) rrr r_ef_group_labelboolrrr)clsoptionsrG9/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/solvers/ef.py_declare_optionsJsz'ExtensiveFormAlgorithm._declare_optionscCs|S)NrG)selfrGrGrH __enter__sz ExtensiveFormAlgorithm.__enter__cGs |dS)N)close)rJargsrGrGrH__exit__szExtensiveFormAlgorithm.__exit__cCsL||jdk rr?)hostportz(Failed to create solver manager of type=)Z#pyomo.solvers.plugins.smanager.pyrosuperr.__init__Z initialized ValueErrorrTinstancerP_solver objectivegaptermination_conditiontermination_message solver_statussolution_statussolver_resultstime pyomo_timerSrrQr printr )rJmanagerrMkwdsrRZsolver_manager_type) __class__rGrHrYsJ      zExtensiveFormAlgorithm.__init__c Cs||drtdt}d}d}d}|drPd}|d}|d}t|jj|d||||d|d d |_|ds|d rtd t|dS) Nr@z Creating extensive form instanceFr5Tr/r6r8r7)Zverbose_outputr5r/r6Zcc_indicator_var_namer7rAz6Time to construct extensive form instance=%.2f seconds)rOrSrfrdr(rT scenario_treer[)rJ start_timer5r/r6rGrGrHbuild_efs,    zExtensiveFormAlgorithm.build_efcCs@|jdk r6x*|jjjD]}|j|j|jqWd|_dS)N)r[rTrj scenariosZ del_componentname_instance_objectiveactivate)rJscenariorGrGrHrOs  z!ExtensiveFormAlgorithm.destroy_efcCs|jdkrtdtj|d}|dkr|jtjkrD|d7}n<|jtj kr^|d7}n"|jtj krx|d7}nt dt }t d|t|j||d }|d s|d rt d t |||fS) Nz`The extensive form instance has not been constructed.Call the build_ef() method to construct it.)z.nlz.lpz.mpsz.lpz.nlz.mpszCould not determine output file format. No recognized ending suffix was provided and no format was indicated was by the --solver-io option.zWriting extensive form to file=symbolic_solver_labelsr@rAz&Time to write output file=%.2f seconds)r[ RuntimeErrorospathsplitextr\Zproblem_formatrZcpxlpnlZmpsrZrdrfr'rS)rJfilenamesufrkZsmap_idrGrGrHwrite%s*      zExtensiveFormAlgorithm.writeTFcCsddlm}|jdkrtdt}|dr8tdd|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_t|j|r|jj|j|di} d| d<|rd | d <|rd | d <|rd | d <|d } t| dkr8t| tkr0i| d<xF| D]4} d| kst| d\} } | | d| <qWn| | d<|dk rL| |t|jj|_|ds|jr|jj|jf|jd d| }n|jj|jfd|ji| }|drtd|j |}|drtd|drtd|j!dd||_t"|j#drDt|j#j$t%sD|j#j$dk rDt&|j#j$|_n$t"|j#drbt&|j#j|_nd|_t"|dr~|j'|_nd|_|j#j |_ d|_ t"|j#dr|j#j |_ nt"|j#dr|j#j(|_ |j#j)|_ t|j*dkrt|j*dkst|j+}|jj,-||*d}t"|drF|jdk rFt|jt%sFt&|j|_nd|_|j)|_ |drhtdx|j.j/j0D]}|1qtW|j.j/2|j.j/34|_|jdk r|jt5j6j7j8kr|j|j|_ n|j|j|_ nd|_d|_d|_ d|_ d}|rn|j t9j:ks|j t9j;ksd }|ds6|rd |j |j |j f}|dr^t||rt|n|drtd!|ds|d"rtd#t||S)$Nr)PersistentSolverz`The extensive form instance has not been constructed.Call the build_ef() method to construct it.r@zQueuing extensive form solve)rsFZload_solutionsTZ keepfilesrsteer<rF=r=)optZ warmstartrz Waiting for extensive form solvez0Done with extensive form solve - loading resultsrBzResults for ef:rr)num user_timerdpyomo_solve_timer`messager^z!Storing solution in scenario treezgEF solve failed solution status check: Solver Status: %s Termination Condition: %s Solution Status: %s z*EF solve completed. Skipping status check.rAzBTime to solve and load results for the extensive form=%.2f seconds)._DeprecatedActivateJSONIOSolutionSaverNcs2|dk rtdt|j||fddi|dS)Nznargs not allowednargsr)rZrXrY)rJoption_stringsdestrkwargs)&_DeprecatedActivateJSONIOSolutionSaverrirGrHrYs zOrunef_register_options.._DeprecatedActivateJSONIOSolutionSaver.__init__cSs,tdt|dg}t|d|dgdS)NzDEPRECATED: The '--activate-json-io-solution-saver command-line option has been deprecated and will be removed in the future. Please the following instead: '----solution-saver-extension=pyomo.pysp.plugins.jsonio'z$CONFIGBLOCK.solution_saver_extensionzpyomo.pysp.plugins.jsonio)loggerwarninggetattrsetattr)rJparser namespacevalues option_stringrrGrGrH__call__szOrunef_register_options.._DeprecatedActivateJSONIOSolutionSaver.__call__)N)N)rrrrYrrrG)r)rirHrsrcSstjdt|S)Nz WARNING: The 'activate_jsonio_solution_saver' config item will be ignored unless it is being used as a command-line option where it can be redirected to 'solution_saver_extension'. Please use 'solution_saver_extension=pyomo.pysp.plugins.jsonio' instead. )sysstderrr{r)rrGrGrH$_warn_activate_jsonio_solution_saverszDrunef_register_options.._warn_activate_jsonio_solution_saverZactivate_jsonio_solution_saverzIDeprecated alias for --solution-saver-extension=pyomo.pysp.plugins.jsoniorrT)rr4Zdeclare_for_argparse)rrrr#rrr rrrrpyutilibmiscconfigargparseActionrr)rFrrG)rrHrunef_register_optionsps      rrGc Cst}t|}t|}t|}t|l}|x(|D] }||}|s:td|q:W|jdk rt||}| | t WdQRXnt dt dt }|j|dd} |j|| |j|j|jd} | j} | `t dt | | | _|jr t d|j|j|j|jdk rJ|jdrd dl} i} x"|jjD]}|j| t|j<qPWt |jd }| j!| |d dd WdQRXn|jd ri} x"|jjD]}|j| t|j<qWt |jd }t"!| |WdQRXnX|jdst dt |jd 0}x(|jjD]}| d|j|jfq WWdQRXx(|D] }|#|sPtd|qPWx|D]}| |jdqzWWdQRXt dt dt|t dd S)z[ Construct a senario tree manager and solve it with the Extensive Form solver. zWLoader extension %s call did not return True. This might indicate failure to load data.Nz:Running the EF solver for stochastic programming problems.T)sparse)rFrrrsz.jsonrw)indent sort_keysz.yamlz.csvzEUnrecognized file extension. Using CSV format to store scenario costsz%s,%r zVSaver extension %s call did not return True. This might indicate failure to save data.rz$Total EF execution time=%.2f seconds)$rdr r#Z initializeloadrrrr.rlr{ryrfrZextract_user_options_to_dictrrrrsrrrjrZpprintSolutionZ pprintCostsrendswithjsonrmZ_coststrrnopendumprsave)rFsolution_loaderssolution_saverssolution_writersrkrZpluginretrZ ef_optionsrrrresultrqfrGrGrHrunefs               rc Csddl}y"t|ttttdddd\}}Wn$tk rN}z|jSd}~XYnXtt ||d|d|dd d |j |j |j d S) Nr)rrrrzCOptimize a stochastic program using the Extensive Form (EF) solver.)Zwith_extensionsprogr1rrr)rrrzrunef: )Zcmd_kwdsZ error_labelrZ profile_countr) Z pyomo.environrrr%r$r& SystemExitcoderrrrr)rMrRrF extensionsZ_excrGrGrHmainZs* rr__main__)N)rGrGrG)N)Pruloggingrrd itertoolsmathZ pyutilib.miscrZ pyutilib.pyrorZ pyomo.solversrRZpyomo.common.dependenciesrZpyomo.core.baserZ pyomo.optrrrrr r Zpyomo.opt.parallelr Z!pyomo.pysp.util.configured_objectr Zpyomo.pysp.util.configr rrrrrrrrrrrrrrrrZpyomo.pysp.util.miscrrr Zpyomo.pysp.phutilsr!Z&pyomo.pysp.scenariotree.manager_solverr"r#Zpyomo.pysp.solutionioextensionsr$r%Zpyomo.pysp.solutionwriterr&Z pyomo.pysp.efr'r(Zpyomo.pysp.solvers.spsolverr)r*r+Zpyomo.pysp.scenariotree.managerr,r- getLoggerrrCr.rrrrZregister_solverrexitrGrGrGrH sN      L     <o   ^ .