B _@sdZddlmZddlZddlmZmZmZddlm Z m Z ddl m Z m Z mZmZmZmZmZmZmZmZmZddlmZddlmZdd lmZdd lmZdd lm Z dd l!m"Z"Gd dde#Z$ddZ%ddZ&ddZ'ddZ(dS)z5Utility functions and classes for the MindtPy solver.)divisionN)fabsfloorlog) ComponentMap ComponentSet) AnyBinaryBlock ConstraintNonNegativeReals ObjectiveRealsSuffixVarminimizevalue) differentiate)current)native_numeric_types) SolverFactory) ProblemSense)PersistentSolverc@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-d4p3hk07/pyomo/contrib/mindtpy/util.pyrsrcCs|j}|j}|jj}|jdkr|jdkr|jdkr|jdt |j t dd}t dd|j Dsr|jdkr|jd|jt|jj|jf|jd S|jd |jt|j}t|tr||j|j|jf|jd St|d sttjd |_dS) zValidate that the model is solveable by MindtPy. Also preforms some preprocessing such as moving the objective to the constraints. rz"Problem has no discrete decisions.T)ctypeactivecss|]}|jdkVqdS))rN)bodypolynomial_degree).0crrr ,sz!model_is_valid..)r"rzGYour model is an NLP (nonlinear program). Using NLP solver %s to solve.FzBYour model is an LP (linear program). Using LP solver %s to solve.dual) direction) working_model MindtPy_utilsresultsZproblemZnumber_of_binary_variablesZnumber_of_integer_variablesZnumber_of_disjunctionsloggerinfonextZcomponent_data_objectsr anyconstraint_listexprr$Z nlp_solverrZsolveZoriginal_modelZnlp_solver_argsZ mip_solver isinstancerZ set_instanceZmip_solver_argshasattrrZIMPORTr() solve_dataconfigmMindtPyZprobobjZmipoptrrrmodel_is_valids6           r:cCst|_|jdkrtjj}n|jdkr.tjj}x`|jjj D]R}|j dkrNq:t t |j }t|j ||d}tddt||D|j|<q:WdS)zGenerate a map of jacobians.reverse_symbolicsympy)r"r)Zwrt_listmodecss|]\}}||fVqdS)Nr)r%varZ jac_wrt_varrrrr'Wsz!calc_jacobians..N)rZ jacobiansZdifferentiate_moderZModesr;r<Zmipr+r1r#r$listEXPRidentify_variableszip)r5r6r=r&Zvars_in_constrZjac_listrrrcalc_jacobiansGs   rCcCs|j}xt|jdD]r\}}|jdkr|r8|jn|j}|jdkrj|j j |j||j j |k}q|j j |j||j j k}qWdS)Nr")rr">L2L1) r+ enumerater1r#r$has_ubupperlowerZfeasibility_normZ MindtPy_feasZfeas_constraintsaddZ slack_var)r7r6r8iZconstrrhsr&rrradd_feas_slacks[s rMcCs|j}|j}x|jD]}|jdkrxtt|jD]z}|rP| rPq8q8|s| rt| |j dq| |j dq8| s8| r||j q8||j q8WqWdS)zThis function will add bound for variables in nonlinear constraints if they are not bounded. This is to avoid an unbound master problem in the LP/NLP algorithm. )r"rr"N)r*r+r1r#r$r?r@rAZhas_lbrG is_integerZsetlbZinteger_var_boundZcontinuous_var_boundZsetub)r5r6r7r8r&r>rrr var_bound_addks rO))r __future__rloggingmathrrrZpyomo.common.collectionsrrZ pyomo.corerr r r r r rrrrrZpyomo.core.exprrrr@Zpyomo.core.expr.numvaluerZ pyomo.optrZpyomo.opt.resultsrZ/pyomo.solvers.plugins.solvers.persistent_solverrobjectrr:rCrMrOrrrrs  4       ,