Math Model API¶
The math_model module contains the internals for building and solving the mixed-integer linear program (MILP) that powers the optimization.
Model Builder¶
Builder for constructing linopy optimization models from energy system parameters.
This module provides the EnergyAlgebraicModelBuilder that assembles variables, constraints, and objectives into a solvable MILP model.
EnergyAlgebraicModelBuilder
¶
Builder class for constructing algebraic energy system optimization models.
This class takes a validated energy system configuration and builds a complete linopy optimization model including variables, constraints, and objectives ready for solving.
The builder ensures the model is constructed only once and prevents multiple builds of the same instance.
Source code in src/odys/math_model/model_builder.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
__init__(energy_system_parameters)
¶
Initialize the model builder with validated energy system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy_system_parameters
|
EnergySystemParameters
|
Parameters of the energy system, containing all assets, demand profiles, and constraints. |
required |
Source code in src/odys/math_model/model_builder.py
50 51 52 53 54 55 56 57 58 59 60 61 | |
add_variable_to_model(variable)
¶
Add a variable to the underlying linopy model.
Source code in src/odys/math_model/model_builder.py
148 149 150 151 152 153 154 155 156 | |
build()
¶
Build the complete optimization model with variables, constraints, and objective.
Returns:
| Type | Description |
|---|---|
EnergyMILPModel
|
The fully constructed EnergyMILPModel ready for solving. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the model has already been built. |
Source code in src/odys/math_model/model_builder.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
get_index_for_dimension(dimension)
¶
Return the model index corresponding to a given dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
ModelDimension
|
The model dimension to look up. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no index exists for the given dimension. |
Source code in src/odys/math_model/model_builder.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
MILP Model¶
MILP model representation for energy system optimization.
This module provides the EnergyMILPModel class that wraps a linopy Model with typed accessors for energy system decision variables.
EnergyMILPModel
¶
Wrapper around a linopy Model with typed variable accessors for energy systems.
Source code in src/odys/math_model/milp_model.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
battery_charge_mode
property
¶
Return the battery charge/discharge mode indicator variable.
battery_power_in
property
¶
Return the battery charging power variable.
battery_power_net
property
¶
Return the battery net power variable (charge - discharge).
battery_power_out
property
¶
Return the battery discharging power variable.
battery_soc
property
¶
Return the battery state of charge variable.
generator_power
property
¶
Return the generator power output variable.
generator_shutdown
property
¶
Return the generator shutdown indicator variable.
generator_startup
property
¶
Return the generator startup indicator variable.
generator_status
property
¶
Return the generator on/off status variable.
indices
cached
property
¶
Return all dimension indices for the model.
linopy_model
property
¶
Return the underlying linopy model.
market_buy_volume
property
¶
Return the market buy volume variable.
market_sell_volume
property
¶
Return the market sell volume variable.
market_trade_mode
property
¶
Return the market buy/sell mode indicator variable.
parameters
property
¶
Return the energy system parameters.
__init__(parameters)
¶
Initialize the MILP model with energy system parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
EnergySystemParameters
|
Validated energy system parameters. |
required |
Source code in src/odys/math_model/milp_model.py
37 38 39 40 41 42 43 44 45 | |
EnergyModelIndices
¶
Bases: BaseModel
Collection of all dimension indices used in the optimization model.
Source code in src/odys/math_model/milp_model.py
21 22 23 24 25 26 27 28 29 30 31 | |
Sets¶
Set and index definitions for the optimization model dimensions.
ModelDimension
¶
Bases: StrEnum
Dimension names used as axes in the optimization model.
Source code in src/odys/math_model/model_components/sets.py
10 11 12 13 14 15 16 17 18 | |
ModelIndex
¶
Bases: BaseModel, ABC
Energy Model Set.
Source code in src/odys/math_model/model_components/sets.py
21 22 23 24 25 26 27 28 29 30 31 | |
coordinates
property
¶
Gets coordinates for xarray objects.
Variables¶
Variable definitions for energy system optimization models.
This module defines variable names and types used in energy system optimization models.
BoundType
¶
Bases: Enum
Lower bound type for optimization variables.
Source code in src/odys/math_model/model_components/variables.py
14 15 16 17 18 | |
ModelVariable
¶
Bases: Enum
All decision variables in the energy system optimization model.
Source code in src/odys/math_model/model_components/variables.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
asset_dimension
property
¶
Get the asset dimension (Generators or Batteries) if present.
dimensions
property
¶
Return the dimensions this variable is defined over.
is_binary
property
¶
Return whether this variable is binary.
lower_bound_type
property
¶
Return the lower bound type for this variable.
var_name
property
¶
Return the variable name used in the linopy model.
VariableSpec
¶
Bases: BaseModel
Specification for an optimization variable (name, type, dimensions, bounds).
Source code in src/odys/math_model/model_components/variables.py
21 22 23 24 25 26 27 28 29 | |
Objectives¶
Objective function definitions for energy system optimization models.
This module defines objective function names and types used in energy system optimization models.
ObjectiveFunction
¶
Builds the objective function for the energy system optimization model.
Source code in src/odys/math_model/model_components/objectives.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
profit
property
¶
Build the total profit expression (market revenue minus operating costs).
__init__(milp_model)
¶
Initialize with the MILP model to build the objective from.
Source code in src/odys/math_model/model_components/objectives.py
16 17 18 | |
get_market_revenue()
¶
Calculate expected market revenue across all scenarios.
Source code in src/odys/math_model/model_components/objectives.py
36 37 38 39 40 41 42 | |
get_operating_costs()
¶
Calculate total generator operating costs (variable + startup).
Source code in src/odys/math_model/model_components/objectives.py
44 45 46 47 48 49 | |
Constraints¶
Model Constraint¶
Named constraint wrapper for linopy constraints.
ModelConstraint
¶
Bases: BaseModel
A named linopy constraint ready to be added to the model.
Source code in src/odys/math_model/model_components/constraints/model_constraint.py
7 8 9 10 11 12 13 | |
Generator Constraints¶
Generator-related constraints for the optimization model.
GeneratorConstraints
¶
Builds constraints for generator power limits, ramping, startup/shutdown, and min uptime.
Source code in src/odys/math_model/model_components/constraints/generator_constraints.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
all
property
¶
Return all generator constraints, or an empty tuple if no generators exist.
__init__(milp_model)
¶
Initialize with the MILP model containing generator variables and parameters.
Source code in src/odys/math_model/model_components/constraints/generator_constraints.py
10 11 12 13 | |
Battery Constraints¶
Battery-related constraints for the optimization model.
BatteryConstraints
¶
Builds constraints for battery charge/discharge, SOC dynamics, and power limits.
Source code in src/odys/math_model/model_components/constraints/battery_constraints.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
all
property
¶
Return all battery constraints, or an empty tuple if no batteries exist.
__init__(milp_model)
¶
Initialize with the MILP model containing battery variables and parameters.
Source code in src/odys/math_model/model_components/constraints/battery_constraints.py
11 12 13 14 | |
Scenario Constraints¶
Scenario-level constraints for the optimization model.
ScenarioConstraints
¶
Builds power balance, available capacity, and non-anticipativity constraints.
Source code in src/odys/math_model/model_components/constraints/scenario_constraints.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
all
property
¶
Return all scenario-level constraints (power balance, capacity, non-anticipativity).
__init__(milp_model)
¶
Initialize with the MILP model containing scenario-level variables and parameters.
Source code in src/odys/math_model/model_components/constraints/scenario_constraints.py
12 13 14 15 16 17 18 19 20 21 22 | |
Market Constraints¶
Market-related constraints for the optimization model.
MarketConstraints
¶
Builds constraints for market trading volumes, mutual exclusivity, and trade direction.
Source code in src/odys/math_model/model_components/constraints/market_constraints.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
all
property
¶
Return all market constraints, or an empty tuple if no markets exist.
__init__(milp_model)
¶
Initialize with the MILP model containing market variables and parameters.
Source code in src/odys/math_model/model_components/constraints/market_constraints.py
11 12 13 14 | |
Parameters¶
Energy System Parameters¶
Parameter definitions for energy system optimization models.
This module defines parameter names and types used in energy system optimization models.
EnergySystemParameters
¶
Bases: BaseModel
Collection of all energy system parameters for optimization models.
Source code in src/odys/math_model/model_components/parameters/parameters.py
16 17 18 19 20 21 22 23 24 25 | |
Generator Parameters¶
Generator parameters for the mathematical optimization model.
GeneratorIndex
¶
Bases: ModelIndex
Index for generator components in the optimization model.
Source code in src/odys/math_model/model_components/parameters/generator_parameters.py
12 13 14 15 | |
GeneratorParameters
¶
Parameters for generator assets in the energy system model.
Source code in src/odys/math_model/model_components/parameters/generator_parameters.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
index
property
¶
Return the generator index.
max_ramp_down
property
¶
Return generator maximum ramp down rate data.
max_ramp_up
property
¶
Return generator maximum ramp up rate data.
min_power
property
¶
Return generator minimum power data.
min_up_time
property
¶
Return generator minimum up time data.
nominal_power
property
¶
Return generator nominal power data.
startup_cost
property
¶
Return generator startup cost data.
variable_cost
property
¶
Return generator variable cost data.
__init__(generators)
¶
Initialize generator parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generators
|
Sequence[PowerGenerator]
|
Sequence of power generator objects. |
required |
Source code in src/odys/math_model/model_components/parameters/generator_parameters.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Battery Parameters¶
Battery parameters for the mathematical optimization model.
BatteryIndex
¶
Bases: ModelIndex
Index for battery components in the optimization model.
Source code in src/odys/math_model/model_components/parameters/battery_parameters.py
12 13 14 15 | |
BatteryParameters
¶
Parameters for battery assets in the energy system model.
Source code in src/odys/math_model/model_components/parameters/battery_parameters.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
capacity
property
¶
Return battery capacity data.
efficiency_charging
property
¶
Return battery charging efficiency data.
efficiency_discharging
property
¶
Return battery discharging efficiency data.
index
property
¶
Return the battery index.
max_power
property
¶
Return battery maximum power data.
soc_end
property
¶
Return battery final state of charge data.
soc_max
property
¶
Return battery maximum state of charge data.
soc_min
property
¶
Return battery minimum state of charge data.
soc_start
property
¶
Return battery initial state of charge data.
__init__(batteries)
¶
Initialize battery parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batteries
|
Sequence[Battery]
|
Sequence of battery objects. |
required |
Source code in src/odys/math_model/model_components/parameters/battery_parameters.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
Load Parameters¶
Load parameters for the mathematical optimization model.
LoadIndex
¶
Bases: ModelIndex
Index for load components in the optimization model.
Source code in src/odys/math_model/model_components/parameters/load_parameters.py
10 11 12 13 | |
LoadParameters
¶
Parameters for load assets in the energy system model.
Source code in src/odys/math_model/model_components/parameters/load_parameters.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
index
property
¶
Return the load index.
__init__(loads)
¶
Initialize load parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loads
|
Sequence[Load]
|
Sequence of load objects. |
required |
Source code in src/odys/math_model/model_components/parameters/load_parameters.py
19 20 21 22 23 24 25 26 27 28 | |
Market Parameters¶
Market parameters for the mathematical optimization model.
MarketIndex
¶
Bases: ModelIndex
Index for market components in the optimization model.
Source code in src/odys/math_model/model_components/parameters/market_parameters.py
12 13 14 15 | |
MarketParameters
¶
Parameters for energy market components in the energy system model.
Source code in src/odys/math_model/model_components/parameters/market_parameters.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
index
property
¶
Return the market index.
max_volume
property
¶
Return maximum trading volume per time step.
stage_fixed
property
¶
Return whether each market's variables are fixed across scenarios.
trade_direction
property
¶
Return the allowed trade direction (buy, sell, or both) per market.
__init__(markets)
¶
Initialize market parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markets
|
Sequence[EnergyMarket]
|
Sequence of energy market objects. |
required |
Source code in src/odys/math_model/model_components/parameters/market_parameters.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Scenario Parameters¶
Scenario parameters for the mathematical optimization model.
ScenarioIndex
¶
Bases: ModelIndex
Index for scenario components in the optimization model.
Source code in src/odys/math_model/model_components/parameters/scenario_parameters.py
23 24 25 26 | |
ScenarioParameters
¶
Parameters for scenarios in the energy system model.
Source code in src/odys/math_model/model_components/parameters/scenario_parameters.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
available_capacity_profiles
property
¶
Return available capacity profiles for generators across scenarios and time.
load_profiles
property
¶
Return load profiles across scenarios and time.
market_prices
property
¶
Return market prices across scenarios and time.
scenario_index
property
¶
Return the scenario index.
scenario_probabilities
property
¶
Returns scenario probabilities as xarray DataArray.
time_index
property
¶
Return the time index.
__init__(number_of_timesteps, scenarios, generators_index, batteries_index, markets_index, loads_index)
¶
Initialize scenario parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_timesteps
|
int
|
Number of time steps in the scenarios. |
required |
scenarios
|
Sequence[StochasticScenario]
|
Sequence of stochastic scenario objects. |
required |
generators_index
|
GeneratorIndex | None
|
Optional generator index. |
required |
batteries_index
|
BatteryIndex | None
|
Optional battery index. |
required |
markets_index
|
MarketIndex | None
|
Optional market index. |
required |
loads_index
|
LoadIndex | None
|
Optional load index. |
required |
Source code in src/odys/math_model/model_components/parameters/scenario_parameters.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
TimeIndex
¶
Bases: ModelIndex
Index for time components in the optimization model.
Source code in src/odys/math_model/model_components/parameters/scenario_parameters.py
17 18 19 20 | |
Linopy Converter¶
Utilities for converting model variables into linopy-compatible parameters.
LinopyVariableParameters
¶
Bases: BaseModel
Parameters needed to add a variable to a linopy model.
Source code in src/odys/math_model/model_components/linopy_converter.py
14 15 16 17 18 19 20 21 22 23 | |
get_variable_lower_bound(indeces, lower_bound_type, *, is_binary)
¶
Calculate lower bounds for a variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indeces
|
list[ModelIndex]
|
List of dimension sets for the variable |
required |
lower_bound_type
|
BoundType
|
Type of lower bound |
required |
is_binary
|
bool
|
Whether the variable is binary |
required |
Returns:
| Type | Description |
|---|---|
ndarray | float
|
Lower bound value or array |
Source code in src/odys/math_model/model_components/linopy_converter.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |