Tutorial 3: Mixed Integer Linear Programming¶
This example is based on the urban scale example model, but with an override. In the model’s scenarios.yaml
file overrides are defined which trigger binary and integer decision variables, creating a MILP model, rather than a conventional LP model.
Units¶
The capacity of a technology is usually a continuous decision variable, which can be within the range of 0 and energy_cap_max
(the maximum capacity of a technology). In this model, we introduce a unit limit on the CHP instead:
chp:
constraints:
units_max: 4
energy_cap_per_unit: 300
energy_cap_min_use: 0.2
costs:
monetary:
energy_cap: 700
purchase: 40000
A unit maximum allows a discrete, integer number of CHP to be purchased, each having a capacity of energy_cap_per_unit
. Any of energy_cap_max
, energy_cap_min
, or energy_cap_equals
are now ignored, in favour of units_max
, units_min
, or units_equals
. A useful feature unlocked by introducing this is the ability to set a minimum operating capacity which is only enforced when the technology is operating. In the LP model, energy_cap_min_use
would force the technology to operate at least at that proportion of its maximum capacity at each time step. In this model, the newly introduced energy_cap_min_use
of 0.2 will ensure that the output of the CHP is 20% of its maximum capacity in any time step in which it has a non-zero output.
Purchase cost¶
The boiler does not have a unit limit, it still utilises the continuous variable for its capacity. However, we have introduced a purchase
cost:
boiler:
costs:
monetary:
energy_cap: 35
purchase: 2000
By introducing this, the boiler now has a binary decision variable associated with it, which is 1 if the boiler has a non-zero energy_cap
(i.e. the optimisation results in investment in a boiler) and 0 if the capacity is 0. The purchase cost is applied to the binary result, providing a fixed cost on purchase of the technology, irrespective of the technology size. In physical terms, this may be associated with the cost of pipework, land purchase, etc. The purchase cost is also imposed on the CHP, which is applied to the number of integer CHP units in which the solver chooses to invest.
MILP functionality can be easily applied, but convergence is slower as a result of integer/binary variables. It is recommended to use a commercial solver (e.g. Gurobi, CPLEX) if you wish to utilise these variables outside this example model.
Asynchronous energy production/consumption¶
The heat pipes which distribute thermal energy in the network may be prone to dissipating heat in an unphysical way. I.e. given that they have distribution losses associated with them, in any given timestep, a link could produce and consume energy in the same timestep, losing energy to the atmosphere in both instances, but having a net energy transmission of zero. This allows e.g. a CHP facility to overproduce heat to produce more cheap electricity, and have some way of dumping that heat. The asynchronous_prod_con
binary constraint ensures this phenomenon is avoided:
heat_pipes:
constraints:
force_asynchronous_prod_con: true
Now, only one of carrier_prod
and carrier_con
can be non-zero in a given timestep. This constraint can also be applied to storage technologies, to similarly control charge/discharge.
Running the model¶
We now take you through running the model in a Jupyter notebook, which is included fully below. To download and run the notebook yourself, you can find it here. You will need to have Calliope installed.
Calliope Urban Scale MILP Example Model¶
For more details on analysing input/output data, see the full urban scale example model
import calliope
# We increase logging verbosity
calliope.set_log_level('INFO')
model = calliope.examples.milp()
# Note, we see the overrides that we have applied printed here, thanks to inreasing logging verbosity
# We also see a warning that we are applying binary/integer decision variables in a model, to remind us that this
# model may take a while to run
# Model inputs can be viewed at `model.inputs`.
# Variables are indexed over any combination of `techs`, `locs`, `carriers`, `costs` and `timesteps`,
# although `techs`, `locs`, and `carriers` are often concatenated.
# e.g. `chp`, `X1`, `heat` -> `X1::chp::heat`
model.inputs
# Individual data variables can be accessed easily, `to_pandas()` reformats the data to look nicer
# Here we look at one of the MILP overrides that we have added, the fixed `purchase` cost
model.inputs.cost_purchase.to_pandas().dropna(axis=1)
# Solve the model. Results are loaded into `model.results`.
# By including logging (see package importing), we can see the timing of parts of the run, as well as the solver's log
model.run()
# Model results are held in the same structure as model inputs.
# The results consist of the optimal values for all decision variables, including capacities and carrier flow
# There are also results, like system capacity factor and levelised costs, which are calculated in postprocessing
# before being added to the results Dataset
model.results
# We can sum operating units of CHP over all locations and turn the result into a pandas DataFrame
df_units = model.get_formatted_array('operating_units').sum('locs').to_pandas().T
#The information about the dataframe tells us about the amount of data it holds in the index and in each column
df_units.info()
# Using .head() to see the first few rows of operating units
df_units.head()
# We can plot this by using the timeseries plotting functionality.
# The top-left dropdown gives us the chance to scroll through other timeseries data too.
model.plot.timeseries()