B _I@sdZddlmZddlZddlmZmZmZmZm Z ddl m Z m Z m Z mZmZmZmZmZmZddlmZddlmZddlmZmZdd lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%dd l&m'Z'm(Z(dd l)m*Z*e+d Z,d Z-e'j.dddGddde/Z0dS)aImplementation of the MindtPy solver. The MindtPy (MINLP Decomposition Toolkit) solver applies a variety of decomposition-based approaches to solve nonlinear continuous-discrete problems. These approaches include: - Outer approximation - Benders decomposition [pending] - Partial surrogate cuts [pending] - Extended cutting plane [pending] This solver implementation was developed by Carnegie Mellon University in the research group of Ignacio Grossmann. For nonconvex problems, the bounds solve_data.LB and solve_data.UB may not be rigorous. Questions: Please make a post at StackOverflow and/or David Bernal )divisionN) ConfigBlock ConfigValueIn PositiveFloat PositiveInt) _DoNothingcopy_var_list_valuescreate_utility_blockrestore_logger_level time_codesetup_results_objectprocess_objectivea_loggerlower_logger_level_to)MindtPy_initialize_master)MindtPy_iteration_loop)MindtPySolveDatamodel_is_valid) BlockConstraintListNonNegativeRealsRangeSetSetSuffixVarvalueVarListTransformationFactory) SolverFactory SolverResults) Containerzpyomo.contrib.mindtpy)rrZmindtpyz?MindtPy: Mixed-Integer Nonlinear Decomposition Toolbox in Pyomo)docc @seZdZdZedZedededddeded e d d ded ed e dddedede ddddgdddedede dddgdddedededd ded!eded"d#ded$ed%ed&d'ded(ed)e d)d*gd+d,ded-ed.d/d0d1ed2ed3e d4d5d6d3d*d7d8gd9d:ded;ed.dee d?d@dAdedBee d?dCdDdedEee d?dFdGdedHedIdJe dKedLedMdNe dKedOedPdQdRedSeddTdRedUedVdWdRedXedPdYdRedZedPd[dRed\ed.d]e dKed^ed_ed`daedbedIdce dKeddedIdee dKedfedIdge dKedhedIdie dKedjedIdke dKedledmdnedKedoedpdqedKedred.dse dKedtedue dvdwdugdxdaedyedze dzd{gd|daed}edId~e dKdddZddZddZddZddZd?S) MindtPySolverz(A decomposition-based MINLP solver. MindtPyZbound_tolerancegh㈵>zBound tolerancez0Relative tolerance for bound feasibility checks.)defaultdomain descriptionr#iteration_limitzIteration limitz:Number of maximum iterations in the decomposition methods.Z time_limitiXz!Time limit (seconds, default=600)zSeconds allowed until terminated. Note that the time limit cancurrently only be enforced between subsolver invocations. You mayneed to set subsolver time limits as well.strategyZOAZGBDZECPZPSCzDecomposition strategyzMINLP Decomposition strategy to be applied to the method. Currently available Outer Approximation (OA), Extended Cutting Plane (ECP), Partial Surrogate Cuts (PSC), and Generalized Benders Decomposition (GBD). init_strategyrNLPZinitial_binaryZ max_binaryzInitialization strategyzInitialization strategy used by any method. Currently the continuous relaxation of the MINLP (rNLP), solve a maximal covering problem (max_binary), and fix the initial value for the integer variables (initial_binary). max_slackg@@zMaximum slack variablezFMaximum slack variable value allowed for the Outer Approximation cuts.ZOA_penalty_factorz(Outer Approximation slack penalty factorzIn the objective function of the Outer Approximation method, the slack variables corresponding to all the constraints get multiplied by this number and added to the objective.Z ECP_toleranceg-C6?z ECP tolerancezFeasibility tolerance used to determine the stopping criterion inthe ECP method. As long as nonlinear constraint are violated for more than this tolerance, the method will keep iterating.Z nlp_solverZipoptZgamszNLP subsolver namezMWhich NLP subsolver is going to be used for solving the nonlinearsubproblems.Znlp_solver_argsTzNLP subsolver optionsz_Which NLP subsolver options to be passed to the solver while solving the nonlinear subproblems.)Zimplicitr(r# mip_solverZglpkZgurobiZcplexZcbcZgurobi_persistentcplex_persistentzMIP subsolver namezVWhich MIP subsolver is going to be used for solving the mixed-integer master problems.Zmip_solver_argszMIP subsolver optionszgWhich MIP subsolver options to be passed to the solver while solving the mixed-integer master problems.Zcall_after_master_solveNz2Function to be executed after every master problemz5Callback hook after a solution of the master problem.Zcall_after_subproblem_solvez.Function to be executed after every subproblemz;Callback hook after a solution of the nonlinear subproblem.Zcall_after_subproblem_feasiblez7Function to be executed after every feasible subproblemzDCallback hook after a feasible solution of the nonlinear subproblem.teeFzStream output to terminal.)r&r(r'loggerzpyomo.contrib.mindtpyz/The logger object or name to use for reporting.Zsmall_dual_toleranceg:0yE>zWhen generating cuts, small duals multiplied by expressions can cause problems. Exclude all duals smaller in absolute value than the following.)r&r(Zinteger_tolerancezTolerance on integral values.Zconstraint_tolerancegư>z%Tolerance on constraint satisfaction.Zvariable_tolerancezTolerance on variable bounds.Zzero_tolerancez$Tolerance on variable equal to zero.Z initial_feasz"Apply an initial feasibility step.Z obj_boundg4&k CzYBound applied to the linearization of the objective function if master MILP is unbounded.)r&r'r(integer_to_binaryz9Convert integer variables to binaries (for integer cuts).add_integer_cutszAdd integer cuts (no-good cuts) to binary variables to disallow same integer solution again.Note that 'integer_to_binary' flag needs to be used to apply it to actual integers and not just binaries. single_treezBUse single tree implementation in solving the MILP master problem.Z solution_poolz5Use solution pool in solving the MILP master problem. add_slackz[whether add slack variable here.slack variables here are used to deal with nonconvex MINLP.Zcontinuous_var_boundg _Bzjdefault bound added to unbounded continuous variables in nonlinear constraint if single tree is activated.Zinteger_var_boundgeAzhdefault bound added to unbounded integral variables in nonlinear constraint if single tree is activated.Z cycling_checkz:check if OA algorithm is stalled in a cycle and terminate.feasibility_normZ L_infinityL1L2z@different forms of objective function in feasibility subproblem.Zdifferentiate_modeZreverse_symbolicZsympyz)differentiate mode to calculate jacobian.Zlinearize_inactivez%Add OA cuts for inactive constraints.cCsdS)zCheck if solver is available. TODO: For now, it is always available. However, sub-solvers may not always be available, and so this should reflect that possibility. T)selfZexception_flagr:r:A/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/contrib/mindtpy/MindtPy.py available szMindtPySolver.availablecCstS)z/Return a 3-tuple describing the solver version.) __version__)r;r:r:r<versionszMindtPySolver.versionc Ks||di}|||jrFd|_d|_d|_d|_|j d|j dkrVd|_t }t |_ t|_g|_g|_||_||_|jrtd|j|jrtjnd}t|jd d d t|j|t|jd |h|j d |jj}t||t ||ddt!dd|j"D|_#d|_$dt%|j&|j j'_(t)||sPdSt*}|_+|,t-dd|_.t*}|_/|,t-dd|_0t-dd|_1|j1,d|_2d|_3d|_4t5d|_6t5d|_7|j6g|_8|j7g|_9t:dd|_;t:dd|_<|j=dks|j=dkrDt:ddt>|j?dDdd|_@tA|j@tBdd |_CntAtBdd |_C|jrptDd|j fdtBd!|_Ed|_FtG|jd"stHtHjId#|j_JtG|jd$stHtHjId#|j_Kt|jd%tL||WdQRXt|jd&tM||WdQRX|j$dk r.tN|j$jj"|j"|d'tN|j"|jOtA||j6|j jP_Q|j7|j jP_RWdQRXWdQRXWdQRX|j|j j'_|jjS|j j'_T|jjS|j j'_U|j3|j j'_V|jr|j2|jWd(krdnd|j j'_X|j S))aPSolve the model. Warning: this solver is still in beta. Keyword arguments subject to change. Undocumented keyword arguments definitely subject to change. Warning: at this point in time, if you try to use PSC or GBD with anything other than IPOPT as the NLP solver, bad things will happen. This is because the suffixes are not in place to extract dual values from the variable bounds for any other solver. TODO: fix needed with the GBD implementation. Args: model (Block): a Pyomo model or block to be solved optionsr"Fr0zTSingle tree implementation is activated. The defalt MIP solver is 'cplex_persistent'gzcontrib.integer_to_binaryNtotalT)Z is_main_timer MindtPy_utilsz---Starting MindtPy---)Zuse_mcppcss|] }|jVqdS)N)r).0vr:r:r< Gsz&MindtPySolver.solve..r%zFeasibility Problem Constraints)r#z integer cutszexplored integer cutsrz-infinf)Zdimenr8r9cSs"g|]\}}|jdkr|qS))r"r)bodyZpolynomial_degree)rCiZconstrr:r:r< sz'MindtPySolver.solve..z{Integer index set over the nonlinear constraints.The set corresponds to the index of nonlinear constraint in constraint_set) initializer#)r'rJ)ZboundsrJr' ipopt_zL_out) direction ipopt_zU_outZinitializationz main loop) from_listZto_listconfigr-)YCONFIGpop set_valuer5r)r6r4r/r2infor.rr resultsr!ZtimingZ curr_int_solZ prev_int_solZoriginal_modelcloneZ working_modelr3rZapply_tor1loggingINFOr rr rBr rlistZ variable_listZinitial_var_valuesZbest_solution_foundstrr+ZsolvernamerrZ MindtPy_feasZ deactivaterZfeas_constraintsZMindtPy_linear_cutsZ integer_cutsZfeasible_integer_cutsZnlp_iterZmip_iterZ mip_subiterfloatZLBZUBZ LB_progressZ UB_progressrZ nlp_itersZ mip_itersr7 enumerateZconstraint_listZnl_constraint_setrrZ slack_varrZ slack_varsZsolution_improvedhasattrrZIMPORTrKrMrrr Zcomponent_data_objectsZproblemZ lower_boundZ upper_boundrAZ user_timeZwallclock_timeZ iterationsr,Z num_nodes) r;modelkwdsrOZ solve_dataZnew_logging_levelr%ZfeasZlinr:r:r<solves                       *  zMindtPySolver.solvecCs|S)Nr:)r;r:r:r< __enter__szMindtPySolver.__enter__cCsdS)Nr:)r;trD tracebackr:r:r<__exit__szMindtPySolver.__exit__)T)__name__ __module__ __qualname____doc__rrPZdeclarerrrrrboolrr=r?r`rardr:r:r:r<r$2s:                                         .r$)1rh __future__rrVZpyomo.common.configrrrrrZpyomo.contrib.gdpopt.utilrr r r r r rrrZ$pyomo.contrib.mindtpy.initializationrZpyomo.contrib.mindtpy.iteraterZpyomo.contrib.mindtpy.utilrrZ pyomo.corerrrrrrrrrrZ pyomo.optrr Z pyutilib.miscr! getLoggerr2r>registerobjectr$r:r:r:r<s  ,  0