# Towards the Real-World Deployment of a Smart Home EMS: A DP Implementation on the Raspberry Pi
## 4 Embedded Implementation of the Chosen EMS
### Dynamic Programming Algorithm  
The _Planning_ and _Online Replanning_ problems of the chosen EMS were initially formulated and solved as MILP problems in [33]. However, in the present work, they have been reformulated using Dynamic Programming (DP) to remove software dependencies from third-party solvers and to simplify the overall implementation process. The algorithm for solving these problems has been designed following the approach presented in [43]. In particular, the state of the system corresponds to the battery SOC, which must be discretized using a suitable step size. The _Planning_ stage problem can be formulated as a DP problem if it is framed as the problem of choosing the optimal policy that reaches the best final state from an initial state in a limited time horizon. At each time step and state, the system can choose among a set of actions, i.e., the power reference values for the battery. This choice results in a state transition from a discretized SOC value to another. The _Planning_ stage problem, therefore, consists in finding the best path from the initial state to a final state. From Equation (2) it is possible to obtain the cost of each action, i.e., the cost of a state transition. In fact, from a given power reference for the battery, the corresponding power exchanged with the grid (and thus the CF) can be obtained through power balance constraints. Furthermore, the optimization problem constraints define the admissibility of an action. The problem, as just described, can be represented using a graph such as that in Figure 3. Graph vertices represent system states arranged in vertical layers according to the time step; edges account \begin{table}
\begin{tabular}{l l l l} \hline \hline
**User** & **Parameter** & **Value** & **Description** \\ \hline all & \(C_{buy}\) & 0.0921 (\(\xi\)/KWh) & price of purchased energy \\ all & \(C_{cell}\) & 0.0765 (\(\xi\)/KWh) & price of sold energy \\ all & \(V_{n}\) & 200 (V) & battery nominal voltage \\ all & \(\eta\) & 0.95 & battery efficiency \\ all & \(SOC_{min}\) & 20\% & lower bound for battery SOC \\ all & \(SOC_{max}\) & 100\% & upper bound for battery SOC \\ all & \(SOC_{start}\) & 80\% & initial value of battery SOC \\ A, C & \(P_{xg}\) & 3 (kW) & maximum grid contractual power \\ B, D & \(P_{xg}\) & 4 (kW) & maximum grid contractual power \\ A, B & \(P_{xp}\) & 3 (kW) & maximum power of the PV generator \\ C & \(P_{xw}\) & 3 (kW) & maximum power of the \(\mu\)WECS \\ D & \(P_{xw}\) & 1.5 (kW) & maximum power of the \(\mu\)WECS \\ A, C & \(C_{b}\) & 4.6 (kWh) & battery capacity \\ B & \(C_{b}\) & 3.8 (kWh) & battery capacity \\ D & \(C_{b}\) & 2.3 (kWh) & battery capacity \\ A, C & \(P_{xb}\) & 1.5 (kW) & maximum battery charge/discharge power \\ B & \(P_{xb}\) & 1.24 (kW) & maximum battery charge/discharge power \\ D & \(P_{xb}\) & 0.75 (kW) & maximum battery charge/discharge power \\ \hline \hline \end{tabular}
\end{table} 
Table 1: System parameters. for the cost of the action that corresponds to the state transition between the states they link. An edge exists only if the corresponding state transition satisfies the constraints of the problem. Such a graph is generated as described in Algorithm 1. With reference to this graph, the best policy corresponds to the choice of the shortest path from the initial state to one of the vertices associated with the last time step of the planning period. Considering that edges can have negative values (negative CF corresponds to power sold to the utility), the shortest path problem can be solved using Bellman-Ford algorithm [44]. This algorithm computes the shortest paths from a single source vertex (the initial SOC) to all the other vertices in the graph. Then, among the vertices corresponding to the last time step of the planning period, the system chooses the vertex with the shortest path as the target state. In addition, it is worth pointing out that the check for negative cycles in Bellman-Ford algorithm can be skipped since the graph never contains cycles.