Generic Framework for the Optimal Implementation of Flexibility Mechanisms in Large-Scale Smart Grids

: This paper aims to provide the smart grid research community with an open and accessible general mathematical framework to develop and implement optimal ﬂexibility mechanisms in large-scale network applications. The motivation of this paper is twofold. On the one hand, ﬂexibility mechanisms are currently a hot topic of research, which is aimed to mitigate variation and uncertainty of electricity demand and supply in decentralised grids with a high aggregated share of renewables. On the other hand, a large part of such related research is performed by heuristic methods, which are generally inefﬁcient (such methods do not guarantee optimality) and difﬁcult to extrapolate for different use cases. Alternatively, this paper presents an MPC-based (model predictive control) framework explicitly including a generic ﬂexibility mechanism, which is easy to particularise to speciﬁc strategies such as demand response, ﬂexible production and energy efﬁciency services. The proposed framework is benchmarked with other non-optimal control conﬁgurations to better show the advantages it provides. The work of this paper is completed by the implementation of a generic use case, which aims to further clarify the use of the framework and, thus, to ease its adoption by other researchers in their speciﬁc ﬂexibility mechanism applications.


Introduction and Literature Review
The implementation of flexibility mechanisms in large-scale power and energy systems [1] is a paradigm of great importance for today's industry, which has become more widespread as systems have grown [2]. Thanks to the development and integration of such automation and optimisation algorithms, the efficiency of the smart grids improves while saving costs and supporting the environment.
In particular, early attempts to solve the complex problem of scheduling in the frame of flexibility mechanisms were mostly based on heuristics, which is a methodology that does not guarantee optimality and is difficult to generalise [3]. More recently, the researchers have adopted approaches based on mathematical optimisation [4], which provides the means to achieve optimality and generalisation of the solutions.
Under such an approach, researchers usually make use of linear programming (LP) and mixed integer linear programming (MILP) that can be applied to optimise schedules based on different criteria under different operating conditions [5]. More sophisticated approaches than LP and MILP also includes hybrid mixed-integer quadratic optimisation [6], multi-objective mixed-binary linear programming [7], and other extensions of MILP [8].
However, despite the sophistication of such optimisation techniques and to the best of this authors' knowledge, most of them rely on ad hoc realisations that are particularly adapted to the specific configuration of the smart grid configuration and flexibility mechanisms under research. This fact hinders their direct extrapolation to other different configurations and usually forces the researchers to develop new formulations from scratch. As an alternative, this paper proposes a structured, general mathematical optimisation Energies 2021, 14, 8063 2 of 14 framework for the optimal and generic implementation of flexibility mechanisms that is based on model predictive control (MPC).
Historically, MPC has been one of the most applied control algorithms for large-scale systems within the industry [9][10][11]. The main reasons behind the success of such kind of controller are its well-known robustness and reliability [12,13], since MPCs are capable of reaching an optimal solution taking into account not only the current state of the system but also its future behaviour throughout the use of a model of the process [14]. Indeed, MPCs can find an adequate and optimal control behaviour even when the model is not perfectly accurate regarding the real-world process [15].
In addition, another key characteristic of MPCs is their flexibility when modelling large-scale systems. MPCs allow to mathematically subdivide, thanks to the use of statespace models (such as energy hub methods [16]), large systems into smaller subsections, usually called "nodes" [17].
Thanks to such advantages, the use of MPC for the implementation of flexibility mechanisms have the potential to provide the researchers with an efficient and easyto-generalise framework for further applications. To illustrate the potential of MPC to provide an efficient and easy-to-generalise framework for the implementation of flexibility mechanisms, the rest of this paper is organised as follows. First, Section 2 briefly introduces the concept of model predictive control to provide some contextualisation. Section 3 describes, in detail, the mathematical framework proposed by the authors along with the explicit consideration of generic flexibility mechanisms. Section 4 shortly defines a benchmarking example based on heuristics. Later, Section 5 presents a generic use case for validating the authors' proposed approach against the benchmarking example. Section 6 discusses the application of the proposed framework to the implementation of smart grids' flexibility mechanisms applications. Finally, Section 7 presents the authors' conclusions about the results achieved and insights about the upcoming works on the topic.

