B @`$@sddlmZddlZddlmZmZmZmZddlm Z ddl m Z ddl m Z mZddlmZmZmZmZddlmZmZdd lmZed Zejd d d GdddeZdS))divisionN) ConfigBlock ConfigValueInadd_docstring_list)unique_component_name) should_stop)reinitialize_variables strategies) ObjectiveVarminimizevalue) SolverFactory SolverStatus)TerminationConditionzpyomo.contrib.multistartZ multistartzMultiStart solver for NLPs)docc @seZdZdZedZededee dddededd d ed eid d ed eddd ededdddededdddedede ddededdd eded d!d e eeZd.d#d$Z d%d&Zd'd(Zd)d*Zd+d,Zd-S)/ MultiStarta\Solver wrapper that initializes at multiple starting points. # TODO: also return appropriate duals For theoretical underpinning, see https://www.semanticscholar.org/paper/How-many-random-restarts-are-enough-Dick-Wong/55b248b398a03dc1ac9a65437f88b835554329e0 Keyword arguments below are specified for the ``solve`` function. strategyrandz/Specify the restart strategy. Defaults to rand.aSpecify the restart strategy. - "rand": random choice between variable bounds - "midpoint_guess_and_bound": midpoint between current value and farthest bound - "rand_guess_and_bound": random choice between current value and farthest bound - "rand_distributed": random choice among evenly distributed values - "midpoint": exact midpoint between the bounds. If using this option, multiple iterations are useless. )defaultdomain descriptionrsolverZipoptz solver to use, defaults to ipopt)rr solver_argsz6Dictionary of keyword arguments to pass to the solver. iterations ztSpecify the number of iterations, defaults to 10. If -1 is specified, the high confidence stopping rule will be used stopping_massg?z3Maximum allowable estimated missing mass of optima.zMaximum allowable estimated missing mass of optima for the high confidence stopping rule, only used with the random strategy. The lower the parameter, the stricter the rule. Value bounded in (0, 1].)rrrstopping_deltaz<1 minus the confidence level required for the stopping rule.z1 minus the confidence level required for the stopping rule for the high confidence stopping rule, only used with the random strategy. The lower the parameter, the stricter the rule. Value bounded in (0, 1].Zsuppress_unbounded_warningFz:True to suppress warning for skipping unbounded variables.)rrrHCS_max_iterationsizSMaximum number of iterations before interrupting the high confidence stopping rule. HCS_tolerancerzWTolerance on HCS objective value equality. Defaults to Python float equality precision.TcCsdS)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. T)selfZexception_flagr!r!B/tmp/pip-unpacked-wheel-bi3529v6/pyomo/contrib/multistart/multi.py availabledszMultiStart.availablecCsdS)NTr!)r"r!r!r#license_is_validmszMultiStart.license_is_validc Ks||di}||t|j}|jtdd}t|d}t|ddk rTtd|dkrdtd| dkrxtdg}|j t krdnd }t d |}|} d} zt |d } t|| t|jtdd |j|f|j} } | jjtjkr| jjtjkrt|j} | }|| d}|j}|jd k}d }|rN|jdksHtd|j}x||kr2|r|t ||j!|j"|j#r|d}P|d7}|dkr|$n|}t%|||j|f|j} | jjtjkr| jjtjkr|jtdd}t|}t|j} || | |||kr| }|} | } |dkrP|} | } qPW|rN|sNt&'d|| |kr\| St(|| }t(| | }x*t)||D]\}}|*s||j|_q|W| St+|| XdS)NoptionsT)activezLMultistart solver is unable to handle model with multiple active objectives.zEMultistart solver is unable to handle model with no active objective.rz8Multistart solver received model with constant objectiveinfZ _vars_list)ctypeZ descend_intoFrz5High confidence stopping rule requires rand strategy.zHigh confidence stopping rule was unable to complete after %s iterations. To increase this limit, change the HCS_max_iterations flag.),CONFIGpop set_valuerrZcomponent_data_objectsr next RuntimeErrorZpolynomial_degreeZsenser floatrsetattrlistr solverstatusrokZtermination_conditiontcZoptimalrexprappendrrAssertionErrorrrrrr cloner loggerwarninggetattrzipZis_fixeddelattr)r"modelkwdsconfigrZ objectivesobjZobj_signZbest_objectiveZ best_modelZ best_resultZtmp_var_list_nameresultZobj_valZnum_iterZmax_iterZ using_HCSZ HCS_completedmZmodel_objectivesZmobjZ orig_var_listZbest_soln_var_listZorig_varZnew_varr!r!r#r4ps                    zMultiStart.solvecCs|S)Nr!)r"r!r!r# __enter__szMultiStart.__enter__cCsdS)Nr!)r"tv tracebackr!r!r#__exit__szMultiStart.__exit__N)T)__name__ __module__ __qualname____doc__rr,Zdeclarerrr keysboolrr$r%r4rGrKr!r!r!r#rsL             jr) __future__rloggingZpyomo.common.configrrrrZpyomo.common.modelingrZ'pyomo.contrib.multistart.high_conf_stoprZpyomo.contrib.multistart.reinitr r Z pyomo.corer r r rZ pyomo.optrrrr7 getLoggerr<registerobjectrr!r!r!r# s