B ÔÂ_Ýã@s´ddlZddlZddlZddlZddlZddlZyddlmZdZWne k r\dZYnXddl m Z ddl m Z ddlmZddlmZdd lmZmZdd lmZmZmZmZdd lmZdd lmZdd lmZddl m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'm(Z(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/m0Z0ddl1Z2dd„Z3dd„Z4d-dd„Z5dd„Z6dd„Z7ej8dd „ƒZ9d!d"„Z:d#d$„Z;d%d&„ZdS)0éN)ÚhpyTF)Úshutdown_pyro_components)Ú import_file)Ú pyomo_command)Úpympler_available)ÚExtensionPointÚSingletonPlugin)ÚmaximizeÚminimizeÚVarÚSuffix)Ú SolverFactory)ÚSolverManagerFactory)Ú undefined)Ú IPHExtension)ÚExtensiveFormAlgorithm)ÚProgressiveHedging)Úreset_nonconverged_variablesÚreset_stage_cost_variablesÚ _OLD_OUTPUT)ÚScenarioTreeInstanceFactory)ÚISolutionWriterExtension)Úlaunch_commandÚload_extensionsc CsXttƒ}ttdd„|ƒƒ}d}|d |¡;}t ¡}||_| d¡}| d¡}| d¡}| d¡}| d ¡}| d ¡} | d ¡} |j d d dddt dd|j dddddt dddd„} |j dddd| t ddt dd dgdd!|j d"d#dd$t dd|j d%d&dd't dd|j d(d)dd*dd+|j d,d-dd.t t d/¡d|j d0d1dd2td3d|j d4d5dd6dd+|j d7d8dd9t dd|j d:d;dd|j d?d@ddAt dBd|j dCdDdEdFdGd+|j dHdIdEdJdGd+|j dKddLtdMdN|j dOdPdEdQdGd+|j dRdSddTt dUd|j dVdWddXt dd|j dYdZdd[t dd|j d\d]dd^t d/d|j d_d`dEdadGd+|j dbdcdddtded|j dfdgdEdhdGd+|j didjddktdld|j dmdndEdodpd+|j dqdrdEdsdGd+|j dtdudEdvdGd+|j dwdxdEdydGd+|j dzd{dEd|dGd+|j d}d~ddtdd|j d€ddd‚tded|j dƒd„dd…td†d|j d‡dˆdd‰t dŠd|j d‹dŒddt dŠd|j dŽddEddGd+|j d‘d’dEd“dGd+|j d”d•dEd–dGd+|j d—d˜dd™t d>d|j dšd›ddœt d>d|j ddžddŸt d>d|j d d¡d¢d£t gd|j d¤d¥d¦d§dpd+|j d¨d©ddªtdd|j d«d¬d¢d­gd+|j d®|dd¯t d°d|j d±d²dd³dd+|j d´dµdd¶t d·d|j d¸d¹ddºdd+|j d»d¼dd½t dd|j d¾d¿dEdÀdGd+|j dÁdÂdt dÃddÄ|j dÅdÆdtdÇdÈdÄ|j dÉdÊdEdËdGd+|j dÌdÍdEdÎdGd+|j dÏdÐdEdÑdGd+|j dÒdÓdEdÔdGd+tjdÕdÖ} |  dס} | `|  |¡|  d×| ¡| |_|j dØdÙddÚt dÛd|j dÜdÝdEdÞdGd+|j dßdàdEdádGd+|j dâdãd¢dät gd| j dådædEdçdGd+| j dèdédEdêdGd+| j dëdìdEdídGd+| j dîdïdEdðdGd+| j dñdòdEdódGd+| j dôdõdEdödGd+| j d÷dødEdùdGd+| j dúdûdEdüdGd+| j dýdþdEdÿdGd+| j dddEddGd+| j dddEddGd+| j dddEddGd+| j d d dEd dGd+| j d d dEddGd+| j ddd¢dt gd| j dddEddGd+| j dddEddGd+| j dddEddGd+tsxtr”| j ddddt dŠd| j ddd dEd!dGd+| j d"d#dd$t dŠd| j d%d&dEd'dGd+| j d(d)dEd*dGd+| j d+d,dEd-dGd+| j d.d/dd0t dd| j d1d2dd3t dŠd|S(4NcSs d|dkS)NÚ_r©)Úxrrú5/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/phinit.pyÚ6óz-construct_ph_options_parser..z…Specify the solver with which to solve scenario sub-problems. The following solver types are currently supported: %s; Default: cplexz, z Input OptionszScenario Tree Optionsz PH OptionszSolver OptionszPostprocessing OptionszOutput Optionsz Other Optionsz-mz--model-directoryzaThe directory in which all model (reference and scenario) definitions are stored. Default is ".".ÚstoreÚmodel_directoryÚ.)ÚhelpÚactionÚdestÚtypeÚdefaultz-iz--instance-directoryz—The directory in which all instance (reference and scenario) definitions are stored. This option is required if no callback is found in the model file.Úinstance_directorycSs,|ddtfkrtS|ddtfkr$tSdSdS)NÚminr Úmaxr )r r )ÚvalrrrÚ_objective_sense_typeVs z:construct_ph_options_parser.._objective_sense_typez-oz--objective-sense-stage-basedzÝThe objective sense to use for the auto-generated scenario instance objective, which is equal to the sum of the scenario-tree stage costs. Default is None, indicating an Objective has been declared on the reference model.Úobjective_senser*r r)r )r#r%r&Úchoicesr'z--ph-warmstart-filez^Disable iteration 0 solves and warmstarts rho, weight, and xbar parameters from solution file.Zph_warmstart_filez--ph-warmstart-indexzaIndicates the index (ph iteration) of the warmstart that should be loaded from a ph history file.Zph_warmstart_indexz--bounds-cfgfilezˆThe name of python script containing a ph_boundsetter_callback function to compute and update scenario variable bounds. Default is None.Zbounds_cfgfile)r#r$r%r'z--scenario-tree-seedzžThe random seed associated with manipulation operations on the scenario tree (e.g., down-sampling or bundle creation). Default is None, indicating unassigned.Úscenario_tree_random_seedédz#--scenario-tree-downsample-fractionz¤The proportion of the scenarios in the scenario tree that are actually used. Specific scenarios are selected at random. Default is 1.0, indicating no down-sampling.Ú!scenario_tree_downsample_fractiongð?z--scenario-bundle-specificationaiThe name of the scenario bundling specification to be used when executing Progressive Hedging. Default is None, indicating no bundling is employed. If the specified name ends with a .dat suffix, the argument is interpreted as a filename. Otherwise, the name is interpreted as a file in the instance directory, constructed by adding the .dat suffix automaticallyÚscenario_bundle_specificationz--create-random-bundlesz“Specification to create the indicated number of random, equally-sized (to the degree possible) scenario bundles. Default is 0, indicating disabled.Úcreate_random_bundlesz-rz --default-rhozDThe default (global) rho for all blended variables. *** Required ***Z default_rhoÚz --xhat-methodz•Specify the method used to compute a bounding solution at PH termination. Defaults to 'closest-scenario'. Other variants are: 'voting' and 'rounding'Z xhat_methodzclosest-scenarioz--disable-xhat-computationzºDisable computation of xhat at the conclusion of a PH run. Useful *only* when diagnosing PH convergence, as disabling means the solution at converence is not a non-anticipative solution.Ú store_trueZdisable_xhat_computationFz --overrelaxzRCompute weight updates using combination of previous and current variable averagesZ overrelaxz--nuÚnugø?)r$r%r&r'z--asyncz?Run PH in asychronous mode after iteration 0. Default is False.Z async_modez--async-buffer-lengthzkNumber of scenarios to collect, if in async mode, before doing statistics and weight updates. Default is 1.Zasync_buffer_lengthéz --rho-cfgfilez{The name of python script containing a ph_rhosetter_callback function to compute and update PH rho values. Default is None.Z rho_cfgfilez--aggregate-cfgfilezThe name of python script containing a ph_aggregategetter_callback function to collect and store aggregate scenario data on PH. Default is None.Zaggregate_cfgfilez--max-iterationsz4The maximum number of PH iterations. Default is 100.Zmax_iterationsz--or-convergersz^Terminate when any one of the convergence criterion is satisfied. Defaults to False, i.e., andZ or_convergersz--termdiff-thresholdzqThe convergence threshold used in the term-diff and normalized term-diff convergence criteria. Default is 0.0001.Ztermdiff_thresholdg-Cëâ6?z(--enable-free-discrete-count-convergencez\Terminate PH based on the free discrete variable count convergence metric. Default is False.Z&enable_free_discrete_count_convergencez--free-discrete-count-thresholdz„The convergence threshold used in the criterion based on when the free discrete variable count convergence criterion. Default is 20.Zfree_discrete_count_thresholdéz(--enable-normalized-termdiff-convergencezRTerminate PH based on the normalized termdiff convergence metric. Default is True.Z&enable_normalized_termdiff_convergenceTz--enable-termdiff-convergencezHTerminate PH based on the termdiff convergence metric. Default is False.Zenable_termdiff_convergencez --enable-outer-bound-convergencezKTerminate PH based on the outer bound convergence metric. Default is False.Zenable_outer_bound_convergencez)--enable-primal-dual-residual-convergencezMTerminate PH based on the primal-dual residual convergence. Default is False.Z'enable_primal_dual_residual_convergencez --enable-inner-outer-convergencezšTerminate PH based on bounds using abs(inner-outer); i.e., *not* relative. Note: setting this does not affect bound computation options. Default is False.Zenable_inner_outer_convergencez#--outer-bound-convergence-thresholdzqThe convergence threshold used in the outer bound convergerence criterion. Default is None, indicating unassignedZ!outer_bound_convergence_thresholdz,--primal-dual-residual-convergence-thresholdzfThe convergence threshold used in the primal-dual residual convergerence criterion. Default is 0.0001.Z*primal_dual_residual_convergence_thresholdz#--inner-outer-convergence-thresholdz\The convergence threshold used in the inner-outer convergerence criterion. Default is 0.001.Z!inner_outer_convergence_thresholdgü©ñÒMbP?z#--linearize-nonbinary-penalty-termsz­Approximate the PH quadratic term for non-binary variables with a piece-wise linear function, using the supplied number of equal-length pieces from each bound to the averageZ!linearize_nonbinary_penalty_termsrz--breakpoint-strategya,Specify the strategy to distribute breakpoints on the [lb, ub] interval of each variable when linearizing. 0 indicates uniform distribution. 1 indicates breakpoints at the node min and max, uniformly in-between. 2 indicates more aggressive concentration of breakpoints near the observed node min/max.Zbreakpoint_strategyz--retain-quadratic-binary-termszGDo not linearize PH objective terms involving binary decision variablesZretain_quadratic_binary_termsz--drop-proximal-termsznEliminate proximal terms (i.e., the quadratic penalty terms) from the weighted PH objective. Default is False.Zdrop_proximal_termsz--enable-ww-extensionszBEnable the Watson-Woodruff PH extensions plugin. Default is False.Úenable_ww_extensionsz--ww-extension-cfgfilezNThe name of a configuration file for the Watson-Woodruff PH extensions plugin.Úww_extension_cfgfilez--ww-extension-suffixfilezPThe name of a variable suffix file for the Watson-Woodruff PH extensions plugin.Úww_extension_suffixfilez--ww-extension-annotationfilezTThe name of a variable annotation file for the Watson-Woodruff PH extensions plugin.Úww_extension_annotationfilez--user-defined-extensionzJThe name of a python module specifying a user-defined PH extension plugin.ÚappendÚuser_defined_extensionsz--preprocess-fixed-variableszsPreprocess fixed/freed variables in scenario instances, rather than write them to solver plugins. Default is False.Ú store_falseZwrite_fixed_variablesz--scenario-mipgapz5Specifies the mipgap for all PH scenario sub-problemsZscenario_mipgapz--scenario-solver-optionsz/Solver options for all PH scenario sub-problemsZscenario_solver_optionsz--solverZ solver_typeZcplexz --solver-iozòThe type of IO used to execute the solver. Different solvers support different types of IO, but the following are common options: lp - generate LP files, nl - generate NL files, python - direct Python interface, os - generate OSiL XML files.Z solver_ioz--solver-managerz]The type of solver manager used to coordinate scenario sub-problem solves. Default is serial.Úsolver_manager_typeÚserialz --pyro-hostz=The hostname to bind on when searching for a Pyro nameserver.Ú pyro_hostz --pyro-portz9The port to bind on when searching for a Pyro nameserver.Ú pyro_portz--handshake-with-phpyroa:When updating weights, xbars, and rhos across the PHPyro solver manager, it is often expedient to ignore the simple acknowledgement results returned by PH solver servers. Enabling this option instead enables hand-shaking, to ensure message receipt. Clearly only makes sense if the PHPyro solver manager is selectedZhandshake_with_phpyroz--phpyro-required-workersa©Set the number of idle phsolverserver worker processes expected to be available when the PHPyro solver manager is selected. This option should be used when the number of worker threads is less than the total number of scenarios (or bundles). When this option is not used, PH will attempt to assign each scenario (or bundle) to a single phsolverserver until the timeout indicated by the --phpyro-workers-timeout option occurs.Úphpyro_required_workers)r#r$r&r%r'z--phpyro-workers-timeoutzÚSet the time limit (seconds) for finding idle phsolverserver worker processes to be used when the PHPyro solver manager is selected. This option is ignored when --phpyro-required-workers is set manually. Default is 30.Úphpyro_workers_timeoutéz.--phpyro-transmit-leaf-stage-variable-solutionaBy default, when running PH using the PHPyro solver manager, leaf-stage variable solutions are not transmitted back to the master PH instance during intermediate PH iterations. This flag will override that behavior for the rare cases where these values are needed. Using this option will possibly have a negative impact on runtime for PH iterations. When PH exits, variable values are collected from all stages whether or not this option was used. Also, note that PH extensions have the ability to override this flag at runtime.Z#phpyro_transmit_leaf_stage_solutionz--disable-warmstartszZDisable warm-start of scenario sub-problem solves in PH iterations >= 1. Default is False.Zdisable_warmstartsz--shutdown-pyrozÀShut down all Pyro-related components associated with the Pyro and PH Pyro solver managers (if specified), including the dispatch server, name server, and any solver servers. Default is False.Ú shutdown_pyroz--shutdown-pyro-workerszaShut down PH solver servers on exit, leaving dispatcher and nameserver running. Default is False.Zshutdown_pyro_workersÚef_)Úoptions_prefixÚef_shutdown_pyroz--ef-output-fileaPThe 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 --solver and --solver-io options, and the appropriate suffix will be appended to the name. Default is 'efout'.Úef_output_fileZefoutz --write-efzyUpon termination, create the extensive-form model and write it - accounting for all fixed variables. See --ef-output-fileÚwrite_efz --solve-efzdUpon termination, create the extensive-form model and solve it - accounting for all fixed variables.Úsolve_efz--ef-solution-writerz¹The plugin invoked to write the scenario tree solution following the EF solve. If specified, overrides the runph option of the same name; otherwise, the runph option value will be used.Úef_solution_writerz--output-scenario-tree-solutionzxIf a feasible solution can be found, report it (even leaves) in scenario tree format upon termination. Default is False.Zoutput_scenario_tree_solutionz--output-solver-logsz5Output solver logs during scenario sub-problem solvesZoutput_solver_logz--symbolic-solver-labelsahWhen interfacing with the solver, use symbol names derived from the model. For example, "my_special_variable[1_2_3]" instead of "v1". Useful for debugging. When using the ASL interface (--solver-io=nl), generates corresponding .row (constraints) and .col (variables) files. The ordering in these files provides a mapping from ASL index to symbolic model names.Zsymbolic_solver_labelsz--output-solver-resultsz?Output solutions obtained after each scenario sub-problem solveZoutput_solver_resultsz--output-timesz2Output timing statistics for various PH componentsÚ output_timesz#--output-instance-construction-timezVOutput timing statistics for instance construction (client-side only when using PHPyroÚ!output_instance_construction_timez--report-only-statisticsz‚When reporting solutions (if enabled), only output per-variable statistics - not the individual scenario values. Default is False.Zreport_only_statisticsz--report-solutionszcAlways report PH solutions after each iteration. Enabled if --verbose is enabled. Default is False.Zreport_solutionsz--report-weightszdAlways report PH weights prior to each iteration. Enabled if --verbose is enabled. Default is False.Zreport_weightsz--report-rhos-all-iterationsz@Always report PH rhos prior to each iteration. Default is False.Zreport_rhos_each_iterationz--report-rhos-first-iterationzWReport rhos prior to PH iteration 1. Enabled if --verbose is enabled. Default is False.Zreport_rhos_first_iterationz--report-subproblem-objectivesz…Always report PH subproblem objective and related statistics after each iteration. Enabled if --verbose is enabled. Default is False.Zreport_subproblem_objectivesz!--report-for-zero-variable-valuesz{Report statistics (variables and weights) for all variables, not just those with values differing from 0. Default is False.Zreport_for_zero_variable_valuesz$--report-only-nonconverged-variablesz]Report statistics (variables and weights) only for non-converged variables. Default is False.Z"report_only_nonconverged_variablesz--solution-writerzSThe plugin invoked to write the scenario tree solution. Defaults to the empty list.Úsolution_writerz%--suppress-continuous-variable-outputz;Eliminate PH-related output involving continuous variables.Z#suppress_continuous_variable_outputz --verbosezPGenerate verbose output for both initialization and execution. Default is False.Úverbosez --disable-gcz7Disable the python garbage collecter. Default is False.Ú disable_gcz--profile-memoryaIf Pympler or Guppy is available (installed), report memory usage statistics for objects created after each PH iteration. A value of 0 indicates disabled. A value of 1 forces summary output after each PH iteration >= 1. Values greater than 2 are currently not supported.Zprofile_memoryz-kz--keep-solver-fileszGRetain temporary input and output files for scenario sub-problem solvesZkeep_solver_filesz --profilezjEnable profiling of Python code. The value of this option is the number of functions that are summarized.Úprofilez --tracebackzkWhen an exception is thrown, show the entire call stack. Ignored if profiling is enabled. Default is False.Ú tracebackz--compile-scenario-instanceszReplace all linear constraints on scenario instances with a more memory efficient sparse matrix representation. Default is False.Úcompile_scenario_instancesz --activate-jsonio-solution-saverz—Activate the jsonio IPySPSolutionSaverExtension. Stores scenario tree node solution in form that can be reloaded for evaluation on other scenario treesÚactivate_jsonio_solution_saverz--jsonsaver-output-namezNThe directory or filename where the scenario tree solution should be saved to.Újsonsaver_output_namez--jsonsaver-save-stagesz€The number of scenario tree stages to store for the solution. The default value of 0 indicates that all stages should be stored.Újsonsaver_save_stages)Úlistr ÚsortedÚfilterÚjoinÚargparseÚArgumentParserÚusageÚadd_argument_groupÚ add_argumentÚstrr r ÚintÚrandomÚ getrandbitsÚfloatrÚregister_optionsÚgetrJZinitialize_argparseZdeclareÚ _ef_optionsrÚguppy_available)Z usage_stringZ solver_listZ solver_helpÚparserZ inputOptsZscenarioTreeOptsZphOptsZ solverOptsZpostprocessOptsZ outputOptsZ otherOptsr,Ú ef_optionsÚtmprrrÚconstruct_ph_options_parser3sÔ            rocCs,tdƒ}| dg¡}|j|_|j |¡|S)Nr4)roÚ parse_argsrjÚimport_argparse)rlÚoptionsrrrÚPH_DefaultOptionsYs   rscCsä|j||j|j|j|j|jd}|jr.| ¡| ¡|jrDtdƒ|j dkràt   ¡}t sbtdƒ|j ||j |j|jd}|js†|jrštdt   ¡|ƒt s¦tdƒt   ¡}|j||jdd |jsÌ|jràtd t   ¡|ƒ|S) N)Úinclude_scenariosZdownsample_fractionZbundlesZrandom_bundlesZ random_seedrRzScenario tree is valid!Úphpyroz$Constructing scenario tree instances)rPrVrRz1Time to construct scenario instances=%.2f secondsz$Linking instances into scenario treeT)r-Úcreate_variable_idsz3Time link scenario tree with instances=%.2f seconds)Zgenerate_scenario_treer1r2r3r/rRÚpprintÚvalidateÚprintr@ÚtimerÚ%construct_instances_for_scenario_treerPrVrOÚlinkInInstancesr-)rrÚscenario_instance_factoryrtÚ scenario_treeÚ start_timeZinstance_dictionaryrrrÚGenerateScenarioTreeForPHfsD     r€cCsnddl}ddl}ddl}ttƒ}x|D] }| ¡q&Wg}t|jƒdkrfx|jD] }|tj krvt d|dƒnBt d|ƒt tj ƒ}tj   dd¡t|ƒt dƒ|tj dd…<ddl}|} |  d¡rØ|  d¡} |  d¡d krô|  d¡d } xl| tj | |j¡D]T\} } t| tƒr | d kr x2|d d D]$}t|| ƒr4| ¡| |¡q4Wq WqTWt|jƒdkrŠ|jd krŠtdƒ‚t|jƒdkr®|jd kr®tdƒ‚t|jƒdkrÒ|jd krÒtdƒ‚tt ƒ} x| D]}| ¡qàWg} |jrªddl!}tt ƒ} xš| d d D]Œ}t||j"j#j$j$ƒr| ¡|  |¡t|jƒdkr^|j|_%nd|_%t|jƒdkr~|j|_&nd|_&t|jƒdkrž|j|_'nd|_'qWt|j(ƒdkræx(|j(D]}|tj krèt d|dƒnBt d|ƒt tj ƒ}tj   dd¡t|ƒt dƒ|tj dd…<ddl}|} |  d¡rL|  d¡} |  d¡d krj|  d¡d } xt| tj | |j¡D]\\} } t| tƒr€| d kr€tt ƒ} x2| d d D]$}t|| ƒr²| ¡|  |¡q²Wq€WqÄWd}d}yü|j)rt d|j*ƒt+|j*|j,|j-d}|dkr6td|j*dƒ‚t.|ƒ}t||j/j#j0j1j2ƒrÖ| 3¡r€t|j4ƒ}t5s t dt6|ƒƒn t|j7ƒ}t5s t dt6|ƒƒ|j8}|dkr´|}|j8dkrÆ|j9nd}| :||¡|j;||| |dWn~|dk r| <¡|dk rZt||j/j#j0j1j2ƒr4|j=|j>dn&t||j/j#j0j?j@ƒrZ|j>rZ| A¡t dƒ‚YnX|S)Nrz'User-defined PH solution writer module=z already imported - skippingz8Trying to import user-defined PH solution writer module=r"zModule successfully loadedz.pyú/éÿÿÿÿrT)ÚallFzfA configuration file was specified for the WW extension module, but the WW extensions are not enabled!z_A suffix file was specified for the WW extension module, but the WW extensions are not enabled!zcA annotation file was specified for the WW extension module, but the WW extensions are not enabled!z!User-defined PH extension module=z2Trying to import user-defined PH extension module=z$Constructing solver manager of type=)ÚhostÚportz(Failed to create solver manager of type=z$ specified in call to PH constructorzBundle solver jobs available: z Scenario solver jobs available: )r~Úsolver_managerÚ ph_pluginsÚsolution_plugins)Úshutdownz2Failed to initialize progressive hedging algorithm)BÚ pyomo.environÚ%pyomo.solvers.plugins.smanager.phpyroÚ#pyomo.solvers.plugins.smanager.pyrorrÚdisableÚlenrQÚsysÚmodulesryrZÚpathÚinsertrÚinspectÚrfindÚrstripÚfindÚsplitÚ getmembersÚisclassÚ issubclassrÚ isinstanceÚenabler=r:r9Ú ValueErrorr;r<rZ pyomo.pysp.plugins.wwphextensionÚpyspÚpluginsZ wwphextensionZ_configuration_filenameZ_suffix_filenameZ_annotation_filenamer>rRr@rrBrCrÚsolversÚsmanagerruÚSolverManager_PHPyroZcontains_bundlesZ_scenario_bundlesrrcÚ _scenariosrDrEZacquire_serversZ initializeÚrelease_componentsÚrelease_serversÚ_shutdown_pyro_workersÚpyroÚSolverManager_PyroÚshutdown_workers)rrr~ÚpyomoZsolution_writer_pluginsÚpluginrˆZthis_extensionZ original_pathr“Zmodule_to_findÚnameÚobjZph_extension_pointr‡Úphr†Znum_jobsZservers_expectedÚtimeoutrrrÚPHAlgorithmBuilder¥s                                r°cCs¸t ¡}|jrtdƒt|j|jƒ}|js0|jrDtdt ¡|ƒyt||ƒ}Wntdƒ| ¡‚YnXd}yt ||ƒ}Wn.tdƒ|dk r¤|  ¡| ¡‚YnX|S)Nz'Importing model and scenario tree fileszCTime to import model and scenario tree structure files=%.2f secondsz4Failed to initialize model and/or scenario tree dataz8A failure occurred in PHAlgorithmBuilder. Cleaning up...) rzrRryrr!r(rOr€Úcloser°r¤)rrrr}r~r®rrrÚ PHFromScratch‹s2  r²ccs&d}zt|ƒ}|VWdt|ƒXdS)N)r²Ú PHCleanup)rrr®rrrÚPHFromScratchManagedContext¾s  r´cCsª|dkr dS| ¡|jdk r„ddl}ddl}ddl}t|j|jjjj j ƒr^|jj |j dn&t|j|jjjj jƒr„|j r„|j ¡|jdk r¦|jjdk r¦|jj ¡dS)Nr)r‰)r¤Ú_solver_managerrŠr‹rŒr›r rŸr¡rur¢r¥r¦r§r¨r©Ú_scenario_treeÚ_scenario_instance_factoryr±)r®rªrrrr³Ês"    r³cCsÄddl}ddl}ddl}t ¡}| ¡}|dk r8tdƒ‚t ¡}tdƒtd||ƒtdƒ|jrn| ¡|  ¡|j rÆtdƒddl }|j j j ¡}| ¡}|j|_|j|_| |¡| |¡d}t|jƒdkræt|jtƒ}n|j}d} |jsþ|jr,t|j|jj jj j!ƒs"| "¡ntdƒ|j#j$j%|j#|j&|j'd} |j#j(| d |j#d d | )¡|j'rrtd ƒt ¡} t*t+f} | ,| ¡t ¡} |jr¬td | | ƒ|j-dk rêtd ƒx(|j#j.D]}|j/|j-||j#|ƒqÊW|j j0 1|¡x8|j2 3¡D]*}x"|j4ddD]}d|_5d|_6qWqW|j}d|_zdtdƒ| 7¡| 8¡\}}tdt9|ƒdt9|j:ƒdƒtdt9|ƒdt9|j;ƒdƒWd||_X|j<}| =d¡j>}t?| =d¡_>t@jA||dddd d|| =d¡_>tBrútdƒt@||dd} |  C¡tD|j#|j2ƒtE|j#|j2ƒ| F¡|jrPtdƒ|  G|jH¡|jr®tdƒ| jd d}|rŽtd| jI| jJ| jKfƒn td| jKƒtd| jJƒtd | jLƒ| jMtNk râtd!| jMƒtd"| jOƒn | jOtNksòtP‚td#ƒtd$ƒ| Q¡tdƒtd%ƒ|jR S¡tdƒtd&ƒ|jR T¡x|D]}| G|jRd'¡qDW|j r®tdƒddl }|j j j ¡}| ¡}|j|_|j|_| |¡| |¡| dk rÀ|  U¡dS)(NrzFailure Encounteredr4z$Total PH execution time=%.2f secondsz)Executing jsonio solution saver extensionrz8Constructing scenario instances for extensive form solve)rPrRF)rvZmaster_scenario_treeZinitialize_solution_dataz8Creating deterministic SymbolMaps for scenario instancesz'PH SymbolMap creation time=%.2f secondsz-Executing user bound setter callback functionT)Úactivez5Pushing fixed variable statuses to scenario instancesz8Number of discrete variables fixed prior to ef creation=z (total=ú)z:Number of continuous variables fixed prior to ef creation=rJrH)rIZsource_options_prefixZ skip_usersetZerror_if_missingz-Creating extensive form for remainder problem)rIzWriting extensive formzSolving extensive form)Zexception_on_failurezgEF solve failed solution status check: Solver Status: %s Termination Condition: %s Solution Status: %s z,EF solve completed and solution status is %sz$EF solve termination condition is %szEF objective: %12.5fzEF gap: %12.5fzEF bound: %12.5fzEF gap: zEF bound: zExtensive form solution:zExtensive form costs:Zpostphef)VrŠr‹rŒrzZsolveÚ RuntimeErrorryrOZprint_time_statsZ save_solutionrWZpyomo.pysp.plugins.jsonioržrŸZjsonioZJSONSolutionSaverExtensionrhrXrYZ set_optionsÚsaverŽrNrrZ_solution_pluginsrLrMr›rµr r¡rur¢Z!_destory_bundle_binding_instancesr¶r·r{Z"_output_instance_construction_timerRr|Z_setup_scenario_instancesr r Z_create_instance_symbol_mapsZ _bound_setterr£Z_callback_functionZphsolverserverutilsZwarmstart_scenario_instancesZ _instancesÚvaluesZblock_data_objectsZ _gen_obj_repnZ _gen_con_repnZ!_push_all_node_fixed_to_instancesZcompute_fixed_variable_countsrcZ_total_discrete_varsZ_total_continuous_varsrjriZ_domainÚboolrZupdate_options_from_argparserZbuild_efrrr¤ÚwriterKZ solver_statusZtermination_conditionZsolution_statusZ objectiveZgaprÚboundÚAssertionErrorZupdate_variable_statisticsr~ZpprintSolutionZ pprintCostsr±)rrr®rªrÚretvalZ end_ph_timeZ jsonsaverZjsonsaver_optionsZef_solution_writersZefZ instancesZ"scenario_ph_symbol_maps_start_timeZ symbol_ctypesZ scenario_ph_symbol_maps_end_timeZscenarioÚinstanceÚblockZph_solver_managerZtotal_fixed_discrete_varsZtotal_fixed_continuous_varsrmZ _orig_domainÚfailedr«rrrÚrun_phës           $              rÅc Cs’ddl}t ¡}z"t|ƒ}t||ƒWdQRXWd|jdksH|jdkrp|jrptdƒtdƒt|j|j ddXtdƒtdt ¡|ƒdS) Nrr§ruÚ z%Shutting down Pyro solver components.)r„r…Z num_retriesr4z!Total execution time=%.2f seconds) rŠrzr´rÅr@rGryrrBrC)rrrªrr®rrrÚ exec_runphês"    rÇc Cstddl}y,tdƒ}|j|d}|j|_|j |¡Wn$tk rX}z|jSd}~XYnXtt|d|j |j |j dS)Nrzrunph [options])Úargszrunph: )Z error_labelrSZ profile_countrU) rŠrorprjrqÚ SystemExitÚcoderrÇrSrTrU)rÈrªZph_options_parserrrZ_excrrrÚmains rËZrunphz+Optimize with the PH solver (primal search)cCs t|dS)N)rÈ)rË)rÈrrrÚPH_main3srÌ)N)N)N)?ÚgcrrzÚ contextlibrer^ZguppyrrkÚ ImportErrorZ pyutilib.pyrorZ pyutilib.miscrZ pyomo.commonrZpyomo.common.dependenciesrZpyomo.common.pluginrrZpyomo.core.baser r r r Zpyomo.opt.baser Zpyomo.opt.parallelrZ pyomo.optrZpyomo.pysp.phextensionrZpyomo.pysp.ef_writer_scriptrZ pyomo.pysp.phrZpyomo.pysp.phutilsrrrZ(pyomo.pysp.scenariotree.instance_factoryrZpyomo.pysp.solutionwriterrZpyomo.pysp.util.miscrrZpyomo.pysp.phsolverserverutilsrªrorsr€r°r²Úcontextmanagerr´r³rÅrÇrËrÌrrrrÚ s\              * =g3 !)