Model Predictive Control
Model predictive control (MPC) is an advanced process control method that has been used in large-scale systems since the 1980s [18]. MPCs are widely used in the industry since they allow to consider both the current and also the future system status in order to satisfy the system constraints and control objectives [19]. This is achieved by minimising an objective function, which allows defining the cost associated with the use of certain variables throughout a prediction time horizon [20].
At each instant, the set of future control values is calculated by following the next criteria: Minimise An objective function (also called "cost function") in terms of actions over the prediction horizon. Subject to Dynamics of the system over the prediction horizon; constraints of the system; measurement of the initial state of the system at the beginning of the current control instant.
Therefore, MPC is based on an iterative optimisation, as shown in Figure 1. At instant k, the information about the current state of the system is received, and a control strategy that minimises the cost is calculated (using a mathematical solver) for a time range from the current instant (k) to a future instant (k + H). Specifically, a solution that emerges from the current state and guides the system through a time range [k, k + H] is found (always complying with objective function cost minimisation and constraints). Then, only the next step (that corresponds to k + 1) of the control strategy is implemented. Then, when the next instant k + 1 arrives, the process starts again. the current state and guides the system through a time range , + is found (always complying with objective function cost minimisation and constraints). Then, only the next step (that corresponds to + 1) of the control strategy is implemented. Then, when the next instant + 1 arrives, the process starts again.

Proposed Approach
In the first part of this section, the mathematical framework proposed by the authors is presented and described in depth. In addition, all the variables, constraints, matrixes, and equations that define the optimisation problem are enunciated. The second part of this section introduces the flexibility mechanism and details how it is integrated into the optimisation problem.

Centralised MPC
A Centralised MPC relies on a control Agent that manages the whole large-scale system by itself. This Agent considers the state variables, inputs, outputs, constants, constraints, disturbances and dynamics of all the elements of the network to satisfy a single global objective function for the entire system. Since this MPC knows every piece of information of every node of the grid, it ensures the convergence and the optimality (global optimum) of the solution [14].
However, some past literature [18] indicated that the disadvantages of a centralised MPC include the difficulty of modelling the entire system and the complication of isolating parts of the system in case one part fails.
Nevertheless, the authors have followed some methodologies that reduce the impact of the disadvantages. First, the system has been modelled as an aggrupation of different and independent state-space models. Accordingly, the system can be easily modelled and the nodes can be rapidly isolated if necessary. Figure 2 shows a large-scale system composed of parts, also called "nodes", where each node has a set of inputs ( ), state variables ( ) and outputs ( ).

Proposed Approach
In the first part of this section, the mathematical framework proposed by the authors is presented and described in depth. In addition, all the variables, constraints, matrixes, and equations that define the optimisation problem are enunciated. The second part of this section introduces the flexibility mechanism and details how it is integrated into the optimisation problem.

Centralised MPC
A Centralised MPC relies on a control Agent that manages the whole large-scale system by itself. This Agent considers the state variables, inputs, outputs, constants, constraints, disturbances and dynamics of all the elements of the network to satisfy a single global objective function for the entire system. Since this MPC knows every piece of information of every node of the grid, it ensures the convergence and the optimality (global optimum) of the solution [14].
However, some past literature [18] indicated that the disadvantages of a centralised MPC include the difficulty of modelling the entire system and the complication of isolating parts of the system in case one part fails.
Nevertheless, the authors have followed some methodologies that reduce the impact of the disadvantages. First, the system has been modelled as an aggrupation of different and independent state-space models. Accordingly, the system can be easily modelled and the nodes can be rapidly isolated if necessary. Figure 2 shows a large-scale system composed of N parts, also called "nodes", where each node has a set of inputs u i (t), state variables x i (t) and outputs y i (t).
next instant + 1 arrives, the process starts again.

