B @`@sddlZddlZddlmZmZmZddlmZddlZddl m Z m Z ddl m Z ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZddlmZm Z m!Z!m"Z"ddl#m$Z$dd l%m&Z&dd l'm(Z(m)Z)dd l*m+Z+m,Z,dd l-m.Z.dd l/m0Z0ddl1Z1e12dZ3ddl4Z5e5j6j7dddkZ8dZ9e:e!j;e!je:e!j?e!j@gZAddZBddZCieC_DddZEddZFddZGGddde(ZHdS)N)iterkeys iteritemsStringIO)xrange)reset_redirectsetup_redirect)toc)minimizevalueTransformationFactory ComponentUIDBlock ConstraintConstraintListParamVarVarListSet ObjectiveSuffixBinaryNonNegativeIntegers) SolverFactory SolverStatusTerminationCondition ProblemFormat) phextension)SolverManager_PHPyro)SingletonPlugin implements)preprocess_block_constraintspreprocess_block_objectives)InvocationType)NormalizedTermDiffConvergencez pyomo.pysp)Fc s|r|j|j}n |j|j}|d}|dk r8|St|_}|d|_|d|_ |d|_ |d|_ |d|_ t |dkstt|_ttd|_ttd|_|}tt|j}t|d |_}t|d d |_t|d dd |_t !|d |j|_"t !|d|j|_#|j rhx<D](} | $|j | %|jq:Wn &di} i|_'x:|D]2} t(|j| dd| } t !| )|| <qWfdd} t*|| d|_+}t,rtdd|j-t.d d dD}nt|j/t.d d d}t |dks*t|d}t !||_0t.t1fdd|Dt2d|_3d|krvt4t4j5d|_6t7r|8ni}t9||d|j3:|:t;d|S)N_interscenario_pluginepsilon cut_scale allow_slack enable_rho enable_cutsr)Zwithin) initializeT)Zdense)Zmutabler-zPHRHO_%sz PHWEIGHT_%scs|||dkS)Nr)bi)_param_sep_srcr.D/tmp/pip-unpacked-wheel-bi3529v6/pyomo/pysp/plugins/interscenario.py_set_var_valuevsz-get_modified_instance.._set_var_value)Zrulecss|]}|dVqdS)r$Nr.).0xr.r.r4 sz(get_modified_instance..)activeZ descend_intor&c3s|]}|dVqdS)r$Nr.)r6r0)r2r.r4r8s)exprZsensedual) direction)idMapz9InterScenario plugin: generated modified problem instance)<contains_bundlesZ_bundle_binding_instance_map_name _instances componentr r'popr(r)r*r+r,lenAssertionErrorrcutlistrr abs_int_varsrabs_binary_vars findRootNodelistrZ_variable_datasr STAGE1VARrseparation_variablesrfixed_variable_valuesweakrefrefrhoweightssetlbsetubfixlocal_stage1_varmapr Zfind_component_onrfixed_variables_constraint PYOMO_4_0Zall_component_datarZcomponent_data_objects original_objsumr separation_objrZ IMPORT_EXPORTr;"FALLBACK_ON_BRUTE_FORCE_PREPROCESS preprocessr deactivater)ph scenario_treescenario_or_bundleoptionsmodelr/rootNodeZvar_idsZ_S1VidxZ _cuidBufferr0Z_cuidr5_conZ_orig_objective_mapr.)r1r2r3r4get_modified_instance7sp                     rfc Cst|tjkrFydtjt|<t||Sdtjt|<t||Si}|jj}tjt|rttd}tr~|j|ddn ||t r| ni}t |j|d|j |dd}|j j}|j j}|tjkr|tkrd} n|tkrd} nd } | rtd | fnDtr||n |j|x$|jjD]} |j|| || <q0Wtrd|j|ddd n|j|dd n&x$|jjD]} |j|| || <q~W|S) NFTzcore.relax_discrete)inplace)r=) warmstart INFEASIBLE NONOPTIMALzpResolving subproblem model with relaxed second-stage discrete variables failed (%s). Dual values not available.)rgundo)rl)idget_dual_valuesdiscrete_stage2_varsr'rUr rVapplyapply_torZr[r solvesolverstatustermination_conditionrok"_acceptable_termination_conditions"_infeasible_termination_conditionsloggerwarningload solutions load_fromrJr;) rsradualsrdxfrmreresultssstcstateZvaridr.r.r4rnsT        rncstd}tr|j|ddn |||j}||j|j |j |j }|r|j }x2D] }| d|dqdWntr|ni}t||dt||dt} t| zBy|j|dd} Wn*tdtd| YnXWdtX| jj} | jj} | tjkr`| t kr`d} trR|!| n |j"#| n| t$krtd } d }nd } d }| r|rn"td | ftd| n2t%fdd|j&D}t'|j}t()||f}| *||j|r4x8D]$}| | ||q Wn +dtrV|j|dddn|j|ddtrlni}t||d|S)Nzcore.relax_discreteT)rg)r=)teez@Exception raised solving the interscenario evaluation subproblemzSolver log: %srirjz!!!!rkz????z@Solving the interscenario cut separation subproblem failed (%s).c3s*|]"}|t|t|ffVqdS)N)r )r6vid)_parr2r.r4r8:sz+solve_separation_problem..r)rgrl)rl),r rVrprqr'rWr\rYactivaterLrKr*r(rQrRZunfixrZr[r!r rrrrryrzgetvaluerrsrtrurrvrwr{r|r}rxdictrJr mathsqrtcloserS)rsrafallbackr_blockr*r(rcre output_bufferrrrranscutobjr.)rr2r4solve_separation_problems                   rc st|||}|j}|j|j|j}|jxD|D]<\} } tfddt| D} | dkr0|| dkq0Wx|D]} g} xt| dD]\} }|j }|j }|j }|j }||||k||| j |k||| j d|k|| |||k| |qW|t| tfddt| dDdkqvWt rt|ni}t||d|r |jj|dd}|jj}|jj}|tjkr|tkrtr||n |j||jtd dtDt|fSdSdS) Nc3sV|]N\}\}}t|td|krd|d|||dVqdS)r&r$N)absmax)r6r0r2r)r3r)r(r.r4r8gs zadd_new_cuts..rr&c3s2|]*\}}|dkr|n d|VqdS)g?r&Nr.)r6rval)r3r.r4r8s)r=T)rhcss |]\}}|t|fVqdS)N)r )r6_idZ_varr.r.r4r8s)rfr'r(r)rErTrXraddrFrGZubappendrZr[r _solverrrrsrtrurrvrwrVr{r|r}rr rW)r]r^r_feasibility_cutsincumbent_cutsresolvemrZ_cutlistZcut_objrr:Z _int_binariesrrr/czyrerrrr.)r3r)r(r4 add_new_cutsZsV            rc Ks6t|||f|}|j}|j}|j}|j} |r8|j} n|jg} td} | } | d| g} g}g}x|D]\}}d}x| D]}||krd}PqW|rt |t |kstxt|D]\}}|||<qWtr|ni}t||dtd|jft}t|zDy|jj|dd}Wn*tdtd |YnXWdtXtd ||jf|jj}|jj}|tj kr.|t!kr.d}t"r|#|n |j$%||& }|& d|'t(|)|& ||j*r|'t+|j|n |'d| 'd q~ds>|t,krd }|'d|'d|j-rt.|j|d}|d kr|j/t0j1kr|t.| |d}nd}| '|td||jfq~d}|'d|'d| 'dtd|ftd |q~W|& || | | 2||| fS)NipoptrFT)r=zpreprocessed scenario %s)rz@Exception raised solving the interscenario evaluation subproblemzSolver log: %sz3solved solution from scenario set %s on scenario %sz. r&z????zX zJsolved separation problem for solution from scenario set %s on scenario %sr$z? z_scenario_namesr?rrOZextract_valuesZ store_valuesrrCrDrrZr[r rrrrrrryrzrrrsrtrurrvrwrVr{r|r}rPrr rWr+rnrxr,rZproblem_formatrnlr\)r]r^r_Zscenario_solutionsZ model_optionsrarr1r2rdZlocal_scenariosrZ_saved_rho_valuesrE obj_values dual_valuesZ var_valuesZscenario_name_listlocalscenarioZvar_idZ var_valueZ var_id_maprrrrrZ_saved_w_valuesrr.r.r4solve_fixed_scenario_solutionss                    rc@seZdZeejddZddZddZddZ d d Z d d Z d dZ ddZ ddZddZddZddZddZddZd'ddZd d!Zd"d#Zd$d%Zd&S)(InterScenarioPlugincCsdd|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d |_ d |_ d |_ d |_ d|_d |_dS)NTgHz>rFdg)\$@gn$@gQ?g?g?g?g-C6?g{Gzd?)enableRhoUpdatesenableFeasibilityCutsenableIncumbentCutsr(r)allow_variable_slackiterationIntervalconvergenceRelativeDegredationconvergenceAbsoluteDegredationrecutThresholdrepeated_solution_threshholdrhoScale rhoDampingcutThreshold_minDiffcutThreshold_crossCutiteration0RecutBoundImprovement)selfr.r.r4__init__0s zInterScenarioPlugin.__init__cCsBd|_d|_d|_d|_g|_g|_d|_d|_t|_ g|_ dS)Nr) incumbentrO x_deviationlastConvergenceMetricrrlastRunaverage_solutionr# convergerunique_scenario_solutions)rr]r.r.r4resetVszInterScenarioPlugin.resetcCs||dS)N)r)rr]r.r.r4pre_ph_initializationbs z)InterScenarioPlugin.pre_ph_initializationcCs2|jr.|j}x|jD]}|||dqWdS)Nr)r_scenario_treerH_xbarssetRhoAllScenarios)rr]rbvr.r.r4post_instance_creationfs   z*InterScenarioPlugin.post_instance_creationcCs^t|jjdkrtd|jtkr&dnd|_|jrZ|j}x|j D]}| ||dqDWdS)Nr$z5InterScenario plugin only works with 2-stage problemsr&r) rCrZ_stages RuntimeErrorZ_objective_senser _sense_to_minrrHrr)rr]rbrr.r.r4post_ph_initializationms  z*InterScenarioPlugin.post_ph_initializationcs||||d}x|jdkr|jr|d7}td|fg}x@t|jD]2\}tfdd|jD}|rP||dqPW| |dtd||||qWd|_ dS)Nrr&z5InterScenario plugin: PH iteration 0 re-solve pass %sc3s"|]}t|tkrdVqdS)r&N)typetuple)r6r)rr.r4r8sz>InterScenarioPlugin.post_iteration_0_solves..Tz3InterScenario plugin: distributed cuts to scenarios) "_collect_unique_scenario_solutionsr'rOrr enumeraterrXextend_distribute_cutsr)rr]countZ_stale_scenarios_solnZ_was_cutr.)rr4post_iteration_0_solves}s$     z+InterScenarioPlugin.post_iteration_0_solvescCs(|j|j||j|j|j|_dS)N)rupdate_current_iterationrr@ lastMetricr)rr]r.r.r4post_iteration_0s   z$InterScenarioPlugin.post_iteration_0cCs|js |jr||dS)N)rrr)rr]r.r.r4pre_iteration_k_solvess  z*InterScenarioPlugin.pre_iteration_k_solvesc CsD|j|j||j|j|j}|j}||}d}|||jkrPt dd}|||j kr|||j kr|t d||fd}|j|j |j krt dd}|jdkrt dd}np|jr"|j}x\t|jD]N\}}|j|} |j|} ||jkr| | |jkrt d||| | fd}PqW|r:|j|_ ||||_dS)NFzBInterScenario plugin: triggered by no change in scenario solutionsTzJInterScenario plugin: triggered by convergence degredation (%0.4f; %+0.4f)z2InterScenario plugin: triggered by iteration limitz1InterScenario plugin: triggered to initialize rhozUInterScenario plugin: triggered by variable divergence with rho==0 (%s: %s; [%s, %s]))rrrrr@rrrrprintrrrrrOrrHrZ _maximumsZ _minimumsr(r') rr]currlastdeltarunrbrrO_max_minr.r.r4post_iteration_k_solvessL         z+InterScenarioPlugin.post_iteration_k_solvescCsdS)Nr.)rr]r.r.r4post_iteration_ksz$InterScenarioPlugin.post_iteration_kcCs||||dS)N)rr')rr]r.r.r4post_ph_executions  z%InterScenarioPlugin.post_ph_executionc sltd\}}}}||_xtjD]\}fdd|dD}tdtdd|Dtfdd|Djdkrd n d jd d d|Dd dd|Dd|dfq4WfddjD}fddjDtfddt|D} t |} t |} j dkrFd} d} n.j }| |t t | t |} dd| f} | _ td| | | | t d| | | fj r|_tfdd|D}tdd|D}jdkr.jj d|j|t|kr.| dks*| jkr.dS||\}}j}jdkrtdi_xt|D]\}}||j|<qjWnbj}xZt|D]N\}}j|rֈj|d|||j|7<n||j|<qWxBtt|D]2\}}|dkrt|nt|j|fqWj}jrhx&tjD]\}}|||qLWdS)Nz9InterScenario plugin: analyzing scenario dual informationcsg|]}j|qSr.)r get_scenario)r6r7)r]r.r4 sz=InterScenarioPlugin._interscenario_plugin..r&z Solution %2d: generated %2d cuts, cut by %2d other scenarios; objective %10s, scenario cost [%s], cut obj [%s] [generated by %s]css|]}t|tkrdVqdS)r&N)rr)r6rr.r.r4r8sz.c3s"|]}t|tkrdVqdS)r&N)rr)r6r)rr.r4r8sNonez%10.2fz, css|]}d|jVqdS)z%10.2fN)_cost)r6r7r.r.r4r8s css.|]&}t|tkrd|dnd|VqdS)z%5.2frz%5sN)rr)r6r7r.r.r4r8s,cs(g|] }|dD]}j|jqqS)r&)rrr)r6sr7)r]r.r4rscs(g|] }|dD]}j|jqqS)r&)rr _probability)r6rr7)r]r.r4rsc3s|]\}}||VqdS)Nr.)r6r0r) scenarioProbr.r4r8sz-----%z%+.2f%%rz7 Average scenario cost: %f (%s) Max-min: %f (%0.2f%%)gY@c3s$|]}tfdd|DVqdS)c3s,|]$}t|tkr|djkrdVqdS)rr&N)rrr)r6r7)rr.r4r8szFInterScenarioPlugin._interscenario_plugin...N)rX)r6r)rr.r4r8scss|]}t|VqdS)N)rC)r6rr.r.r4r8sr)outerz&InterScenario plugin: initializing rho)r_solve_interscenario_solutions_compute_objectivefeasible_objectivesrrrrXjoinrminrrrr_update_incumbentr_update_reported_boundsrrCr_process_dual_informationrrOrrsortedrrHrr)rr]partial_obj_valuesrcuts probabilityZsoln _scenariosZ scenarioCostsZ_avgrrZ_del_avgZ _del_avg_str_prevZcutCountZsubProblemCountZnew_rhologinfoZ_scalerrZ_dampinglrbr.)rr]rrr4r's          $        *   z)InterScenarioPlugin._interscenario_plugincCs*|j}g|_|j}x|jD]}t|j|j}x@t|j|jD],\}}||s`| |rDt t |||<qDWd}x0|jD]&} || dkr| d |jd}PqW|s|j ||jgfqWd} x6|D].\} } x$|jD]\} }| | kr| d7} PqWqWt d| t|jft| t|jS)NFrr&TzAInterscenario plugin: %s unchanged scenario solutions (out of %s))rrrHrr_xr?ris_variable_binaryis_variable_integerintroundrrrCfloat)rr]Z_old_unique_scenario_solutionsrbrZ _this_solrZ_valfoundZ_solZ _unchangedZ _old_solnZ _old_scenrZ_scenr.r.r4rPs6       z6InterScenarioPlugin._collect_unique_scenario_solutionscCsgggf}g}t|jt}g}|jr2|jj}n|jj}x|D]}||j|j f}|j |j |j |j |jd} |r||jjd|j|j|jtjjddd| |d q@t||j|f|| } x"t|D]\} } | | | qWq@W|rxd} t|}x|D]} | dg|qWx\| |krv|j}||}|j|} x"t|D]\} } | | | |<qPW| d7} qW||fS) N)r(r)r*r+r,invoke_external_functionTz pyomo.pysp.plugins.interscenarior) actionname queue_nameinvocation_typegenerateResponse module_name function_name function_kwds function_argsrr&) isinstance_solver_managerrrr>_scenario_bundlesrrrrr(r)rrrqueuer?_phpyro_job_worker_mapr"SingleInvocationkeyrrrCrwait_anyindex get_results)rr]rr distributedZaction_handles subproblemsproblemr`Z kwd_optionsZ_tmpr0rnum_results_so_far num_results_ahZ_ah_idr.r.r4rxsZ                z2InterScenarioPlugin._solve_interscenario_solutionsFcsd}tfddjD}|rJ|ttdjt|t|dndt|jt}|j rv|j j }dd}n|j j }dd}g}xD|D]:} g} xt jD]~\\} } d} x || D]}|j| krd } PqW| r| fd djDqjdkr| fd djDqW| sDjsD|dq|t| 7}|r||jjd | j|j| jtjjd d dd| j|fd qt||j | | j|}||td| j|rdndfqWtd|t|fg_jrtdtjfg_|rltdd|D}t|}x<||krj|j}||}|j|||<|d7}q0W|r|j }x|t |D]p\}} ||}|dkrqxN|| D]B}|d|_!t|dt|j"|jkst#|d|j"|j<qWqW|$dS)Nrc3s:|]2}|D](}t|tkr |djkr |dVq qdS)rN)rrr)r6r7r)rr.r4r8s  z7InterScenarioPlugin._distribute_cuts..r&cSs|jS)N)r)r7r.r.r4z6InterScenarioPlugin._distribute_cuts..cSs|gS)Nr.)r7r.r.r4r#r$FTc3s8|]0}t|tkr|djkr|VqdS)rN)rrr)r6r)rmrr.r4r8sc3s6|].}t|tkr|dkr|VqdS)rN)rr)r6r)allCutThresholdrmr.r4r8sr z pyomo.pysp.plugins.interscenarior) r r r r rrrrrz!distributed cuts to scenario %s%sz and resolved scenariorizIInterScenario plugin: added %d feasibility cuts from a library of %s cutsz-InterScenario plugin: added %d incumbent cutscss|]}|dkrdVqdS)Nr&r.)r6r7r.r.r4r8s)%rrrrrrCrrrrr>rrrrr?rrrrrrr"rrrrrrXrrrrHrrrDZupdate_variable_statistics)rr]rZ totalCutsZcutObjrrZ get_scenariosZresolvesrrr7rrrrr r!r"Z_ah_idxrbrr.)r%rmrr4rs                  z$InterScenarioPlugin._distribute_cutscCsng}xdtt|jD]R}d}x>t|D]2\}}|||dkrDd}P|||||7}q&W||qW|S)Ng)rrCrrr)rrrrsoln_idrZscen_or_bundle_idpr.r.r4rsz&InterScenarioPlugin._compute_objectivec s^ddtjD}|s$tddStdtdd|Dftfdd|Dtdd \}}g}g}g}|j}xt|j d j |j D]^} | | rq| | r|| q|| r|| q|| rd std q|| qWjdksjd j|jkrjrpjrp|spjdd jtfd d|Dtfdd|Df|jj||f_|j|ddj} t| t| n2jd j|jkrdj} t| d}|sjsdSxb|D]Z\} } | |krqj| d jtfdd|Dtfdd|DfqWdS)NcSsg|]}|ddk r|qS)r&Nr.)r6or.r.r4r,sz9InterScenarioPlugin._update_incumbent..zAInterScenario plugin: No scenario solutions are globally feasiblez-InterScenario plugin: Feasible objectives: %scss|]}|dVqdS)r&Nr.)r6r(r.r.r4r84sz8InterScenarioPlugin._update_incumbent..c3s$|]}|dj|dfVqdS)rr&N)r)r6r7)rr.r4r87sr&)rrFZFIXMEc3s|]}|t|fVqdS)N)r)r6r)rr.r4r8Qsc3s|]}|t|fVqdS)N)r)r6r)rr.r4r8Rs)innerz0InterScenario plugin: NEW incumbent: %s = %s, %sz,InterScenario plugin: incumbent: %s = %s, %src3s|]}|t|fVqdS)N)r)r6r)rr.r4r8osc3s|]}|t|fVqdS)N)r)r6r)rr.r4r8ps)rrrrroperator itemgetterrrHrrr?Zis_variable_fixedrrrZis_variable_semicontinuousrDrrr(rrrrrryinfo) rr]Z feasible_objZbest_idZbest_objZ binary_varsZ integer_varsZcontinuous_varsrbrmsgrr.)rrr4r+sh           z%InterScenarioPlugin._update_incumbentc* s,dgtj}xFtjD]8\}}x.|dD]"}|j|}|||j7<q.WqWt|} |jj} j dkrt fdd| D_ t dd| D} t dd| D} x.t|D] \}} j|d}t dd| D}d}xt|D]\}}|||dkrqx\t |||D]H\}}||t || ||||7<t| |t|| |<q$W||7}qW|rx|D]}|||<qWx..c3s|]}|dVqdS)rNr.)r6r)rr.r4r8sN)rrr)r6)r)rr4r8sz@InterScenarioPlugin._process_dual_information..css|]}|dfVqdS)gNr.)r6rr.r.r4r8scss|]}|dfVqdS)gNr.)r6rr.r.r4r8srcss|]}|dfVqdS)gNr.)r6rr.r.r4r8sg?css|]}|dkr|VqdS)rNr.)r6Z_rhor.r.r4r8sz<%4s: %6s [%7s, %7s] %7s; %6s [%6s, %6s] %6s; RHO %7s : %7s) z---ZDualrrstdevrrrr.Zcomputedfinalcss|]}t|VqdS)N)r)r6r7r.r.r4r8scss|]}|dVqdS)r$Nr.)r6r7r.r.r4r8sr$css|]}t|VqdS)N)r)r6r7r.r.r4r8scss|]}|dVqdS)r$Nr.)r6r7r.r.r4r8sgư>zQ%4d: %6.1f [%7.1f, %7.1f] %7.1f; %6.1f [%6.1f, %6.1f] %6.1f; RHO %7.2f : %%7.2f)rCrrrrrrXrHrrrrrcopysignrrrrrr)*rr]rrZ soln_probr&Z soln_infoZ src_scen_nameZsrc_scenZtotal_soln_probZxbarZmax_dualZ weighted_rhoZsoln_pr7Zavg_dualZp_totalZscen_idr'rdZx_devZvar_infokZ dual_infoZsidZscenario_resultsZsolutionZ_min_rhorr~Zd_minZd_maxZ_sumZ_sumsqnZd_avgZd_stdevZx_minZx_maxZx_avgZx_stdevr.)rr4rts     (  .   "     z-InterScenarioPlugin._process_dual_informationN)F)__name__ __module__ __qualname__rrZ IPHExtensionrrrrrrrrrrrr'rrrrrrr.r.r.r4r,s& & 1w(= j Ir)Ir*rsixrrrZ six.movesrrMZ pyutilib.miscrrZpyomo.common.timingrZ pyomo.corer r r r r rrrrrrrrrrZ pyomo.optrrrrZ pyomo.pysprZ%pyomo.solvers.plugins.smanager.phpyrorZpyomo.common.pluginrrZpyomo.repn.standard_repnr r!Zpyomo.pysp.phsolverserverutilsr"Zpyomo.pysp.convergencer#logging getLoggerryZ pyomo.versionZpyomoversion version_inforVrZsetZoptimalZgloballyOptimalZlocallyOptimalrwZ infeasibleZinvalidProblemrxrfrnrorrrrr.r.r.r4 sD  D       pGiG