# Tutorial 3: Mixed Integer Linear Programming¶

This example is based on the urban scale example model, but with an override. An override file exists in which binary and integer decision variables are triggered, creating a MILP model, rather than the conventional Calliope LP model.

Warning

Integer and Binary variables are still experimental and may not cover all edge cases as intended. Please raise an issue on GitHub if you see unexpected behaviour.

## 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.

## 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
# cufflinks allows for easy plotly plots to be
# produced from a pandas DataFrame
import cufflinks
# 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
```

```
# 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()
```

```
# plot.capacities gives a graphical view of the non-timeseries variables, both input and output
# Note, because we fix unit size, CHP now has a maximum capacity of 300kW,
# compared to 260kW in the non-MILP case
model.plot.capacity()
```

See the Calliope documentation for more details on setting up and running a Calliope model.