Optimizer

class Optimizer

Bases: Generic[_T, _C]

Methods

__init__

Initialize the optimizer.

add_random_training_data

Add randomly generated solution vectors to the training data.

add_solution

Add a new solution to the training data.

evaluate_objective

Evaluate the objective function for a given solution.

fallback_solution

Try to find an alternative solution when only duplicate solutions are returned from annealing.

find_unique_solution

Find a unique solution from a list of solutions.

minimize_surrogate

Find solutions that minimize the surrogate model.

optimize

Optimize the black-box function.

train_surrogate

Train the surrogate model.

Attributes

amplify_model

Returns the amplify model.

best

Returns the best solution found so far.

blackbox

Returns the black-box function.

encoding_info

Returns the encoding information for the black-box function.

history

Returns the history of optimization iterations.

initial_training_data

Returns the initial training data for the black-box function.

rng

Returns the random number generator used in the optimizer.

surrogate_model

Returns the surrogate model.

surrogate_training_data

Returns the training data for the surrogate model (encoded if pre_encoding is True).

training_data

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: Constraint | ConstraintList | None = None,
pre_encoding: bool = True,
seed: int = 0,
) 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.

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.AmplifyClient.

  • 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:
  • num_data (int) – The number of solutions to be added.

  • max_trials (int | None, optional) – The max number of trials. This value must be greater than or equal to

  • None. (num_data. If None is given max_trials is set as 2 * num_data. Defaults to)

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:

float

fallback_solution(
solutions: list[FlattenedSolution],
max_trials: int,
) FlattenedSolution | None

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],
) FlattenedSolution | None

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.

train_surrogate() None

Train the surrogate model.

__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>, '_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 best: Solution

Returns the best solution found so far.

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 rng: Generator

Returns the random number generator used in the optimizer.

property surrogate_model: SurrogateModel

Returns the surrogate model.

property surrogate_training_data: Dataset

Returns the training data for the surrogate model (encoded if pre_encoding is True).

property training_data: Dataset

Returns the training data for the black-box function.