foxes_opt.core.farm_vars_problem

Classes

FarmVarsProblem

Abstract base class for models that optimize

Module Contents

class foxes_opt.core.farm_vars_problem.FarmVarsProblem(name: str, algo: foxes.core.Algorithm, sel_turbines: list[int] | None = None, calc_farm_args: dict[str, Any] | None = None, points: numpy.ndarray | None = None, **kwargs: Any)[source]

Bases: foxes_opt.core.farm_opt_problem.FarmOptProblem

Abstract base class for models that optimize farm variables.

Parameters:
name

The problem’s name

algo

The algorithm

sel_turbines

The turbines selected for optimization, or None for all

calc_farm_args

Additional parameters for algo.calc_farm()

points

The probe points, shape: (n_states, n_points, 3)

kwargs

Additional parameters for iwopy.Problem

add_constraint(constraint: iwopy.core.constraint.Constraint, varmap_int: collections.abc.Mapping[str | int, str | int | numpy.integer] | None = None, varmap_float: collections.abc.Mapping[str | int, str | int | numpy.integer] | None = None, verbosity: int = 0) → None

Add a constraint to the problem.

Parameters:
constraint

The constraint

varmap_int

Mapping from objective variables to problem variables. Key: str or int, value: str or int

varmap_float

Mapping from objective variables to problem variables. Key: str or int, value: str or int

verbosity

The verbosity level, 0 = silent

add_objective(objective: iwopy.core.objective.Objective, varmap_int: collections.abc.Mapping[str | int, str | int | numpy.integer] | None = None, varmap_float: collections.abc.Mapping[str | int, str | int | numpy.integer] | None = None, verbosity: int = 0) → None

Add an objective to the problem.

Parameters:
objective

The objective

varmap_int

Mapping from objective variables to problem variables. Key: str or int, value: str or int

varmap_float

Mapping from objective variables to problem variables. Key: str or int, value: str or int

verbosity

The verbosity level, 0 = silent

add_to_layout_figure(ax: matplotlib.axes.Axes, **kwargs: Any) → matplotlib.axes.Axes

Add to a layout figure

Parameters:
ax

The figure axis

apply_individual(vars_int: numpy.ndarray, vars_float: numpy.ndarray) → Any

Apply new variables to the problem.

Parameters:
vars_int

The integer variable values, shape: (n_vars_int,)

vars_float

The float variable values, shape: (n_vars_float,)

Returns:
problem_results

The results of the variable application to the problem

apply_population(vars_int: numpy.ndarray, vars_float: numpy.ndarray) → Any

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

Parameters:
vars_int

The integer variable values, shape: (n_pop, n_vars_int)

vars_float

The float variable values, shape: (n_pop, n_vars_float)

Returns:
problem_results

The results of the variable application to the problem

calc_jacobian(vars_int: numpy.ndarray, vars_float: numpy.ndarray, func: iwopy.core.function.OptFunction, components: collections.abc.Sequence[int] | numpy.ndarray | None, ivars: list[int], fvars: list[int], vrs: list[int], pop: bool = False, verbosity: int = 0, func_values: numpy.ndarray | None = None, max_deltas: numpy.ndarray | None = None) → scipy.sparse.csr_matrix

Assemble a CSR Jacobian from analytical partial derivatives.

Call get_jacobian instead of calling this method directly. Derived classes can supply numerical derivatives for unresolved entries.

Parameters:
vars_int

The integer variable values, shape: (n_vars_int,)

vars_float

The float variable values, shape: (n_vars_float,)

func

The initialized function to differentiate.

components

Component indices in the requested row order, or None for all.

ivars

The indices of the function int variables in the problem

fvars

The indices of the function float variables in the problem

vrs

The function float variable indices wrt which the derivatives are to be calculated

func_values

Previously calculated values for the selected components, with shape (n_components,). The analytical implementation ignores them.

max_deltas

Positive finite call-local displacement caps in complete problem float-variable order, shape (n_vars_float,). Ignored analytically.

pop

Flag for vectorizing calculations via population

verbosity

The verbosity level, 0 = silent