Proposed Approach
In the first part of this section, the mathematical framework proposed by the authors is presented and described in depth. In addition, all the variables, constraints, matrixes and equations that define the optimisation problem are enunciated. The second part o this section introduces the flexibility mechanism and details how it is integrated into the optimisation problem.

Centralised MPC
A Centralised MPC relies on a control Agent that manages the whole large-scale system by itself. This Agent considers the state variables, inputs, outputs, constants constraints, disturbances and dynamics of all the elements of the network to satisfy a single global objective function for the entire system. Since this MPC knows every piece of information of every node of the grid, it ensures the convergence and the optimality (global optimum) of the solution [14].
However, some past literature [18] indicated that the disadvantages of a centralised MPC include the difficulty of modelling the entire system and the complication o isolating parts of the system in case one part fails.
Nevertheless, the authors have followed some methodologies that reduce the impac of the disadvantages. First, the system has been modelled as an aggrupation of differen and independent state-space models. Accordingly, the system can be easily modelled and the nodes can be rapidly isolated if necessary. Figure 2 shows a large-scale system composed of parts, also called "nodes" where each node has a set of inputs ( ), state variables ( ) and outputs ( ).  According to the formulation: with i being the number of nodes, α the number of states of the node, β the number of inputs of the node and γ the number of outputs of the node. In general, the dynamics of each node are given by the continuous, non-linear and time-varying description: without loss of generality, each node is modelled by a discrete, linear, time-invariant description with a state-space model [21]: where: The local control problem for each node i is then written as: where k is the execution or simulation instant, l is the prediction window instant and H is the control horizon: This mathematical approach is also supported and derived from the work of other authors [14,16,21,22]. Thanks to this mathematical description of a node, a centralised MPC can be efficiently defined for a large-scale system.

Introducing the Flexibility Mechanism into the Centralised MPC
As mentioned above, this paper intends to develop and validate an MPC controller capable of working with flexibility mechanisms. A "flexibility mechanism" is the need for several nodes in a large-scale smart grid to compensate or comply with constraints together [23,24]. This means that a set of variables from different nodes of a smart grid must participate together to satisfy a specific constraint.
In general, this constraint is exemplified by ensuring that the sum of the inputs of the various nodes in the system is not greater than a specific value. For the specific application to smart grids, such constraint would be used to ensure, for example, that the combined electricity consumption of several houses does not exceed a value.
Specifically, a centralised MPC applied to a large-scale system of N nodes with a flexibility mechanism can be represented as follows (Figure 3).
This mathematical approach is also supported and derived from the work of other authors [14,16,21,22]. Thanks to this mathematical description of a node, a centralised MPC can be efficiently defined for a large-scale system.

Introducing the Flexibility Mechanism into the Centralised MPC
As mentioned above, this paper intends to develop and validate an MPC controller capable of working with flexibility mechanisms. A "flexibility mechanism" is the need for several nodes in a large-scale smart grid to compensate or comply with constraints together [23,24]. This means that a set of variables from different nodes of a smart grid must participate together to satisfy a specific constraint.
In general, this constraint is exemplified by ensuring that the sum of the inputs of the various nodes in the system is not greater than a specific value. For the specific application to smart grids, such constraint would be used to ensure, for example, that the combined electricity consumption of several houses does not exceed a value.
Specifically, a centralised MPC applied to a large-scale system of nodes with a flexibility mechanism can be represented as follows (Figure 3). Under this approach, an External Agent defines the flexibility mechanism that all the nodes must jointly satisfy. A general flexibility mechanism is defined by an upper limit ( ), which is further explained later in this section.
The diagram includes a High-Level Agent, which implements the centralised MPC and performs the calculation of the optimal control actions to solve the optimisation problem. Once the optimal control actions are computed, the High-Level Agent sends the corresponding set-points to the Low-Level Agents, which are assumed to perform the low-level control actions (and thus, can be omitted in the high-level approach discussed herein).
This way, the control problem of a large-scale system of interconnected nodes can be seen as an aggregation of the local control problems plus the constraints of the flexibility mechanisms. Its equations can be extrapolated from Equations (6)  Under this approach, an External Agent defines the flexibility mechanism that all the nodes must jointly satisfy. A general flexibility mechanism is defined by an upper limit M u (k), which is further explained later in this section.
The diagram includes a High-Level Agent, which implements the centralised MPC and performs the calculation of the optimal control actions to solve the optimisation problem. Once the optimal control actions are computed, the High-Level Agent sends the corresponding set-points to the Low-Level Agents, which are assumed to perform the lowlevel control actions (and thus, can be omitted in the high-level approach discussed herein).
This way, the control problem of a large-scale system of N interconnected nodes can be seen as an aggregation of the local control problems plus the constraints of the flexibility mechanisms. Its equations can be extrapolated from Equations (6)-(18) as follows: subject to X(k + 1 + l) = AX(k + l) + BU(k + l) where: Energies 2021, 14, 8063

