B _H@svddlZddlZddlZddlZddlmZddlTddlm Z ddl m Z m Z e dZGdddejjje ZdS) N) SolverFactory)*) phextension) _PHBoundBase$ExtractInternalNodeSolutionsforInnerz pyomo.pyspc@seZdZejjejejj dddZ ddZ ddZ ddZ d d Zd d Zd dZddZddZddZddZddZddZddZddZdd Zd!S)"convexhullboundextensioncOs t|d|_i|_i|_dS)N)r__init__ _master_model_past_objective_values_past_var_values)selfargskwdsrO/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/plugins/convexhullboundextension.pyrs z!convexhullboundextension.__init__cCst|}||||||||||||||j|j d}t |rt d|j rzt dnt d|j|<|j|j|<nH|jrt d||jr|||||\|j|<|j|<t d|j rdnd||j|||||||j|j d d }t |rjt d |j rNt dnt d|j|<|j|j|<nL|jrt d ||jr|||||\|j|<|j|<|j|j|d ||dS)N) warmstartzGFailed to compute duality-based bound due to one or more solve failuresz-infinfzUSuccessfully completed PH bound extension iteration %s solves - solution statistics: zComputing objective %s boundinnerouterF)rZexception_on_failurezAFailed to compute bound at xbar due to one or more solve failureszgSuccessfully completed PH bound extension fixed-to-xbar solves for iteration %s - solution statistics: )r)rZCachePHSolutionZRelaxPHFixedVariablesZ"DeactivatePHObjectiveProximalTermsZCachePHWeights_push_weights_to_phZ_push_w_to_instances _assign_cksZsolve_subproblemsZ_disable_warmstartslenprint_is_minimizingfloat_outer_bound_historyZSTATUS_SOLVE_FAILED_outer_status_history_verbose_scenario_treeZcontains_bundlesZreport_bundle_objectivesZreport_scenario_objectivesComputeOuterBound_update_reported_boundsZ DeactivatePHObjectiveWeightTermsZFixScenarioTreeVariablesZ_inner_bound_historyZ_inner_status_historyZComputeInnerBoundZ RestorePH)r phZ storage_keyZ candidate_solZfailuresrrr_iteration_k_bound_solves,sP            z2convexhullboundextension._iteration_k_bound_solvesc st|_xjjD]}x|jddD]}dt|jdt|j}dt|jdt|jd}t|j|t|j t dt|j|t |j t ddd t|jd t|jtt dq&WqWt|jd t d dd fdd}t t |d|j_t|j_xjjddD]}x|jD]~}xv|j D]l}d} xN|jD]D}|j} dt|jdt|j}|j|} | | | |7} q6W|jj| dkq&WqWqWt|j_dS)NWVAR__WDATA__K)withingT)r(defaultmutableV_CKg?)r)r*csd}xjjD]}x|jddD]~}dt|jdt|j}dt|jdt|jd}|||||dd|jtfd d D7}q"W|t|d t|j8}qW|S) Ngr#r$r%r&r'g?g@c3s.|]&}|dd||VqdS)g@Nr).0i) w_k_parameter w_variablerr szaconvexhullboundextension._construct_bundle_dual_master_model..obj_rule..r+) r _scenarios _node_liststr_namefind_componentr,sumgetattr)mexprscenario tree_nodenew_w_variable_namenew_w_k_parameter_name)r!)r0r1robj_rules   .zNconvexhullboundextension._construct_bundle_dual_master_model..obj_rule)ZsenseZrule)Z ConcreteModelr rr3r4r5r6setattrZVarZ_standard_variable_idsZRealsZParamZ ObjectiveminimizeZ TheObjectiveZConstraintListZ W_BalanceZ_stagesZ _tree_nodesZ _probabilityr7addV_Bound) r r!r<r=r>r?r@Zstageidxr;Zscenario_probabilityr1r)r!r#_construct_bundle_dual_master_modelsF          $z.)Z solver_io)Z/pyomo.solvers.plugins.solvers.persistent_solverrG_current_iterationrr3Z _objectiver r6r AssertionErrorcopydeepcopy_xr4r5r r7_wr9r8rDrCrZ _solver_typeZ _solver_io isinstanceZ set_instanceZsolveZ solutionsZ load_from)r r!rGZcurrent_iterationr<Zprimal_objective_valuer?r0 ph_weightsrEZv_varr;r>Zsolverresultsr)rHrIr=r1r"_populate_bundle_dual_master_models>      &    z;convexhullboundextension._populate_bundle_dual_master_modelcCs~xx|jjD]l}xf|jddD]T}dt|jdt|j}|j|}|j|j}x|D]}||j||<q\WqWq WdS)Nr#r$r%) rr3r4r5r6r r7rOvalue)r r!r<r=r>r1rQrErrrr%s   z,convexhullboundextension._push_weights_to_phcCs|j|j_dS)N)Z_rhor r,)r r!rrrr6sz$convexhullboundextension._assign_ckscCs |dS)N)r)r r!rrrreset>szconvexhullboundextension.resetcCsdS)z2 Called before PH initialization. Nr)r r!rrrpre_ph_initializationAsz.convexhullboundextension.pre_ph_initializationcCsdS)z Called after PH initialization has created the scenario instances, but before any PH-related weights/variables/parameters/etc are defined! Nr)r r!rrrpost_instance_creationGsz/convexhullboundextension.post_instance_creationcCs||jrtd|jtkrdnd|_d}|tjkr\ttj||_tdt |jd|ntdt |j| |dS) z1 Called after PH initialization! zAInvoking post initialization callback in convexhullboundextensionTFZPHBOUNDINTERVALz/convexhullboundextension using update interval=z&, extracted from environment variable=z7convexhullboundextension using default update interval=N) rrZ_objective_senserBrosenvironint_update_intervalr5rF)r r!Zupdate_interval_variable_namerrrpost_ph_initializationOs z/convexhullboundextension.post_ph_initializationcCsj|jrtd|jr*td||dSd}|||\|j|<|j|<|j|j|d||dS)z$Called after the iteration 0 solves!zDInvoking post iteration 0 solve callback in convexhullboundextensionzKPH warmstart detected. Bound computation requires solves after iteration 0.N)r) rrZ_ph_warmstartedpre_iteration_k_solvesrrrr rS)r r!ph_iterrrrpost_iteration_0_solvesjs  z0convexhullboundextension.post_iteration_0_solvescCsdS)zl Called after the iteration 0 solves, averages computation, and weight computation! Nr)r r!rrrpost_iteration_0sz)convexhullboundextension.post_iteration_0cCsf|jrtd|jd}||jdkr*dS||||||jdkrbtdd|_||dS)zC Called immediately before the iteration k solves! zCInvoking pre iteration k solve callback in convexhullboundextensionrNzZWE ARE PAST ITERATION 5 - STARTING TO TRUST CONVEX HULL BOUND EXTENSION FOR WEIGHT UPDATESF)rrrJr[r"rSZ_ph_weight_updated_enabledr)r r!r^rrrr]s    z/convexhullboundextension.pre_iteration_k_solvescCsdS)z6 Called after the iteration k solves! Nr)r r!rrrpost_iteration_k_solvessz0convexhullboundextension.post_iteration_k_solvescCsdS)zd Called after the iteration k is finished, after weights have been updated! Nr)r r!rrrpost_iteration_ksz)convexhullboundextension.post_iteration_kcCsB|jrtd|j}||jdkr.|||||dS)z1 Called after PH has terminated! z s