Returns:
jacobian

CSR Jacobian with shape (n_components, n_vrs). Independent entries and exact zeros are not stored. Unavailable analytical derivatives remain explicitly stored as NaN.

check_constraints_individual(constraint_values: numpy.ndarray, verbosity: int = 0) → numpy.ndarray

Check if the constraints are fullfilled for the given individual.

Parameters:
constraint_values

The constraint values, shape: (n_components,)

verbosity

The verbosity level, 0 = silent

Returns:
values

The boolean result, shape: (n_components,)

check_constraints_population(constraint_values: numpy.ndarray, verbosity: int = 0) → numpy.ndarray

Check if the constraints are fullfilled for the given population.

Parameters:
constraint_values

The constraint values, shape: (n_pop, n_components)

verbosity

The verbosity level, 0 = silent

Returns:
values

The boolean result, shape: (n_pop, n_components)

evaluate_individual(vars_int: numpy.ndarray, vars_float: numpy.ndarray, ret_prob_res: False = False) → tuple[numpy.ndarray, numpy.ndarray]
evaluate_individual(vars_int: numpy.ndarray, vars_float: numpy.ndarray, ret_prob_res: True) → tuple[numpy.ndarray, numpy.ndarray, object | None]

Evaluate a single individual of the problem.

Parameters:
vars_int

The integer variable values, shape: (n_vars_int,)

vars_float

The float variable values, shape: (n_vars_float,)

ret_prob_res

Flag for additionally returning of problem results

Returns:
objs

The objective function values, shape: (n_objectives,)

con

The constraints values, shape: (n_constraints,)

prob_res

The problem results

evaluate_population(vars_int: numpy.ndarray, vars_float: numpy.ndarray, ret_prob_res: False = False) → tuple[numpy.ndarray, numpy.ndarray]
evaluate_population(vars_int: numpy.ndarray, vars_float: numpy.ndarray, ret_prob_res: True) → tuple[numpy.ndarray, numpy.ndarray, object | None]

Evaluate all individuals of a population.

Parameters:
vars_int

The integer variable values, shape: (n_pop, n_vars_int)

vars_float

The float variable values, shape: (n_pop, n_vars_float)

ret_prob_res

Flag for additionally returning of problem results

Returns:
objs

The objective function values, shape: (n_pop, n_objectives)

cons

The constraints values, shape: (n_pop, n_constraints)

prob_res

The problem results

finalize(verbosity: int = 0, /) → None
finalize(*, verbosity: int = 0) → None

Finalize the object.

Parameters:
verbosity

The verbosity level, 0 = silent

finalize_individual(vars_int: numpy.ndarray, vars_float: numpy.ndarray, verbosity: int = 1) → tuple[object | None, numpy.ndarray, numpy.ndarray]

Finalization, given the champion data.

Parameters:
vars_int

The optimal integer variable values, shape: (n_vars_int,)

vars_float

The optimal float variable values, shape: (n_vars_float,)

verbosity

The verbosity level, 0 = silent

Returns:
problem_results

The results of the variable application to the problem

objs

The objective function values, shape: (n_objectives,)

cons

The constraints values, shape: (n_constraints,)

finalize_population(vars_int: numpy.ndarray, vars_float: numpy.ndarray, verbosity: int = 0) → tuple[object | None, numpy.ndarray, numpy.ndarray]

Finalization, given the final population data.

Parameters:
vars_int

The integer variable values of the final generation, shape: (n_pop, n_vars_int)

vars_float

The float variable values of the final generation, shape: (n_pop, n_vars_float)

verbosity

The verbosity level, 0 = silent

Returns:
problem_results

The results of the variable application to the problem

objs

The final objective function values, shape: (n_pop, n_components)

cons

The final constraint values, shape: (n_pop, n_constraints)

get_active_constraint_components(vars_float: numpy.ndarray, lower: numpy.ndarray, upper: numpy.ndarray) → numpy.ndarray

Obtain bound-certified constraint components in problem order.

Parameters:
vars_float

Initial float variables, shape: (n_vars_float,).