Benchmark Example: Decentralised MPC
As highlighted in Section 1, one of the simplest and widely adopted methods to ensure multiple nodes in a network to comply with an overall constraint is to heuristically distribute this constraint between all the nodes of the grid. That way, each of the nodes must independently satisfy their respective assigned part of the overall constraint. In this paper, such a heuristic approach is formulated as a "Decentralised MPC" [25].
Under the decentralised MPC, the whole large-scale system is split into small subsystems. Each of these subsystems is totally independent and ignores (there is no communication between agents) the rest of the nodes. Each one has its own MPC agent, which is responsible for fulfilling the constraints, dynamics, and objective function of the model of its particular subsystem [26]. Since the nodes do not communicate with each other, the only way to fulfil the flexibility mechanisms is to distribute them heuristically among the agents. A direct way to distribute the limits imposed by the flexibility mechanism M u (k) is its equal split among the N Agents, that is, M u (k)/N.
It is easy to infer that, under such an approach, a global optimum is not guaranteed. A decentralised MPC applied to a large-scale system of N nodes with a flexibility mechanism can be represented as follows (Figure 4).   The mathematical formulation of the benchmarking decentralised MPC is derived from previous Equations (6)- (18), and results as follows:

Use Case
This section presents a use case to illustrate the implementation of the proposed formulation to a relatively simple network composed of four nodes, each of them with two inputs (u i,1 and u i,2 ), two states (x i,1 and x i,2 ) and one output (y i,1 ), as depicted in Figure 5.
The mathematical formulation of the benchmarking decentralised M from previous Equations (6)- (18), and results as follows:

Use Case
This section presents a use case to illustrate the implementation of formulation to a relatively simple network composed of four nodes, each two inputs ( , and , ), two states ( , and , ) and one output ( , ), Figure 5.

Use Case for the Centralised MPC
The application of the centralised MPC to the specific use case synthetised in Figure 6.

Use Case for the Centralised MPC
The application of the centralised MPC to the specific use case is graphically synthetised in Figure 6. The specific state-space models for each node are:  The specific state-space models for each node are: The aggregation of the individual nodes results in the following formulation: • Aggregated objective function: The aggregation of the four nodes results in the following set of overall equations: • Aggregated model: • Space of variables:

Decentralised MPC Applied to the Use Case
The application of the decentralised MPC to the specific use case is graphically synthetised in Figure 7.

Decentralised MPC Applied to the Use Case
The application of the decentralised MPC to the specific use case is graphically synthetised in Figure 7. Under this approach, there is no aggregation and each local MPC implements the local problem defined as follows: • Objective function: where is the matrix that contains the quadratic cost of each variable of the node, and is the matrix that contains the linear cost of each variable of the node. Under this approach, there is no aggregation and each local MPC implements the local problem defined as follows:

•
Objective function: where Q i is the matrix that contains the quadratic cost of each variable of the node, and f i is the matrix that contains the linear cost of each variable of the node. The use case data (Equations (30)-(33)) can be implemented in the Equations (28), (29) and (40) in such a way that: • Model: • Initial conditions (k = 0): • Space of variables: • Prediction horizon: With this, the decentralised MPC control problem is particularised for the use case.

