B _Ad@s~ddlZddlmZmZmZddlZddlmZddl m Z ddl m Z m Z ddlZGddde ZGdd d ejjjZdS) N) iteritems itervaluesprint_) phextension)ConvergenceBase)minimizemaximizec@seZdZddZddZdS)EcksteinCombettesConvergercOs"tj|f||d|_d|_dS)NzEckstein-Combettes (u,v) norm)r__init___name_last_computed_uv_norm_value)selfargskwdsrQ/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/plugins/ecksteincombettesextension.pyr sz#EcksteinCombettesConverger.__init__cCs|jS)N)r )r phZ scenario_treeZ instancesrrr computeMetric$sz(EcksteinCombettesConverger.computeMetricN)__name__ __module__ __qualname__r rrrrrr sr c@seZdZejjejejj dddZ ddZ ddZ dd Z d d Zd d ZddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&S)'EcksteinCombettesExtensionZecksteincombettesextensioncCsHddl}|dtdd|_d|_g|_d|_d|_i|_d|_ dS)Nriz'Kludge warning: set random seed to 1234FzPhiSummary.csv) randomseedprint _check_output_JName_subproblems_to_queue'_queue_only_negative_subphi_subproblems_total_projection_steps_projection_step_of_last_update _converger)r rrrrr 0s z#EcksteinCombettesExtension.__init__cCsttdxf|jjddD]R}xL|jD]B}x<|jD]2}d}x(|jD]}||j||j|j7}qBWq2Wq&WqWdS)Nz} |j|}||j||7}| j|}|| j||7}qWqWqWqW|jrusz>EcksteinCombettesExtension.compute_updates..z-**** YIKES! QUEUING SUBPROBLEMS AT RANDOM****z ****HERE****z SCENARIO=zSCENARIO BUNDLE=zQueueing sub-problem=%szKQueueing sub-problems whose scenarios possess the most negative phi values:zFQueueing sub-problems whose scenarios possess the smallest phi values:z %30s %16e)2rrr r#r$r%Z _dual_mode RuntimeError _averages_zr'_wr Z_rho_xr&Z_objective_senser_xbars_for_solve _ws_for_solver(_urr)_vstrmathsqrtopenrwriteZ_current_iteration _node_listZis_variable_stalesortedkeys_xbarsr!r appendrlenrshuffle_async_buffer_lengthcontains_bundlesZget_scenario_bundleZfoobarrr),r r subproblemsscenario_solve_countsZ scale_factor subproblemr*r+Ztree_node_averagesZ tree_node_zsr.Z weight_valuesZ rho_valuesZ var_valuesr,ZvarvalZ z_for_solveZ w_for_solver-Zp_unormZp_vnormZ this_v_valZ this_u_valfphiZ sub_phi_mapZcumulative_sub_phiZthis_sub_phi_term scenario_nametau denominatorthetaZunorm_normalizerZthis_node_unorm_normalizerZ this_z_valueZvnorm_normalizerZthis_scenario_vnorm_normalizerZthis_scenario_wsZscalarized_normZsub_phi_to_scenario_mapr7Znegative_sub_phisZall_phisZ sorted_phisrrrcompute_updatesTs                      *     $          $2     "             $ "         z*EcksteinCombettesExtension.compute_updatescCs |dS)N)r )r rrrrresetsz EcksteinCombettesExtension.resetcCsdS)zCalled before PH initializationNr)r rrrrpre_ph_initializationsz0EcksteinCombettesExtension.pre_ph_initializationc Csft|jdP}|d|dx |jjD]}|d|jq,W|d|dWdQRXdS)z,Called after the instances have been createdwz/Phi Summary; generally two lines per iteration z Iteration z, %10sz, Subproblems Returnedr3N)rErrFr#r'r )r rrSr.rrrpost_instance_creations   z1EcksteinCombettesExtension.post_instance_creationcCsP|jstdd|_x|jjD]}d|j|j<qWtdd|_|j |jdS)zCalled after PH initializationzOPH is not in async mode - this is required for the Eckstein-Combettes extensionrgh㈵>)Zconvergence_thresholdN) Z _async_moder9rr#r'r r r r!Z _convergersrK)r rr.rrrpost_ph_initializations z1EcksteinCombettesExtension.post_ph_initializationcCsd|_d|_dS)z#Called after the iteration 0 solvesFN)Z_ph_weight_updates_enabledZ_ph_xbar_updates_enabled)r rrrrpost_iteration_0_solvessz2EcksteinCombettesExtension.post_iteration_0_solvescstdtdxr|jjD]f}i|_i|_xT|jddD]Bj}|dk sPt|jdd|D|jdd|Dq:WqWxf|jj ddD]R}xL|j D]Bj}|dk stt dd|D_ t fd d|D_ qWqWxV|jj ddD]B}x<|j D]2j}|dk s"tt fd d|D_q WqWx |jjD]}|||jqNW|j}d d |jjD}t||d |j|_dS)zQCalled after the iteration 0 solves, averages computation, and weight computationzPOST ITERATION 0 CALLBACKz$****ADDING Y, U, V, and Z PARAMETERSNr"css|]}|dfVqdS)gNr)r6r,rrr sz>EcksteinCombettesExtension.post_iteration_0..css|]}|dfVqdS)gNr)r6r,rrrr`scss|]}|dfVqdS)rNr)r6irrrr`sc3s|]}|j|fVqdS)N)r:)r6ra)r+rrr`sc3s|]}|j|fVqdS)N)r;)r6ra)r+rrr`scSsg|] }|jqSr)name)r6rRrrrr8sz?EcksteinCombettesExtension.post_iteration_0..r)rr#r'r(r@rGr&AssertionErrorupdater$r%dictrAr;rJrPasynchronous_pre_scenario_queuerbrNrrMr)r rr.Znodal_index_setr*rRZasync_buffer_lengthZall_subproblemsr)r+rpost_iteration_0s6     " z+EcksteinCombettesExtension.post_iteration_0cCsdS)z$Called before each iteration k solveNr)r rrrrpre_iteration_k_solvessz1EcksteinCombettesExtension.pre_iteration_k_solvescCsdS)z#Called after the iteration k solvesNr)r rrrrpost_iteration_k_solvessz2EcksteinCombettesExtension.post_iteration_k_solvescCsdS)z(Called after the iteration k is finishedNr)r rrrrpost_iteration_ksz+EcksteinCombettesExtension.post_iteration_kcCsdS)z5Called before the asynchronous solve loop is executedNr)r rrrrpre_asynchronous_solvessz2EcksteinCombettesExtension.pre_asynchronous_solvescCsg}|jr:x<|j|jD]}||j|qWn||j|x|D]}i|_x6|jddD]$}tddt |j D|j|j <qlWi|_ x<|jddD]*}tddt |j |j D|j |j <qWqRWdS)z8Called right before each subproblem solve is been queuedNr"css|]\}}||fVqdS)Nr)r6kvrrrr`%szMEcksteinCombettesExtension.asynchronous_pre_scenario_queue..css|]\}}||fVqdS)Nr)r6rlrmrrrr`)s)r#rOZ get_bundleZscenario_namesrKZ get_scenarior>rGrerr;r r?r<)r rZsubproblem_nameZscenarios_to_processrUr.r+rrrrfs  $z:EcksteinCombettesExtension.asynchronous_pre_scenario_queuecCs"tdtd||||dS)zeCalled after a batch of asynchronous sub-problems are solved and corresponding statistics are updatedr1z1Computing updates in Eckstein-Combettes extensionN)rrY)r rrPrQrrrpost_asynchronous_var_w_update+sz9EcksteinCombettesExtension.post_asynchronous_var_w_updatecCsdS)z4Called after the asynchronous solve loop is executedNr)r rrrrpost_asynchronous_solves1sz3EcksteinCombettesExtension.post_asynchronous_solvescCs|j}g|_|S)zHCalled after subproblems within buffer length window have been processed)r)r rresultrrr!asynchronous_subproblems_to_queue5sz s