saealib.Optimizer#
- class saealib.Optimizer(problem, seed=None)[ソース]#
ベースクラス:
objectOptimizer class for evolutionary algorithms.
Integrates problem definition, evolutionary algorithm, surrogate model, model manager, and termination condition, and manages the optimization process.
- パラメータ:
problem (Problem)
seed (int | None)
- strategy#
The optimization strategy.
- Type:
- termination#
The termination condition.
- Type:
- popsize#
The population size.
- Type:
int
- seed#
The master random seed.
Nonemeans non-deterministic.- Type:
int or None
- fe#
The current number of function evaluations.
- Type:
int
- gen#
The current generation number.
- Type:
int
- cbmanager#
The callback event manager.
- Type:
- instance_name#
The name of the optimizer instance.
- Type:
str
Methods
Initialize the Optimizer. |
|
Return diagnostics for the currently configured component graph. |
|
Describe the most recently compiled plan, if one exists. |
|
Dispatch an event to the callback manager. |
|
Build an Optimizer from a Python file defining a problem. |
|
Iterate the optimization process. |
|
Resume iteration from an existing context (e.g. loaded from checkpoint). |
|
Load an optimizer and context from a pickle checkpoint. |
|
Fill unset components with library defaults (Registry + presets file). |
|
Run the optimization process. |
|
Resume and run to completion from an existing context. |
|
Save the optimizer and context together as a pickle checkpoint. |
|
Save the currently configured components as a reusable preset file. |
|
Set the independent acquisition component. |
|
Set the evolutionary algorithm. |
|
Configure asynchronous evaluation for built-in strategies. |
|
Set the evaluation planner. |
|
Set the evaluator. |
|
Set the feedback builder. |
|
Set execution history channels and return self. |
|
Set the initializer. |
|
Set the instance name. |
|
Set a user-defined preset. |
|
Set the master random seed. |
|
Set the optimization strategy. |
|
Wrap a raw Surrogate in a LocalSurrogateManager. |
|
Set the surrogate manager. |
|
Set the termination condition. |
|
Check configuration consistency. |
Method Details
- Optimizer.__init__(problem, seed=None)[ソース]#
Initialize the Optimizer.
- パラメータ:
problem (Problem) -- The optimization problem.
seed (int or None, optional) -- Master random seed propagated to the initializer.
None(default) means non-deterministic.
- Optimizer.contract_diagnostics()[ソース]#
Return diagnostics for the currently configured component graph.
- 戻り値の型:
DiagnosticBag
- Optimizer.dispatch(event)[ソース]#
Dispatch an event to the callback manager.
- パラメータ:
event (Event)
- 戻り値の型:
None
- classmethod Optimizer.from_problem_file(problem_path, preset=None)[ソース]#
Build an Optimizer from a Python file defining a problem.
The file at problem_path is executed as a standalone module (it is not registered in
sys.modules) and must define a top-levelproblemvariable holding aProbleminstance. If preset is given, it is applied first viaset_preset(); any of the top-level variablesalgorithm,strategy,surrogate_manager,terminationdefined in the file are then applied via the correspondingset_*()method, so the file can override individual components of the preset. A top-levelseedvariable, if present, is passed to theOptimizerconstructor.- パラメータ:
problem_path (str or Path) -- Path to a
.pyfile defining a top-levelproblemvariable.preset (str, Path, dict, or None, optional) -- Preset applied before the file's own component definitions, so that the file can override individual components. See
set_preset().
- 戻り値:
A configured, not-yet-run
Optimizerinstance. Callrun()oriterate()on it.- 戻り値の型:
- 例外:
ValidationError -- If the file does not define a top-level
problemvariable, or it is not aProbleminstance. Also raised if a top-levelalgorithm,strategy,surrogate_manager,termination, orseedvariable is defined but is not an instance of its expected type.FileNotFoundError -- If problem_path does not exist.
サンプル
>>> # problem.py: >>> # problem = Problem(func=..., dim=2, n_obj=1, direction=[-1]) >>> # seed = 42 >>> opt = Optimizer.from_problem_file("problem.py", preset="preset.yaml") >>> ctx = opt.run()
- Optimizer.iterate(checkpoint_path=None, checkpoint_interval=1, checkpoint_format='npz', checkpoint_delete_on_success=False)[ソース]#
Iterate the optimization process.
- パラメータ:
checkpoint_path (str, Path, or None, optional) -- If provided, checkpoints are saved to this directory every checkpoint_interval generations.
checkpoint_interval (int, optional) -- Generations between checkpoints. Default: 1.
checkpoint_format ({'npz', 'pickle', 'both'}, optional) -- Checkpoint format. Default:
'npz'.checkpoint_delete_on_success (bool, optional) -- Delete checkpoints on normal termination. Default: False.
- 戻り値:
Generator of OptimizationState.
- 戻り値の型:
Generator[OptimizationState]
- Optimizer.iterate_from(ctx, checkpoint_path=None, checkpoint_interval=1, checkpoint_format='npz', checkpoint_delete_on_success=False)[ソース]#
Resume iteration from an existing context (e.g. loaded from checkpoint).
Does not call the initializer; all other components must be configured.
- パラメータ:
ctx (OptimizationState) -- Context to resume from.
checkpoint_path (str, Path, or None, optional) -- If provided, checkpoints are saved to this directory every checkpoint_interval generations.
checkpoint_interval (int, optional) -- Generations between checkpoints. Default: 1.
checkpoint_format ({'npz', 'pickle', 'both'}, optional) -- Checkpoint format. Default:
'npz'.checkpoint_delete_on_success (bool, optional) -- Delete checkpoints on normal termination. Default: False.
- 戻り値の型:
Generator[OptimizationState, None, None]
- classmethod Optimizer.load_pickle(path)[ソース]#
Load an optimizer and context from a pickle checkpoint.
The returned context has
resumed=True. Callrun_from()oriterate_from()on the returned optimizer to continue the optimization.警告
Pickle files are tied to specific Python and library versions.
- パラメータ:
path (str or Path) -- Path to the
.pklfile. The extension is added if absent.- 戻り値の型:
tuple[Optimizer, OptimizationState]
- Optimizer.resolve_defaults()[ソース]#
Fill unset components with library defaults (Registry + presets file).
Components already set via
set_*()are never overwritten. Gaps are filled first from a user preset (set_preset()), if any, then from a bundled preset selected by (1) the algorithm's registered name ifalgorithmis set, else (2) a Problem-shape rule, else (3) the universal fallback preset.initializeris computed from semantic defaults and is not part of any preset;terminationfalls back to the resolved maximum-evaluations default (200 * problem.dimunless a provider recommends another value) only if neitherset_*()nor a preset supplies one.- 戻り値の型:
None
- Optimizer.run(checkpoint_path=None, checkpoint_interval=1, checkpoint_format='npz', checkpoint_delete_on_success=False)[ソース]#
Run the optimization process.
- パラメータ:
checkpoint_path (str, Path, or None, optional) -- If provided, checkpoints are saved to this directory every checkpoint_interval generations.
checkpoint_interval (int, optional) -- Generations between checkpoints. Default: 1.
checkpoint_format ({'npz', 'pickle', 'both'}, optional) -- Checkpoint format. Default:
'npz'.checkpoint_delete_on_success (bool, optional) -- Delete checkpoints on normal termination. Default: False.
- 戻り値:
The optimization context.
- 戻り値の型:
OptimizationState
- Optimizer.run_from(ctx, checkpoint_path=None, checkpoint_interval=1, checkpoint_format='npz', checkpoint_delete_on_success=False)[ソース]#
Resume and run to completion from an existing context.
- パラメータ:
ctx (OptimizationState) -- Context to resume from.
checkpoint_path (str, Path, or None, optional) -- If provided, checkpoints are saved to this directory every checkpoint_interval generations.
checkpoint_interval (int, optional) -- Generations between checkpoints. Default: 1.
checkpoint_format ({'npz', 'pickle', 'both'}, optional) -- Checkpoint format. Default:
'npz'.checkpoint_delete_on_success (bool, optional) -- Delete checkpoints on normal termination. Default: False.
- 戻り値:
The final optimization context.
- 戻り値の型:
OptimizationState
- Optimizer.save_pickle(ctx, path)[ソース]#
Save the optimizer and context together as a pickle checkpoint.
This preserves fitted surrogate state and all component objects, offering complete reproducibility within the same environment.
警告
Pickle files are tied to specific Python and library versions. Use
OptimizationState.save()for a more portable format.- パラメータ:
ctx (OptimizationState) -- Current optimization context.
path (str or Path) -- Destination file path. The
.pklextension is added if absent.
- 戻り値の型:
None
- Optimizer.save_preset(path)[ソース]#
Save the currently configured components as a reusable preset file.
Serializes
algorithm,surrogate_manager,strategy, andtermination(whichever are set viaset_*()) into a preset dict and writes it as YAML. Problem-owned parameters (dim,direction) are stripped so the preset can be reused across problems of different dimensionality.- パラメータ:
path (str or Path) -- Destination file path. The
.yamlextension is added if absent.- 戻り値:
The path the preset was written to.
- 戻り値の型:
Path
- 例外:
ValidationError -- If no components are configured, or a configured component cannot be serialized (e.g. holds a raw lambda).
- Optimizer.set_algorithm(algorithm)[ソース]#
Set the evolutionary algorithm. Returns self.
- パラメータ:
algorithm (Algorithm)
- 戻り値の型:
Self
- Optimizer.set_async_evaluation_scheduler(scheduler)[ソース]#
Configure asynchronous evaluation for built-in strategies.
- パラメータ:
scheduler (AsyncEvaluationScheduler | None)
- 戻り値の型:
Self
- Optimizer.set_evaluation_planner(planner)[ソース]#
Set the evaluation planner.
- パラメータ:
planner (EvaluationPlanner)
- 戻り値の型:
Self
- Optimizer.set_evaluator(evaluator)[ソース]#
Set the evaluator. Returns self.
- パラメータ:
evaluator (Evaluator)
- 戻り値の型:
Self
- Optimizer.set_feedback_builder(builder)[ソース]#
Set the feedback builder.
- パラメータ:
builder (FeedbackBuilder)
- 戻り値の型:
Self
- Optimizer.set_history(channels)[ソース]#
Set execution history channels and return self.
On resume, the checkpoint's channels take precedence unless this method was called; calling it with a different set fails validation. Assigning to
history_channelsdirectly does not count as calling it.- パラメータ:
channels (Sequence[str])
- 戻り値の型:
Self
- Optimizer.set_initializer(initializer)[ソース]#
Set the initializer. Returns self.
- パラメータ:
initializer (Initializer)
- 戻り値の型:
Self
- Optimizer.set_instance_name(name)[ソース]#
Set the instance name. Returns self.
- パラメータ:
name (str)
- 戻り値の型:
Self
- Optimizer.set_preset(preset)[ソース]#
Set a user-defined preset. Returns self.
The preset fills components that are not already configured via
set_*(); explicitly set components take precedence.- パラメータ:
preset (str | Path | dict)
- 戻り値の型:
Self
- Optimizer.set_seed(seed)[ソース]#
Set the master random seed. Returns self.
- パラメータ:
seed (int | None)
- 戻り値の型:
Self
- Optimizer.set_strategy(strategy)[ソース]#
Set the optimization strategy. Returns self.
- パラメータ:
strategy (OptimizationStrategy)
- 戻り値の型:
Self
- Optimizer.set_surrogate(surrogate, n_neighbors=50)[ソース]#
Wrap a raw Surrogate in a LocalSurrogateManager. Returns self.
Equivalent to
set_surrogate_manager(LocalSurrogateManager(surrogate, ...)).- パラメータ:
surrogate (Surrogate)
n_neighbors (int)
- 戻り値の型:
Self
- Optimizer.set_surrogate_manager(manager)[ソース]#
Set the surrogate manager. Returns self.
- パラメータ:
manager (SurrogateManager)
- 戻り値の型:
Self
- Optimizer.set_termination(termination)[ソース]#
Set the termination condition. Returns self.
- パラメータ:
termination (Termination)
- 戻り値の型:
Self
- Optimizer.validate(*, require_initializer=True)[ソース]#
Check configuration consistency. Returns list of issues.
Call
resolve_defaults()first when validation results should reflect the components that will be used for execution.- パラメータ:
require_initializer (bool, optional) -- When False, skip the initializer presence check. Use this when resuming from a checkpoint where initialization is not needed.
- 戻り値の型:
list[str]