lower, upper

Enforced bounds in problem float-variable order, with the same shape. Lower bounds must not exceed upper bounds.

Returns:
components

Sorted global constraint indices. Every omitted component must be certified feasible throughout the box by its owner.

Raises:
ValueError

If bounds have invalid shapes or a selector returns invalid or duplicate component indices.

get_jacobian(vars_int: numpy.ndarray, vars_float: numpy.ndarray, func: iwopy.core.function.OptFunction | None = None, components: collections.abc.Sequence[int] | numpy.ndarray | None = None, vars: collections.abc.Sequence[str | int] | None = None, pop: bool = False, verbosity: int = 0, func_values: numpy.ndarray | None = None, max_deltas: numpy.ndarray | None = None) → scipy.sparse.csr_matrix

Obtain a CSR Jacobian for a function linked to this 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

The integer variable values, shape: (n_vars_int,)

vars_float

The float variable values, shape: (n_vars_float,)

func

The functions to be differentiated, or None for a list of all objectives and all constraints (in that order)

components

Component indices in the requested row order, or None for all.

vars

Floating variable names, integer problem-variable indices, or name patterns, or None for all problem floating variables. Selection order and repeated matches are preserved. Every selected variable must belong to the function.

func_values

Previously calculated values in the selected component order, with shape (n_components,). Numerical wrappers can reuse them.

max_deltas

Positive finite call-local displacement caps in complete problem float-variable order, shape (n_vars_float,). LocalFD uses the smaller of its configured delta and this cap. Analytical and fixed-grid derivatives are unchanged.

verbosity

The verbosity level, 0 = silent

pop

Flag for vectorizing calculations via population

Returns:
jacobian

CSR Jacobian with shape (n_components, n_vars). Rows follow the selected component order and columns the selected floating variable order. Independent entries and exact zeros are implicit. Use jacobian.toarray() when a dense array is required.

Raises:
ValueError

If the function belongs to another problem, a selected variable is unavailable, an analytical derivative remains unresolved, or displacement caps have invalid shapes or non-positive/non-finite values.

TypeError

If a variable selector is neither a name nor an integer index.

initial_values_float() → numpy.typing.ArrayLike | None

The initial values of the float variables.

Returns:
values

Initial float values, shape: (n_vars_float,)

initial_values_int() → numpy.typing.ArrayLike

The initial values of the integer variables.

Returns:
values

Initial int values, shape: (n_vars_int,)

initialize(verbosity: int = 1, model_vars: dict[str, list[str]] | list[str] | None = None, **kwargs: Any) → None[source]

Initialize the object.

Parameters:
model_vars

The variables to optimize. For mappings, each model name maps to the configured variable names.

verbosity

The verbosity level, 0 = silent

kwargs

Additional parameters for super class init

max_values_float() → numpy.typing.ArrayLike

The maximal values of the float variables.

Use numpy.inf for unbounded.

Returns:
values

Maximal float values, shape: (n_vars_float,)

max_values_int() → numpy.typing.ArrayLike

The maximal values of the integer variables.

Use self.INT_INF for unbounded.

Returns:
values

Maximal int values, shape: (n_vars_int,)

min_values_float() → numpy.typing.ArrayLike

The minimal values of the float variables.

Use -numpy.inf for unbounded.

Returns:
values

Minimal float values, shape: (n_vars_float,)

min_values_int() → numpy.typing.ArrayLike

The minimal values of the integer variables.

Use -self.INT_INF for unbounded.

Returns:
values

Minimal int values, shape: (n_vars_int,)

classmethod new(problem_type: str, *args: Any, **kwargs: Any) → Any[source]

Run-time farm vars opt problem factory.

Parameters:
problem_type

The selected derived class name

args

Additional parameters for the constructor

kwargs

Additional parameters for the constructor

abstract opt2farm_vars_individual(vars_int: numpy.ndarray, vars_float: numpy.ndarray) → dict[str, numpy.ndarray][source]

Translates optimization variables to farm variables

Parameters:
vars_int

The integer optimization variable values, shape

vars_float

