bag3_testbenches.design.optimize.fun

This module defines special interpolation function classes that are used for the optimization based design flow.

Module Contents

Classes

ConstraintMixin

A mixin class that creates constraints in a dictionary format compatible with

ArgMapFunction

A DiffFunction that supports optimization constraints and argument mapping.

ArgMapNormFunction

An ArgMapFunction that supports input (de-)normalization.

CustomVectorArgMapFunction

An interpolating function that takes in multiple sub-functions and computes a vector.

CustomVectorReduceArgMapFunction

A CustomVectorArgMapFunction that performs vector reduction.

Functions

_denorm_input_elem_helper(→ Union[float, numpy.ndarray])

A helper function for de-normalizing an input element.

_norm_input_elem_helper(→ Union[float, numpy.ndarray])

A helper function for normalizing an input element.

_denorm_input_helper(→ numpy.ndarray)

A helper function for de-normalizing an input vector.

_norm_input_helper(→ numpy.ndarray)

A helper function for normalizing an input vector.

class bag3_testbenches.design.optimize.fun.ConstraintMixin[source]

A mixin class that creates constraints in a dictionary format compatible with scipy.optimize.minimize’s constraints parameter.

It is meant to be used in multiple inheritance with DiffFunction.

__eq__(other: Union[float, int, bag.math.dfun.DiffFunction]) Dict[str, Any][source]

Return self==value.

__ge__(other: Union[float, int, bag.math.dfun.DiffFunction]) Dict[str, Any][source]

Return self>=value.

__le__(other: Union[float, int, bag.math.dfun.DiffFunction]) Dict[str, Any][source]

Return self<=value.

__gt__(other: Union[float, int, bag.math.dfun.DiffFunction]) Dict[str, Any][source]

Return self>value.

__lt__(other: Union[float, int, bag.math.dfun.DiffFunction]) Dict[str, Any][source]

Return self<value.

class bag3_testbenches.design.optimize.fun.ArgMapFunction(fn: bag.math.dfun.DiffFunction, idx_map: Optional[Dict[int, int]] = None, fixed_vals: Optional[Dict[int, Union[int, float]]] = None, input_bounds: Optional[Dict[int, Tuple[Optional[Union[int, float]], Optional[Union[int, float]]]]] = None)[source]

Bases: ConstraintMixin, bag.math.dfun.DiffFunction

A DiffFunction that supports optimization constraints and argument mapping.

Parameters:
  • fn (DiffFunction) – the inner/parent function.

  • idx_map (Optional[Dict[int, int]]) – the dictionary mapping the inner function’s argument indices to outer argument indices. Defaults to None.

  • fixed_vals (Optional[Dict[int, Union[int, float]]]) – the dictionary mapping the inner function’s argument indices to their fixed values. Defaults to None.

  • input_bounds (Optional[Dict[int, Tuple[Optional[Union[int, float]], Optional[Union[int, float]]]]]) – the dictionary mapping the inner function’s argument indices to their new bounds/ranges. These bounds are intersected with the inner function’s input ranges to get the new input ranges. Defaults to None.

property inner_ndim: int[source]

Return the number of dimensions for the inner function.

_get_args(xi: numpy.ndarray) numpy.ndarray[source]

Applies argument mapping.

Parameters:

xi (np.ndarray) – The input coordinates, with shape (…, ndim)

Returns:

new_xi – The output coordinates, with shape (…, inner_ndim).

Return type:

np.ndarray

