B @`@sdZddlmZddlZddlmZddlmZmZddl m Z ddl m Z ddl mZdd lmZed ZGd d d eZd dZddZddZddZdS)z5Utility functions and classes for the MindtPy solver.)divisionN) ComponentMap) ObjectiveSuffix) differentiate)current) SolverFactory)PersistentSolverz pyomo.contribc@seZdZdZdS)MindtPySolveDatazData container to hold solve-instance data. Key attributes: - original_model: the original model that the user gave us to solve - working_model: the original model after preprocessing N)__name__ __module__ __qualname____doc__rr>/tmp/pip-unpacked-wheel-bi3529v6/pyomo/contrib/mindtpy/util.pyr sr cCs>|j}|j}|jj}|jdkr|jdkr|jdkr|jdt |j t dd}t dd|j Dsx|jdkr|jd|jt|jj|jfd |ji|jd S|jd |jt|j}t|tr||j|jdkr|j|jd <|j|jfd |ji|jd St|d s:|jr:ttj d|_!dS)a Determines whether the model is solveable by MindtPy. This function returns True if the given model is solveable by MindtPy (and performs some preprocessing such as moving the objective to the constraints). Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: MindtPy configurations contains the specific configurations for the algorithm Returns ------- Boolean value (True if model is solveable in MindtPy else False) rz"Problem has no discrete decisions.T)ctypeactivecss|]}|jdkVqdS))rN)bodypolynomial_degree).0crrr >sz!model_is_valid..)rrzGYour model is an NLP (nonlinear program). Using NLP solver %s to solve.teeFzBYour model is an LP (linear program). Using LP solver %s to solve.threadsdual) direction)" working_model MindtPy_utilsresultsZproblemZnumber_of_binary_variablesZnumber_of_integer_variablesZnumber_of_disjunctionsloggerinfonextZcomponent_data_objectsranyconstraint_listexprrZ nlp_solverrZsolveZoriginal_modelZ solver_teeZnlp_solver_argsZ mip_solver isinstancer Z set_instanceroptionsZmip_solver_argshasattrZuse_dualrZIMPORTr) solve_dataconfigmMindtPyZprobobjZmipoptrrrmodel_is_valid"s<             r.cCst|_|jdkrtjj}n|jdkr.tjj}x`|jjj D]R}|j dkrNq:t t |j }t|j ||d}tddt||D|j|<q:WdS)a Generates a map of jacobians for the variables in the model This function generates a map of jacobians corresponding to the variables in the model and adds this ComponentMap to solve_data Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: MindtPy configurations contains the specific configurations for the algorithm reverse_symbolicsympy)rr)Zwrt_listmodecss|]\}}||fVqdS)Nr)rvarZ jac_wrt_varrrrrwsz!calc_jacobians..N)rZ jacobiansZdifferentiate_moderZModesr/r0Zmiprr$rrlistEXPRidentify_variableszip)r)r*r1rZvars_in_constrZjac_listrrrcalc_jacobians[s   r7cCs|j}xt|jdD]\}}|jdkr|r~|jdkr`|jj |j|j |jj |k}n|jj |j|j |jj k}| r|jdkr|jj |j|j |jj | k}q|jj |j|j |jj k}qWdS)a Adds feasibility slack variables according to config.feasibility_norm (given an infeasible problem) Parameters ---------- m: model Pyomo model config: ConfigBlock contains the specific configurations for the algorithm r)rr>L1L2N)r enumerater$rrhas_ubZfeasibility_normZ MindtPy_feasZfeas_constraintsaddupperZ slack_varhas_lblower)r+r*r,iZconstrrrrradd_feas_slacks{s&        rAcCs|j}|j}x|jD]}|jdkrxtt|jD]z}|rP| rPq8q8|s| rt| |j dq| |j dq8| s8| r||j q8||j q8WqWdS)a This function will add bounds for variables in nonlinear constraints if they are not bounded. (This is to avoid an unbounded master problem in the LP/NLP algorithm.) Thus, the model will be updated to include bounds for the unbounded variables in nonlinear constraints. Parameters ---------- solve_data: MindtPy Data Container data container that holds solve-instance data config: ConfigBlock contains the specific configurations for the algorithm )rrrN)rrr$rrr3r4r5r>r; is_integerZsetlbZinteger_var_boundZcontinuous_var_boundZsetub)r)r*r+r,rr2rrr var_bound_adds rC)r __future__rloggingZpyomo.common.collectionsrZ pyomo.corerrZpyomo.core.exprrrr4Z pyomo.optrZ/pyomo.solvers.plugins.solvers.persistent_solverr getLoggerr objectr r.r7rArCrrrr s        9 #