Selectors¶
Abstract Base Selector¶
-
class
glompo.opt_selectors.baseselector.
BaseSelector
(*avail_opts, allow_spawn: Optional[Callable[GloMPOManager, bool]] = None)[source]¶ Base selector from which all selectors must inherit to be compatible with GloMPO. Selectors are classes which return an optimizer and its configuration when asked by the manager. This selection will then be used to start a new optimizer. The full manager is supplied to the selector allowing sophisticated decisions to be designed.
Parameters: - *avail_opts –
A set of optimizers available to the minimization. Elements may be:
- Subclasses (not instances) of
BaseOptimizer
. - Tuples of:
BaseOptimizer
subclasses (not instances);- Dictionary of kwargs sent to
BaseOptimizer.__init__
, orNone
; - Dictionary of kwargs sent to
BaseOptimizer.minimize()
, orNone
.
- Subclasses (not instances) of
- allow_spawn – Optional function sent to the selector which is called with the manager object as argument. If it returns
False
the manager will no longer spawn optimizers. See Spawn Control.
Examples
>>> DummySelector(OptimizerA, (OptimizerB, {'setting_a': 7}, None))
The
DummySelector
above may choose from two optimizers (OptimizerA
orOptimizerB
).OptimizerA
has no special configuration settings.OptimizerB
is configured withsetting_a = 7
at initialisation, but no special arguments are needed forOptimizerB.minimize()
and thusNone
is sent in the last place of the tuple.>>> DummySelector(OptimizerA, allow_spawn=IterSpawnStop(50_000))
In this case the selector will only spawn
OptimizerA
optimizers but not allow any spawning after 50000 function evaluations.-
allow_spawn
¶ Function used to control if a new optimizer should be allowed to be created.
Type: Callable[[GloMPOManager], bool]
-
avail_opts
¶ Set of optimizers and configuration settings available to the optimizer.
Type: Union[Type[BaseOptimizer], Tuple[Type[BaseOptimizer], Optional[Dict[str, Any]], Optional[Dict[str, Any]]]]
-
logger
¶ logging.Logger
instance into which status messages may be added.Type: logging.Logger
-
select_optimizer
(manager: GloMPOManager, log: glompo.core.optimizerlogger.BaseLogger, slots_available: int) → Union[Tuple[Type[glompo.optimizers.baseoptimizer.BaseOptimizer], Dict[str, Any], Dict[str, Any]], None, bool][source]¶ Selects an optimizer to start from the available options.
Parameters: - manager –
GloMPOManager
instance managing the optimization from which various attributes can be read. - log –
BaseLogger
instance containing the details and iteration history of every optimizer started thus far. - slots_available – Number of processes/threads the manager is allowed to start according to
GloMPOManager.max_jobs
and the number of existing threads. GloMPO assumes that the selector will use this parameter to return an optimizer which requires fewer processes/threads than slots_available. If this is not possible thenNone
is returned.
Returns: Optimizer class and configuration parameters: Tuple of optimizer class, dictionary of initialisation parameters, and dictionary of minimization parameters (see
__init__
). Manager will use this to initialise and start a new optimizer.None
is returned in the case that no available optimizer configurations can satisfy the number of worker slots available.False
is a special return which flags that the manager must never try and start another optimizer for the remainder of the optimization.Return type: Union[Tuple[Type[BaseOptimizer], Dict[str, Any], Dict[str, Any]], None, bool]
- manager –
- *avail_opts –
Spawn Control¶
As seen above, spawning new optimizers can be stopped without shutting down the entire optimization. This can be used to make sure that functioning optimizers are given time to explore, and function evaluations are not wasted on new optimizers towards the end of an optimization when they will be unlikely to have enough time to develop. GloMPO comes bundled with a couple of simple convenient controllers.
The __call__()
method of these classes is expected to be implemented as follows:
-
__call__
(manager: GloMPOManager)¶ Evaluated everytime
BaseSelector.select_optimizer()
is called. Determines if new optimizers should stop spawning for the remainder of the optimization.Returns: True
if spawning should be allowed,False
otherwise.Return type: bool
Included Selectors¶
For convenience, GloMPO comes bundled with several simple selectors already included.
-
class
glompo.opt_selectors.
ChainSelector
(*avail_opts, fcall_thresholds: List[float], allow_spawn: Optional[Callable[GloMPOManager, bool]] = None)[source]¶ Bases:
glompo.opt_selectors.baseselector.BaseSelector
Selects the type of optimizer to start based on the number of function evaluations already used. Designed to start different types of optimizers at different stages of the optimization. Selects sequentially from the list of available optimizers based on the number of function evaluations used.
Parameters: - *avail_opts – See
BaseSelector
. - fcall_thresholds –
A list of length
n
orn-1
, wheren
is the length of avail_opts.The first
n-1
elements of this list indicate the function evaluation point at which the selector switches to the next type of optimizer in avail_opts.The optional
n
th element indicates the function evaluation at which optimizer spawning is turned off. - allow_spawn – See
BaseSelector
.
Examples
>>> ChainSelector(OptimizerA, OptimizerB, fcall_thresholds=[1000])
In this case
OptimizerA
instances will be started in the first 1000 iterations andOptimizerB
instances will be started thereafter.>>> ChainSelector(OptimizerA, OptimizerB, fcall_thresholds=[1000, 2000])
In this case
OptimizerA
instances will be started in the first 1000 iterations andOptimizerB
instances will be started until iteration 2000. No new optimizers will be spawned thereafter.- *avail_opts – See
-
class
glompo.opt_selectors.
CycleSelector
(*avail_opts, allow_spawn: Optional[Callable] = None)[source]¶ Bases:
glompo.opt_selectors.baseselector.BaseSelector
Cycles through the list of available optimizers. Iterates through the list of available optimizers everytime
select_optimizer()
is called. When the last element is reached, the selector loops back to the beginning.Examples
>>> selector = CycleSelector(OptimizerA, OptimizerB, OptimizerC) >>> for i in range(5): ... selector.select_optimizer(manager, log, 1) OptimizerA OptimizerB OptimizerC OptimizerA OptimizerB
-
class
glompo.opt_selectors.
RandomSelector
(*avail_opts, allow_spawn: Optional[Callable[GloMPOManager, bool]] = None)[source]¶ Bases:
glompo.opt_selectors.baseselector.BaseSelector
Randomly selects an optimizer from the available options.