We present here a microgrid problem introduced and solved by MILP techniques in [1], later studied in [2] with direct and Hamilton-Jacobi-Bellman methods. In the context of an isolated village in the Chilean mountains, we consider a microgrid comprised of a diesel generator, photo-voltaic panels and a battery energy storage system (BESS). The aim is to satisfy the energy demand (power load) from the villagers at all times, while minimizing the overall diesel consumption. One of the difficulties is that the diesel generator has a turn-on cost, thus we have to keep track of its on/off state over time. This problem falls in the class of switched systems and can be solved with a dynamic programming approach.

We consider a fixed horizon T = 48 hours. For t ∈ [0, T], we denote by PS(t) the solar power from the photovoltaic panels, PD(t) the diesel generator power and PL(t) the electricity load.

**Dynamics**. The state of charge SOC(t) of the BESS evolves according to

where Q_B is the maximum capacity of the battery, are the input and output power of the BESS, and are the efficiency ratios for the charge and discharge processes, assumed constant. We introduce the slack variable that represents the excess power , which has to be shed, or the missing power in the microgrid , which turns into unmet demand. The addition of this variable ensures the mathematical feasibility of the problem.

**Cost function.**We model the fuel consumption of the diesel generator by

The fuel consumption curve is strictly concave and was extrapolated from the datasheet provided by the diesel generator manufacturer as in [1]. Note that turning the diesel generator on adds a fixed instantaneous consumption of 2l. Total cost is the sum of the diesel cost and unserved energy cost , with and the unit price for diesel (Chilean Pesos).

**Constraints.** For physical reasons, the system is subject to the following constraints at every time These include the bounds for the battery state of charge, diesel generator power output, and discharge charge limits for the BESS

Moreover, the power equilibrium must hold at all times and can be used to eliminate some of the variables:

**External data.** We choose two test cases of a 48h time frame in winter and summer. The power load and solar power are shown below.

*summer data*

*winter data*

**The ‘PMP trick’.**This problem can be solved directly by BocopHJB using a basic control discretization for and . However, in this particular case, we are able to exploit the properties of the problem to improve the method. If is the optimal control, denote by the optimal state and the costate associated to the dynamics constraint . Defining the Hamiltonian the Pontryagin’s Maximum Principle (PMP) says that for all we have

Since the dynamics is continuous and piecewise affine, the Hamiltonian is the sum of a continuous, piecewise affine and of a continuous strictly concave functions, and therefore is continuous, piecewise strictly concave. Therefore it can attain its minimum only at one of the extreme points of the pieces. Taking into account the constraints, we have at most five possible optimal controls, as illustrated below. Moreover, the values of these controls can be computed explicitly, since they do not depend on (which is not true in general). Therefore, instead of the usual control discretization we only test these controls, gaining both in speed and precision.

So:

– if the Diesel is off (mode 0), we simply take .

– if the Diesel is on (mode 1), we test the five cases:

- (minimum power)
- (maximum power)
- such that (battery unused)
- such that (maximal charge)
- such that (maximal discharge)

We present the results of the optimization for the two test cases. The Solar Power fills the demand, with any excess power used to charge the battery. The Diesel is always off when solar power is available, and is switched on once a day during the evening peak in demand. The Diesel output is often greater than power demand: it is also used to charge the battery. The battery fills the gaps between production and demand especially at night.

**Other formulations.** We can also solve the problem using a manual control discretization for Finally, given the small number of admissible controls, we can actually model each of them with modes and remove the control altogether. The solutions obtained by the different formulations are quite close.

**References**

[1] R. Palma-Behnke, C. Benavides, F. Lanas, B. Severino, L. Reyes, J. Llanos, and D. Sez. A microgrid energy management system based on the rolling horizon strategy. IEEE Transactions on Smart Grid, 4(2):996–1006, 2013.

[2] Benjamin Heymann, J. Frédéric Bonnans, Pierre Martinon, Francisco J. Silva, Fernando Lanas, and Guillermo Jiménez-Estévez. Continuous optimal control approaches to microgrid energy management. Energy Systems, 2017. DOI