Optimizer¶
- class Optimizer¶
Bases:
Generic[_T,_C]Methods
Initialize the optimizer.
Add randomly generated solution vectors to the training data.
Add a new solution to the training data.
Evaluate the objective function for a given solution.
Try to find an alternative solution when only duplicate solutions are returned from annealing.
Find a unique solution from a list of solutions.
Find solutions that minimize the surrogate model.
Optimize the black-box function.
Train the surrogate model.
Attributes
Returns the amplify model.
Returns the best solution found so far.
Returns the black-box function.
Returns the encoding information for the black-box function.
Returns the history of optimization iterations.
Returns the initial training data for the black-box function.
Returns the random number generator used in the optimizer.
Returns the data transformer for the surrogate model training data.
Returns the surrogate model.
Returns the training data for the surrogate model (encoded if pre_encoding is True).
Returns the training data for the black-box function.
- classmethod __class_getitem__(params)¶
- __init__(
- blackbox: BlackBoxFuncBase,
- trainer: _T,
- client: _C = FixstarsClient({'url': 'https://optigan.fixstars.com', 'token': '', 'compression': true, 'parameters': {'outputs': {}}}),
- *,
- training_data: Dataset | None = None,
- constraints: amplify.Constraint | amplify.ConstraintList | None = None,
- pre_encoding: bool = True,
- seed: int = 0,
- surrogate_data_transformer: DataTransformer | None = None,
Initialize the optimizer.
- Parameters:
blackbox (BlackBoxFuncBase) – A black-box function defined with the @blackbox decorator.
trainer (_T) – A Trainer class instance.
client (_C, optional) – A solver client defined with the Amplify SDK. Defaults to _default_client.
training_data (Dataset | None) – A training dataset for the surrogate model. Defaults to None.
constraints (amplify.Constraint | amplify.ConstraintList | None, optional) – Constraints. Defaults to None.
pre_encoding (bool, optional) – Whether to perform pre-encoding (True) or post-encoding (False). Defaults to True.
seed (int, optional) – A random seed. Defaults to 0.
surrogate_data_transformer (SurrogateDataTransformer | None, optional) – A data transformer for the surrogate model training data. Defaults to None.
- Raises:
TypeError – If blackbox is not an subclass of BlackBoxFuncBase.
TypeError – If trainer is not an instance of Trainer.
TypeError – If client is not an instance of amplify.BaseClient.
TypeError – If training_data is not an instance of Dataset or None.
TypeError – If constraints is not an instance of amplify.Constraint, amplify.ConstraintList or None.
- classmethod __init_subclass__(*args, **kwargs)¶
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- add_random_training_data(num_data: int, max_trials: int | None = None) None¶
Add randomly generated solution vectors to the training data.
If user-defined constraints are given to the optimizer, the random solutions are generated to satisfy the constraints.
- Parameters:
- Raises:
ValueError – If max_trials is less than num_data.
- add_solution(solution: FlattenedSolution, objective: float) None¶
Add a new solution to the training data.
- Parameters:
solution (FlattenedSolution) – The solution to add.
objective (float) – The objective value for the solution.
- evaluate_objective(solution: FlattenedSolution, use_cache: bool = False) float¶
Evaluate the objective function for a given solution.
- Parameters:
solution (FlattenedSolution) – The solution to evaluate.
use_cache (bool, optional) – Whether to use cached results. Defaults to False.
- Returns:
The objective value for the given solution.
- Return type:
- fallback_solution(
- solutions: list[FlattenedSolution],
- max_trials: int,
Try to find an alternative solution when only duplicate solutions are returned from annealing.
If user-defined constraints are given to the optimizer, the generated fallback solution is expected to satisfy the constraints.
- Parameters:
solutions (list[FlattenedSolution]) – The duplicate input vectors (solutions of the optimization).
max_trials (int) – The max number of trials to find an alternative solution that satisfies the given constraints if any. If max_trials is 0 or max_trials runs out, the first solution (annealing best) is returned regardless of the uniqueness of the solution.
- Returns:
The alternative solution if found, otherwise None.
- Return type:
FlattenedSolution | None
- Raises:
ValueError – If max_trials is less than 0.
- find_unique_solution(
- solutions: list[FlattenedSolution],
Find a unique solution from a list of solutions.
- Parameters:
solutions (list[FlattenedSolution]) – The list of solutions to search.
- Returns:
The unique solution if found, otherwise None.
- Return type:
FlattenedSolution | None
- minimize_surrogate(max_retries: int = 10) tuple[list[FlattenedSolution], Result]¶
Find solutions that minimize the surrogate model.
- Parameters:
max_retries (int, optional) – Maximum number of retries for annealing. Defaults to 10.
- Returns:
List of optimized solutions and the Amplify SDK’s raw result. Solutions are in order of the objective value (from lowest to highest).
- Return type:
tuple[list[FlattenedSolution], amplify.Result]
- optimize(num_iterations: int, max_deduplication_trials: int | None = None) None¶
Optimize the black-box function.
- Parameters:
num_iterations (int) – The number of iterations to perform.
max_deduplication_trials (int | None, optional) – The maximum number of trials to find an alternative solution when duplicate solutions are returned from annealing. If 0 is set, the first solution (annealing best) is returned regardless of the uniqueness of the solution. If None is given, it is set as the number of variables in the black-box function. Defaults to None.
- Raises:
ValueError – If num_iterations is less than or equal to 0.
ValueError – If max_deduplication_trials is less than 0.
RuntimeError – If no feasible solution was found in the iteration.
- __dict__ = mappingproxy({'__module__': 'amplify_bbopt.optimizer', '__init__': <function Optimizer.__init__>, 'blackbox': <property object>, 'training_data': <property object>, 'surrogate_training_data': <property object>, 'initial_training_data': <property object>, 'surrogate_model': <property object>, 'amplify_model': <property object>, 'encoding_info': <property object>, 'surrogate_data_transformer': <property object>, '_transform_surrogate_training_data': <function Optimizer._transform_surrogate_training_data>, '_solution_from_flattened_values': <function Optimizer._solution_from_flattened_values>, 'best': <property object>, 'history': <property object>, 'rng': <property object>, '_mutate_solution': <function Optimizer._mutate_solution>, '_generate_mutated_solutions': <function Optimizer._generate_mutated_solutions>, '_generate_random_solutions': <function Optimizer._generate_random_solutions>, '_encode_blackbox_solution': <function Optimizer._encode_blackbox_solution>, '_search_dataset': <function Optimizer._search_dataset>, 'train_surrogate': <function Optimizer.train_surrogate>, 'minimize_surrogate': <function Optimizer.minimize_surrogate>, 'evaluate_objective': <function Optimizer.evaluate_objective>, 'add_solution': <function Optimizer.add_solution>, 'find_unique_solution': <function Optimizer.find_unique_solution>, 'fallback_solution': <function Optimizer.fallback_solution>, 'optimize': <function Optimizer.optimize>, 'add_random_training_data': <function Optimizer.add_random_training_data>, '__orig_bases__': (typing.Generic[~_T, ~_C],), '__dict__': <attribute '__dict__' of 'Optimizer' objects>, '__weakref__': <attribute '__weakref__' of 'Optimizer' objects>, '__doc__': None, '__parameters__': (~_T, ~_C), '__annotations__': {'_history': 'list[IterationResult]', '_pre_encoding': 'Final[bool]', '_num_initial_data': 'Final[int]'}})¶
- __orig_bases__ = (typing.Generic[~_T, ~_C],)¶
- __parameters__ = (~_T, ~_C)¶
- __slots__ = ()¶
- __weakref__¶
list of weak references to the object (if defined)
- property amplify_model: AmplifyModel¶
Returns the amplify model.
- property blackbox: BlackBoxFuncBase¶
Returns the black-box function.
- property encoding_info: EncodingInfo¶
Returns the encoding information for the black-box function.
- property history: list[IterationResult]¶
Returns the history of optimization iterations.
- property initial_training_data: Dataset¶
Returns the initial training data for the black-box function.
- property surrogate_data_transformer: DataTransformer | None¶
Returns the data transformer for the surrogate model training data.
- property surrogate_model: SurrogateModel¶
Returns the surrogate model.