iwopy.wrappers.simple_constraint.SimpleConstraint

class iwopy.wrappers.simple_constraint.SimpleConstraint[source]

Bases: Constraint

A simple constraint that assumes the same variables as defined by the problem.

Parameters:
  • problem (iwopy.Problem) – The underlying optimization problem

  • name (str) – The function name

  • n_components (int) – The number of components

  • mins (float or array) – The minimal values of components, shape: (n_components,)

  • maxs (float or array) – The maximal values of components, shape: (n_components,)

  • cnames (list of str, optional) – The names of the components

  • bool (has_ana_derivs =) – Flag for analytical derivatives

__init__(problem, name, n_components=1, mins=-inf, maxs=0.0, cnames=None, has_ana_derivs=True)[source]

Methods

__init__(problem, name[, n_components, ...])

ana_deriv(vars_int, vars_float, var[, ...])

Calculates the analytic derivative, if possible.

calc_individual(vars_int, vars_float, ...[, ...])

Calculate values for a single individual of the underlying problem.

calc_population(vars_int, vars_float, ...[, ...])

Calculate values for all individuals of a population.

check_individual(constraint_values[, verbosity])

Check if the constraints are fullfilled for the given individual.

check_population(constraint_values[, verbosity])

Check if the constraints are fullfilled for the given population.

f(*x)

The function.

finalize([verbosity])

Finalize the object.

finalize_individual(vars_int, vars_float, ...)

Finalization, given the champion data.

finalize_population(vars_int, vars_float, ...)

Finalization, given the final population data.

g(var, *x[, components])

The analytical derivative of the function f, df/dvar, if available.

get_bounds()

Returns the bounds for all components.

initialize([verbosity])

Initialize the object.

n_components()

Returns the number of components of the function.

rename_vars_float(varmap)

Rename float variables.

rename_vars_int(varmap)

Rename integer variables.

vardeps_float()

Gets the dependencies of all components on the function float variables

vardeps_int()

Gets the dependencies of all components on the function int variables

Attributes

component_names

The names of the components

initialized

Flag for finished initialization

n_vars_float

The number of float variables

n_vars_int

The number of int variables

var_names_float

The names of the float variables

var_names_int

The names of the integer variables

__init__(problem, name, n_components=1, mins=-inf, maxs=0.0, cnames=None, has_ana_derivs=True)[source]
ana_deriv(vars_int, vars_float, var, components=None)[source]

Calculates the analytic derivative, if possible.

Use numpy.nan if analytic derivatives cannot be calculated.

Parameters:
  • vars_int (np.array) – The integer variable values, shape: (n_vars_int,)

  • vars_float (np.array) – The float variable values, shape: (n_vars_float,)

  • var (int) – The index of the differentiation float variable

  • components (list of int) – The selected components, or None for all

Returns:

deriv – The derivative values, shape: (n_sel_components,)

Return type:

numpy.ndarray

calc_individual(vars_int, vars_float, problem_results, components=None)[source]

Calculate values for a single individual of the underlying problem.

Parameters:
  • vars_int (np.array) – The integer variable values, shape: (n_vars_int,)

  • vars_float (np.array) – The float variable values, shape: (n_vars_float,)

  • problem_results (Any) – The results of the variable application to the problem

  • components (list of int, optional) – The selected components or None for all

Returns:

values – The component values, shape: (n_sel_components,)

Return type:

np.array

calc_population(vars_int, vars_float, problem_results, components=None)[source]

Calculate values for all individuals of a population.

Parameters:
  • vars_int (np.array) – The integer variable values, shape: (n_pop, n_vars_int)

  • vars_float (np.array) – The float variable values, shape: (n_pop, n_vars_float)

  • problem_results (Any) – The results of the variable application to the problem

  • components (list of int, optional) – The selected components or None for all

Returns:

values – The component values, shape: (n_pop, n_sel_components,)

Return type:

np.array

check_individual(constraint_values, verbosity=0)

Check if the constraints are fullfilled for the given individual.

