B @`H@sddlZddlZddlZddlZddlmZddlmZm Z m Z m Z m Z m Z mZddlmZddlmZmZedZGdddejjjeZdS) N) SolverFactory) ConcreteModelVarRealsParam ObjectiveConstraintListminimize) 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-bi3529v6/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)r ZCachePHSolutionZRelaxPHFixedVariablesZ"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)rphZ 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/r0csd}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..r1) r$ _scenarios _node_liststr_namefind_componentr2sumgetattr)mexprscenario tree_nodenew_w_variable_namenew_w_k_parameter_name)r')r6r7robj_rules   .zNconvexhullboundextension._construct_bundle_dual_master_model..obj_rule)ZsenseZrule)rrr$r9r:r;r<setattrrZ_standard_variable_idsrrrr Z TheObjectiverZ W_BalanceZ_stagesZ _tree_nodesZ _probabilityr=addV_Bound) rr'rBrCrDrErFZstageidxrAZscenario_probabilityr7r)r'r#_construct_bundle_dual_master_modelsF          $z.)Z solver_io)Z/pyomo.solvers.plugins.solvers.persistent_solverrL_current_iterationr$r9Z _objectiverr<rAssertionErrorcopydeepcopy_xr:r;rr=_wr?r>rIrHrZ _solver_typeZ _solver_io isinstanceZ set_instanceZsolveZ solutionsZ load_from)rr'rLZcurrent_iterationrBZprimal_objective_valuerEr6 ph_weightsrJZv_varrArDZsolverresultsr)rMrNrCr7r"_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+) r$r9r:r;r<rr=rTvalue)rr'rBrCrDr7rVrJrrrr%s   z,convexhullboundextension._push_weights_to_phcCs|j|j_dS)N)Z_rhorr2)rr'rrrr6sz$convexhullboundextension._assign_ckscCs |dS)N)r)rr'rrrreset>szconvexhullboundextension.resetcCsdS)z2 Called before PH initialization. Nr)rr'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)rr'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) r#rZ_objective_senser rosenvironint_update_intervalr;rK)rr'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) r#rZ_ph_warmstartedpre_iteration_k_solvesr%r!r"r&rX)rr'ph_iterrrrpost_iteration_0_solvesjs  z0convexhullboundextension.post_iteration_0_solvescCsdS)zl Called after the iteration 0 solves, averages computation, and weight computation! Nr)rr'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)r#rrOr`r(rXZ_ph_weight_updated_enabledr)rr'rcrrrrbs    z/convexhullboundextension.pre_iteration_k_solvescCsdS)z6 Called after the iteration k solves! Nr)rr'rrrpost_iteration_k_solvessz0convexhullboundextension.post_iteration_k_solvescCsdS)zd Called after the iteration k is finished, after weights have been updated! Nr)rr'rrrpost_iteration_ksz)convexhullboundextension.post_iteration_kcCsB|jrtd|j}||jdkr.|||||dS)z1 Called after PH has terminated! z s $