B u `SQ@sdZddlZddlZddlZddlmZddlmZmZddl m Z m Z m Z ddl mZmZmZmZmZmZmZddlmZddlmZmZmZmZmZmZdd lmZdd l m!Z!m"Z"m#Z#dd l m$Z%d Z&Gd dde'Z(e!j)dddGddde'Z*dS)zMain driver module for GDPbb solver. 19.5.7 changes: - added support for time limits - rewrote algorithm to solve linear relaxation at root node and improve stability N) deprecated) ComponentSet Container) ConfigBlock ConfigValue PositiveInt)create_utility_block time_codea_loggerrestore_logger_levelsetup_results_objectget_main_elapsed_timeprocess_objective) satisfiable) ObjectiveTransformationFactoryminimizevalue ConstraintSuffix) Disjunction) SolverFactory SolverStatus SolverResults)TerminationCondition)c@s eZdZdS)GDPbbSolveDataN)__name__ __module__ __qualname__r"r"=/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/contrib/gdpbb/GDPbb.pyrsrgdpbbz!Branch and Bound based GDP Solver)docc seZdZdZedZededddededd d ed ed ed deded eddededde dedede ddde ddddfddZ d.d d!Z d"d#Zd$d%Zed&d'Zed(d)Zd*d+Zd,d-ZZS)/ GDPbbSolvera A branch and bound-based solver for Generalized Disjunctive Programming (GDP) problems The GDPbb solver solves subproblems relaxing certain disjunctions, and builds up a tree of potential active disjunctions. By exploring promising branches, it eventually results in an optimal configuration of disjunctions. Keyword arguments below are specified for the ``solve`` function. r$solverZbaronz+Subproblem solver to use, defaults to baron)default description solver_argsTz1Block of keyword arguments to pass to the solver.)Zimplicitr)teeFz(Flag to stream solver output to console.)r(domainr) check_satzaWhen True, GDPBB will check satisfiability via the pyomo.contrib.satsolver interface at each nodeloggerzpyomo.contrib.gdpbbz/The logger object or name to use for reporting.)r(r)r, time_limitiXz!Time limit (seconds, default=600)zSeconds allowed until terminated. Note that the time limit cancurrently only be enforced between subsolver invocations. You mayneed to set subsolver time limits as well.)r(r,r)r%z^GDPbb has been merged into GDPopt. You can use the algorithm using GDPopt with strategy='LBB'.z pyomo.solversz5.6.9)r.versioncstt|j||dS)N)superr&__init__)selfargskwargs) __class__r"r#r2OszGDPbbSolver.__init__cCsdS)zCheck if solver is available. TODO: For now, it is always available. However, sub-solvers may not always be available, and so this should reflect that possibility. Tr")r3Zexception_flagr"r"r# availableVszGDPbbSolver.availablecCstS)N) __version__)r3r"r"r#r0_szGDPbbSolver.versionc$ s||di}||tdj|d|j|j|j|j|j |j dS|j d d tt||jfd t|j|jj_t|||j }|_!t"|||jj#t$dd }t%|d}|j&|jj'_&t(d d|j)j*D|j)_+t,dd|j)j+D|j)_-x|j)j-D]} | .qWt/|ds.t0|_1t,|j)_2x$|j#t3dd D]} dd| j4D} dd| j4DxD].qvWt5t5| j4dkr| sfdd| j4D} xr| D]jt,} x>#t6D]0} | |j)j-kr| 7| | 8d|j1| <qW|j)j-| 8_-|j)j27qW| rJ| j9sLt:d| jf|j)j+;| qJW|jrt<||j dkrt=t>d} n|j d|?||\} }}t=| t>dkr8|j dt@|j|jj'j&tAkrt>d|jj'_Bd|jj'_Cnd|jj'_Bt>d|jj'_C|j|jj_d|jj_DtEjF|jj_G|jSg}d}t5|j)j+}tHI|t=| || f|||fx|t5|dkrtHJ|\}}}}|\}}}|j d||f|dkr4|j d|j}x"tK|j)jL|D]\}}||_MqW|j'jB|jj'_B|j'jC|jj'_C|j|jj_||jj_D|jjG|jj_G|jS|j)j+d}|j d|j|j9sjt:d|jfd}xVtN|j4D]F\}tOfd d|j4Drq|t@|j|j kr\|jj'j&tAkr||jj'_Bt>d|jj'_Cnt>d|jj'_B||jj'_C|j d!P|j t@|j|j d"P|jj'jB|jj'jC|j|jj_||jj_DtEjQ|jj_G|jS| }|j)j+d} | j4|}!|!jRSdx"| j4D]}"|"|!k r|".qWt,} x>|!#t6D]0} | |j)j-kr| 7| | 8d|j1| <qW|j)j-| 8_-|j)j27|!|j)j2krZ|d7}t=||d| f}#tHI||#|||f|d7}q||jrxt<||j dkrxq||?||\} }}|d7}t=| |d| f}#tHI||#|||f|d7}q|W|j d#||dt5|fqpWWdQRXWdQRXWdQRXdS)$NoptionsZgdpoptZLBB)ZstrategyZ minlp_solverZminlp_solver_argsr+r-r.r/totalT)Z is_main_timer GDPbb_utilsz/Starting GDPbb version %s using %s as subsolver.z GDPbb - %s)activecss|]}|jr|VqdS)N)r=).0 disjunctionr"r"r# sz$GDPbbSolver.solve..cSs>g|]6}|jD]*}|jtddD]}|jdkr |q qqS)T)ctyper=)r) disjunctscomponent_data_objectsrbodyZpolynomial_degree)r>r?disjunctconstrr"r"r# s z%GDPbbSolver.solve..BigMcSs$g|]}|jjr|jjdkr|qS)rB) indicator_varfixedr)r>rFr"r"r#rHscSs$g|]}|jjr|jjdkr|qS)r)rJrKr)r>rFr"r"r#rHsrBcsg|]}|kr|qSr"r")r>rF)fixed_false_disjunctsr"r#rHsz_GDPbb only handles disjunctions in which one term can be selected. %s violates this assumption.FinfzSolving the root node.zHModel was found to be infeasible at the root node. Elapsed %.2f seconds.z-infrz:Exploring node with LB %.10g and %s inactive disjunctions.z Model solved.zBranching on disjunction %sc3s*|]"}|k r|jjo |jjdkVqdS)rBN)rJrKr)r>disj)rFr"r#r@szUGDPopt unable to converge bounds before time limit of {} seconds. Elapsed: {} secondsz"Final bound values: LB: {} UB: {}zIAdded %s new nodes with %s relaxed disjunctions to the heap. Size now %s.)TCONFIGpop set_valuersolver'r*r+r-r.r/validate_modelrrZtimingoriginal_modelrresultsgetEffectiveLevelr r rloggingINFOsetLevelinfojoinmapstrr0namer cloneZ working_modelrrDrnextsenseZproblemlistr;Zdisjunction_listZunenforced_disjunctionsrZdeactivated_constraintsZ deactivatehasattrrrIZdisjuncts_fixed_TruerrClenraddactivatexorAssertionErrorremoverobj_signfloatsubproblem_solver rZ lower_boundZ upper_boundZ iterationstc infeasibletermination_conditionheapqheappushheappopzip variable_listr enumerateanyformatZ maxTimeLimitrJZfix)$r3modelkwdsconfigZ solve_dataZold_logger_levelroot objectivesobjrGr?Zfixed_true_disjunctsZnewly_activatedZ obj_valueresult var_valuesheapZtotal_nodes_counterZdisjunctions_leftZ sort_tupleZincumbent_modelZincumbent_resultsZincumbent_var_valuesZincumbent_obj_value_rTZorig_varvalZnext_disjunctionZnew_nodes_counterichildZdisjunction_to_branchZchild_disjunctrNZordering_tupler")rFrLr#rRbs0                                           zGDPbbSolver.solvecCshx$|jtddD]}|jstdqW|jtdd}t|d}t|ddk rTtd|dkrdtddS)NT)rAr=z8GDPbb solver unable to handle non-exclusive disjunctions)r=zGGDPbb solver is unable to handle model with multiple active objectives.z@GDPbb solver is unable to handle model with no active objective.)rDrrg ValueErrorrr` RuntimeError)rxdr|r}r"r"r#rS`s  zGDPbbSolver.validate_modelc sv|}td|t|jtdd}|jtkr6dnd}yt|j j |f|j }Wn`t k r}zB|j dt|tfdd|jjD}|td t|fSd}~XYnXd d|jjD}|j j|j jtjkrtfd d tjtjtjfDrt|j ||fStj!kr2|td ||fStj"krP|td ||fS|j d|td ||fSdS)Nzgdp.bigmT)r=rBzCSolver encountered RuntimeError. Treating as infeasible. Msg: %s %scSsg|] }|jqSr")r)r>vr"r"r#rH~sz0GDPbbSolver.subproblem_solve..rMcSsg|] }|jqSr")r)r>rr"r"r#rHsc3s|]}|kVqdS)Nr")r>Z valid_cond) term_condr"r#r@sz/GDPbbSolver.subproblem_solve..z-infz#Unknown termination condition of %s)#r_rZapply_tor`rDrrarrr'rRr*rr.warningr] traceback format_excr;rtrkrrostatusrokrvrmZoptimalZlocallyOptimalZfeasiblerexprZ unboundedrn)ZgdprzZ subproblemZmain_objrjr~err")rr#rlqs.""  zGDPbbSolver.subproblem_solvecCs|S)Nr")r3r"r"r# __enter__szGDPbbSolver.__enter__cCsdS)Nr")r3trrr"r"r#__exit__szGDPbbSolver.__exit__)T)rr r!__doc__rrOZdeclarerboolr rrr2r7r0rR staticmethodrSrlrr __classcell__r"r")r6r#r&!sJ          r&)+rrprWrZ pyomo.commonrZpyomo.common.collectionsrrZpyomo.common.configrrrZpyomo.contrib.gdpopt.utilrr r r r r rZ!pyomo.contrib.satsolver.satsolverrZ pyomo.corerrrrrrZ pyomo.gdprZ pyomo.optrrrrrmr8objectrregisterr&r"r"r"r#s" $