The float optimization variable values, shape

Returns:
farm_vars

The foxes farm variables. Key: var name, value (n_states, n_sel_turbines)

abstract opt2farm_vars_population(vars_int: numpy.ndarray, vars_float: numpy.ndarray, n_states: int) → dict[str, numpy.ndarray][source]

Translates optimization variables to farm variables

Parameters:
vars_int

The integer optimization variable values, shape

vars_float

The float optimization variable values, shape

n_states

The number of original (non-pop) states

Returns:
farm_vars

The foxes farm variables. Key: var name, value (n_pop, n_states, n_sel_turbines)

classmethod parse_tvar(tvr: str) → tuple[str, int]

Parse foxes variable name and turbine index from turbine variable

Parameters:
tvr

The turbine variable name

Returns:
var

The foxes variable name

turbine_i

The turbine index

prob_res_einsum_individual(prob_res_list: collections.abc.Sequence[object | None], coeffs: numpy.ndarray) → object | None

Calculate the einsum of problem results

Parameters:
prob_res_list

The problem results

coeffs

The coefficients

Returns:
prob_res

The weighted sum of problem results

prob_res_einsum_population(prob_res_list: collections.abc.Sequence[object | None], coeffs: numpy.ndarray) → object | None

Calculate the einsum of problem results

Parameters:
prob_res_list

The problem results

coeffs

The coefficients

Returns:
prob_res

The weighted sum of problem results

classmethod tvar(var: str, turbine_i: int) → str

Gets turbine variable name

Parameters:
var

The variable name

turbine_i

The turbine index

Returns:
value

The turbine variable name

update_problem_individual(vars_int: numpy.ndarray, vars_float: numpy.ndarray) → None[source]

Update the algo and other data using the latest optimization variables.

This function is called before running the farm calculation.

Parameters:
vars_int

The integer variable values, shape: (n_vars_int,)

vars_float

The float variable values, shape: (n_vars_float,)

update_problem_population(vars_int: numpy.ndarray, vars_float: numpy.ndarray) → None[source]

Update the algo and other data using the latest optimization variables.

This function is called before running the farm calculation.

Parameters:
vars_int

The integer variable values, shape: (n_pop, n_vars_int,)

vars_float

The float variable values, shape: (n_pop, n_vars_float,)

var_names_float() → list[str]

The names of float variables.

Returns:
names

The names of the float variables

var_names_int() → list[str]

The names of integer variables.

Returns:
names

The names of the integer variables

INT_INF = -999999
algo
property all_turbines : bool

Flag for all turbines optimization

Returns:
value

True if all turbines are subject to optimization

calc_farm_args
cons : iwopy.core.function_list.OptFunctionList[iwopy.core.constraint.Constraint]
property counter : int | None

The current value of the application counter

Returns:
value

The current value of the application counter

property farm : foxes.core.WindFarm

The wind farm

Returns:
value

The wind farm

property initialized : bool

Flag for finished initialization

Returns:
initialized

True if initialization has been done

property max_values_constraints : numpy.ndarray | None

Gets the maximal values of constraints

Returns:
cma

The maximal constraint values, shape: (n_constraints,)

property maximize_objs : numpy.ndarray

Flags for objective maximization

Returns:
maximize

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

memory : iwopy.core.memory.Memory | None = None
property min_values_constraints : numpy.ndarray | None

Gets the minimal values of constraints

Returns:
cmi

The minimal constraint values, shape: (n_constraints,)

property n_constraints : int

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

Returns:
n_con

The total number of constraint functions

property n_objectives : int

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

Returns:
n_obj

The total number of objective functions

property n_sel_turbines : int

The numer of selected turbines

Returns:
value

The numer of selected turbines

property n_vars_float : int

The number of float variables

Returns:
n

The number of float variables

property n_vars_int : int

The number of int variables

Returns:
n

The number of int variables

name = 'Base'
objs : iwopy.core.function_list.OptFunctionList[iwopy.core.objective.Objective]
points = None
property sel_turbines : list[int]

The selected turbines

Returns:
value

Indices of the selected turbines