Back to Blog
Machine Learning June 29, 2026 10 min read

Optimal Active & Reactive Energy Management for Smart Microgrids (Moroccan Tariff Code)

A multi-objective optimization framework using Lexicographic Particle Swarm Optimization (PSO) and Stacking-based ANN forecasting for active and reactive power flow in smart microgrids under the Moroccan Range Tariff System.

Introduction & The Moroccan Tariff Code

Managing energy in smart microgrids (SMGs) is typically focused on active power flow over a 24-hour horizon, relying on Time-of-Use (TOU) or Peak pricing. However, the Moroccan Electrical Power Grid (EPG) operates under a unique Range Tariff System (RTS), where billing is calculated based on total cumulative consumption at the end of a 30-day month. Furthermore, reactive power consumption is usually neglected in conventional EMS, causing power factor degradation, higher transmission losses, and penalty fees.

This study introduces a novel Active and Reactive Energy Management System (AR-EMS) tailored to the Moroccan context, optimizing energy flow over a 30-day horizon with 15-minute intervals.


Proposed AR-EMS Architecture

To handle the complexity of a 30-day horizon and mitigate forecast uncertainties, the proposed AR-EMS is structured in a two-stage hierarchical model:

[ Long-Term Forecasting (30 days) ] ──> [ Long-Term Optimization (LTOL) ]
                                                   │ (Initial Planning Setpoints)
                                                   ▼
[ Short-Term Forecasting (24 hours) ] ──> [ Short-Term Optimization (STOL) ]
                                                   │ (Real-Time Adjustment)
                                                   ▼
                                        [ Converters & Microgrid Actuators ]
  1. Long-Term Optimization Layer (LTOL): Establishes a monthly planning baseline for photovoltaic (PV) generation, energy storage system (ESS), and grid exchange.
  2. Short-Term Optimization Layer (STOL): Runs daily over a rolling 24-hour window to correct LTOL deviations using high-accuracy short-term forecasts.

AI-Based Stacking Predictor

A key contribution of the paper is the forecasting pipeline for PV production, active load, and reactive load. It utilizes a Stacking Ensemble of Artificial Neural Networks (ANN). By combining multiple base learners, the meta-model reduces the Normalized Mean Absolute Error (NMAE):

  • PV Power Prediction: NMAE of 3.68%
  • Active Load Consumption: NMAE of 2.00%
  • Reactive Load Consumption: NMAE of 2.53%

Mathematical Modeling & Optimization

Multi-Objective Function

The optimization addresses multiple conflicting objectives, solved via a Lexicographic Particle Swarm Optimization (PSO):

  1. Objective 1 (Total Energy Bill): Minimizes active/reactive energy costs under the RTS, plus a customized ESS battery degradation model that accounts for both active and reactive power charging cycles.
  2. Objective 2 (Grid Cost Factor): Minimizes the Peak-to-Average Ratio (PAR) and carbon dioxide emissions ($CO_2$).

$$\min \ F = [f_1(Bill_{active}, Bill_{reactive}, Degradation_{ESS}), \ f_2(PAR, Emissions_{CO2})]$$


Key Findings & Results

The system was simulated using real-world weather and consumption profiles. The results demonstrate the superiority of the joint active-reactive management:

MetricActive-only EMS (A-EMS)Proposed AR-EMSImprovement
Active Energy BillBaseline-58.9%High
Reactive Energy BillBaseline-83.2%Extremely High
$CO_2$ EmissionsBaseline-49.3%-49.3%
Grid Power FactorBaseline+6.0%+6.0%

By incorporating reactive power management, the system avoids purchasing costly capacitor banks and minimizes line losses, showcasing a 57% reduction in the total monthly energy bill.

Smart GridPSOEnergy ManagementMoroccan Grid CodeAI Forecasting
Z

Zakaria Kassemi

Data Scientist & AI Engineer — Morocco