B _(T@sdZddlmZddlZddlZddlmZddlmZddl Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZmZdd lmZmZmZmZmZmZmZmZdd l m!Z!ddl"m#Z#m$Z$ddl%m&Z&m'Z'ddl(m)Z)ddl*m+Z+Gddde,Z-Gddde,Z.ddZ/d9ddZ0ddZ1d:ddZ2d d!Z3d"d#Z4d$d%Z5d&d'Z6ed;d(d)Z7d*d+Z8e d,d-d.ed/d0Z9ed._do_nothingr)rattrr#rrr __getattr__&sz_DoNothing.__getattr__N)__name__ __module__ __qualname____doc__r!r"r%rrrr rsrc@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 filter:sz8SuppressInfeasibleWarning.InfeasibleWarningFilter.filterN)r&r'r(r.rrrr InfeasibleWarningFilter9sr/cCstd}||jdS)Nz pyomo.core)logging getLogger addFilterwarning_filter)rloggerrrr __enter__@s z#SuppressInfeasibleWarning.__enter__cCstd}||jdS)Nz pyomo.core)r0r1 removeFilterr3)rZexception_typeZexception_value tracebackr4rrr __exit__Ds z"SuppressInfeasibleWarning.__exit__N) r&r'r(r)r0Filterr/r3r5r8rrrr r*-s  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 Wsz"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_disjunctionsr4infonextcomponent_data_objectsranyconstraint_listexprr=Z nlp_solverrZsolveoriginal_modelZnlp_solver_argsZ mip_solverZmip_solver_args) solve_dataconfigmZGDPoptprobobjrCrrr presolve_lp_nlpIs.        rTFTc 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\} } |j| |j| |jtkrdt |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;)rMz%Model has multiple active objectives.)r;rz#Moving objective to constraint set.z4Objective is nonlinear. Moving it to constraint set.)domainZ initialize)rMsenseN)'rAgetattrutil_block_namelistrJrlenrCrDZnumber_of_objectivesr4warningZdummy_objective ValueErrorrXrrZmaximizeZobjective_senserMr=rHrrZobjective_valuer r ZsetubupperZsetlblowerrr Zobjective_constrZ deactivateZ objective variable_listappendrL) rOrPZmove_linear_objectiveZuse_mcpprQutil_blkZactive_objectivesZmain_objZmc_objlbubrrr process_objectivelsN         rfcCst|tjr|St|SdS)zCReturns a logger when passed either a logger name or logger object.N) isinstancer0Loggerr1)Z str_or_loggerrrr a_loggers ric 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^rYstrintround is_integerrinteger_toleranceZzero_tolerance) from_listto_listrPZ skip_staleZ skip_fixedZignore_integralityZv_fromZv_toerrerr_msgZvar_valZ rounded_valrrr copy_var_list_valuess,      rxcCst|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)rUr:NzFound active disjunct %s. This function is not intended to check feasibility of disjunctive models, only transformed subproblems.zChecking if model is feasible.)rUr:rVz%s: body %s < LB %sFz%s: body %s > UB %s)rUrVz%s: value %s < LB %sz%s: value %s > UB %szModel is feasible.)rIrJrNotImplementedErrornamer4debugr r`rr<Zconstraint_tolerancerHr_rZhas_lbrdZvariable_toleranceZhas_ubre)modelrPdisjconstrvarrrr 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. rLT)rUr:rVZ disjunct_listZdisjunction_listF)Z include_fixed)rUr:c3s|]}|kr|VqdS)Nr)r>v)var_setrr r@:sz0build_ordered_component_lists..)rUrVraN)rYrZrsetattrr[rJr r rrrr<addrrM indicator_varr)r|rOrcr~rrSr}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.)rCrDrNrzZnumber_of_nonzerossolverZtermination_conditionrk user_timeZ system_timewallclock_timeZtermination_messagerZ activated constraintsZnumber_of_constraintsZ disjunctionsrG variablesZnumber_of_variablesZbinary_variablesrEZcontinuous_variablesZnumber_of_continuous_variablesZinteger_variablesrFr4rHZnonlinear_constraints)rOrPresrRZnum_ofrrr setup_results_object@s4        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) rJr rrZ disjunctsrrrrrs)r|rPr~Zobserved_disjunctsZdisjctnr}rrr constraints_in_True_disjuncts{s   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)timeit 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|}|dk r4||kr4||dV||ndVdS)z7Increases logger verbosity by lowering reporting level.N)rr)r4levelrrrr lower_logger_level_tos   rccsZd}t||rtd|n&d}t||tdd||_t||dV|rV||dS)NFzlGDPopt needs to create a Block named %s on the model object, but an attribute with that name already exists.Tz4Container for GDPopt solver utility modeling objects)doc)hasattr RuntimeErrorrr rZrZ del_component)r|rzrOZcreated_util_blockrrr create_utility_blocks    rc cst}||_t|_t|_|jr(tjnd}t |jdddt |j |t |d|||_ ||_t|||j|_|jj}tdd|jD|_d|_tdd|_d |_d |_d |_td |_td |_i|_ d |_!|V|jdk r|j|j k rt"|jjj|j jj|d WdQRXWdQRXWdQRX|j|jj#_$|j|jj#_%|j|jj&_'|j|jj&_|jj(|jj&_)|jj(|jj&_*dS)NtotalT)rrBcss|] }|jVqdS)N)r)r>rrrr r@sz+setup_solver_environment..z integer cuts)rrz-infinfF)rtrurP)+r rPrrCrZtimingteer0INFOrrr4rrNclonerArZstrategyZactive_strategyrBr[raZinitial_var_valuesZbest_solution_foundrZ integer_cutsZmaster_iterationZ mip_iterationZ nlp_iterationfloatZLBZUBZ iteration_logZfeasible_solution_improvedrxrDZ lower_boundZ upper_boundrZ iterationsrrr)r|rPrOZmin_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 r@szindent..T)join splitlines)textrr)rr indentsr)FT)FTF)F)N)>r) __future__rr0r contextlibrmathrrZ pyutilib.miscrZ pyomo.commonrZpyomo.common.collectionsrZpyomo.contrib.fbbt.fbbtrZpyomo.contrib.gdpopt.data_classr Zpyomo.contrib.mcpp.pyomo_mcppr r Z pyomo.corer r rrrrrrZpyomo.core.expr.currentrZ pyomo.gdprrZ pyomo.optrrZpyomo.opt.resultsrZpyomo.util.model_sizerobjectrr*rTrfrirxrrrrrrrrrrrrrrr sR        (   # D  42;    <