B @`pV@sdZddlmZddlZddlmZddlmZddlZddl m Z m Z ddl m Z mZddlmZdd lmZdd lmZmZdd lmZmZmZmZmZmZmZmZdd lm Z dd l!m"Z"m#Z#ddl$m%Z%m&Z&ddl'm(Z(ddl)m*Z*Gddde+Z,Gddde+Z-ddZ.d8ddZ/ddZ0d9ddZ1dd Z2d!d"Z3d#d$Z4d%d&Z5ed:d'd(Z6d)d*Z7e d+d,d-ed.d/Z8ed;d0d1Z9ed2d3Z:ed4d5Z;d6d7Z._do_nothingr)rattrr$rrr! __getattr__.sz_DoNothing.__getattr__N)__name__ __module__ __qualname____doc__r"r#r&rrrr!r"src@s8eZdZdZGdddejZeZddZddZ dS) SuppressInfeasibleWarningaSSuppress the infeasible model warning message from solve(). The "WARNING: Loading a SolverResults object with a warning status" warning message from calling solve() is often unwanted, but there is no clear way to suppress it. This is modeled on LoggingIntercept from pyomo.common.log, but different in function. c@seZdZddZdS)z1SuppressInfeasibleWarning.InfeasibleWarningFiltercCs|d S)Nz@Loading a SolverResults object with a warning status into model=) getMessage startswith)rrecordrrr!filterBsz8SuppressInfeasibleWarning.InfeasibleWarningFilter.filterN)r'r(r)r/rrrr!InfeasibleWarningFilterAsr0cCstd}||jdS)Nz pyomo.core)logging getLogger addFilterwarning_filter)rloggerrrr! __enter__Hs z#SuppressInfeasibleWarning.__enter__cCstd}||jdS)Nz pyomo.core)r1r2 removeFilterr4)rZexception_typeZexception_value tracebackr5rrr!__exit__Ls z"SuppressInfeasibleWarning.__exit__N) r'r(r)r*r1Filterr0r4r6r9rrrr!r+5s  r+cCs|j}|j}|jj}|jdkr|jdkr|jdkr|jdt |j t dd}t dd|j Dsr|jdkr|jd|jt|jj|jf|j}d|fS|jd |jt|jj|jf|j}d|fSd S) z6If the model is an LP or NLP, solve it directly. rz"Problem has no discrete decisions.T)activecss|]}|jdkVqdS))rN)bodypolynomial_degree).0crrr! _sz"presolve_lp_nlp..)r<rzGYour model is an NLP (nonlinear program). Using NLP solver %s to solve.zBYour model is an LP (linear program). Using LP solver %s to solve.)FN) working_model GDPopt_utilsresultsproblemnumber_of_binary_variablesnumber_of_integer_variablesnumber_of_disjunctionsr5infonextcomponent_data_objectsranyconstraint_listexprr>Z nlp_solverrZsolveoriginal_modelZnlp_solver_argsZ mip_solverZmip_solver_args) solve_dataconfigmZGDPoptprobobjrDrrr!presolve_lp_nlpQs.        rUFTc Cs|j}t||j}t|jtddd}t||jj_ t|dkr`|j dtdd|_ |j }nt|dkrvt dn|d}|jdkrtjntj|jj_|j|_|jdks|r|r|j d n |j d ttdd |_tr|rt|j}|j||j|n:t|j\} } |jjjtjkrJ|j| n |j| |jtkrxt |j|jkd|_!nt |j|jkd|_!|"t|j|jd |_#|j$%|j|j&%|j!d S)aProcess model objective function. Check that the model has only 1 valid objective. If the objective is nonlinear, move it into the constraints. If no objective function exists, emit a warning and create a dummy objective. Parameters ---------- solve_data (GDPoptSolveData): solver environment data class config (ConfigBlock): solver configuration options move_linear_objective (bool): if True, move even linear objective functions to the constraints T)ctyper; descend_intorz7Model has no active objectives. Adding dummy objective.r<)rNz%Model has multiple active objectives.)r<rz#Moving objective to constraint set.z4Objective is nonlinear. Moving it to constraint set.)domainZ initialize)rNsenseN)'rBgetattrutil_block_namelistrKrlenrDrEZnumber_of_objectivesr5warningZdummy_objective ValueErrorrYrrZmaximizeZobjective_senserNr>rIrrZobjective_valuer r ZsetubupperZsetlblowerr rZobjective_constrZ deactivateZ objective variable_listappendrM) rPrQZmove_linear_objectiveZuse_mcpprRutil_blkZactive_objectivesZmain_objZmc_objlbubrrr!process_objectivetsP        rgcCst|tjr|St|SdS)zCReturns a logger when passed either a logger name or logger object.N) isinstancer1Loggerr2)Z str_or_loggerrrr!a_loggers rjc Csxt||D]\}}|r |jr q |r.|r.q y |t|dd|rLd|_Wq tk r}zt|dt|} t|} tt | } |r| rt|dd|_nJ| rt | | |j kr|| n$d| krt | |j kr|dnWdd}~XYq Xq WdS)zCopy variable values from one list to another. Rounds to Binary/Integer if neccessary Sets to zero for NonNegativeReals if neccessary F) exceptionmessagez!is not in domain NonNegativeRealsrN)zipstaleZis_fixed set_valuerr_rZstrintround is_integerrinteger_toleranceZzero_tolerance) from_listto_listrQZ skip_staleZ skip_fixedZignore_integralityZv_fromZv_toerrerr_msgZvar_valZ rounded_valrrr!copy_var_list_valuess,      rycCst|jtddd}|dk r*td|j|jdx|jtdddD]}|jdk rt |jt |j |j kr|j d|jt |j t |jfdS|j dk rHt |j t |j |j krH|j d |jt |j t |j fdSqHWx|jtdd D]}|rFt |jt ||jkrF|j d |jt |t |jfdS|rt |t |j|jkr|j d |jt |t |jfdSqW|j d dS)zChecks to see if the algebraic model is feasible in its current state. Checks variable bounds and active constraints. Not for use with untransformed GDP models. T)rVr;NzFound active disjunct %s. This function is not intended to check feasibility of disjunctive models, only transformed subproblems.zChecking if model is feasible.)rVr;rWz%s: body %s < LB %sFz%s: body %s > UB %s)rVrWz%s: value %s < LB %sz%s: value %s > UB %szModel is feasible.)rJrKrNotImplementedErrornamer5debugrrarr=Zconstraint_tolerancerIr`rZhas_lbreZvariable_toleranceZhas_ubrf)modelrQdisjconstrvarrrr! is_feasiblesD      rc s6t||j}tt|dt|jtdttfdt|dt|jtdttfdt|dt|jt dttfdx4t|dD]&}x t |j ddD]} |qWq~Wx8|jt ddD]&}x t |jddD]} |qWqWxt|dD]} |jqWtfd d |jtttfd D}t|d |d S)zDefine lists used for future data transfer. Also attaches ordered lists of the variables, constraints, disjuncts, and disjunctions to the model so that they can be used for mapping back and forth. rMT)rVr;rWZ disjunct_listZdisjunction_listF)Z include_fixed)rVr;c3s|]}|kr|VqdS)Nr)r?v)var_setrr!rADsz0build_ordered_component_lists..)rVrWrbN)rZr[rsetattrr\rKrr rrrr=addrrN indicator_varr)r}rPrdrrrTr~Zvar_listr)rr!build_ordered_component_listss: rc Cs|j}|j}|jj|j_d|j_d|j_d|j_d|j_d|j_ d|j_ d|j_ t |j}|j j|_|j j|_|j j|_|j j|_|j j|_|j j|_|jd|j j|j j|j j|j j|j j|j j|j jfdS)z-Record problem statistics for original model.NzOriginal model has %s constraints (%s nonlinear) and %s disjunctions, with %s variables, of which %s are binary, %s are integer, and %s are continuous.)rDrErOr{Znumber_of_nonzerossolverZtermination_conditionrl user_timeZ system_timewallclock_timeZtermination_messagerZ activated constraintsZnumber_of_constraintsZ disjunctionsrH variablesZnumber_of_variablesZbinary_variablesrFZcontinuous_variablesZnumber_of_continuous_variablesZinteger_variablesrGr5rIZnonlinear_constraints)rPrQresrSZnum_ofrrr!setup_results_objectJs4        rccsx|tD] }|Vq Wt}xf|tD]X}xR|jD]H}||krFq8||t|jjd|j kr8x|tD] }|VqrWq8Wq,WdS)zQYield constraints in disjuncts where the indicator value is set or fixed to True.r<N) rKrrrZ disjunctsrrrrrt)r}rQrZobserved_disjunctsZdisjctnr~rrr!constraints_in_True_disjunctss   rccs@t}|r||_dVt|}||d}||||<dS)aStarts timer at entry, stores elapsed time at exit If `is_main_timer=True`, the start time is stored in the timing_data_obj, allowing calculation of total elapsed time 'on the fly' (e.g. to enforce a time limit) using `get_main_elapsed_time(timing_data_obj)`. Nr)r default_timermain_timer_start_timeget)timing_data_objZcode_block_name is_main_timer start_timeZ elapsed_timeZ prev_timerrr! time_codes  rc CsVt}y ||jStk rP}z dt|kr@t|tdWdd}~XYnXdS)z.z integer cuts)rrz-infinfF)rurvrQ)+r rQrrDrrteer1INFOrrr5rrOclonerBrZstrategyZactive_strategyrCr\rbZinitial_var_valuesZbest_solution_foundrZ integer_cutsZmaster_iterationZ mip_iterationZ nlp_iterationfloatZLBZUBZ iteration_logZfeasible_solution_improvedryrEZ lower_boundZ upper_boundrZ iterationsrrr)r}rQrPZmin_logging_levelZ util_blockrrr!setup_solver_environmentsJ      &    rcsdfdd|dDS)zPThis should be replaced with textwrap.indent when we stop supporting python 2.7.c3s|]}|VqdS)Nr)r?line)prefixrr!rA szindent..T)join splitlines)textrr)rr!indentsr)FT)FTF)F)N)=r* __future__rr1 contextlibrmathrrZ pyomo.commonrrZpyomo.common.collectionsrrZpyomo.contrib.fbbt.fbbtr Zpyomo.contrib.gdpopt.data_classr Zpyomo.contrib.mcpp.pyomo_mcppr r Z pyomo.corer rrrrrrrZpyomo.core.expr.currentrZ pyomo.gdprrZ pyomo.optrrZpyomo.opt.resultsrZpyomo.util.model_sizerobjectrr+rUrgrjryrrrrrrrrrrrrrrr! sN     (   # F  42;    <