Figures 8-10
show the results obtained from running the centralised MPC approach under the four-node use case. Specifically, Figure 8 shows the solution for the u-variables (inputs of the nodes), while Figure 9 shows the results for the y-variables (outputs of the nodes) and Figure 10 shows the evolution of the objective function cost throughout the simulation.
• Prediction horizon: With this, the decentralised MPC control problem is particularised for the use case.

Figures 8-10
show the results obtained from running the centralised MPC approach under the four-node use case. Specifically, Figure 8 shows the solution for the u-variables (inputs of the nodes), while Figure 9 shows the results for the y-variables (outputs of the nodes) and Figure 10 shows the evolution of the objective function cost throughout the simulation.    Under this optimal solution, provided by the proposed centralised MPC, the evaluation of the objective cost function is expected to decrease as quickly as possible (concretely, Figure 10 shows that the cost reaches 0 at simulation step 42). While doing so, Figure 8 shows that the constraint related to the flexibility mechanism is always fulfilled (concretely, the sum of the inputs is always below the maximum value 4). These trade-off results in the optimal sequential evolution of the outputs shown in Figure 9.  Under this optimal solution, provided by the proposed centralised MPC, the evaluation of the objective cost function is expected to decrease as quickly as possible (concretely, Figure 10 shows that the cost reaches 0 at simulation step 42). While doing so, Figure 8 shows that the constraint related to the flexibility mechanism is always fulfilled (concretely, the sum of the inputs is always below the maximum value 4). These trade-off results in the optimal sequential evolution of the outputs shown in Figure 9.
For benchmarking such an optimal solution, Figures 11-13 summarise the result of the non-optimal decentralised MPC. Specifically, Figure 11 shows the solution for the uvariables (inputs of the nodes), Figure 12 shows the results for the y-variables (outputs of the nodes), and Figure 13 shows the evolution of the sum of the local objective function costs under the decentralised MPC simulation in comparison with the aggregated objective function cost under the centralised MPC.  For benchmarking such an optimal solution, Figures 11-13 summarise the result of the non-optimal decentralised MPC. Specifically, Figure 11 shows the solution for the u-variables (inputs of the nodes), Figure 12 shows the results for the y-variables (outputs of the nodes), and Figure 13 shows the evolution of the sum of the local objective function costs under the decentralised MPC simulation in comparison with the aggregated objective function cost under the centralised MPC.  Under this optimal solution, provided by the proposed centralised MPC, the evaluation of the objective cost function is expected to decrease as quickly as possible (concretely, Figure 10 shows that the cost reaches 0 at simulation step 42). While doing so, Figure 8 shows that the constraint related to the flexibility mechanism is always fulfilled (concretely, the sum of the inputs is always below the maximum value 4). These trade-off results in the optimal sequential evolution of the outputs shown in Figure 9.
For benchmarking such an optimal solution, Figures 11-13 summarise the result of the non-optimal decentralised MPC. Specifically, Figure 11 shows the solution for the uvariables (inputs of the nodes), Figure 12 shows the results for the y-variables (outputs of the nodes), and Figure 13 shows the evolution of the sum of the local objective function costs under the decentralised MPC simulation in comparison with the aggregated objective function cost under the centralised MPC.   As expected, the evolution of the sum of the local objective function costs under the non-optimal decentralised MPC is consistently higher than the objective function cost under the optimal centralised MPC (as shown in Figure 14). Specifically, the area under the blue line in Figure 14    As expected, the evolution of the sum of the local objective function costs under the non-optimal decentralised MPC is consistently higher than the objective function cost under the optimal centralised MPC (as shown in Figure 14). Specifically, the area under the blue line in Figure 14  The higher costs associated with the non-optimal approach are caused by the infrautilisation of the inputs by the decentralised MPC, as shown in Figure 11, where the sum of the inputs is far from the limit allowed by the flexibility mechanism. This comparison clearly shows the advantage of the proposed centralised MPC over heuristically-based approaches and confirms that the automatic flexibility mechanism designed by the authors is an optimal and efficient method to define joint constraints between several nodes in a large-scale system.   As expected, the evolution of the sum of the local objective function costs under the non-optimal decentralised MPC is consistently higher than the objective function cost under the optimal centralised MPC (as shown in Figure 14). Specifically, the area under the blue line in Figure 14 (centralised MPC cost) is 3925.4, while the area under the red line in the same figure (decentralised MPC cost) is 9205.6.
The higher costs associated with the non-optimal approach are caused by the infrautilisation of the inputs by the decentralised MPC, as shown in Figure 11, where the sum of the inputs is far from the limit allowed by the flexibility mechanism. This comparison clearly shows the advantage of the proposed centralised MPC over heuristically-based approaches and confirms that the automatic flexibility mechanism designed by the authors is an optimal and efficient method to define joint constraints between several nodes in a large-scale system.  The higher costs associated with the non-optimal approach are caused by the infrautilisation of the inputs by the decentralised MPC, as shown in Figure 11, where the sum of the inputs is far from the limit allowed by the flexibility mechanism. This comparison clearly shows the advantage of the proposed centralised MPC over heuristically-based approaches and confirms that the automatic flexibility mechanism designed by the authors is an optimal and efficient method to define joint constraints between several nodes in a large-scale system.
To conclude this section, the following Table 1 summarises all the characteristics of the algorithms that have been analysed in this document.

