B u `l@sddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z m Z mZddlmZddlmZddlmZmZmZmZmZmZmZmZmZmZmZmZm Z ddl!m"Z"m#Z#m$Z$ddl%m&Z&dd l'm(Z(dd l)m*Z*m+Z+dd l,m-Z-dd l.m/Z/m0Z0dd l1m2Z2m3Z3m4Z4ddl5m6Z6ddl'm7Z7e8dZ9dZ:GdddeZ;Gddde2eZd!ddZ?e4@de<eAdkreBe?dS)"N)yaml) SolverFactory UndefinedData ProblemFormat UnknownSolverSolutionStatus)SolverManagerFactory)PySPConfiguredObject) PySPConfigValuePySPConfigBlocksafe_register_common_optionsafe_register_unique_optionsafe_declare_common_optionsafe_declare_unique_option_domain_nonnegative_domain_must_be_str_domain_unit_interval_domain_tuple_of_str_output_options_group_title_extension_options_group_title_deprecated_options_group_title)parse_command_linelaunch_commandsort_extensions_by_precedence)find_active_objective)ScenarioTreeManagerClientSerial)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) r rr r_ef_group_labelboolrrr)clsoptionsr?9/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/pysp/solvers/ef.py_declare_options>sz'ExtensiveFormAlgorithm._declare_optionscCs|S)Nr?)selfr?r?r@ __enter__sz ExtensiveFormAlgorithm.__enter__cGs |dS)N)close)rBargsr?r?r@__exit__szExtensiveFormAlgorithm.__exit__cCsL||jdk r=r5)optZ warmstartrwz Waiting for extensive form solvez0Done with extensive form solve - loading resultsr:zResults for ef:rj)num user_timer\pyomo_solve_timerXmessagerVz!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.r9zBTime to solve and load results for the extensive form=%.2f seconds)r?r?r@rAszEFSolver._declare_optionscs tt|||dS)N)rPrrQrset_options_to_default)rB)rar?r@rQszEFSolver.__init__cCs||_dS)N)r_options)rBr?r?r@rszEFSolver.set_options_to_defaultcCs|jS)N)r)rBr?r?r@r>szEFSolver.optionscCsdS)Nefr?)rBr?r?r@rfsz EFSolver.nameFc Cst|ttfrtdi}|jrlxF|jjD]:}|jjdk r.|jj||<d|j_|j j r^t |j q.Wz6t ||j }||j|||ddWdQRXWdx2|D]&\}}||j_|j j st |j qWXt} |j| _|j| _|j| _|j| j_|j| j_|j| j_|j| j_|j| j_d| _|jdk ri} | _xn|jj ddD]Z} xR| j!D]H} i} | | j"<| jd}|j#| j"}x| j$D]}||| |<qWqdWqXW| S)aM Solve a stochastic program by building the extensive form and calling and a Pyomo solver. See the 'solve' method on the base class for additional keyword documentation. Args: sp: The stochastic program to solve. Pyro based managers are not accepted. All scenario models must be managed locally. output_solver_log (bool): Stream the solver output during the solve. keep_solver_files (bool): Retain temporary solver input and output files after the solve completes. symbolic_solver_labels (bool): Generate solver input files using human-readable symbols (makes debugging easier). Returns: A results object with information about the solution. z?The EF solver does not handle Pyro-based scenario tree managersNF)rrrkrr)%rIr$r% TypeErrorrbZcontains_bundlesreZ _instance_parentrgactiverrhr&rrdritemsZ deactivater"rUr|rZrrYr1rWrXr{r\r]xhatZstagesZnodesrf_xZ _variable_ids)rBsprrrkZ orig_parentsrirparentrrZstagenodeZ node_xhatZreference_scenarioZnode_xZid_r?r?r@ _solve_impl sX                 zEFSolver._solve_impl)N)FFF) rrrrrArQrpropertyr>rfrrr?r?)rar@rs    rc s*|dkrt}t|t|t|dt|dt|dt|dt|dt|dt|dt|dt|d t|d t|d td td ddddditdt|dtdt ddddt dt|dtdt ddddGfdddt j dd}t |dtd|ddddditdd |S)!Nr8 disable_gcprofile tracebackrkrroutput_scenario_tree_solutionsolution_saver_extensionsolution_loader_extensionsolution_writerr?aThe name of a python module specifying a user-defined plugin implementing the ISolutionWriterExtension interface. Invoked to save a scenario tree solution. Use this option when generating a template configuration file or invoking command-line help in order to include any plugin-specific options. This option can used multiple times from the command line to specify more than one plugin.r)r(r)r*r+actionappend)ap_kwdsr, output_filea^The name of the extensive form output file (currently LP, MPS, and NL file formats are supported). If the option value does not end in '.lp', '.mps', or '.nl', then the output format will be inferred from the settings for the chosen solver interface, and the appropriate suffix will be appended to the name. Use of this option will disable the solve.)r,output_scenario_costszA file name where individual scenario costs from the solution will be stored. The format is determined from the extension used in the filename. Recognized extensions: [.csv, .json, .yaml]cs*eZdZdfdd ZdddZZS)zFrunef_register_options.._DeprecatedActivateJSONIOSolutionSaverNcs2|dk rtdt|j||fddi|dS)Nznargs not allowednargsr)rRrPrQ)rBoption_stringsdestrkwargs)&_DeprecatedActivateJSONIOSolutionSaverrar?r@rQs 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)rBparser namespacevalues option_stringrr?r?r@__call__szOrunef_register_options.._DeprecatedActivateJSONIOSolutionSaver.__call__)N)N)rrrrQrrr?)r)rar@rsrcSstjdt|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. )sysstderrrsr)rr?r?r@$_warn_activate_jsonio_solution_saverszDrunef_register_options.._warn_activate_jsonio_solution_saverZactivate_jsonio_solution_saverzIDeprecated alias for --solution-saver-extension=pyomo.pysp.plugins.jsoniorjT)rr,Zdeclare_for_argparse)r rrrr r r rrrrargparseActionrr)r>rr?)rr@runef_register_optionsds      rr?c 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)r>rrrkz.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)$r\rrZ initializeloadrrrr&rdrsrqr^rZextract_user_options_to_dictrrrrkrrrbrZpprintSolutionZ pprintCostsrendswithjsonreZ_coststrrfopendumprsave)r>solution_loaderssolution_saverssolution_writersrcrZpluginretrZ ef_optionsrrrresultrifr?r?r@runefs               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_extensionsprogr)rrr)rrrzrunef: )Zcmd_kwdsZ error_labelrZ profile_countr) Z pyomo.environrrrrr SystemExitcoderrrrr)rErJr> extensionsZ_excr?r?r@mainNs* rr__main__)N)r?r?r?)N)Crmloggingrr\rZ pyomo.solversrJZpyomo.common.dependenciesrZ pyomo.optrrrrrZpyomo.opt.parallelrZ!pyomo.pysp.util.configured_objectr Zpyomo.pysp.util.configr r r r rrrrrrrrrZpyomo.pysp.util.miscrrrZpyomo.pysp.phutilsrZ&pyomo.pysp.scenariotree.manager_solverrZpyomo.pysp.solutionioextensionsrrZpyomo.pysp.solutionwriterrZ pyomo.pysp.efrr Zpyomo.pysp.solvers.spsolverr!r"r#Zpyomo.pysp.scenariotree.managerr$r% getLoggerrr;r&rrrrZregister_solverrexitr?r?r?r@ sF   <       <o   _ .