B @`@sddlmZddlZddlZddlZddlmZmZddlZ ddl m Z ddl m Z mZmZmZddlmZddlmZddlZddlmZdd lmZd d Zd d ZddZGddde jjjZdS))divisionN)yamlyaml_load_args) phextension)isVariableNameIndexedextractVariableNameAndIndexindexMatchesTemplate indexToString)/transmit_external_function_invocation_to_worker)2scenario_tree_node_variables_generator_noinstances) iteritems)xrangec cst|}tj|ft}WdQRXxt|D]\}}d}xX|jjddD]D}||jkrf|j}PqNx |jD]}|j|krn|g}PqnW|dk rNPqNWt |dkrt d|x6t|D](\} } d} d} t | rt | \} } n| } d} i} x|D]x}g}| |j<xdt|j D]V\}\}}| |kr| dkrP|dkrPt d| d|t|| r||qWqWxN|D]F}t t| t | kstt | |jdkrvt d| |jfqvWx"t| D]\}}||| fVqWqWq.WdS)Nrz\***WW PH Extension: %s does not name an existing non-leaf Stage or Node in the scenario treez+***WW PH Extension: Illegal match template=z specified in file=zENo variables matching string '%s' were found in scenario tree node %s)openrloadrr _scenario_tree_stages_name _tree_nodeslen RuntimeErrorrr _variable_idsrappendsetAssertionError)phfilenamefZ config_dataZnode_or_stage_nameZvariable_dicts node_liststage tree_nodeZvariable_stringZvariable_config_data variable_nameZindex_templateZmatch_variable_idsnodeZnode_match_variable_ids variable_idnameindexZ config_nameZ config_valuer&D/tmp/pip-unpacked-wheel-bi3529v6/pyomo/pysp/plugins/wwphextension.py_parse_yaml_filesR         r(cCs\|jdk sti}xDt|jD]6\}}|dd}|sL|j|jf||<qd||<qW|S)Nr)NN)Z_variable_datasrr Zis_expression_typelbub)r Z var_boundsr#ZvardatasZvardatar&r&r'collect_node_variable_boundscs  r+cCs,i}x"|D]}|j|}t|||<q W|S)N)_tree_node_mapr+)rZ scenario_treeZscenario_or_bundlerZvariable_bounds node_namer r&r&r' external_collect_variable_boundsrs   r.c@seZdZejjejejj dddZ ddZ ddZ dd Z d d Zd d ZddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Z d0d1Z!d2d3Z"d4d5Z#d6S)7 wwphextensionZ WWPHExtensionc Osd|_dddddddd d d d d dg |_i|_d|jd<d|jd<d|jd<d|jd<ddddg|jd<d|jd<d|jd<d|jd<tt|j|jdksti|_x|jD]}i|j|<qWx|jD]}i|j|<qWi|_ d|_ d|_ d|_ g|_ d|_dS)NFIter0FixIfConvergedAtLBIter0FixIfConvergedAtUBIter0FixIfConvergedAtNB CostForRhoFixWhenItersConvergedAtLBFixWhenItersConvergedAtUBFixWhenItersConvergedAtNB CanSlamToLB CanSlamToMin CanSlamToUB CanSlamToMaxCanSlamToAnywhereSlammingPriorityZ going_priceobj_effect_family_nameZobj_effect_family_factorZdecision_hierarchy_levelZdownZupZeitherNoneZfeasibility_directionZ relax_intZreasonable_intZlow_intr)_iteration_0_called_valid_suffix_names_AnnotationTypesrr intersectionkeysr _suffixes_variable_stage_configuration_filename_suffix_filename_annotation_filenamevariables_with_detected_cyclesvariable_fixed)selfargskwdsr$r&r&r'__init__sH          zwwphextension.__init__c Csbg|_|jdkrdStj|jdkr8td|jdtd|jxt||jD]\}}}||jkrttd|d}d}d}yt |}d}Wnt k rYnXyt |}d}Wnt k rYnX|s|std|jd||j |}x,t |D] \} } x| D]} ||| <q WqWqTWt|j d |_|jgkr^|jj|j d jdd dS) NFz$***WW PH Extension: The suffix file z( either does not exist or cannot be readz5WW PH Extension: Loading variable suffixes from file=z1***WW PH Extension: %s is not a valid suffix nameTzXWW PH Extension unable to deduce type of data referenced in ww ph extension suffix file=z; value=r<)keyreverse) slam_listrGospathexistsrprintr(r@bool ValueErrorintrDr listrCsortget) rKrZ suffix_nameZ suffix_value variable_idsZis_intZis_boolconverted_valueself_suffix_dictr-node_variable_idsr#r&r&r'process_suffix_filesL      z!wwphextension.process_suffix_filec Cs>i|_|jdkrdStj|jdkr8td|jdtd|jxt||jD]\}}}||jkrtdtdx|jD] }t|qWtd|jd||j|dk r||j|krtd|jd |d ||d kr||jkrd |j|<|j |}x.t |D]"\}}x|D]} t || <qWqWqTWdS) NFz(***WW PH Extension: The annotation file z( either does not exist or cannot be readz8WW PH Extension: Loading variable annotations from file=zError encountered.zEHere are the annotations that can be given (they are case sensitive):z WW ph extension annotation file=z; contains unknown annotation: z$; contains unknown annotation value=z for: r=g) Z_obj_family_normalized_rhorHrRrSrTrrUr(rArDr r]) rKrZannotation_nameZannotation_valuer\ir^r-r_r#r&r&r'process_annotation_files8         z%wwphextension.process_annotation_filec Cst|jtjjjjjsNx2|jj ddD]}x|j D]}t ||_ q4Wq(Wni}|j rx|jj D]r}|sf|jdj}d}x"|jjD]}||jkr|j}PqW|dkst||kr|jg||<qf|||jqfWnNxL|jj D]@}|s|jdj}||kr|jg||<q|||jqWi}x8t|D],\} } | f} t|| ddd| d} | || <q:Wd} t|}x`| |kr|j}|j|}||} x(t|D]\}}|jj|}||_ qW| d7} qxWdS)Nrrz pyomo.pysp.plugins.wwphextensionr.T)Zreturn_action_handle function_args) isinstanceZ_solver_managerpyomoZsolversZpluginsZsmanagerZphpyroZSolverManager_PHPyrorrrr+_variable_boundsZcontains_bundlesZ is_leaf_node _scenariosrZ_scenario_bundlesZ_scenario_namesrrr r rZwait_anyZ get_resultsr,)rKrrr Z nodes_neededZrepresentative_scenarioZrepresentative_bundleZbundleZaction_handle_mapZ object_namerrcZnew_action_handleZnum_results_so_farZ num_resultsZahresultsr-Z node_boundsr&r&r'_collect_variable_boundsEs`             z&wwphextension._collect_variable_boundscCs |dS)N)rN)rKrr&r&r'resetszwwphextension.resetcCsdS)Nr&)rKrr&r&r'pre_ph_initializationsz#wwphextension.pre_ph_initializationcCsdS)Nr&)rKrr&r&r'post_instance_creationsz$wwphextension.post_instance_creationc s"d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d |_tjjd j|_d|_ d|_!d|_"d|_#d|_$d|_%tjjd j|_&t'd |j&d |_(d|_)d |_*d |_+|j+|_,d |_-d|_.d|_/|j0}xjjdd D]xjD]z}t12|j3d|_4t12|j3d|_5t1fdd|j3D|_6t12|j3g|_7t12|j3d|_8|9fdd|j3DqPWqDW|j:dk rt;j<=|j:dkrvt>d|j:yVt?j@rLtA|j:d,}tB|C|j:d}tD|tEktFfWdQRXn tG|j:WntHd|j:YnXntHd|j:dnt>d|I|J|jdkrt>d|j|j_Kd|_Lx:jMD]0}|jNdkr||_LPn|jNdkr||_LPqW|jLdkrtHddS)NFrgg?gh㈵>T rdrdi%i3lig?c3s&|]}jD]}||fdfVq qdS)rN)Z_iteration_index_set).0r#Zph_iter)rr&r' ,sz7wwphextension.post_ph_initialization..c3s|]}|fVqdS)Nr&)rrr#)rr&r'rs7sz@WW PH Extension: Loading user-specified configuration from file=rbexecz(Failed to load WW PH configuration file=z+***WW PH Extension: The configuration file z( either does not exist or cannot be readzOWW PH Extension: No user-specified configuration file supplied - using defaultszOWW PH Extension: Setting mipgap to %.7f, as specified in the configuration filezNormalized term diffz Term diffzfWW PH extension plugin failed to locate a normalized term-diff or term-diff converger on the PH object)OVerbosecumulative_discrete_fixed_count!cumulative_continuous_fixed_countfix_continuous_variables(fix_converged_discrete_variables_at_exitZIteration0MipGap InitialMipGap FinalMipGapMipGapExponentWHashComparisonToleranceReportCycleWHashDetailStartMipGapFromIter0MetricRhoReductionFactorScenarioRhoPerturbationr0r1r2r4r5r6FixWhenItersConvergedContinuousr7r8r;r:r9PH_Iters_Between_Cycle_Slamsrrrr SlamAfterIterSlamOnlyIfNotConvergingCheckWeightOscillationAfterIter#FixIfWeightOscillationCycleLessThanZComputeRhosWithpreSEPComputeRhosWithSEP RhosSEPMultCycleLengthSlamThresholdmaxW_hash_history_lenReportPotentialCycles_last_slam_iter W_hash_seedW_hash_rand_valW_hash_aW_hash_bW_hash_crEdictfromkeysr_num_iters_converged_last_converged_val_w_hash_w_sign_vector_w_last_sign_flip_iterupdaterFrRrSrTrUsixPY3rcompilereadruglobalslocalsZexecfilerr`rb_mipgapconverger_to_useZ _convergersr)rKrmy_stage_suffixr rcodeZ convergerr&)rrr'post_ph_initializations               z$wwphextension.post_ph_initializationcCsvd|_|||jd}|jd}|jd}|jd}x6t|jddddD]\}}}} } } | dkrN|jrt||krt|j|} d} d}xB|jD]8}|j}|j |j |}| |t || 7} ||7}qW| |} | ||j|}| ||j|}||rt||d d }n t| d }|js6|jrXtd |||j|t|f||||||j|| dkrN||rH| ||j|}| ||j|}|||||||j}||kr||}|j}||kr||}|j}||kr||}||||||r,|||||n|jd krn||||qN|j|}|j|}| |||||qNWdS) NTr0r1r2r3F)includeDerivedVariablesincludeLastStagerZgrdz5wwphextension is setting rho to %f for variable id=%sr)!r?rjrDr rr _averagesrhZ _probability_xrmathfabsInt_If_Close_Enough _minimums _maximumsis_variable_discreterrv_verboserUrstrZsetRhoAllScenarios_int_convergence_trackingr0r1r2 _should_fix_discrete_due_to_conv_fix_varr_w_history_accounting _continuous_convergence_tracking)rKrr0r1r2r3rr r#variable_valuesis_fixedis_staleZ node_averageZdeviation_from_averageZsumprobscenarioZscenario_probabilityZ var_valuenode_minnode_max denominatorr)r*nbr&r&r'post_iteration_0_solveshs            "         z%wwphextension.post_iteration_0_solvescCsdS)Nr&)rKrr&r&r'post_iteration_0szwwphextension.post_iteration_0c Cs |jdkr(|js(td||d|_|jdkr|jdkr|j|jkr|jdkrptd|j|j|_n|jjd}|jjd}|j }|jj }|}|j dkr|}d||||}d||j }|j} |j} || | | } | | kr td| | f| } n td | | |_dS) NrdzhWW PH extension detected iteration 0 solves were skipped. Executing post_iteration_0_solves plugin code.TrzOWW PH Extension: Setting mipgap to %.7f, as specified in the configuration fileFg?zpWW PH Extension: ***CAUTION - setting mipgap to thresholded maximal initial value=%.7f; unthresholded value=%.7fzYWW PH Extension: Setting mipgap to %.7f, based on current value of the convergence metric) _current_iterationr?rUrr{r|rrZ_metric_historyZ lastMetricZ_convergence_thresholdrr}) rKrZm0m1mZmlastZ basis_valuexyZg0ZglastZnew_gapr&r&r'pre_iteration_k_solvess<           z$wwphextension.pre_iteration_k_solvescCs|jd}|jd}|jd}g|_d|_tdxt|jddddD]\}}}}} } |j|\} } | dkrH| dkrH| dks| dks| | krH|j|\} }| t|}| |r| ||j |}| ||j |}| ||||||j}||kr||}|j}||kr||}|j}||kr4||}||||||rZ|||||nb|jdkr||||||||d\}}|dkr||jks|jr|j|||||fqH|j |}|j |}|jdkr|||||||||||||rH|||||qHWt|jdkrtd |jsF|jrVtd td x^|jD]R\}}}}}|js|jrtd |||f||jkr`|j|j|j kr`|js|jrt|td t!|jd|"||#||||n|dkrJ||jkrJ|js"|jrt|tdt!|dt!|jnh||jkr`|j|j|j kr`|jsz|jr`t|tdt!|dt!|jdt!|j dq`W|j|j$kr|j|j|j kr|j%r|j&'|j r|j%std|"|d|_(nd|_(nd|_(xt|jddddD]z\}}}}} } |j)|}|j|}|j*dkr6|j|j*kr6|dks||j+krn"|,|||dkr6d|j)|<dSq6WdS)Nr4r5r6Fz/WW PH Extension: Analyzing variables for fixingT)rrrZrzQWW PH Extension: Cycles were detected - initiating cycle analysis and remediationz#WW PH Extension: Cycle information:z2Variable Cycle Length Cycle Detailsz%20s %4d %sz: Cycle length exceeds iteration slamming threshold=z; choosing a variable to slamrdz- Taking no action to break cycle - length=z does not exceed slam threshold=z does exceed slam threshold=z3, but another variable was slammed within the past z iterationsz:Slamming criteria are satisifed - accelerating convergence)-rDrIrJrUr rrgrr rrrrrr4r5r6rrrrcompute_cycle_lengthrrrr"_should_fix_continuous_due_to_convrrvrrrrr_pick_one_and_slam_it_reset_w_reduce_rhorrrZ isImprovingZ_just_slammed_rrr_slam)rKrr4r5r6rr r#rrrZlbvalZubvalr!r%Zfull_variable_namerrr)r*rZcomputed_cycle_lengthmsgZ cycle_msgZlast_flip_iterZ flip_durationr&r&r'post_iteration_k_solves(s                           &         z%wwphextension.post_iteration_k_solvescCsdS)Nr&)rKrr&r&r'post_iteration_kszwwphextension.post_iteration_kcCs |jdkrtd||dS)NTzPWW PH extension: Fixing all discrete variables that are converged at termination)rzrU%_fix_all_converged_discrete_variables)rKrr&r&r'post_ph_execution s zwwphextension.post_ph_executioncCs*tt|||jkr"tt|S|SdS)N)absround_integer_tolerancerX)rKrrr&r&r'rs z!wwphextension.Int_If_Close_EnoughcCsd||krLt|tkrL||j|kr6|j|d7<q`d|j|<||j|<nd|j|<d|j|<dS)Nrdrg?)typerXrr)rKrr r#rrr&r&r'rs   z'wwphextension._int_convergence_trackingcCsnt|||jkrVt||j||jkr@|j|d|j|<qjd|j|<||j|<nd|j|<d|j|<dS)NrdrgUHc?)rrrr)rKrr r#rrr&r&r'r.s   z.wwphextension._continuous_convergence_trackingc Cs|j|_g}|j|}xp|jD]f}|j|j|}d}|dkrBd}|j||jf||j7<|||j |j |j|j |_qW|j||jf} d} x.t dt |D]} || || kr| d7} qW||j|<| dkr|j|j|<dS)NTgFrrd)rrrrh_wrrrrrrrr rr) rKrr r#Znew_sign_vectorZold_sign_vectorrZ weight_valueZ weight_signZhash_valZ num_flipsrar&r&r'rDs*     z#wwphextension._w_history_accountingcCs|j|dkr||jkrd}|j||jf}xtt|j|jdd|jddD]l}|j||f}|dkr~td}nt ||t |d}||j krX|dkr|j |\} } t d| t | d|jd t|d t|d t|d t|j|} |jr|j |\} } t d | t | t d|t dxjtt|j|jdd|jddD]B} |j|| f} t | |t |}t d| | |dfqrW|j|| fSqXWdS)NrrdginfgY@TzA Possible cycle detected via PH weight hashing - variable=z node=z Current hash value=z' matched (within tolerance) hash value=z found at PH iteration=z; cycle length=z)Cycle W hash history detail for variable=zCurrent hash value: %fz>Iteration Hash Value % Diff. Relative to Current Hash Valuez%4d %16.6f %10.4f)r)rZ_fixedrrr rrfloatrrr~rrUr rrr)rKrr r#Zreport_possible_cyclesZcurrent_hash_valueraZthis_hash_valueZ relative_diffr!r%rjZiteration_j_hash_valuer&r&r'rfs8    6 z"wwphextension.compute_cycle_lengthcCs|j|\}}|js|jrZtd|t|d|jdt|dt|j|d|||| |r|j d7_ n|j d7_ d|_ dS)Nz Fixing variable=z at tree node=z to value=z; converged for z iterationsrdT) rrvrrUr rrrZ fix_variablerrwrxrJ)rKrr r# fix_valuer!r%r&r&r'rs 6  zwwphextension._fix_varc Cs|dkr|j||krdS|j|\}}|j|}|dk r\|dkr\|j||kr\||kr\dS|dk r|dkr|j||kr||krdSdS)NrTF)rrgr) rKr r#Zlb_itersZub_itersZnb_itersr)r*Zconvalr&r&r'rs  z.wwphextension._should_fix_discrete_due_to_convcCs(|jr$|jdkr$|j||jkr$dSdS)NrTF)ryrr)rKr r#r&r&r'rs  z0wwphextension._should_fix_continuous_due_to_convc Cs|j|\}}|j|\}}||rZ|||j|}|||j|} t|j|} n|j|}|j|} |j|} d} d} |jd ||j r|} d} |jd ||j r|} d} |jd ||j r|} d} |jd ||j r| } d } |jd  ||jr| } d } | dkr:td |t|d |jdStd|t|d |jdt| d| ||||| dSdS)Nrr7z lower boundr8z node minimumr9z upper boundr:z node maximumr;znode average (anywhere)z. Warning: Not allowed to slam variable=z at tree node=Fz Slamming variable=z to value=z; value=T)rrgrrrrrrrDr[r7r8r9r:r;rUr rrr) rKrr r#r!r%r)r*rrZanywhereZslam_basis_stringrr&r&r'rsV              0zwwphextension._slamcCs|j}xf|jD]\}d}||kr||}x2|jD](}||jkr.||sT||||}Pq.W|r|j|_dSqW|jsz|j rt dt |jdkr|js|j rt ddS)NFzY Warning: Nothing free with a non-zero slam priority - no variable will be slammedrzA (No Slamming Priorities were specified in a suffix file.)) rErQrrZis_variable_fixedrrrrvrrUr)rKrrr#ZdidoneZvariable_stager r&r&r'r1s$       z#wwphextension._pick_one_and_slam_itc CsJ|js |jr|jrFtddS) Nz! Resetting W for variable=z to zerog?z/ and multiplying rho by the RhoReductionFactor=gz/- new average (across all scenarios) rho value=z; perturbing rho per-scenarior)rvrrUrrrhrrZ_rhorrrrandomuniform) rKrr r#r!Zaverage_rho_valuerr)r*r&r&r'rZs.          &z!wwphextension._reset_w_reduce_rhoc Csd}xt|jdddD]n\}}}}}}|dkr|dkr||r|||j|} |||j|} | | kr||||| |d7}qWtdt|dS)NrF)rrrdzVWW PH Extension: Total number of variables fixed at PH termination due to convergence=) r rrrrrrrUr) rKrZnum_variables_fixedrr r#rrrrrr&r&r'r|s"    z3wwphextension._fix_all_converged_discrete_variablesN)$__name__ __module__ __qualname__rfcommonpluginZ implementsrZ IPHExtensionaliasrNr`rbrjrkrlrmrrrrrrrrrrrrrrrrrrrr&r&r&r'r/s>A>>CV3^ "5&$ ?)"r/) __future__rrrRrZpyomo.common.dependenciesrrZpyomo.common.pluginrfZ pyomo.pysprZpyomo.pysp.phutilsrrrr Zpyomo.pysp.phsolverserverutilsr Zpyomo.pysp.generatorsr rr Z six.movesr r(r+r.rrZSingletonPluginr/r&r&r&r' s      D