B _$@sdZddlmZyddlmZWnddlmZYnXddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZddlmZddlmZmZmZddlmZmZmZmZdd lmZm Z dd l!Z!dd l"Z"e"#d Z$d Z%d Z&ej'dddGdddeZ(d S)z| Cutting plane-based GDP reformulation. Implements a general cutting plane-based reformulation for linear and convex GDPs. )division) OrderedDict)unique_component_name) AnyBlock Constraint ObjectiveParamVarSortComponentsTransformationTransformationFactoryvaluer ) SolverFactory)Disjunct Disjunction GDP_Error)verify_successful_solveNORMAL INFEASIBLE NONOPTIMAL)iterkeys itervaluesNzpyomo.gdp.cuttingplaneZipoptFzgdp.cuttingplanezWRelaxes a linear disjunctive model by adding cuts from convex hull to Big-M relaxation.)doccsNeZdZfddZdddZddZdd Zd d Zd d ZddZ Z S)CuttingPlane_Transformationcstt|dS)N)superr__init__)self) __class__B/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/gdp/plugins/cuttingplane.pyr4sz$CuttingPlane_Transformation.__init__Nc Ksr|di}|r*tddt|f|rHtddt|f|||\}}}}||||||dS)Noptionsz6GDP(CuttingPlanes): unrecognized keyword arguments: %s z,GDP(CuttingPlanes): unrecognized options: %s)poploggerwarningjoinr_setup_subproblems_generate_cuttingplanes) rinstancebigMkwdsr!instance_rBigMinstance_rHullvar_infotransBlockNamerrr _apply_to7s z%CuttingPlane_Transformation._apply_toc s||d\}}tdd|jtttftjdD|_t t |_ t d}t d}t d}| |}|||j||d| |} ||ttt|jd dd _| |tfd dt|jD} || | || |fS) N"_pyomo_gdp_cuttingplane_relaxationcss|]}|s|VqdS)N)Zis_fixed).0vrrr PszACuttingPlane_Transformation._setup_subproblems..)Z descend_intosortzgdp.bigmzgdp.hullzcore.relax_integrality)r*T)Zmutabledefaultc3s0|](\}}|j|j|j|fVqdS)N)all_varsxstar)r2ir3)transBlock_rBigMtransBlock_rHullrr r4s)_add_relaxation_blocklistcomponent_data_objectsr rrr Z deterministicr7rrcutsr Z create_usingZapply_to componentr rangelenr8tuple enumerate_add_separation_objective) rr)r*r/ transBlockZbigMRelaxationZhullRelaxationZrelaxIntegralityr-r,r.r)r:r;r r'Hs0          z.CuttingPlane_Transformation._setup_subproblemscCsVtt}d}d}td} d} ||} ||} t|jtddd} | dkrTtdx|rP|j|t d}t |t k rt d|fdSt| }t d |fx$|D]\}}}}|j|_|j|_qW|j|t d}t |t k rt d |fdS||| | | |}t|p@t|d kr2t|| knt|| | k}|} |d 7}qVWdS) NTrinfg{Gz?)activezTCannot apply cutting planes transformation without an active objective in the model!)teezhGDP.cuttingplane: Relaxed BigM subproblem did not solve normally. Stopping cutting plane generation. %sz&gdp.cuttingplane: rBigM objective = %szkGDP.cuttingplane: Hull separation subproblem did not solve normally. Stopping cutting plane generation. %s)rSOLVERfloatr@nextr>rrZsolvestream_solversrrr$r%r_add_cutmathisinfabs)rr)r,r-r.r/optZ improving iterationZprev_objepsilonrFr:Z rBigM_objresultsZ rBigM_objValx_bigmx_rbigmx_hullx_starZobj_diffrrr r(sF         z3CuttingPlane_Transformation._generate_cuttingplanescCs$t|d}t}|||||fS)Nr1)rrZ add_component)rr)namer/rFrrr r<s  z1CuttingPlane_Transformation._add_relaxation_blockc CsZx|tD] }|qWd}x$|D]\}}}}|||d7}q*Wt|d|_dS)Nr)expr)modelr>rZ deactivateZseparation_objective) rr.r;oZobj_exprrWrXrYrZrrr rEs  z5CuttingPlane_Transformation._add_separation_objectivec Cst|j}td|fd}d}xH|D]@\}}} } || j| j|| j7}|| j| j|| j7}q(W|j||dk|j||dkdS)Nz,gdp.cuttingplane: Adding cut %s to BM model.r)rBr?r$r%radd) rr.rFr:Z cut_numberZ cutexpr_bigmZ cutexpr_rBigMrWrXrYrZrrr rOs  z$CuttingPlane_Transformation._add_cut)N) __name__ __module__ __qualname__rr0r'r(r<rErO __classcell__rr)rr r0s  I?  r))__doc__ __future__r collectionsrZ ordereddictZpyomo.common.modelingrZ pyomo.corerrrrr r r r r rZ pyomo.optrZ pyomo.gdprrrZpyomo.gdp.utilrrrrsixrrrPlogging getLoggerr$rKrNregisterrrrrr s$  4