Hunters¶
Abstract Base Hunter¶
-
class
glompo.hunters.basehunter.
BaseHunter
[source]¶ Base hunter from which all hunters must inherit to be compatible with GloMPO.
-
logger
¶ logging.Logger
instance into which status messages may be added.Type: logging.Logger
-
__call__
(log: glompo.core.optimizerlogger.BaseLogger, hunter_opt_id: int, victim_opt_id: int) → bool[source]¶ Compares two optimizers and returns if one should be terminated according to some condition. When called, this method may check any values within the logs of both the hunter or the victim.
Parameters: - log – Instance of
BaseLogger
that contains the iteration history of every optimizer. - hunter_opt_id – ID number of the ‘hunter’; defined as the optimizer which has found the lowest function value.
- victim_opt_id – ID number of the ‘victim’; defined as any optimizer which is not the hunter.
Returns: True
if the condition in the method is met,False
otherwise.Return type: Notes
For proper functionality, the result of this method must be saved to
last_result
before returning.- log – Instance of
-
__iter__
() → Iterable[glompo.common.corebase._CoreBase]¶ Provides a (flattened) iteration through all the bases which comprise the combined hunter/checker.
-
__str__
() → str¶ Produces a string of the hunter/checker’s name and configuration.
-
last_result
¶ The result of the last
__call__()
.
-
reset
()¶ Clears previous evaluation result to avoid misleading printing. Resets
last_result
toNone
. Given that hunters and checkers are evaluated lazily, it is possible for misleading results to be returned bystr_with_result()
indicating a hunt/check has been evaluated when it has not. Bases are thus reset before__call__()
to prevent this.
-
str_with_result
() → str¶ String representation of the object with its convergence result.
-
Combining Base Hunters¶
BaseHunter
is based on the same structure as BaseChecker
. Thus, simple conditions can also be combined into more sophisticated termination conditions. See Combining Base Checkers.
Included Hunters¶
For convenience, GloMPO comes bundled with several simple hunters already included.
-
class
glompo.hunters.
BestUnmoving
(calls: int, tol: float = 0)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Considers the lowest function value seen by the optimizer thus far. Returns
True
if the victim’s best value has not changed significantly in a given amount of time.Parameters: - calls – Number of function evaluations between comparison points.
- tol – Tolerance fraction between 0 and 1.
Returns: True
if:abs(latest_f_value - f_value_calls_ago) <= abs(f_value_calls_ago * self.tol)
Return type:
-
class
glompo.hunters.
EvaluationsUnmoving
(calls: int, tol: float = 0)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Considers function values the optimizers are currently exploring. Used to terminate an optimizer when its function evaluations are unchanging, usually indicating that it is approaching some convergence. Best used with a hunter which monitors step size to ensure a widely exploring optimizer is not killed.
Parameters: - calls – Number of function evaluations between comparison points.
- tol – Tolerance fraction between 0 and 1.
Returns: Returns
True
if the standard deviation of the last calls function evaluations is belowtol * abs(latest_f_eval)
.Return type:
-
class
glompo.hunters.
LastPointsInvalid
(n_iters: int = 1)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Checks for non-numerical solutions. Some pathological functions may have undefined regions within them or combinations of parameters which return non-finite results.
Parameters: n_iters – Number of allowed invalid function evaluations. Returns: Returns True
if the optimizer fails to find a valid function evaluation in the last n_iters function evaluations.Return type: bool
-
class
glompo.hunters.
MinFuncCalls
(min_pts: int)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Keeps an optimizer alive for a minimum number of function evaluations.
Parameters: min_pts – Minimum number of points for which an optimizer should be kept alive. Returns: Returns True
after the function has been evaluated at least min_pts times.Return type: bool
-
class
glompo.hunters.
ParameterDistance
(bounds: Sequence[Tuple[float, float]], relative_distance: float, test_all: bool = False)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Terminates optimizers which are too close in parameter space.
Parameters: - bounds – Bounds of each parameter.
- relative_distance – Fraction (between 0 and 1) of the maximum distance in the space (from the point at all lower bounds to the point at all upper bounds) below which the optimizers are deemed too close and the victim will be killed.
- test_all – If
True
the distance between victim and all other optimizers is tested, else only the hunter and victim are compared.
Returns: Returns
True
if optimizers are calculated to be too close together.Return type: Notes
Calculates the maximum Euclidean distance in parameter space between the point of lower bounds and the point of upper bounds (
max_distance
). Calculates the Euclidean distance between the final iterations of the hunter and victim (inter_optimizer_distance
). ReturnsTrue
if:inter_optimizer_distance <= max_distance * relative_distance
Caution
Use this hunter with care and only in problems where the parameters have been standardised so that every parameter is dimensionless and on the same order of magnitude.
-
class
glompo.hunters.
StepSize
(bounds: Sequence[Tuple[float, float]], calls: int, relative_tol: float = 0.05)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Monitors distance in parameter space between function evaluations. This hunter will kill an optimizer that is excessively focused on one area of parameter space.
Parameters: - bounds – Bounds of each parameter.
- calls – Number of function evaluations over which to perform the averaging.
- relative_tol – Fraction (between 0 and 1) of the maximum distance in the space (from the point at all lower bounds to the point at all upper bounds) below which the optimizers are deemed too close and the victim will be killed.
Returns: True
if the victim’s average step size over the last calls function evaluations is less than:relative_tol * maximum_parameter_space_distance
Return type:
-
class
glompo.hunters.
TimeAnnealing
(crit_ratio: float = 1)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Keeps optimizers alive based on how long they have been alive. Randomly keeps optimizers alive based on how long (in function evaluations) they have been active. The newer an optimizer is, the more likely it will pass the test and be kept alive. Used to temper very strict termination conditions.
Parameters: crit_ratio – Critical ratio of function calls between hunter and victim above which the victim is guaranteed to survive. Values lower than one are looser and allow the victim to survive even if it has been in operation longer than the hunter. Values higher than one are stricter and may kill the victim even if it has iterated fewer times than the hunter. Returns: True
if an optimizer has been alive long enough and fails a comparison test with a uniformly randomly generated number.Return type: bool Notes
This condition calculates the quotient (
num_hunter_fcalls / num_victim_fcalls
). A random number is then generated between zero and crit_ratio. Only if the quotient is larger than this number does the victim remain alive.
-
class
glompo.hunters.
TypeHunter
(opt_to_kill: glompo.optimizers.baseoptimizer.BaseOptimizer)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Kills an optimizer based on its class. Intended for use with other hunters to allow for specific hunting conditions based on the type of optimizer.
Parameters: opt_to_kill – BaseOptimizer
class which is targeted.Returns: True
if the victim is an instance of opt_to_kill.Return type: bool Examples
>>> TypeHunter(CMAOptimizer) & HunterA() | TypeHunter(Nevergrad) & HunterB()
In this case
HunterA
will only killCMAOptimizer
s andHunterB
will only killNevergrad
optimizers. This is useful in cases where exploratory optimizers should be killed quickly but late stage optimizers encouraged to converge and iterate for longer periods.
-
class
glompo.hunters.
ValueAnnealing
(med_kill_chance: float = 0.5)[source]¶ Bases:
glompo.hunters.basehunter.BaseHunter
Keeps optimizers alive based on the function values they are exploring. This condition is unlikely to kill a victim which is very near the hunter (in terms of function value) but the probability of killing increases with the difference between them. This condition can be applied in combination with others to prevent ‘competitive’ optimizers for being killed while still terminating poorly performing ones.
The decision criteria follows an exponential distribution which corresponds to the probability of survival. Control of the probability can be achieved through the med_kill_chance initialisation criteria.
Parameters: med_kill_chance (float) – The probability of killing a victim which is twice as large as the hunter in absolute value. The default is 50%. Returns: True
if the difference between the explored function values of the hunter and victim fails a comparison test with a uniformly randomly generated number.Return type: bool