Parameters:
  • constraint_values (np.array) – The constraint values, shape: (n_components,)

  • verbosity (int) – The verbosity level, 0 = silent

  • Returns

  • values (np.array) –

  • ------- – The boolean result, shape: (n_components,)

check_population(constraint_values, verbosity=0)

Check if the constraints are fullfilled for the given population.

Parameters:
  • constraint_values (np.array) – The constraint values, shape: (n_pop, n_components,)

  • verbosity (int) – The verbosity level, 0 = silent

Returns:

values – The boolean result, shape: (n_pop, n_components)

Return type:

np.array

property component_names

The names of the components

Returns:

names – The component names

Return type:

list of str

abstract f(*x)[source]

The function.

Parameters:

x (tuple) – The int and float variables in that order. Variables are either scalars or numpy arrays in case of populations.

Returns:

result – For one component, a float, else a list of floats. For population results, a array with shape (n_pop,) in case of one component or a list of such arrays otherwise.

Return type:

float (or numpy.ndarray) or list of float (or numpy.ndarray)

finalize(verbosity=0)

Finalize the object.

Parameters:

verbosity (int) – The verbosity level, 0 = silent

finalize_individual(vars_int, vars_float, problem_results, verbosity=1)

Finalization, given the champion data.

Parameters:
  • vars_int (np.array) – The optimal integer variable values, shape: (n_vars_int,)

  • vars_float (np.array) – The optimal float variable values, shape: (n_vars_float,)

  • problem_results (Any) – The results of the variable application to the problem

  • verbosity (int) – The verbosity level, 0 = silent

Returns:

values – The component values, shape: (n_components,)

Return type:

np.array

finalize_population(vars_int, vars_float, problem_results, verbosity=1)

Finalization, given the final population data.

Parameters:
  • vars_int (np.array) – The integer variable values of the final generation, shape: (n_pop, n_vars_int)

  • vars_float (np.array) – The float variable values of the final generation, shape: (n_pop, n_vars_float)

  • problem_results (Any) – The results of the variable application to the problem

  • verbosity (int) – The verbosity level, 0 = silent

Returns:

values – The component values, shape: (n_pop, n_components)

Return type:

np.array

g(var, *x, components=None)[source]

The analytical derivative of the function f, df/dvar, if available.

Parameters:
  • var (int) – The index of the derivation varibable within the function float variables

  • x (tuple) – The int and float variables in that order.

  • components (list of int, optional) – The selected components, or None for all

Returns:

result – For one component, a float, else a list of floats. The length of list is 0 or 1 in case of single component, or n_sel_components otherwise.

Return type:

float or list of float

get_bounds()[source]

Returns the bounds for all components.

Non-existing bounds are expressed by np.inf.

Returns:

  • min (np.array) – The lower bounds, shape: (n_components,)

  • max (np.array) – The upper bounds, shape: (n_components,)

initialize(verbosity=0)

Initialize the object.

Parameters:

verbosity (int) – The verbosity level, 0 = silent

property initialized

Flag for finished initialization

Returns:

True if initialization has been done

Return type:

bool

n_components()[source]

Returns the number of components of the function.

Returns:

The number of components.

Return type:

int

property n_vars_float

The number of float variables

Returns:

n – The number of float variables

Return type:

int

property n_vars_int

The number of int variables

Returns:

n – The number of int variables

Return type:

int

rename_vars_float(varmap)

Rename float variables.

Parameters:

varmap (dict) – The name mapping. Key: old name str, Value: new name str

rename_vars_int(varmap)

Rename integer variables.

Parameters:

varmap (dict) – The name mapping. Key: old name str, Value: new name str

property var_names_float

The names of the float variables

Returns:

names – The float variable names

Return type:

list of str

property var_names_int

The names of the integer variables

Returns:

names – The integer variable names

Return type:

list of str

vardeps_float()

Gets the dependencies of all components on the function float variables

Returns:

deps – The dependencies of components on function variables, shape: (n_components, n_vars_float)

Return type:

numpy.ndarray of bool

vardeps_int()

Gets the dependencies of all components on the function int variables

Returns:

deps – The dependencies of components on function variables, shape: (n_components, n_vars_int)

Return type:

numpy.ndarray of bool