# Stochastic Predictive Energy Management of Multi-Microgrid Systems
## 3 Two-Stage Stochastic Programming with Recourse  
In the two-stage stochastic programming with recourse variables, decision variables include the first and the second-stage variables. The first-stage decision variables are determined before the realization of uncertainty while the second-stage variables are set after uncertain parameters are realized and considered as recourse variables (actions). Taking into account the second-stage variables for compensating any real-time infeasibility, the first-stage decision variables are determined with the allowance of constraint violation as a result of changing random parameters [32,33]. In other words, in this model, the constraints which include random parameters are modeled as soft constraints. To have a more quantitative point of view, consider the optimization problem illustrated in the following.  
\[min\quad C^{T}x, \tag{11}\]  
\[Ax=b, \tag{12}\]  
\[Tx+H\mu=0, \tag{13}\]  
\[x\in X. \tag{14}\]  
In (11)-(14), \(x\in R^{n_{1}}\) represents the first-stage decision variables and \(\mu\in R^{\prime}\) contains the uncertain parameters. In (12), A and b belong to the \(R^{m_{1}\times n_{1}}\) and \(R^{m_{1}}\), respectively. While the  
Figure 1: Proposed hierarchical control structure.  
constraint represented in (12) is a deterministic constraint, the random constraint of (13) includes both deterministic and random parameters where \(T\in R^{m_{2}\times n_{1}}\) and \(H\in R^{m_{2}\times r}\) are coefficient matrices.  
Since the only information about the uncertain parameters is their past behavior which could be incorporated to extract some statistical characteristics, these kinds of constraints could not be guaranteed to hold before the realization of random variables. In the two-stage stochastic programming, some slack variables \(\gamma^{+}\) and \(\gamma^{-}\) are introduced into the model as the second-stage decision variables as represented below.  
\[Tx+H\mu\leq\gamma^{+}, \tag{15}\] \[-(Tx+H\mu)\leq\gamma^{-}. \tag{16}\]  
In (15) and (16), \(\gamma^{+}\) and \(\gamma^{-}\) are slack variables, which represent corrective actions for compensating real-time infeasibilities. To deal with the violated random constraints, a second-stage or penalty function is included in the objective function. In general, the penalty function is considered in the form of \(q^{+}\gamma^{+}+q^{-}\gamma^{-}\) in which \(q^{+}\) and \(q^{-}\) are penalty coefficients.  
However, since the penalty function could only be evaluated after the realization of uncertain parameters, the second-stage cost could be replaced with an appropriate measure of risk. Thus, the first-stage decision variables should be selected in a process that encompasses possible realizations of \(\mu\) represented by a finite set of scenarios. Considering the expected value for evaluating the probable penalty cost of different realizations of the uncertain variables and assuming finite discrete probability distribution for \(\mu\), the two-stage stochastic optimization problem can be stated as follows.  
\[\underset{x,\gamma}{m}\,in C^{T}x+\sum_{j=1}^{S}\pi_{j}(q^{+}\gamma_{j}^{+}+q^{-} \gamma_{j}^{-}), \tag{17}\] \[s.t.\quad Ax=b,\] (18) \[Tx+H\mu_{j}\leq\gamma_{j}^{+},\] (19) \[-(Tx+H\mu_{j})\leq\gamma_{j}^{-},\] (20) \[x\in X,\quad\gamma_{j}^{+},\gamma_{j}^{-}\geq 0,\,j=1,...,S. \tag{21}\]  
In (17)-(21), \(S\) represents the number of scenarios and \(\pi_{j}\) shows related discrete probability of the \(j\)th scenario. Thus, in a two-stage programming problem, the objective is to decide the first-stage decision variables such that the total cost including the operation cost and expected penalty cost resulted from real-time constraint violation is minimized. Although increasing the number of scenarios will result in improving quality of the solution, a long computational time might be required. Consequently, the solution quality should be traded against tractability of the problem.