B u `>C@sdZddlmZddlZddlmZddlmZmZm Z m Z ddl m Z m Z mZmZddlmZmZmZddlmZdd lmZdd lmZdd lmZdd lmZed ZddZ ddZ!ddZ"dS)zIteration loop for MindtPy.)divisionN) add_ecp_cuts)solve_OA_masterhandle_master_mip_optimal"handle_master_mip_other_conditionshandle_master_mip_infeasible)solve_NLP_subproblemhandle_NLP_subproblem_optimal handle_NLP_subproblem_infeasible'handle_NLP_subproblem_other_termination)minimize ObjectiveVar) ProblemSense)TerminationCondition)get_main_elapsed_time)PersistentSolver) SolverFactoryzpyomo.contrib.mindtpyc Cs|j}t|jtdd}xx|j|jkr|jd|jd|_|j dkrt ||\}}|j dkr|j j tjkrt|||n0|j j tjkrt|||d}Pnt|||||||ntt||ddrd}P|j dkrb|j dkrbt||\}}|j j tjtjtjhkr&t|||n0|j j tjkrDt|||nt||j j |||||t||ddrxd}P|j dkrt|j||qW|j rt!|||d S) a Main loop for MindtPy Algorithms This is the outermost function for the algorithms in this package; this function controls the progression of solving the model. Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: ConfigBlock contains the specific configurations for the algorithm T)activez!---MindtPy Master Iteration %s---r>ECPGOAOAF) check_cyclingrN)" working_modelnextcomponent_data_objectsr mip_iteriteration_limitloggerinfoZ mip_subiterstrategyr single_treesolvertermination_conditiontcoptimalr infeasiblerrZcall_after_master_solveNotImplementedErroralgorithm_should_terminaterlocallyOptimalfeasibler r r Zcall_after_subproblem_solvermipZadd_nogood_cuts bound_fix) solve_dataconfigrmain_objectiveZ master_mipmaster_mip_resultslast_iter_cuts fixed_nlpfixed_nlp_resultr4A/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/contrib/mindtpy/iterate.pyMindtPy_iteration_loop$sV         'r6c Csv|j|j|jkr>|jd|j|j|jtj|jj _ dS|j |j kr|jd|j |jd|j|j|j rtj|jj _ n tj|jj _ dSt|j|jkr|jd|jt|j|jd|j|jtj|jj _ dSt|j|jkrt|jd|j|j |jkr|jd|jd|j|j|jdk rbtj|jj _ n$|j|_|jd tj|jj _ dS|jd kr|jj}d d |j D}x|D]}|!ry |"}Wnt#t$fk rd }YnX||j% kr|jd|dS|&ry |'}Wnt#t$fk rFd }YnX||j% kr|jd|dSqW|j(dkr|j|_n|j|_|jd|j|j|j|_tj|jj _ dS|j)rr|j dkrr|rrg}x6|j*j+t,dD]$} | -r|.t/t0| j1qW||_2|j2|j3krj|jd|j |jd|j|jtj|jj _ dS|j2|_3dS)a Checks if the algorithm should terminate at the given point This function determines whether the algorithm should terminate based on the solver options and progress. (Sets the solve_data.results.solver.termination_condition to the appropriate condition, i.e. optimal, maxIterations, maxTimeLimit) Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: ConfigBlock contains the specific configurations for the algorithm check_cycling: bool check for a special case that causes a binary variable to loop through the same values Returns ------- boolean True if the algorithm should terminate else returns False zBMindtPy exiting on bound convergence. LB: {} + (tol {}) >= UB: {} Tz=MindtPy unable to converge bounds after {} master iterations.z"Final bound values: LB: {} UB: {}zVMindtPy unable to converge bounds before time limit of {} seconds. Elapsed: {} secondsz@Algorithm is not making enough progress. Exiting iteration loop.NzAlgorithm did not find a feasible solution. Returning best bound solution. Consider increasing stalling_limit or bound_tolerance.rcSsg|]}|jdkr|qS))r)bodyZpolynomial_degree).0cr4r4r5 sz.algorithm_should_terminate..izQMindtPy-ECP continuing as {} has not met the nonlinear constraints satisfaction. Fr8zIMindtPy-ECP exiting on nonlinear constraints satisfaction. LB: {} UB: {} )ctypezCycling happens after {} master iterations. This issue happens when the NLP subproblem violates constraint qualification. Convergence to optimal solution is not guaranteed.)4LBbound_toleranceUBrrformatr$r%resultsr"r#rrr!r*Z maxIterationsrtiming time_limitZ maxTimeLimitlen LB_progressZstalling_limitabszero_toleranceZbest_solution_foundrclonewarningZ noSolutionr MindtPy_utilsZconstraint_listZhas_lbZlslack ValueError OverflowErrorZ ecp_toleranceZhas_ubZuslackZobjective_senseZ cycling_checkr+rr is_integerappendintroundvalueZ curr_int_solZ prev_int_sol) r-r.rMindtPyZnonlinear_constraintsZnlcZ lower_slackZ upper_slacktempvarr4r4r5r(s     $                   r(cCs|jrD|jd|jjjtjkr2|j|j |_ n|j|j |_ nr|jdd|_ |dkrt ||\}}|j jtjtjtjhkrt|||n.|j jtjkrt|||nt||j j|||jj}|jdkr6|jjjtjkr|j|j }n |j|j }xZt|dt|jjdD]}|jj|qWn$|jdkrZ|jjt|jjt |j!}t"|t#r|j$|jdd t%|j&} t'|j(} t)t*|j+| d} |j!d kr| ,d g| d <| d -d |j.d kr|j.|j/d<|j0|jfd|j1i| } t2|j3j4t5dd} | jtkrRt*| jj6g|j7dd|_ |j7-|j n,t8| jj9g|j:dd|_ |j:-|j |jd;|j |j |j |j<|j krtj|jj _dS)NzMFix the bound to the value of one iteration before optimal solution is found.zcSolve the master problem without the last nogood cut to fix the bound.zero_tolerance is set to 1E-4g-C6?Frr8rT)Zsymbolic_solver_labelsZgams add_optionszoption optcr=0.001;rthreadstee)rr7z"Fixed bound values: LB: {} UB: {})=r!rrrBZproblemZsenserr Z stored_boundr@r>rHrr"r#r$r%r)r*r r&r r r+rKr Znum_no_good_cuts_addedrangerEZMindtPy_linear_cutsZ integer_cutsZ deactivaterZ mip_solver isinstancerZ set_instancedictZmip_solver_argsrrCrPmaxrDgetrOrWoptionsZsolveZ solver_teerrrr Z lower_boundrFminZ upper_boundZ UB_progressrAr?)r-r.r1r2r3rSZvalid_no_good_cuts_numiZ masteroptZmip_argselapsed remainingr0r/r4r4r5r,-sp              r,)#__doc__ __future__rloggingZ$pyomo.contrib.mindtpy.cut_generationrZpyomo.contrib.mindtpy.mip_solverrrrZpyomo.contrib.mindtpy.nlp_solverr r r Z pyomo.corer r rZpyomo.opt.resultsrZ pyomo.optrr$Zpyomo.contrib.gdpopt.utilrZ/pyomo.solvers.plugins.solvers.persistent_solverrr getLoggerrr6r(r,r4r4r4r5 s         o