MPC in buildings. Repository & playing around


Buildings are inertial systems and require anticipating the use of heating/cooling systems to be comfortable inside. Commonly, Heating, Ventilation and Air Conditioning (HVAC) systems are activated a few hours before actually needing it, so that there is time for the concrete, brick, furniture… to heat/cool and ensure a comfortable indoor environment. When using electric systems such as heat pumps, variations in the cost of electricity may add some complexity to the operation of HVAC systems to ensure an economic optimum.

In the past decades, many researchers have sought more advanced control approaches. MPC (Model Predictive Control) is a promising approach for this. I am investigating on this and want to share my progress.

In MPC, we model the behavior of a system (a building), given some contextual information (meteorological conditions, occupancy…), and optimize the control signals to ensure an optimal behavior.

This last paragraph is quite short and abstract. Some questions arise, and there are some decisions to take:

  • How do we model the building as a system? A simplified RC model, with some HVAC equations seems a good option
  • Which are the control signals? The right approach is to consider what are the actual control signals in a system. Typically, an on/off relay or a thermostatic control.
  • What does “optimal behavior” mean? For me, ensuring that the building is within comfort boundaries whenever someone is inside, at the minimum economic cost.
  • How do we get to the optimal solution? There are many options out there. For me, genetic algorithms provided a good solution in previous works. So I came to them again.

I have reviewed many works on MPC. They present approaches on building & HVAC modelling, and their definition of “optimal behavior” is mathematically defined. Some good works, also discuss on the input/output variables available in the control system, and the wisest selection for the MPC optimization. But there is very little information on optimizer parametrization, so I tried to get some answers myself:

  1. Does MPC really optimize? The answer is obvious, but I needed to verify 😊.
  2. How far in the future should we look to get an optimum? A few hours, one day?…
  3. How often should we optimize? Every hour? Once per day?…
  4. Is MPC feasible in real life? How much time does it take to get an optimum?
  5. How should we parametrize the genetic algorithm to ensure that we optimize the output?

All these questions are very relevant within our project STUNNED. There, we aim at optimizing the energy performance of buildings in such a way that they can participate in the day-ahead markets.

As said, I built a simulator based on a well-known RC model, additional equations for HVAC systems that we published last summer; adaptations on the occupancy schedules, thermostatic activation of heating systems performed together with Noe Fontier throughout this autumn; and a reward formula quite weighted towards comfort. Noe also sourced contextual information on meteorological information and energy costs.

Then I developed an R code that goes through a years’ worth of contextual information and periodically optimizes HVAC operation in the near future. This optimization sets the heating setpoint temperature for every hour in the future.

All this, together with some auxiliary scripts and some sample output, is freely available here.

Evolution of indoor temperature, required heating input and associated costs over a random week.

So, the obvious answer to question 1 is YES. MPC optimizes, even if it is coded by someone like me. Then I will go briefly through the answers to the other questions.

How far in the future to optimize? I have found that <12h seems to be suboptimal, as it does not allow for sufficient anticipation to get the most out of inertia and energy price variations.

How often to optimize? Ideally, continuously. But this does not seem practical. I don’t have a real answer here. As far as I went, every 12 is ok. But I need to look further.

How much time does it take to get an optimum? This was a large concern to me. I found that it is possible to have a 24h-ahead optimization in about <10minutes (very dependent on hyperparameters). So, this is compatible with running the optimizer frequently.

How should we parametrize genetic algorithm? A genetic algorithm basically resembles human evolution. It takes a “population” where each individual is a random choice of control inputs. And then allows the most popular ones to “reproduce” into the next generations. After many generations, the solution is expected to be a good one.

I took profit of the Donostia International Physics Centers Super Computing Centre to explore these hyperparameters with the following tentative output:

  • Population Size. ~35-40 seems to be OK
  • Number of generations/iterations. ~35-40 seems to be OK
  • Number of runs. ~5 seems to be OK

Exploration of hyperparameter impact in solution quality.

The computational cost of the optimization is the product of these three values. There is a need to simulate 5000 times the optimization horizon to get the desired value. That’s the reason for the above-mentioned 10 minutes.

Interestingly, I required about 2000 simulations, each of them in between a few hours and >1day to get to a viable solution suitable to deliver optimal control setpoints in <10minutes.

For the moment, this is just preliminary output. I will be polishing it throughout the coming months. And hopefully publish it as a scientific paper. But the repository is public for anyone that wants to have a go.

In the coming months, I expect to improve the code, extend it to other systems, optimize not only for energy, but also flexibility,…adapt models to real-life mechanical and control constraints,….so that we solve our share of STUNNED.

The repository is available here: Roberto Garay-Martinez, Model Predictive Control for Buildings, 2025, https://github.com/robgaray/Model-Predictive-Control-in-Buildings, https://doi.org/10.5281/zenodo.18030579

If you are curious, have some feedback, are facing a similar problem… please contact me.

NOTE: This post was written on dec 2025 and scheduled for publication in feb 2026. The post itself is based on version v1.0 of the code. Since then, I released v1.1 and v1.2, with significant upgrades. I expect to perform further upgrades through Q1 and Q2 2026. When I do so, I will re-post with the novelties.