B u `T+@sdZddlmZddlZddlmZmZmZmZddl m Z m Z ddl m Z ddlmZddlmZmZmZmZmZmZmZdd lmZmZdd lmZdd l mZmZm Z dd lm!Z!ddl"Z"e#d Z$ddZ%ddZ&ddZ'dS)zInitialization functions.)divisionN)SuppressInfeasibleWarning _DoNothingcopy_var_list_valuesget_main_elapsed_time) add_oa_cutsadd_affine_cuts)solve_NLP_subproblem)calc_jacobians)ConstraintList ObjectiveTransformationFactorymaximizeminimizevalueVar) SolverFactoryTerminationCondition)PersistentSolver)handle_NLP_subproblem_optimal handle_NLP_subproblem_infeasible'handle_NLP_subproblem_other_termination) var_bound_addzpyomo.contrib.mindtpycCs`|jrt|||j}|_|j}|jr6|j|j dkrZt ||t dd|j _ n"|j dkr|t ||t dd|j _|jdkr|j dkrd|_nd |_|jd |j|jdkrt||n|jd krt||n~|jd kr\|j dkr\t||\}}|jjtjtjtjhkr,t|||n0|jjtjkrJt|||nt||jj||dS) a Initializes the decomposition algorithm and creates the master MIP/MILP problem. This function initializes the decomposition problem, which includes generating the initial cuts required to build the master MIP/MILP Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: ConfigBlock contains the specific configurations for the algorithm OAzOuter approximation cuts)docZECPzExtended Cutting PlanesN>GOArZrNLPZ max_binaryz'{} is the initial strategy being used. Zinitial_binary) Z single_treer working_modelclonemip MindtPy_utilsuse_dualdual deactivatestrategyr r ZMindtPy_linear_cutsZoa_cutsZecp_cutsZ init_strategyloggerinfoformat init_rNLPinit_max_binariesr solvertermination_conditiontcoptimallocallyOptimalfeasibler infeasiblerr) solve_dataconfigmMindtPyZ fixed_nlpZfixed_nlp_resultr4H/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/contrib/mindtpy/initialization.pyMindtPy_initialize_master#sF                 r6c s|jd7_|j|jd|jfj}tdt|j }t |j }t t |j|d}|jdkr|dg|d<|dd|t$t|jjfd|ji|}WdQRX|jj}|tjtjtjhkr^|tjtjhkr|jd tjtd d }td d |j D} |j!rDtfdd |j"Dnd} |j#t$krt%&|ddds|ddd|_'n*t%&|ddds|ddd|_(|jd|jt)|j*|j'|j(f|j+dkrt,jj |j-jj |d d|j+dkrt.|j-| ||n|j+dkr(t/||x|j-jt0dD] } | 1r8t t2| j)| _)q8Wnb|tj3krx|jdnH|tj4kr|jdn.|tj5kr|jdnt6d||jj7fdS)au Initialize the problem by solving the relaxed NLP (fixed binary variables) and then store the optimal variable values obtained from solving the rNLP Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: ConfigBlock contains the specific configurations for the algorithm z!NLP %s: Solve relaxed integralityzcore.relax_integer_varsgams add_optionszoption reslim=%s;teeNz(relaxed NLP is not solved to optimality.T)activecss|] }|jVqdS)N)r).0vr4r4r5 szinit_rNLP..c3s|]}j|VqdS)N)r!)r<c)r2r4r5r>sZProblemrz Lower boundz Upper boundzNLP %s: OBJ: %s LB: %s UB: %s>rr)Zignore_integralityrr)ctypezEInitial relaxed NLP problem is infeasible. Problem may be infeasible.z4NLP subproblem failed to converge within time limit.z9NLP subproblem failed to converge within iteration limit.zTMindtPy unable to handle relaxed NLP termination condition of %s. Solver message: %s)8Znlp_iterrrr$r%rr Zapply_todictZnlp_solver_argsrtimingintmax time_limitZ nlp_solvergetappendrrsolve solver_teer)r*r+r,r.r-nextcomponent_data_objectsr list variable_listr constraint_listsensermathisnanZLBZUBrexprr#rrrrr is_integerroundr/ maxTimeLimit maxIterations ValueErrormessage) r0r1r3Znlp_argselapsed remainingresultsZsubprob_terminate_condZmain_objectiveZnlp_solution_valuesZ dual_valuesvarr4)r2r5r'gsl      $$          r'c Cs|j}|jr|j|j}|jd7_|jd|j x$|j D]}|j dkrH|qHWt |jtdd}|dd|jtdD}ttd d|Dtd |_t|d tt|d tt|j}t|tr||t|j}t|j} tt |j!| d} |jd krF|"dg|d<|d#d|j$|fd|j%i|} | j&j'} | t(j)krt*|j+|jjj+|n^| t(j,krt-dnH| t(j.kr|jdn.| t(j/kr|jdnt-d| | j&j0fdS)a Modifies model by maximizing the number of activated binary variables Note - The user would usually want to call solve_NLP_subproblem after an invocation of this function. Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: ConfigBlock contains the specific configurations for the algorithm r7z#MILP %s: maximize value of binaries)r7rT)r;css |]}|r|js|VqdS)N)Z is_binaryZfixed)r<r=r4r4r5r>sz$init_max_binaries..)r@css|] }|VqdS)Nr4)r<r=r4r4r5r>s)rRrOZ ipopt_zL_outZ ipopt_zU_outr8r9zoption optcr=0.001;r:z[MILP master problem is infeasible. Problem may have no more feasible binary configurations.z4NLP subproblem failed to converge within time limit.z9NLP subproblem failed to converge within iteration limit.zTMindtPy unable to handle MILP master termination condition of %s. Solver message: %sN)1rrr r!r"rZ mip_subiterr$r%Zmip_iterrNbodyZpolynomial_degreerJrKr rsumrZMindtPy_max_binary_objgetattrrrZ mip_solver isinstancerZ set_instancerAZmip_solver_argsrrBrCrDrErFrGrHrIr)r*r+r,rrMr/rWrUrVrX) r0r1r2r3r?Z objectiveZ binary_varsoptZmip_argsrYrZr[Zsolve_terminate_condr4r4r5r(s^               r()(__doc__ __future__rloggingZpyomo.contrib.gdpopt.utilrrrrZ$pyomo.contrib.mindtpy.cut_generationrrZpyomo.contrib.mindtpy.nlp_solver Zpyomo.contrib.mindtpy.utilr Z pyomo.corer r r rrrrZ pyomo.optrrr+Z/pyomo.solvers.plugins.solvers.persistent_solverrrrrrrP getLoggerr$r6r'r(r4r4r4r5 s    $   DK