__call__(xi: numpy.ndarray) numpy.ndarray[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi: numpy.ndarray, j: int) numpy.ndarray[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi: numpy.ndarray) numpy.ndarray[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag3_testbenches.design.optimize.fun.ArgMapNormFunction(fn: bag.math.dfun.DiffFunction, idx_map: Optional[Dict[int, int]] = None, fixed_vals: Optional[Dict[int, Union[int, float]]] = None, input_bounds: Optional[Dict[int, Tuple[Optional[Union[int, float]], Optional[Union[int, float]]]]] = None)[source]

Bases: ArgMapFunction

An ArgMapFunction that supports input (de-)normalization. Optimizers are sensitive to the input range.

Parameters:
  • fn (DiffFunction) – the inner function.

  • idx_map (Optional[Dict[int, int]]) – the dictionary mapping the inner function’s argument indices to outer argument indices. Defaults to None.

  • fixed_vals (Optional[Dict[int, Union[int, float]]]) – the dictionary mapping the inner function’s argument indices to their fixed values. Defaults to None.

  • input_bounds (Optional[Dict[int, Tuple[Optional[Union[int, float]], Optional[Union[int, float]]]]]) – the dictionary mapping the inner function’s argument indices to their new bounds/ranges. These bounds are intersected with the inner function’s input ranges to get the new input ranges. Defaults to None.

property input_ranges_norm: List[Tuple[Optional[float], Optional[float]]][source]

Return the normalized input ranges.

denorm_inner_input_elem(val: Union[float, numpy.ndarray], idx: int) numpy.ndarray[source]

De-normalizes the inner function’s input element.

Parameters:
  • val (Union[float, np.ndarray]) – the inner function’s input element to de-normalize

  • idx (int) – the index to de-normalize on

Returns:

ans – The de-normalized inner function’s input element.

Return type:

np.ndarray

norm_inner_input_elem(val: Union[float, numpy.ndarray], idx: int) numpy.ndarray[source]

Normalizes the inner function’s input element.

Parameters:
  • val (Union[float, np.ndarray]) – the inner function’s input element to normalize

  • idx (int) – the index to normalize on

Returns:

ans – The normalized inner function’s input element.

Return type:

np.ndarray

denorm_inner_input(val: numpy.ndarray) numpy.ndarray[source]

De-normalizes the inner function’s input vector.

Parameters:

val (np.ndarray) – the inner function’s input vector to de-normalize

Returns:

ans – The de-normalized inner function’s input vector.

Return type:

np.ndarray

norm_inner_input(val: numpy.ndarray) numpy.ndarray[source]

Normalizes the inner function’s input vector.

Parameters:

val (np.ndarray) – the inner function’s input vector to normalize

Returns:

ans – The normalized inner function’s input vector.

Return type:

np.ndarray

denorm_input_elem(val: Union[float, numpy.ndarray], idx: int) numpy.ndarray[source]

De-normalizes the input element.

Parameters:
  • val (Union[float, np.ndarray]) – the input element to de-normalize

  • idx (int) – the index to de-normalize on

Returns:

ans – The de-normalized input element.

Return type:

np.ndarray

norm_input_elem(val: Union[float, numpy.ndarray], idx: int) numpy.ndarray[source]

Normalizes the input element.

Parameters:
  • val (Union[float, np.ndarray]) – the input element to normalize

  • idx (int) – the index to normalize on

Returns:

ans – The normalized input element.

Return type:

np.ndarray

denorm_input(val: numpy.ndarray) numpy.ndarray[source]

De-normalizes the input vector.

Parameters:

val (np.ndarray) – the input vector to de-normalize

Returns:

ans – The de-normalized input vector.

Return type:

np.ndarray

norm_input(val: numpy.ndarray) numpy.ndarray[source]

Normalizes the input vector.

Parameters:

val (np.ndarray) – the input vector to normalize

Returns:

ans – The normalized input vector.

Return type:

np.ndarray

_get_args(xi: numpy.ndarray, is_norm_input: bool = True)[source]

Applies argument mapping and applies de-normalization (if applicable).

Parameters:
  • xi (np.ndarray) – The input coordinates, with shape (…, ndim)

  • is_norm_input (bool) – True if the input is normalized. Default is True.

Returns:

new_xi – The output coordinates, with shape (…, inner_ndim).

Return type:

np.ndarray

__call__(xi: numpy.ndarray, is_norm_input: bool = True) numpy.ndarray[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

abstract deriv(xi: numpy.ndarray, j: int) numpy.ndarray[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

abstract jacobian(xi: numpy.ndarray) numpy.ndarray[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag3_testbenches.design.optimize.fun.CustomVectorArgMapFunction(fn_list: List[bag.math.dfun.DiffFunction], indep_idx_list: Optional[List[int]] = None, fixed_vals: Optional[Dict[int, Union[int, float, List[Union[int, float]], numpy.ndarray]]] = None, input_bounds: Optional[Dict[int, Tuple[Optional[Union[int, float]], Optional[Union[int, float]]]]] = None)[source]

Bases: object

An interpolating function that takes in multiple sub-functions and computes a vector.

Parameters:
  • fn_list (List[DiffFunction]) – the list of sub-functions. All sub-functions must have the same dimensions and input ranges.

  • indep_idx_list (Optional[List[int]]) – the list of sub-function’s argument indices for which the input value can be independently set per sub-function. Defaults to None.

  • fixed_vals (Optional[Dict[int, Union[int, float, List[Union[int, float]], np.ndarray]]]) – the dictionary mapping the sub-function’s argument indices to their fixed values. Defaults to None.

  • input_bounds (Optional[Dict[int, Tuple[Optional[Union[int, float]], Optional[Union[int, float]]]]]) – the dictionary mapping the sub-function’s argument indices to their new bounds/ranges. These bounds are intersected with the sub-function’s input ranges to get the new input ranges. Defaults to None.

property ndim: int[source]

Returns the number of input dimensions.

property num_fn: int[source]

Returns the number of sub-functions.

property input_ranges: List[Tuple[Optional[float], Optional[float]]][source]

Returns the list of input ranges.

property input_ranges_norm: List[Tuple[Optional[float], Optional[float]]][source]

Returns the list of normalized input ranges.

property scale_list: List[Tuple[float, float]][source]

Returns a list of (offset, spacing).

__call__(xi: numpy.ndarray, **kwargs) numpy.ndarray[source]
abstract deriv(xi: numpy.ndarray, j: int) numpy.ndarray[source]
abstract jacobian(xi: numpy.ndarray) numpy.ndarray[source]
unmap_input(xi: numpy.ndarray) List[Union[float, numpy.ndarray]][source]

Unmaps the input.

Parameters:

xi (np.ndarray) – The input coordinates.

Returns:

rv – The unmapped coordinates.

Return type:

List[Union[float, np.ndarray]]

map_input(xi: List[Union[float, numpy.ndarray]]) numpy.ndarray[source]

Maps the input.

Parameters:

xi (List[Union[float, np.ndarray]]) – The input coordinates.

Returns:

rv – The mapped coordinates.

Return type:

np.ndarray

denorm_input_elem(val: Union[float, numpy.ndarray], idx: int) numpy.ndarray[source]

De-normalizes the input element.

Parameters:
  • val (Union[float, np.ndarray]) – the input element to de-normalize

  • idx (int) – the index to de-normalize on

Returns:

ans – The de-normalized input element.

Return type:

np.ndarray

norm_input_elem(val: Union[float, numpy.ndarray], idx: int) numpy.ndarray[source]

Normalizes the input element.

Parameters:
  • val (Union[float, np.ndarray]) – the input element to normalize

  • idx (int) – the index to normalize on

Returns:

ans – The normalized input element.

Return type:

np.ndarray

denorm_input(val: numpy.ndarray) numpy.ndarray[source]

De-normalizes the input vector.

Parameters:

val (np.ndarray) – the input vector to de-normalize

Returns:

ans – The de-normalized input vector.

Return type:

np.ndarray

norm_input(val: numpy.ndarray) numpy.ndarray[source]

Normalizes the input vector.

Parameters:

val (np.ndarray) – the input vector to normalize

Returns:

ans – The normalized input vector.

Return type:

np.ndarray

__eq__(other)[source]

Return self==value.

__ge__(other)[source]

Return self>=value.

__le__(other)[source]

Return self<=value.

__gt__(other)[source]

Return self>value.

__lt__(other)[source]

Return self<value.

random_input(rng: Optional[numpy.random.Generator] = None, norm: bool = True) List[float][source]

Generates a random input.

Parameters:
  • rng (Optional[np.random.Generator]) – the random number generator to use.

  • norm (bool) – True to return normalized random inputs. Default is True.

Returns:

ans – The randomly generated input vector.

Return type:

List[float]

class bag3_testbenches.design.optimize.fun.CustomVectorReduceArgMapFunction(*args, reduce_fn: Callable)[source]

Bases: CustomVectorArgMapFunction

A CustomVectorArgMapFunction that performs vector reduction.

Parameters:
  • parameters. (Refer to CustomVectorArgMapFunction for the shared) –

  • reduce_fn (Callable) – the reduction function.

__call__(xi: numpy.ndarray, **kwargs) numpy.ndarray[source]
bag3_testbenches.design.optimize.fun._denorm_input_elem_helper(val: Union[float, numpy.ndarray], idx: int, offset_list: numpy.ndarray, spacing_list: numpy.ndarray) Union[float, numpy.ndarray][source]

A helper function for de-normalizing an input element.

Parameters:
  • val (Union[float, np.ndarray]) – the element to de-normalize

  • idx (int) – the index to de-normalize on

  • offset_list (np.ndarray) – the offset list

  • spacing_list (np.ndarray) – the spacing list

Returns:

ans – The de-normalized input element.

Return type:

np.ndarray

bag3_testbenches.design.optimize.fun._norm_input_elem_helper(val: Union[float, numpy.ndarray], idx: int, offset_list: numpy.ndarray, spacing_list: numpy.ndarray) Union[float, numpy.ndarray][source]

A helper function for normalizing an input element.

Parameters:
  • val (Union[float, np.ndarray]) – the element to normalize

  • idx (int) – the index to normalize on

  • offset_list (np.ndarray) – the offset list

  • spacing_list (np.ndarray) – the spacing list

Returns:

ans – The normalized input element.

Return type:

np.ndarray

bag3_testbenches.design.optimize.fun._denorm_input_helper(val: numpy.ndarray, offset_list: numpy.ndarray, spacing_list: numpy.ndarray) numpy.ndarray[source]

A helper function for de-normalizing an input vector.

Parameters:
  • val (np.ndarray) – the input vector to de-normalize

  • offset_list (np.ndarray) – the offset list

  • spacing_list (np.ndarray) – the spacing list

Returns:

ans – The de-normalized input vector.

Return type:

np.ndarray

bag3_testbenches.design.optimize.fun._norm_input_helper(val: numpy.ndarray, offset_list: numpy.ndarray, spacing_list: numpy.ndarray) numpy.ndarray[source]

A helper function for normalizing an input vector.

Parameters:
  • val (np.ndarray) – the input vector to normalize

  • offset_list (np.ndarray) – the offset list

  • spacing_list (np.ndarray) – the spacing list

Returns:

ans – The normalized input vector.

Return type:

np.ndarray