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
Maximum number of ASD iterations or hyperopt evaluations
Maximum ASD time
Optimization method name
Methods
Return total objective function
Apply all constraints in-place, return penalty
copy
Retrieve adjustment by name
Return Measurable baseline values
Get hard constraints
Get initial values for each adjustment
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.
- 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 proposedProgramInstructions
hard_constraints (
list
) – A list of hard constraints the same length as the number of constraints
- Return type:
- 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:
- 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:
- Returns:
A list of Measurable baseline values
- get_hard_constraints(x0, instructions)[source]¶
Get hard constraints
This method calls
get_hard_constraint
on eachConstraint
in theOptimization
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:
- 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 theAdjustment
itself.Note also that the return arrays have length equal to the number of
Adjustables
(since anAdjustment
may contain severalAdjustables
).- Parameters:
progset (
ProgramSet
) – The program set to extract initial conditions frominstructions (
ProgramInstructions
) – Instructions to extract initial conditions from
- Return type:
- 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
) – TheProgramInstructions
instance to update
- Return type: