iwopy.core.problem.Problem

class iwopy.core.problem.Problem[source]

Bases: Base

Abstract base class for optimization problems.

Parameters:
  • name (str) – The problem’s name

  • mem_size (int, optional) – The memory size, default no memory

  • mem_keyf (Function, optional) – The memory key function. Parameters: (vars_int, vars_float), returns key Object

objs

The objective functions

Type:

iwopy.core.OptFunctionList

cons

The constraints

Type:

iwopy.core.OptFunctionList

memory

The memory, or None

Type:

iwopy.core.Memory

__init__(name, mem_size=None, mem_keyf=None)[source]

Methods

__init__(name[, mem_size, mem_keyf])

add_constraint(constraint[, varmap_int, ...])

Add a constraint to the problem.

add_objective(objective[, varmap_int, ...])

Add an objective to the problem.

apply_individual(vars_int, vars_float)

Apply new variables to the problem.

apply_population(vars_int, vars_float)

Apply new variables to the problem, for a whole population.

calc_gradients(vars_int, vars_float, func, ...)

The actual gradient calculation, not to be called directly (call get_gradients instead).

check_constraints_individual(constraint_values)

Check if the constraints are fullfilled for the given individual.

check_constraints_population(constraint_values)

Check if the constraints are fullfilled for the given population.

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

Evaluate a single individual of the problem.

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

Evaluate all individuals of a population.

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.

get_gradients(vars_int, vars_float[, func, ...])

Obtain gradients of a function that is linked to the problem.

initial_values_float()

The initial values of the float variables.

initial_values_int()

The initial values of the integer variables.

initialize([verbosity])

Initialize the problem.

max_values_float()

The maximal values of the float variables.

max_values_int()

The maximal values of the integer variables.

min_values_float()

The minimal values of the float variables.

min_values_int()

The minimal values of the integer variables.

prob_res_einsum_individual(prob_res_list, coeffs)

Calculate the einsum of problem results

prob_res_einsum_population(prob_res_list, coeffs)

Calculate the einsum of problem results

var_names_float()

The names of float variables.

var_names_int()

The names of integer variables.

Attributes

INT_INF

constraints_tol

Gets the tolerance values of constraints

initialized

Flag for finished initialization

max_values_constraints

Gets the maximal values of constraints

maximize_objs

Flags for objective maximization

min_values_constraints

Gets the minimal values of constraints

n_constraints

The total number of constraints, i.e., the sum of all components

n_objectives

The total number of objectives, i.e., the sum of all components

n_vars_float

The number of float variables

n_vars_int

The number of int variables

__init__(name, mem_size=None, mem_keyf=None)[source]
add_constraint(constraint, varmap_int=None, varmap_float=None, verbosity=0)[source]

Add a constraint to the problem.

Parameters:
  • constraint (iwopy.Constraint) – The constraint

  • varmap_int (dict, optional) – Mapping from objective variables to problem variables. Key: str or int, value: str or int

  • varmap_float (dict, optional) – Mapping from objective variables to problem variables. Key: str or int, value: str or int

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

add_objective(objective, varmap_int=None, varmap_float=None, verbosity=0)[source]

Add an objective to the problem.

Parameters:
  • objective (iwopy.Objective) – The objective

  • varmap_int (dict, optional) – Mapping from objective variables to problem variables. Key: str or int, value: str or int

  • varmap_float (dict, optional) – Mapping from objective variables to problem variables. Key: str or int, value: str or int

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

apply_individual(vars_int, vars_float)[source]

Apply new variables to the 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,)

Returns:

problem_results – The results of the variable application to the problem

Return type:

Any

apply_population(vars_int, vars_float)[source]

Apply new variables to the problem, for a whole 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)

Returns:

problem_results – The results of the variable application to the problem

Return type:

Any

calc_gradients(vars_int, vars_float, func, components, ivars, fvars, vrs, pop=False, verbosity=0)[source]

The actual gradient calculation, not to be called directly (call get_gradients instead).

Can be overloaded in derived classes, the base class only considers analytic derivatives.

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,)

  • func (iwopy.core.OptFunctionList, optional) – The functions to be differentiated, or None for a list of all objectives and all constraints (in that order)

  • components (list of int, optional) – The function’s component selection, or None for all

  • ivars (list of int) – The indices of the function int variables in the problem

  • fvars (list of int) – The indices of the function float variables in the problem

  • vrs (list of int) – The function float variable indices wrt which the derivatives are to be calculated

  • pop (bool) – Flag for vectorizing calculations via population

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

Returns:

gradients – The gradients of the functions, shape: (n_components, n_vrs)

Return type:

numpy.ndarray

check_constraints_individual(constraint_values, verbosity=0)[source]

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 – The boolean result, shape: (n_components,)

Return type:

np.array

check_constraints_population(constraint_values, verbosity=0)[source]

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 constraints_tol

Gets the tolerance values of constraints

Returns:

ctol – The constraint tolerance values, shape: (n_constraints,)

Return type:

numpy.ndarray

evaluate_individual(vars_int, vars_float, ret_prob_res=False)[source]

Evaluate a single individual of the 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,)

  • ret_prob_res (bool) – Flag for additionally returning of problem results

Returns:

  • objs (np.array) – The objective function values, shape: (n_objectives,)

  • con (np.array) – The constraints values, shape: (n_constraints,)

  • prob_res (object, optional) – The problem results

evaluate_population(vars_int, vars_float, ret_prob_res=False)[source]

Evaluate 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)

  • ret_prob_res (bool) – Flag for additionally returning of problem results

Returns:

  • objs (np.array) – The objective function values, shape: (n_pop, n_objectives)

  • cons (np.array) – The constraints values, shape: (n_pop, n_constraints)

  • prob_res (object, optional) – The problem results

finalize(verbosity=0)

Finalize the object.

Parameters:

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

finalize_individual(vars_int, vars_float, verbosity=1)[source]

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,)

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

Returns:

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

  • objs (np.array) – The objective function values, shape: (n_objectives,)

  • cons (np.array) – The constraints values, shape: (n_constraints,)

finalize_population(vars_int, vars_float, verbosity=0)[source]

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)

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

Returns:

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

  • objs (np.array) – The final objective function values, shape: (n_pop, n_components)

  • cons (np.array) – The final constraint values, shape: (n_pop, n_constraints)

get_gradients(vars_int, vars_float, func=None, components=None, vars=None, pop=False, verbosity=0)[source]

Obtain gradients of a function that is linked to the problem.

The func object typically is a iwopy.core.OptFunctionList object that contains a selection of objectives and/or constraints that were previously added to this problem. By default all objectives and constraints (and all their components) are being considered, cf. class ProblemDefaultFunc.

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,)

  • func (iwopy.core.OptFunctionList, optional) – The functions to be differentiated, or None for a list of all objectives and all constraints (in that order)

  • components (list of int, optional) – The function’s component selection, or None for all

  • vars (list of int or str, optional) – The float variables wrt which the derivatives are to be calculated, or None for all

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

  • pop (bool) – Flag for vectorizing calculations via population

Returns:

gradients – The gradients of the functions, shape: (n_components, n_vars)

Return type:

numpy.ndarray

initial_values_float()[source]

The initial values of the float variables.

Returns:

values – Initial float values, shape: (n_vars_float,)

Return type:

numpy.ndarray

initial_values_int()[source]

The initial values of the integer variables.

Returns:

values – Initial int values, shape: (n_vars_int,)

Return type:

numpy.ndarray

initialize(verbosity=1)[source]

Initialize the problem.

Parameters:

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

property initialized

Flag for finished initialization

Returns:

True if initialization has been done

Return type:

bool

property max_values_constraints

Gets the maximal values of constraints

Returns:

cma – The maximal constraint values, shape: (n_constraints,)

Return type:

numpy.ndarray

max_values_float()[source]

The maximal values of the float variables.

Use numpy.inf for unbounded.

Returns:

values – Maximal float values, shape: (n_vars_float,)

Return type:

numpy.ndarray

max_values_int()[source]

The maximal values of the integer variables.

Use self.INT_INF for unbounded.

Returns:

values – Maximal int values, shape: (n_vars_int,)

Return type:

numpy.ndarray

property maximize_objs

Flags for objective maximization

Returns:

maximize – Boolean flag for maximization of objective, shape: (n_objectives,)

Return type:

numpy.ndarray

property min_values_constraints

Gets the minimal values of constraints

Returns:

cmi – The minimal constraint values, shape: (n_constraints,)

Return type:

numpy.ndarray

min_values_float()[source]

The minimal values of the float variables.

Use -numpy.inf for unbounded.

Returns:

values – Minimal float values, shape: (n_vars_float,)

Return type:

numpy.ndarray

min_values_int()[source]

The minimal values of the integer variables.

Use -self.INT_INF for unbounded.

Returns:

values – Minimal int values, shape: (n_vars_int,)

Return type:

numpy.ndarray

property n_constraints

The total number of constraints, i.e., the sum of all components

Returns:

n_con – The total number of constraint functions

Return type:

int

property n_objectives

The total number of objectives, i.e., the sum of all components

Returns:

n_obj – The total number of objective functions

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

prob_res_einsum_individual(prob_res_list, coeffs)[source]

Calculate the einsum of problem results

Parameters:
  • prob_res_list (list) – The problem results

  • coeffs (numpy.ndarray) – The coefficients

Returns:

prob_res – The weighted sum of problem results

Return type:

object

prob_res_einsum_population(prob_res_list, coeffs)[source]

Calculate the einsum of problem results

Parameters:
  • prob_res_list (list) – The problem results

  • coeffs (numpy.ndarray) – The coefficients

Returns:

prob_res – The weighted sum of problem results

Return type:

object

var_names_float()[source]

The names of float variables.

Returns:

names – The names of the float variables

Return type:

list of str

var_names_int()[source]

The names of integer variables.

Returns:

names – The names of the integer variables

Return type:

list of str