Constraint Functions
This module provides a library of pre-defined ParMOO constraint function implementations and templates to define your own constraint function.
ConstraintFunction Template (ABC)
An abstract base class (ABC) for ParMOO constraint functions.
- class constraints.const_func.ConstraintFunction(des_type, sim_type)
ABC defining ParMOO constraint functions.
Extend this class to create a callable object that matches ParMOO can use as a constraint function, such as an Objective or its Gradient.
- Contains 2 methods:
__init__(des_type, sim_type)__call__(x, sx)
The
__init__method is already implemented, and is the constructor. It can be overwritten if additional inputs (besides the design variable and simulation output types) are needed.The
__call__method is left to be implemented, and performs the constraint function evaluation.- __init__(des_type, sim_type)
Constructor for ConstraintFunction class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
Constraint Function Library
This module contains a library of common constraint functions, matching ParMOO’s interface.
- The common constraints are:
SingleSimBound– bound on a single simulation output’s valueSumOfSimSquaresBound– bound on the SOS for several sim outputsSumOfSimsBound– bound on the (abs) sum of several sim outputs
- And their corresponding gradient functions are:
SingleSimBoundGradientSumOfSimSquaresBoundGradientSumOfSimsBoundGradient
- class constraints.const_lib.SingleSimBound(des_type, sim_type, sim_ind, bound_type='upper', bound=0.0)
Class for bounding a single simulation’s output.
If upper-bounding:
``` def ConstraintFunction(x, sx):
return sx[self.sim_ind] - upper_bound
If lower-bounding:
``` def ConstraintFunction(x, sx):
return lower_bound - sx[self.sim_ind]
Also supports derivative usage.
- Contains 2 methods:
__init__(des_type, sim_type, sim_ind, bound_type='min', bound=0)__call__(x, sim)
The
__init__method inherits from the ConstraintFunction ABC.The
__call__returns the slack (negative when feasible).- __init__(des_type, sim_type, sim_ind, bound_type='upper', bound=0.0)
Constructor for SingleSimBound class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
sim_ind (str or tuple) – The name (or name, output index pair for simulations with more than one output field) designating the simulation output to minimize or maximize.
bound_type (str) – Either ‘lower’ to lower-bound or ‘upper’ to upper-bound. Defaults to ‘upper’.
bound (float) – The lower/upper bound for this constraint. Defaults to 0.
- __call__(x, sx)
Define constraint evaluation.
- Parameters:
x (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of a design point to evaluate.
sx (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of the simulation output(s) at x.
- Returns:
The (negative when feasible) slack in this constraint for the input sx (x is unused).
- Return type:
float
- class constraints.const_lib.SumOfSimSquaresBound(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0)
Class for constraining the sum-of-squared simulation outputs.
If upper bounding:
``` ConstraintFunction(x, sx):
return sum([sx[i]**2 for all i]) - upper_bound
If lower bounding:
``` def ConstraintFunction(x, sx):
return lower_bound - sum([sx[i]**2 for all i])
Also supports derivative usage.
- Contains 2 methods:
__init__(des_type, sim_type, sim_inds, bound_type='upper', bound=0)__call__(x, sx)
The
__init__method inherits from the ConstraintFunction ABC.The
__call__evaluate the slack (negative values are feasible).- __init__(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0)
Constructor for SumOfSimSquaresBound class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
sim_inds (list) – The list of names (or name, index pairs for sims with more than one output) of the simulation outputs to sum over.
bound_type (str) – Either ‘lower’ to lower-bound or ‘upper’ to upper-bound. Defaults to ‘upper’.
bound (float) – The lower/upper bound for this constraint. Defaults to 0.
- __call__(x, sx)
Define constraint evaluation.
- Parameters:
x (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of a design point to evaluate.
sx (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of the simulation output(s) at x.
- Returns:
The (negative when feasible) slack in this constraint for the input sx (x is unused).
- Return type:
float
- class constraints.const_lib.SumOfSimsBound(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0, absolute=False)
Class for bounding the (absolute) sum of simulation outputs.
If upper bounding:
``` def const_func(x, sx):
return sum([sx[i] for all i]) - upper_bound
If lower bounding:
``` def const_func(x, sx):
return lower_bound - sum([sx[i] for all i])
If upper bounding absolute sum:
``` def const_func(x, sx):
return sum([abs(sx[i]) for all i]) - upper_bound
If lower bounding absolute sum:
``` def const_func(x, sx):
return lower_bound - sum([abs(sx[i]) for all i])
Also supports derivative usage.
- Contains 2 methods:
- ``__init__(des_type, sim_type, sim_inds,
bound_type=’upper’, bound=0, absolute=False)``
__call__(x, sim)
The
__init__method inherits from the ConstraintFunction ABC.The
__call__evaluate the slack (negative values are feasible).- __init__(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0, absolute=False)
Constructor for SumOfSimsBound class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
sim_inds (list) – The list of names (or name, index pairs for sims with more than one output) of the simulation outputs to sum over.
bound_type (str) – Either ‘lower’ to lower-bound or ‘upper’ to upper-bound. Defaults to ‘upper’.
bound (float) – The lower/upper bound for this constraint. Defaults to 0.
absolute (bool) – True to bound absolute sum, False to bound raw sum. Defaults to False.
- __call__(x, sx)
Define constraint evaluation.
- Parameters:
x (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of a design point to evaluate.
sx (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of the simulation output(s) at x.
- Returns:
The (negative when feasible) slack in this constraint for the input sx (x is unused).
- Return type:
float
- class constraints.const_lib.SingleSimBoundGradient(des_type, sim_type, sim_ind, bound_type='upper', bound=0.0)
Gradient class for SingleSimBound.
Inherits from the
SingleSimBoundclass, but overwrites the__call__method to return the gradients wrt x and sx, respectively.- __init__(des_type, sim_type, sim_ind, bound_type='upper', bound=0.0)
Constructor for SingleSimBound class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
sim_ind (str or tuple) – The name (or name, output index pair for simulations with more than one output field) designating the simulation output to minimize or maximize.
bound_type (str) – Either ‘lower’ to lower-bound or ‘upper’ to upper-bound. Defaults to ‘upper’.
bound (float) – The lower/upper bound for this constraint. Defaults to 0.
- __call__(x, sx)
Define gradient evaluation.
- Parameters:
x (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of a design point to evaluate.
sx (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of the simulation output(s) at x.
- Returns:
The gradient output of the simulation at self.sim_ind given the input (x, sx) wrt x and sx, respectively.
- Return type:
dict, dict
- class constraints.const_lib.SumOfSimSquaresBoundGradient(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0)
Gradient class for SumOfSimSquaresBound.
Inherits from the
SumOfSimSquaresBoundclass, but overwrites the__call__method to return the gradients wrt x and sx, respectively.- __init__(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0)
Constructor for SumOfSimSquaresBound class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
sim_inds (list) – The list of names (or name, index pairs for sims with more than one output) of the simulation outputs to sum over.
bound_type (str) – Either ‘lower’ to lower-bound or ‘upper’ to upper-bound. Defaults to ‘upper’.
bound (float) – The lower/upper bound for this constraint. Defaults to 0.
- __call__(x, sx)
Define gradient evaluation.
- Parameters:
x (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of a design point to evaluate.
sx (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of the simulation output(s) at x.
- Returns:
The gradient of the sum of squared simulation outputs given the input (x, sx) wrt x and sx, respectively.
- Return type:
dict, dict
- class constraints.const_lib.SumOfSimsBoundGradient(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0, absolute=False)
Gradient class for SumOfSimsBound.
Inherits from the
SumOfSimsBoundclass, but overwrites the__call__method to return the gradients wrt x and sx, respectively.- __init__(des_type, sim_type, sim_inds, bound_type='upper', bound=0.0, absolute=False)
Constructor for SumOfSimsBound class.
- Parameters:
des_type (np.dtype) – The numpy.dtype of the design variables.
sim_type (np.dtype) – The numpy.dtype of the simulation outputs.
sim_inds (list) – The list of names (or name, index pairs for sims with more than one output) of the simulation outputs to sum over.
bound_type (str) – Either ‘lower’ to lower-bound or ‘upper’ to upper-bound. Defaults to ‘upper’.
bound (float) – The lower/upper bound for this constraint. Defaults to 0.
absolute (bool) – True to bound absolute sum, False to bound raw sum. Defaults to False.
- __call__(x, sx)
Define gradient evaluation.
- Parameters:
x (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of a design point to evaluate.
sx (dict or structured array) – A Python dictionary or numpy structured array containing the names (as keys) and corresponding values of the simulation output(s) at x.
- Returns:
The gradient of the sum of (absolute) simulation outputs given the input (x, sx) wrt x and sx, respectively.
- Return type:
dict, dict