atomica.optimization.Optimization

class atomica.optimization.Optimization(name=None, adjustments=None, measurables=None, constraints=None, maxtime=None, maxiters=None, method='asd')[source]

Bases: NamedItem

Instructions on how to perform an optimization

The Optimization object stores the information that defines an optimization operation. Optimization can be thought of as a function mapping one set of program instructions to another set of program instructions. The parameters of that function are stored in the Optimization object, and amount to

  • A definition of optimality

  • A specification of allowed changes to the program instructions

  • Any additional information required by a particular optimization algorithm e.g. ASD

Parameters:
  • name

  • adjustments – An Adjustment or list of Adjustment objects

  • measurables – A Measurable or list of Measurable objects

  • constraints – Optionally provide a Constraint or list of Constraint objects

  • maxtime – Optionally specify maximum ASD time

  • maxiters – Optionally specify maximum number of ASD iterations or hyperopt evaluations

  • method – One of [‘asd’,’pso’,’hyperopt’] to use - asd (to use normal ASD) - pso (to use particle swarm optimization from pyswarm) - hyperopt (to use hyperopt’s Bayesian optimization function)

NamedItem constructor

A name must be a string

Parameters:

name

Attributes

maxiters

Maximum number of ASD iterations or hyperopt evaluations

maxtime

Maximum ASD time

method

Optimization method name

Methods

compute_objective

Return total objective function

constrain_instructions

Apply all constraints in-place, return penalty

copy

get_adjustment

Retrieve adjustment by name

get_baselines

Return Measurable baseline values

get_hard_constraints

Get hard constraints

get_initialization

Get initial values for each adjustment

update_instructions

Apply all Adjustments

compute_objective(model, baselines)[source]

Return total objective function

This method accumulates the objective values returned by each Measurable, passing in the corresponding baseline values where required.

Parameters:
  • model – A simulated Model object

  • baselines (list) – List of baseline values the same length as the number of Measurables

Return type:

float

Returns:

The total/net objective value

constrain_instructions(instructions, hard_constraints)[source]

Apply all constraints in-place, return penalty

This method takes in the proposed instructions, and a list of hard constraints. Each constraint is applied to the instructions iteratively, passing in that constraint’s own hard constraint, and the penalty is accumulated and returned.

Parameters:
  • instructions (ProgramInstructions) – The current proposed ProgramInstructions

  • hard_constraints (list) – A list of hard constraints the same length as the number of constraints

Return type:

float

Returns:

The total penalty value (if not finite, model integration will be skipped and the parameters will be rejected)

get_adjustment(name)[source]

Retrieve adjustment by name

Parameters:

name (str)

Return type:

Adjustment

Returns:

get_baselines(pickled_model)[source]

Return Measurable baseline values

This method is run at the start of the optimize script, and is used to retrieve the baseline values for the Measurable. Note that the baseline values are obtained based on the original instructions (stored in the pickled model), independent of the initial parameters used for optimization. The logic is that the initial parameters for the optimization are a choice dictated by the numerics of optimization (e.g. needing to start from a particular part of the parameter space) rather than anything intrinsic to the problem, whereas the initial instructions reflect the actual baseline conditions.

Parameters:
  • pickled_model

  • x0 – The initial parameter values

  • hard_constraints – List of hard constraint values

Return type:

list

Returns:

A list of Measurable baseline values

get_hard_constraints(x0, instructions)[source]

Get hard constraints

This method calls get_hard_constraint on each Constraint in the Optimization iteratively, and returns them as a list.

Note that the initial optimization values x0 are applied _before_ the hard constraint is computed. This ensures that the hard constraints are relative to the initial conditions in the optimization, not the initial instructions. For example, if a parametric overwrite is present, the hard constraint will be relative to whatever spending is produced by the initial values of the parametric overwrite.

Parameters:
  • x0 – The initial values for optimization - these are applied to the instructions prior to extracting hard constraints

  • instructions (ProgramInstructions) – The initial instructions

Return type:

list

Returns:

A list of hard constraints, as many items as there are constraints

get_initialization(progset, instructions)[source]

Get initial values for each adjustment

The initial conditions depend nontrivially on both the progset and the instructions. Spending is present in the progset and optionally overwritten in the instructions. Therefore, it is necessary to check both when determining initial spending. Extraction of the initial values for each Adjustment is delegated to the Adjustment itself.

Note also that the return arrays have length equal to the number of Adjustables (since an Adjustment may contain several Adjustables).

Parameters:
  • progset (ProgramSet) – The program set to extract initial conditions from

  • instructions (ProgramInstructions) – Instructions to extract initial conditions from

Return type:

tuple

Returns:

Tuple containing (initial,low,high) with arrays for - The initial value of each adjustable - The lower limit for each adjustable - The upper limit for each adjustable

maxiters

Maximum number of ASD iterations or hyperopt evaluations

maxtime

Maximum ASD time

method

Optimization method name

update_instructions(asd_values, instructions)[source]

Apply all Adjustments

This method takes in a list of values (same length as number of adjustables) and iteratively calls each Adjustment in the optimization to update the instructions (in place)

Parameters:
  • asd_values – A list of values

  • instructions (ProgramInstructions) – The ProgramInstructions instance to update

Return type:

None