foxes_opt.problems.opt_farm_vars

Classes

OptFarmVars

Optimize a selection of farm variables.

Module Contents

class foxes_opt.problems.opt_farm_vars.OptFarmVars(*args: Any, **kwargs: Any)[source]

Bases: foxes_opt.core.FarmVarsProblem

Optimize a selection of farm variables.

Parameters:
args

Arguments for FarmVarsProblem

kwargs

Keyword arguments for FarmVarsProblem

add_constraint(constraint, varmap_int=None, varmap_float=None, verbosity=0)

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)

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

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

Add to a layout figure

Parameters:
ax

The figure axis

add_var(variable: str, typ: type[float] | type[int] | str, init: float | int, min: float | int, max: float | int, level: str = 'uniform', sel: Any = None, model_key: str | None = None) → None[source]

Add a variable.

Parameters:
variable

The foxes farm variable name

typ

The variable type, either float or int

init

The initial value

min

The min value

max

The max value

level

Choices

sel

States/turbines/state-turbine selection, depending on the level

model_key

Creates sub-model which can then be placed in the turbine model list. Repeated keys are added to the same turbine model

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_gradients(vars_int, vars_float, func, components, ivars, fvars, vrs, pop=False, verbosity=0)

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: numpy.ndarray

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

check_constraints_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_constraints_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: np.array

The boolean result, shape: (n_pop, n_components)

evaluate_individual(vars_int, vars_float, ret_prob_res=False)

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)

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)

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)

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)

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: numpy.ndarray

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

initial_values_float() → numpy.ndarray[source]

The initial values of the float variables.

Returns:
values

Initial float values, shape: (n_vars_float,)

initial_values_int() → numpy.ndarray[source]

The initial values of the int 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:
verbosity

The verbosity level, 0 = silent

kwargs

Additional parameters for super class init

max_values_float() → numpy.ndarray[source]

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.ndarray[source]

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.ndarray[source]

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.ndarray[source]

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

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

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)

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_states, n_pop, 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, coeffs)

Calculate the einsum of problem results

Parameters:
prob_res_list: list

The problem results

coeffs: numpy.ndarray

The coefficients

Returns:
prob_res: object

The weighted sum of problem results

prob_res_einsum_population(prob_res_list, coeffs)

Calculate the einsum of problem results

Parameters:
prob_res_list: list

The problem results

coeffs: numpy.ndarray

The coefficients

Returns:
prob_res: object

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

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

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][source]

The names of float variables.

Returns:
names

The names of the float variables

var_names_int() → list[str][source]

The names of int variables.

Returns:
names

The names of the int 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
property constraints_tol

Gets the tolerance values of constraints

Returns:
ctol: numpy.ndarray

The constraint tolerance values, shape: (n_constraints,)

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

Flag for finished initialization

Returns:
bool

True if initialization has been done

property max_values_constraints

Gets the maximal values of constraints

Returns:
cma: numpy.ndarray

The maximal constraint values, shape: (n_constraints,)

property maximize_objs

Flags for objective maximization

Returns:
maximize: numpy.ndarray

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

memory = None
property min_values_constraints

Gets the minimal values of constraints

Returns:
cmi: numpy.ndarray

The minimal constraint values, shape: (n_constraints,)

property n_constraints

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

Returns:
n_con: int

The total number of constraint functions

property n_objectives

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

Returns:
n_obj: int

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

The number of float variables

Returns:
n: int

The number of float variables

property n_vars_int

The number of int variables

Returns:
n: int

The number of int variables

name
objs
points = None
property sel_turbines : list[int]

The selected turbines

Returns:
value

Indices of the selected turbines