B ÔÂ_0ã@s¤ddlmZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZddlmZddlmZmZGd d „d eƒZGd d „d ejjjeƒZdS) é)ÚdivisionN)Ú phextension)Úminimize)Ú UndefinedData)Ú itemgetter)Ú iteritems)Ú _PHBoundBaseÚ$ExtractInternalNodeSolutionsforInnerc@sleZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„ZdS)Ú_PHBoundExtensionImplcCst |¡dS)N)rÚ__init__)Úself©r úG/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/plugins/phboundextension.pyr !sz_PHBoundExtensionImpl.__init__cCsRy t|ƒ}Wntd|jƒd}YnX| |¡| |¡| |¡|dk rJ| |¡| ||¡|jr†td|j r~dndƒ|j |j dd}t |ƒràtd|j r®dndƒ|j rÄt dƒnt d ƒ|j|<|j|j|<nT|jrtd |ƒ|j ¡r| ¡| ¡| ||¡\}}||j|<||j|<| |¡| |¡n2|j rbt dƒ|j|<nt d ƒ|j|<|j|j|<|j|j|d | ¡|j |j dd}t |ƒrætd ƒ|j rÊt d ƒnt dƒ|j|<|j|j|<n^|jrtd |ƒ|j ¡r| ¡| ¡| ||¡\|j|<|j|<|j|j|d| |¡dS)NzsFailed to extract candiate xhat for inner bound computation using xhat_method %s. Skipping inner bound computation.zComputing objective %s boundÚinnerÚouterF)Z warmstartZexception_on_failurezDFailed to compute %s bound at xhat due to one or more solve failuresÚinfz-infzgSuccessfully completed PH bound extension fixed-to-xhat solves for iteration %s - solution statistics: )rzGFailed to compute duality-based bound due to one or more solve failureszjSuccessfully completed PH bound extension weight-term only solves for iteration %s - solution statistics: )r)r ÚprintZ _xhat_methodZCachePHSolutionZRelaxPHFixedVariablesZ"DeactivatePHObjectiveProximalTermsZ DeactivatePHObjectiveWeightTermsZFixScenarioTreeVariablesÚ_verboseÚ_is_minimizingZsolve_subproblemsZ_disable_warmstartsÚlenÚfloatZ_inner_bound_historyZSTATUS_SOLVE_FAILEDZ_inner_status_historyZ_scenario_treeZcontains_bundlesZreport_bundle_objectivesZreport_scenario_objectivesZComputeInnerBoundZRestoreLastPHChangeZ STATUS_NONEÚ_update_reported_boundsZ_push_w_to_instancesÚ_outer_bound_historyÚ_outer_status_historyÚComputeOuterBoundZ RestorePH)r ÚphZ storage_keyZ candidate_solZfailuresZIBvalZIBstatusr r rÚ_iteration_k_bound_solves%sl                    z/_PHBoundExtensionImpl._iteration_k_bound_solvescCs | ¡dS)zCInvoked to reset the state of a plugin to that of post-constructionN)r )r rr r rÚresetªsz_PHBoundExtensionImpl.resetcCsdS)z2 Called before PH initialization. Nr )r rr r rÚpre_ph_initialization®sz+_PHBoundExtensionImpl.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 rr r rÚpost_instance_creation´sz,_PHBoundExtensionImpl.post_instance_creationc CsÖ|jrtdƒ|jtkrdnd|_d}d}tj |¡rŠtd|ƒt|ƒ}|  ¡}WdQRXt |t ƒrxtd|ƒ||_ qÒt d t|ƒƒ‚nH|tjkrÀt tj|ƒ|_ td t|j ƒd |ƒntd t|j ƒƒdS) z0 Called after PH initialization z9Invoking post initialization callback in phboundextensionTFZPHBOUNDINTERVALzPHB_.DATz:phboundextension is getting the update interval from file=Nzupdate interval=z6The value must be of type integer, but the value read=z'phboundextension using update interval=z&, extracted from environment variable=z/phboundextension using default update interval=)rrZ_objective_senserrÚosÚpathÚisfileÚopenÚreadÚ isinstanceÚintÚ_update_intervalÚ RuntimeErrorÚstrÚenviron)r rZupdate_interval_variable_nameZupdate_interval_file_nameZifileZifilevalr r rÚpost_ph_initialization¼s(     z,_PHBoundExtensionImpl.post_ph_initializationcCs`|jrtdƒ|jr*tdƒ| |¡dSd}| ||¡\|j|<|j|<|j|j|ddS)z5 Called after the iteration 0 solves zs      %