B ÔÂ_–kã@sÌddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z ddlmZddlmZmZmZmZmZmZmZmZmZmZddlmZmZddlmZddl Z!ddl"m#Z#dd l$m%Z%m&Z&dd l'm(Z(e )d ¡Z*d Z+iZ,e j-e j.e j-fe,d <e j-e j.e j-fe,d<e j-e j.e j-fe,d<e j/e j0e j1fe,d<e j/e j2e j/fe,d<dZ3dd„Z4de4_5de6e4j5ƒe4_7Gdd„de(eƒZ8d!dd„Z9dd„Z:d"dd„Z;e& e;ƒ¡dS)#éN)Úmaximize)ÚTerminationConditionÚ SolverStatusÚSolutionStatus)ÚPySPConfiguredObject) ÚPySPConfigValueÚPySPConfigBlockÚsafe_register_common_optionÚsafe_register_unique_optionÚsafe_declare_common_optionÚsafe_declare_unique_optionÚ_domain_positiveÚ_domain_positive_integerÚ_domain_nonnegative_integerÚ_domain_must_be_str)Úparse_command_lineÚlaunch_command)ÚScenarioTreeManagerFactory)Ú EmbeddedSP)ÚSPSolverResultsÚSPSolverFactory)ÚSPSolverShellCommandz pyomo.pyspz SD OptionszCOMPROMISE SOLUTIONz MEAN SOLUTIONzREPLICATION SOLUTIONz NO SOLUTIONz???aß // ---------------Changes made below here are not recommended----------------- // 0 for LP master, 1 for QP master MASTER_TYPE 1 // 0 for zero lower bound, 1 for mean value lower bound LB_TYPE 1 // Amount of improvement which must be observed in order to update incumbent X. R 0.2 // For updating the scaling factor. zl R2 0.95 // For updating the scaling factor. zl R3 2.0 // The Minimum value of the cell->quad_scalar. zl MIN_QUAD_SCALAR 0.001 // The Maximum value of the cell->quad_scalar. zl MAX_QUAD_SCALAR 10000.0 // Number of iterations after which incumbent cut is reevaluated. TAU 2 // Ratio used in pre_test for optimality: (Sm - Lm) / Fk < PRE_EPSILON. PRE_EPSILON 0.01 // MIN_ITER will be max(ITER_FACT*xdim, MIN_ITER) JH 3/13/98 ITER_FACT 0 // Percent of resampled solutions which must meet test for optimality. PERCENT_PASS 0.95 // Number of resampling to take when checking optimality. M 50 // Multiplier for the number of cuts; with QP, SD sets this to 1 CUT_MULT 5 // Level of confidence when choosing the set of cuts, T, for optimality test. CONFID_HI 1.0 // Level of confidence when conducting the optimality pre-test. CONFID_LO 1.45 // Percent of the number by which two "equal" numbers may differ. TOLERANCE 0.001 // number by which two "equal" numbers may differ (used in judging feasibility). FEA_TOLER 0.05 // Like tolerance, but used when trying to thin omegas. THIN_TOLER 0.001 // Number of iterations before SD tries to thin the data structures. START_THIN 9001 // Number of iterations between successive thinning of structures. THIN_CYCLE 200 // Number of consecutive iterations a cut must be slack before its dropped. DROP_TIME 16300 // 0 for pre-tests only, 1 for full_test TEST_TYPE 1 // Number of iterations before checking new pi's impact PI_EVAL_START 1 // Print out detailed solutions, set this to 1 DETAILED_SOLN 1 // Number of iterations between evaluation of the impact of new dual vertex PI_CYCLE 1 // The flag for subproblem lower bound checker. // 0 -- no subproblem LB checking. // 1 -- check subproblem LB. SUB_LB_CHECK 0 // The flag for seed generation method // 0 -- user provide seeds manually // 1 -- SD generates seeds automatically AUTO_SEED 0 // 16 digits are recommended for the seed // Random number seed for generating observations of omega. // RUN_SEED1 9495518635394380 RUN_SEED1 9495518635394380 RUN_SEED2 4650175399072632 RUN_SEED3 6070772756632709 RUN_SEED4 5451675876709589 RUN_SEED5 5285327724846206 RUN_SEED6 5588857889468088 RUN_SEED7 1098833779416153 RUN_SEED8 6192593982049265 RUN_SEED9 4756774140130874 RUN_SEED10 6784592265109609 RUN_SEED11 9728429908537680 RUN_SEED12 1163479388309571 RUN_SEED13 3279282318700126 RUN_SEED14 8773753208032360 RUN_SEED15 9337302665697748 RUN_SEED16 4415169667296773 RUN_SEED17 4220432037464045 RUN_SEED18 3554548844580680 RUN_SEED19 1814300451929103 RUN_SEED20 5339672949292608 RUN_SEED21 5638710736762732 RUN_SEED22 3154245808720589 RUN_SEED23 2414929536171258 RUN_SEED24 7998609999427572 RUN_SEED25 7080145164625719 RUN_SEED26 3648862740490586 RUN_SEED27 7772725003305823 RUN_SEED28 5982768791029230 RUN_SEED29 1395182510837913 RUN_SEED30 3735836402047426 cCs,t|ƒ}|tjkr(td|ttjƒfƒ‚|S)NzValue %s is not one of %s.)ÚstrÚ_domain_sd_toleranceÚ_valuesÚ ValueError)Úval©rú9/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/solvers/sd.pyrÓs  r)ÚlooseÚnominalÚtightz csdeZdZeddd„ƒZ‡fdd„Zdd„Zedd „ƒZed d „ƒZ dd d„Z dd„Z dd„Z ‡Z S)ÚSDSolverNc CsV|dkrtƒ}t|dtdtdttjƒdddtdt|dtdtd dddtdt|d td td dddtdt|d tdtddddtdt|dtdtddddtdt|dtdt ddddtdt|dtdt ddddtdt|dtdt ddddtdt|dtdt ddddtdt|dtdtddddtdt |dtd|S) NÚstopping_rule_tolerancer zXStopping rule tolerance used by the SD solver. Must be one of: %s. Default is 'nominal'.r)ÚdomainÚ descriptionÚdocZ visibility)Zap_groupÚsingle_replicationFzLDisables multiple replication procedure in SD and uses a single replication.Ú print_cycleédzHNumber of iterations between output of solution data to screen and file.Ú eval_run_flagaSet to evaluate on the run. This should be only used for instances with relatively complete recourse. This flag is not recommended because accurate function evaluations are unnecessarily time consuming. It is best to use a large print cycle when this option is activated.Ú eval_flagz]Set to get an estimated objective function value for the final incumbent of each replication.Ú eval_seed1lùÝ6ƒlKztRandom number seed for re-sampling omegas during optimality test. Default is None, meaning no seed will be provided.Ú eval_errorg{®Gáz„?z\Objective evaluation is accurate to within this much, with 95%% confidence. Default is 0.01.Úmean_devgš™™™™™©?zLSolution tolerance for deciding the usage of mean solution. Default is 0.05.Úmin_iterationszpNumber of iterations which must pass before optimality is checked. Default is None, meaning no minimum is given.Úmax_iterationsiˆzDMaximum number of iterations for any given problem. Default is 5000.Úverbose) rr rrrrÚ_sd_group_labelÚboolrÚintr rr )ÚclsÚoptionsrrrÚ_declare_optionsàsÖ zSDSolver._declare_optionscs&tt|ƒ | ¡¡| ¡d|_dS)NÚsd)Úsuperr"Ú__init__Úregister_optionsÚset_options_to_defaultÚ _executable)Úself)Ú __class__rrr:sszSDSolver.__init__cCs| ¡|_dS)N)r;Ú_options)r>rrrr<xszSDSolver.set_options_to_defaultcCs|jS)N)r@)r>rrrr6{szSDSolver.optionscCsdS)Nr8r)r>rrrÚnamesz SDSolver.nameFcKsR|jtkrtdƒ‚| d¡}tj |d¡}|jd<| d|¡tj |dd¡}| d|¡tj |dd¡}| d|¡tj |d ¡}| d |¡|  d ¡r¨t d |ƒt  |¡tj  |¡sÂt ‚tj  |¡rÒt ‚| |¡|  d ¡rötj |d¡} ntj |d¡} | d| ¡t|tƒrÚspÚoutput_solver_logÚkwdsZworking_directoryZconfig_filenameZsdinput_directoryZsdoutput_directoryÚlogfileÚsolution_filenameZ input_filesÚ_ÚkeyZ _cmd_stringÚstartÚrcrDÚstopr^ÚresultsrrrÚ _solve_implƒsŽ                        zSDSolver._solve_implc Cs–t|dƒ€}| d¡dkr<| d¡| d¡| d¡nd| d¡dkrj| d¡| d¡| d ¡n6| d¡d kr˜| d¡| d ¡| d ¡nd s t‚| d| d¡r´dnd¡| d| d¡¡| d| d¡rädnd¡| d| d¡rdnd¡| d| d¡¡| d| d¡¡| d| d¡¡| d¡dk rj| d | d¡¡| d!| d"¡¡| t¡WdQRXdS)#z Writes an SD config file Úwr#rz// nominal tolerance z EPSILON 0.01 z SCAN_LEN 64 r zEPSILON 0.001 z SCAN_LEN 256 r!zEPSILON 0.00001 z SCAN_LEN 512 FzMULTIPLE_REP %d r'rézPRINT_CYCLE %d r(zEVAL_RUN_FLAG %d r*z EVAL_FLAG %d r+zEVAL_SEED1 %d r,zEVAL_ERROR %r r-z MEAN_DEV %r r.r/Nz MIN_ITER %d z MAX_ITER %d r0)ÚopenrNÚwriterRÚsd_advanced_config_section)r>ÚfilenameÚfrrrrSs8         zSDSolver._write_configc Csji}t|ƒ,}x$|D]}| ¡ ¡\}}|||<qWWdQRXtƒ}d|_d|j_d|j_d|j_i} yÄt|dƒ®}| ¡}|  d¡sŽt ‚| ¡d ¡dks¦t ‚| ¡}|  d¡s¼t ‚| ¡}|  d¡sÒt ‚| ¡}|  d¡sèt ‚| ¡}|  d ¡s |  d ¡s t ‚| ¡}|  d ¡s$t ‚| d ¡d ¡|j_t   |jjt jtjtjf¡\|_|j_|j_| ¡}|  d ¡s~t ‚| d ¡}|d ¡dkrœn°t|ƒdks®t ‚|d}d|kr>| d¡}t|ƒdksÜt ‚t|dƒ|_|d  d¡sþt ‚|d d¡st ‚t|ddd…ƒt|ddd…ƒf|_nt|dƒ|_| ¡}|  d¡sdt ‚| d ¡}|d ¡dkr‚n¢d|dkr|d d¡}t|ƒdks°t ‚t|dƒ|_|d  d¡sÒt ‚|d d¡sæt ‚t|ddd…ƒt|ddd…ƒf|_nt|d ¡ƒ|_| ¡}| ¡dks>t ‚| ¡}|  d¡sVt ‚| ¡}|  d¡snt ‚| ¡}|  d¡s†t ‚d} | ¡}x|  | ¡s¬| ¡}q”W| ¡}|  d¡sÆt ‚| ¡ ¡ ¡}xH|r|dd…\} } |  ¡} t| ƒ} | | || <| ¡ ¡ ¡}qØWWdQRXWn4ttfk r`t d|t ¡f¡d} YnX| |fS)z Parses an SD solution file NÚrzProblem:rmrCzFirst Stage Rows:zFirst Stage Columns:zFirst Stage Non-zeros:zReplication No.zNumber of replications:zStatus:ú:z%Total Objective Function Upper Bound:Úéz half-widthú,rú[éú]éÿÿÿÿz%Total Objective Function Lower Bound:zFirst Stage Solutions:zV No. Row name Activity Lower bound Upper bound Dual Dual STDEVzY------ ------------ ------------- ------------- ------------- ------------- -------------zT No. Column name Activity Lower bound Upper bound Reduced Cost RC STDEVéz>Exception encountered while parsing sd solution file '%s': %s')rnÚstripÚsplitrÚstatusr]Ztermination_conditionÚmessageÚreadlineÚ startswithrRÚ_sd_status_mapÚgetrÚunknownrrÚlenÚfloatZ objectiveÚendswithZobjective_intervalÚboundZbound_intervalÚIOErrorÚOSErrorÚloggerÚwarnÚ tracebackÚ format_exc) r>Zsymbols_filenamerdZ symbol_maprrÚlineZ lp_symbolZscenario_tree_idrjr^Zxhat_start_lineZvarlabelZvarvaluerrrr\!s²         $ zSDSolver._read_solution)N)F)Ú__name__Ú __module__Ú __qualname__Ú classmethodr7r:r<Úpropertyr6rArkrSr\Ú __classcell__rr)r?rr"Þs     | r"cCsv|dkrtƒ}t |¡t |¡t|dƒt|dƒt|dƒt|dƒt|dƒt|dƒt|dƒt|dƒ|S) Nr1Ú disable_gcÚprofilerŽÚoutput_scenario_tree_solutionÚkeep_solver_filesraÚsymbolic_solver_labels)rr"r;rr )r6rrrÚrunsd_register_options™s*  rœc CsÈt ¡}t|ƒ’}| ¡tdƒtdƒtƒ}|j|dd}|j|||j|j|j d}|j }|` tdƒt|ƒ|j ržtdƒ|j   ¡|j  ¡|j  ¡WdQRXtdƒtdt ¡|ƒdS) zO Construct a senario tree manager and solve it with the SD solver. ruz5Running SD solver for stochastic programming problemsT)Úsparse)r6raršr›Nz!Total execution time=%.2f secondsr)rXrZ initializerOr"Zextract_user_options_to_dictZsolveraršr›r^r™r_ZsnapshotSolutionFromScenariosZpprintSolutionZ pprintCosts)r6Ú start_timer`r8Z sd_optionsrjr^rrrÚrunsd°s6    rŸc Cs\ddl}yt|tddd}Wn$tk r@}z|jSd}~XYnXtt|d|j|j|j dS)NrrŸz2Optimize a stochastic program using the SD solver.)Úprogr%zrunsd: )Z error_labelr—Z profile_countrŽ) Z pyomo.environrrœÚ SystemExitÚcoderrŸr—r˜rŽ)ÚargsrUr6Z_excrrrÚmainØs r¤r8Ú__main__)N)N)?rJÚsysrXrŽÚloggingZpyutilib.subprocessrYZpyutilib.servicesZ pyomo.corerZ pyomo.optrrrZ!pyomo.pysp.util.configured_objectrZpyomo.pysp.util.configrrr r r r r rrrZpyomo.pysp.util.miscrrZpyomo.pysp.scenariotree.managerrZpyomo.pysp.convert.smpsrUZpyomo.pysp.embeddedsprZpyomo.pysp.solvers.spsolverrrZ'pyomo.pysp.solvers.spsolvershellcommandrÚ getLoggerrŒr2rƒZoptimalÚokr…ÚwarningZ noSolutionÚerrorrprrrr&r"rœrŸr¤Zregister_solverr‘ÚexitrrrrÚs`  0          ~> ( "