zen_garden.model.component

Contains the classes which initialize parameters, variables and constraints. This is a proxy for pyomo parameters, since the construction of parameters has a significant overhead. Indexing within ZEN-garden is also defined here.

Classes

Component()

Class to prepare parameter, variable and constraint data to suit linopy.

Constraint(index_sets, model)

DictParameter()

This is a helper class to store the dictionary parameters.

IndexSet()

Class to prepare parameter data for pyomo parameter prerequisites.

Parameter(optimization_setup)

Variable(optimization_setup)

ZenIndex(index_values[, index_names])

A multiindex class that can be easily used with xarray.

ZenSet(data[, name, doc, index_set])

Similiar to pyomo.Set.

class zen_garden.model.component.Component

Class to prepare parameter, variable and constraint data to suit linopy.

__init__()

Instantiate object of Component class.

static compile_doc_string(doc, index_list, name, domain=None)

Compile docstring from doc and index_list.

Parameters:
  • doc – docstring to be compiled

  • index_list – list of indices

  • name – name of parameter/variable/constraint

  • domain – domain of parameter/variable/constraint (e.g., reals, non negative reals, …)

Return complete_doc:

complete docstring composed of name, doc and dims

static get_index_names_data(index_list)

Splits index_list in data and index names.

Parameters:

index_list – list of indices (names and values)

Return index_values:

names of indices

Return index_names:

values of indices

class zen_garden.model.component.Constraint(index_sets, model)
__init__(index_sets, model)

Initialization of a constraint.

Parameters:
  • index_sets – A reference to the index sets of the model

  • model – A reference to the linopy model

add_constraint(name, constraint, doc='')

Initialization of a constraint.

Parameters:
  • name – name of variable

  • constraint – a linopy constraint or a dictionary of constraints or None

  • doc – docstring of variable

add_pw_constraint(model, name, index_values, yvar, xvar, break_points, f_vals, cons_type='EQ')

Adding piece-wise linear constraints to the model.

Adds a piece-wise linear constraint of the type f(x) = y for each index in the index_values, where f is defined by the breakpoints and f_vals (x_1, y_1), …, (x_n, y_n).

Note that these method will create helper variables in form of a S0S2, sources:

Parameters:
  • model – The model to add the constraints to

  • name – The name of the constraint

  • index_values – A list of index values that will be used to build the constraints

  • yvar – The name of the yvar, a variable compatible with the index values used for y

  • xvar – The name of the xvar, a variable compatible with the index values used for x

  • break_points – A mapping index -> list that provides the breakpoints for each index

  • f_vals – A mapping index -> list that provides the function values for each index

  • cons_type – Type of the constraint (currently only EQ supported)

add_single_constraint(name, constraint)

Adds a single constraint to the model.

Parameters:
  • name – name of variable

  • constraint – linopy constraint

reorder_group(lhs, sign, rhs, index_values, index_names, model, drop=None)

Reorders constraints in a group to get full shape based on indexes and names.

