B _'@sdZddlZddlmZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZed ZGd d d ejjjeZdS) ao Tight bounds should be correlated with effective W values; that is almost a tautology. A closely related measure of quality is generated by the konvw extension that produces wonly.ssv showing the convergence of each variable when the proximal term is *not* used. A truly good W should produce convergence without the proximal term (which is zero at convergence). N)fabs) phextension)2scenario_tree_node_variables_generator_noinstances) indexToString) itemgetter) _PHBoundBasez pyomo.pyspc@seZdZejjejejj dddZ ddZ ddZ ddZ d d Zd d Zd dZddZddZddZddZddZddZdS)phweightinspectextensioncCs<t|d|_td|jt|jd}|d|_dS)Nz wonly.ssvz*konvw is creating W-only convergence file=wg{Gz?)r__init__ wonly_fileprintopenclose+_valid_weights_objective_relative_tolerance)selfofiler3szIphweightinspectextension._inspect_variable_convergence..css$|]}|jD]}|jdfVq qdS)rN)rr)rrrrrrr8srF)ZincludeDerivedVariablesZincludeLastStagegaT)keyreversez;  )dict_scenario_treeZ_stagesrrrr r rsorteditemsrZ _variable_idswritestrrr)rphph_iterZ term_diffZnode_fixed_cntZtotal_fixed_cntrrZ variable_idZvariable_valuesZis_fixedis_staleZ var_node_avgZ var_valueZscenario_probabilityZ var_term_diffrZ variable_nameindexrrr_inspect_variable_convergence/s@  Jz6phweightinspectextension._inspect_variable_convergencec Cs||}||||||j|j dtd||jrT| | i}x|jj D]}|j ||j <qjW||||j|j dtd||jr| | i}x|jj D]}|j ||j <qWtdtdt|jdd}x|jj D]}t||j ||j t||j }||jkrltdt|j d t|dd }n"tdt|j d t|dqW|rtd td td ntdtdtdtd|||||dS)N)Z warmstartzaSuccessfully completed PH weight inspection extension iteration %s solves - solution statistics: zqSuccessfully completed PH weight inspection extension iteration %s solves (FIXED TO XBAR) - solution statistics: z6Weight Inspection Results: (Using relative tolerance: )F z: FAIL (relative error: Tz: OKAY (relative error: z******************************z Weight Inspection Failed! zWeight Inspection Okay)Z0ExtractInternalNodeSolutionsWithDiscreteRoundingZCachePHSolutionZ"DeactivatePHObjectiveProximalTermsZ_push_w_to_instancesZsolve_subproblemsZ_disable_warmstartsr r!Zcontains_bundlesZreport_bundle_objectivesZreport_scenario_objectivesZ _scenariosZ _objectiverZFixScenarioTreeVariablesreprrrr%r*Z RestorePH) rr&Z storage_keyZ candidate_solZweight_only_ph_objectiveZscenarioZfixed_xbar_ph_objectiveZfailureerrorrrr_iteration_k_solvesgsV       "(  z,phweightinspectextension._iteration_k_solvescCs |dS)N)r )rr&rrrresetszphweightinspectextension.resetcCsdS)z2 Called before PH initialization. Nr)rr&rrrpre_ph_initializationsz.phweightinspectextension.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_creationsz/phweightinspectextension.post_instance_creationcCs|jrtddS)z0 Called after PH initialization z9Invoking post initialization callback in phboundextensionN)_verboser )rr&rrrpost_ph_initializationsz/phweightinspectextension.post_ph_initializationcCsdS)z5 Called after the iteration 0 solves Nr)rr&rrrpost_iteration_0_solvessz0phweightinspectextension.post_iteration_0_solvescCsdS)zc Called after the iteration 0 solves, averages computation, and weight computation Nr)rr&rrrpost_iteration_0sz)phweightinspectextension.post_iteration_0cCsdS)zB Called immediately before the iteration k solves Nr)rr&rrrpre_iteration_k_solvessz/phweightinspectextension.pre_iteration_k_solvescCsdS)z6 Called after the iteration k solves! Nr)rr&rrrpost_iteration_k_solvessz0phweightinspectextension.post_iteration_k_solvescCs0|jrtd|j|r,|j}|||dS)z\ Called after the iteration k is finished, after weights have been updated! z6Invoking post iteration k callback in phboundextensionN)r4r Z _convergerZ isConvergedZ_current_iterationr0)rr&r'rrrpost_iteration_ks  z)phweightinspectextension.post_iteration_kcCsdS)z1 Called after PH has terminated! Nr)rr&rrrpost_ph_executionsz*phweightinspectextension.post_ph_executionN)__name__ __module__ __qualname__pyomocommonpluginZ implementsrZ IPHExtensionaliasr r*r0r1r2r3r5r6r7r8r9r:r;rrrrr s 8` r)__doc__loggingmathrZpyomo.common.pluginr?Z pyomo.pysprZpyomo.pysp.generatorsrZpyomo.pysp.phutilsroperatorrZ#pyomo.pysp.plugins.phboundextensionr getLoggerloggerr@rAZSingletonPluginrrrrrs