B _d@sdZddlmZddlmZmZmZddlmZm Z ddl m Z ddl m Z ddlmZmZmZmZmZmZmZddlmZdd lmZdd lmZdd l m Z mZmZmZdd l mZd dZ ddZ!ddZ"dS)zInitialization functions.)division)SuppressInfeasibleWarning _DoNothingcopy_var_list_values) add_oa_cutsadd_objective_linearization)solve_NLP_subproblem)calc_jacobians)ConstraintList ObjectiveTransformationFactorymaximizeminimizevalueVar)TerminationCondition) SolverFactory)PersistentSolver)rhandle_NLP_subproblem_optimal handle_NLP_subproblem_infeasible'handle_NLP_subproblem_other_termination) var_bound_addcCs |jrt|||j}|_|j}|j|jdkrRt ||t dd|j _ |j dkrv|jdkrnd|_ nd|_ n|j dkrt||n||j dkrt||t||\}}|jjtjks|jjtjkrt|||n.|jjtjkrt|||nt||jj||dS)zInitialize the decomposition algorithm. This includes generating the initial cuts require to build the master problem. OAzOuter approximation cuts)docNZrNLPZ max_binary)Z single_treer working_modelclonemip MindtPy_utilsdual deactivatestrategyr r ZMindtPy_linear_cutsZoa_cutsZ init_strategy init_rNLPinit_max_binariesrsolvertermination_conditiontcoptimallocallyOptimalr infeasiblerr) solve_dataconfigmMindtPyZ fixed_nlpZfixed_nlp_resultr-H/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/contrib/mindtpy/initialization.pyMindtPy_initialize_masters0            r/c s|jd7_|j|jd|jfj}tdtt |j j f|j }WdQRX|j j}|tjks|tjkrxtjtdd}tdd|jD}tfd d|jD}|jtkrt|j|_n t|j|_|jd |jt|j|j|jf|jd krtjj|j jj|dd t!|j |||xb|j jt"d D] }|#rRt$t%|j|_qRWn.|tj&kr|jdnt'd||j j(fdS)z:Initialize by solving the rNLP (relaxed binary variables).z!NLP %s: Solve relaxed integralityzcore.relax_integer_varsNT)activecss|] }|jVqdS)N)r).0vr-r-r. Yszinit_rNLP..c3s|]}j|VqdS)N)r)r2c)r+r-r.r4ZszNLP %s: OBJ: %s LB: %s UB: %sr)Zignore_integrality)ctypezEInitial relaxed NLP problem is infeasible. Problem may be infeasible.zTMindtPy unable to handle relaxed NLP termination condition of %s. Solver message: %s))Znlp_iterrrloggerinforr Zapply_torrZ nlp_solversolveZnlp_solver_argsr#r$r%r&r'nextcomponent_data_objectsr list variable_listconstraint_listsenserrexprZLBZUBr rrrr is_integerintroundr( ValueErrormessage) r)r*r,resultsZsubprob_terminate_condZmain_objectiveZnlp_solution_valuesZ dual_valuesvarr-)r+r.r!KsF         r!c Cs|j}|j|j}|jd7_|jd|jx$|j D]}|j dkrB|qBWt |j tdd}|dd|j tdD}ttd d|Dtd |_t|d tt|d tt|j}t|tr||t|j}|jd kr"|dg|d<|dd|j|f|} | jj } | t!j"krZt#|j$|jjj$|n*| t!j%krpt&dnt&d| | jj'fdS)zInitialize by turning on as many binary variables as possible. The user would usually want to call _solve_NLP_subproblem after an invocation of this function. r0z#MILP %s: maximize value of binaries)r0rT)r1css |]}|r|js|VqdS)N)Z is_binaryZfixed)r2r3r-r-r.r4sz$init_max_binaries..)r6css|] }|VqdS)Nr-)r2r3r-r-r.r4s)r@r?Z ipopt_zL_outZ ipopt_zU_outZgams add_optionszoption optcr=0.0;z[MILP master problem is infeasible. Problem may have no more feasible binary configurations.zTMindtPy unable to handle MILP master termination condition of %s. Solver message: %sN)(rrrrrZ mip_subiterr7r8Zmip_iterr>bodyZpolynomial_degreer:r;r rsumr ZMindtPy_max_binary_objgetattrrrZ mip_solver isinstancerZ set_instancedictZmip_solver_argsgetappendr9r#r$r%r&rr=r(rDrE) r)r*r+r,r5Z objectiveZ binary_varsoptZmip_argsrFZsolve_terminate_condr-r-r.r"ysL            r"N)#__doc__ __future__rZpyomo.contrib.gdpopt.utilrrrZ$pyomo.contrib.mindtpy.cut_generationrrZpyomo.contrib.mindtpy.nlp_solverZpyomo.contrib.mindtpy.utilr Z pyomo.corer r r r rrrZ pyomo.optrr%rZ/pyomo.solvers.plugins.solvers.persistent_solverrrrrrr/r!r"r-r-r-r.s   $    6.