Parameters:
  • lhs – The lhs of the constraints

  • sign – The sign of the constraints, can be None if only lhs should be restructured

  • rhs – The rhs of the constraints, can be None if only lhs should be restructured

  • index_values – The index values corresponding to the group numbers

  • index_names – The index names of the indices

  • model – The model

  • drop – Which group to drop (the dummy group

Returns:

An anonymous constraint

class zen_garden.model.component.DictParameter

This is a helper class to store the dictionary parameters.

add_param(name, data)

Add a parameter.

Parameters:
  • name – The name of the param

  • data – The data of the param

class zen_garden.model.component.IndexSet

Class to prepare parameter data for pyomo parameter prerequisites.

__init__()

Initialization of the IndexSet object.

add_set(name, data, doc, index_set=None)

Adds a set to the IndexSets (this set it not indexed).

Parameters:
  • name – The name of the set

  • data – The data used for the init

  • doc – The docstring of the set

  • index_set – The name of the index set if the set it self is indexed

create_variable_bounds(bounds, coords, index_arrs, index_list, index_values)

Creates the bounds for the variables.

Parameters:
  • bounds – The bounds of the variable

  • coords – The coordinates of the variable

  • index_arrs – The index values as xarrays

  • index_list – The list of the index names

  • index_values – The list of the index values

Returns:

The lower and upper bounds as xarrays

create_variable_mask(coords, index_arrs, index_list, model)

Creates the mask for the variables.

Parameters:
  • coords – The coordinates of the variable

  • index_arrs – The index values as xarrays

  • index_list – The list of the index names

  • model – The model to which the mask belongs, note that indices which don’t match existing indices are renamed to match the model

Returns:

The mask as xarray

get_coord(data, name)

Transforms data into a coordinate to avoid same name with different values.

Transforms the data into a proper coordinate. If the name of the data is in a set, the sets superset is returned otherwise all unique data values are returned, this is to avoid having sets with the same name and different values.

Parameters:
  • data – The data to transform

  • name – The name of the set

Returns:

The proper coordinate

get_index_name(name)

Returns the index name of an indexed set.

Parameters:

name – The name of the indexed set

Returns:

The name of the index set

indices_to_mask(index_values, index_list, bounds, model=None)

Transforms a list of index values into a mask.

Parameters:
  • index_values – A list of index values (tuples)

  • index_list – The list of the names of the indices

  • bounds – Either None, tuple, array or callable to define the bounds of the variable

  • model – The model to which the mask belongs, note that indices which don’t match existing indices are renamed to match the model

Returns:

The mask as xarray

is_indexed(name)

Checks if the set with the name is indexed.

Parameters:

name – The name of the set

Returns:

True if indexed, False otherwise

static tuple_to_arr(index_values, index_list, unique=False)

Transforms list of tuples into a list of xarrays with everything from tuple.

Parameters:
  • index_values – The list of tuples with the index values

  • index_list – The names of the indices, used in case of emtpy values

  • unique – If True, the values are unique

Returns:

A list of arrays

class zen_garden.model.component.Parameter(optimization_setup)
__init__(optimization_setup)

Initialization of the parameter object.

add_helper_parameter(name, data)

Adding a helper parameter that is not added to the docs and results.

Adds a helper param. Note that this param is not added to the docs and therefore

not saved in the results. Also, the data is taken as is and is not transformed.

Parameters:
  • name – The name of the param

  • data – The data

add_parameter(name, doc, data=None, calling_class=None, index_names=None, set_time_steps=None, capacity_types=False)

Initialization of a parameter.

Parameters:
  • name – name of parameter

  • doc – docstring of parameter

  • data – non default data of parameter and index_names

  • calling_class – class type of the object add_parameter is called for

  • index_names – names of index sets, only if calling_class is not EnergySystem

  • set_time_steps – time steps, only if calling_class is EnergySystem

  • capacity_types – boolean if extracted for capacities

static convert_to_dict(data)

Converts the data to a dict if pd.Series.

Parameters:

data – non default data of parameter and index_names

Return data:

data as dict

convert_to_xarr(data, index_list)

Converts the data to a dict if pd.Series.

Parameters:
  • data – non default data of parameter and index_names

  • index_list – list of indices

Return data:

data as xarray

static get_param_units(data, dict_of_units, index_list, name)

Creates series of units with identical multi-index as data has.

Parameters:
  • data – non default data of parameter and index_names

  • dict_of_units – units of parameter

  • index_list – list of index names

save_min_max(data, name)

Stores min and max parameter.

Parameters:
  • data – non default data of parameter and index_names

  • name – name of parameter

class zen_garden.model.component.Variable(optimization_setup)
__init__(optimization_setup)

Initialization of a variable.

Parameters:

optimization_setup – OptimizationSetup object

add_variable(model: Model, name, index_sets, unit_category, integer=False, binary=False, bounds=None, doc='', mask=None)

Initialization of a variable.

Parameters:
  • model – parent block component of variable, must be linopy model

  • name – name of variable

  • index_sets – Tuple of index values and index names

  • unit_category – dict defining the dimensionality of the variable’s unit

  • integer – If it is an integer variable

  • binary – If it is a binary variable

  • bounds – bounds of variable

  • doc – docstring of variable

  • mask – mask of variable

get_var_units(unit_category, var_index_values, index_list, mask=None)

Creates series of units with identical multi-index as variable has.

Parameters:
  • unit_category – dict defining the dimensionality of the variable’s unit

  • var_index_values – list of variable index values

  • index_list – list of index names

  • mask – mask of variable

Returns:

series of variable units

class zen_garden.model.component.ZenIndex(index_values, index_names=None)

A multiindex class that can be easily used with xarray.

__init__(index_values, index_names=None)

Initialize the multiindex.

Parameters:
  • index_values – A list of index values as tuples

  • index_names – Optional list of index names as strings, if the length does not match the tuple length, it is ignored

get_unique(levels, as_array=False)

Returns a list of unique tuples across potentially multiple levels.

Parameters:
  • levels – A list of levels eithes by position or name

  • as_array – If True, the return value a list of xarrays

Returns:

A list of tuples if given multiple levels, otherwise a list of values

get_values(locs, levels, dtype=<class 'list'>, unique=False)

Get all values of the levels over a given set of locations.

Parameters:
  • locs – A list of locs used for the “get_locs” method of the index

  • levels – A single level or a list of levels to get the values for

  • dtype – The dtype of the return value, either list or xr.DataArray

  • unique – If True, only unique values are returned

Returns:

A single list or xr.DataArray if only one level is given, otherwise a list of lists or xr.DataArrays

class zen_garden.model.component.ZenSet(data, name='', doc='', index_set='UnnamedIndex')

Similiar to pyomo.Set.

__init__(data, name='', doc='', index_set='UnnamedIndex')

Initialize the set.

Parameters:
  • data – The data of the set, either an iterable or a dictionary for an indexed set

  • name – The name of the set

  • doc – The corresponding docstring

  • index_set – The name of the index set

get_index_name()

Returns the index name if indexed.

is_indexed()

Check if the set is indexed, just here because pyomo has it.