Extrapolation of Use Case to Energy Flexibility Mechanisms
Thus far in this document, the proposed mathematical formulation has been described generally, so other researchers can easily extrapolate the formulation and conclusions achieved in this work to any kind of system. Nevertheless, the specific application of the proposed framework to energy flexibility mechanisms is straightforward.
On the one hand, the authors recommend adopting the so-called energy hub technique [16] for constructing the models of the smart grids' nodes. On the other hand, the flexibility mechanism can be directly included in the formulation, as presented in Section 3.2.
Specifically, when controlling such kind of large electrical network, it is a common practice to activate a demand response or flexibility mechanism services to ensure the overall consumption is in line with the needs of the grid. For instance, in the event of a consumption peak, the energy supplier asks different clients (nodes) of the network to temporality reduce their consumption to reduce the stress over the network. This is usually called "peak clipping". Another example can be the "load shifting", which consists of incentivising the displacement of the consumption to certain hours with lower demand favouring the equilibrium of the network, so the energy extracted from renewable sources is maximised while the one extracted from no renewable production (gas, carbon, oil, etc.) is not used.

Conclusions and Future Work
This research offers a general and effective method for automating the distribution of aggregate constraints between nodes of a large-scale system. Although the use case presented has been maintained as simple to allow for a straightforward analysis, the proposed method can be extrapolated to other more complex large-scale systems without losing effectiveness.
It was demonstrated that the designed centralised MPC achieves the optimal solution and produces more efficient results than other methods used in the control of large-scale systems with aggregate constraints between nodes. Effectiveness has been demonstrated through the benchmarking with a non-optimal decentralised MPC, which prevents communication between nodes and thus forces subdivisions of the flexibility mechanisms.
The flexibility mechanisms are optimally distributed across the instants of the prediction window in the MPC proposed by the authors (Figure 8). The performance achieved in this research presents promising results for industry sectors seeking to implement controllers in large-scale systems. One of the main characteristics of the proposed method, compared to traditional methods, is that it uses the advantages of today's technology without losing the benefits of node modelling. In this way, it is possible to reach the desired results and distribute the constraints efficiently, without complex modelling or mathematics, achieving the most optimal solutions.
The formulation and use case presented in this paper has been designed as generically as possible so that other researchers can use the proposed control algorithm in any largescale smart grid system. Furthermore, the extrapolation of the proposed framework has been discussed in Section 6 to ease the extrapolation to a specific case. Regarding future work, the authors of this paper will extend the capabilities of the control solution, specifying their use for large-scale energy systems to other use cases.