Posted on April 17, 2025 by SmartGridEnthusiast
Smart grids represent the future of electricity distribution, offering intelligent ways to balance power supply and demand.
Today, I’ll walk you through a practical example of how we can optimize this balance using Python and mathematical programming.
Let’s tackle a realistic scenario: managing multiple power sources (traditional and renewable) while meeting consumer demand throughout the day, all while minimizing costs and ensuring stability.
The Smart Grid Optimization Problem
In our example, we’ll model a smart grid with:
- Multiple power generators (coal, gas, solar, wind)
- Varying demand throughout a 24-hour period
- Different production costs and constraints
- Energy storage capabilities
The mathematical formulation of our problem involves minimizing the total cost while satisfying all constraints:
$$ \min \sum_{t=1}^{T} \sum_{g=1}^{G} C_g \cdot P_{g,t} + \sum_{t=1}^{T} C_{storage} \cdot |S_t - S_{t-1}| $$
Subject to:
$$ \sum_{g=1}^{G} P_{g,t} + S_{t-1} - S_t = D_t \quad \forall t \in T $$
$$ P_{g,t}^{min} \leq P_{g,t} \leq P_{g,t}^{max} \quad \forall g \in G, t \in T $$
$$ 0 \leq S_t \leq S^{max} \quad \forall t \in T $$
Where:
- $P_{g,t}$ is the power output of generator $g$ at time $t$
- $C_g$ is the cost coefficient for generator $g$
- $S_t$ is the stored energy at time $t$
- $D_t$ is the demand at time $t$
Let’s implement this model using Python with the PuLP library for optimization:
1 | import numpy as np |
Code Explanation
Let’s break down this implementation step by step:
1. Problem Setup
First, we define our smart grid parameters:
- Four generator types (Coal, Natural Gas, Solar, Wind)
- Cost structures for each generator
- Operational constraints (min/max output)
- Time-dependent availability for renewables
- Demand curve throughout the day
- Energy storage capabilities
The solar availability follows a realistic daily pattern with peak availability during mid-day hours, while wind power has some natural variability.
Demand follows a common pattern with morning and evening peaks.
2. Mathematical Model Implementation Using PuLP
We use the PuLP library to define our linear programming model:
Decision Variables:
power_output[g,t]: How much power each generator produces each hourstorage[t]: Battery storage level at each hourcharging[t]anddischarging[t]: Energy flow in/out of storage
Objective Function:
Minimize the total cost of generation plus storage operations.Constraints:
- Power balance at each hour (supply = demand)
- Generator output limitations
- Storage capacity constraints
- Storage dynamics (tracking energy flow)
3. Solving the Model
The PuLP solver (CBC) finds the optimal solution that minimizes costs while meeting all constraints.
This shows how different power sources should be dispatched throughout the day.
4. Visualization and Analysis
We generate comprehensive visualizations to understand the solution:
- A stacked bar chart showing how each generator contributes hourly
- A line plot tracking storage levels
- A cost breakdown by energy source
Results Analysis
Let’s examine the graphs to understand the optimal solution:
Status: Optimal Total Cost: $251791.35


Hourly Power Generation and Storage:
Coal Natural Gas Solar Wind Storage Demand
0 50.0 20.00 10.00 33.00 50.00 113
1 50.0 20.00 10.00 65.62 85.62 110
2 50.0 20.00 10.00 68.78 120.40 114
3 50.0 20.00 10.00 72.15 145.56 127
4 50.0 20.00 10.00 70.66 146.22 150
5 50.0 20.00 10.00 73.05 120.27 179
6 50.0 20.00 10.00 72.70 58.97 214
7 50.0 29.75 30.71 81.58 0.00 251
8 50.0 110.00 50.00 76.00 0.00 286
9 50.0 124.96 66.57 73.47 0.00 315
10 50.0 145.47 79.28 60.25 0.00 335
11 50.0 138.88 87.27 65.85 0.00 342
12 50.0 138.01 90.00 57.99 0.00 336
13 50.0 134.39 87.27 44.34 0.00 316
14 50.0 117.54 79.28 38.18 0.00 285
15 50.0 95.20 66.57 33.23 0.00 245
16 50.0 69.13 50.00 30.87 0.00 200
17 50.0 42.18 30.71 31.11 0.00 154
18 50.0 24.82 10.00 29.18 0.00 114
19 50.0 20.00 10.00 3.00 0.00 83
20 50.0 20.00 0.00 0.00 7.00 63
21 50.0 20.00 0.00 0.00 20.00 57
22 50.0 20.00 0.00 0.00 26.00 64
23 50.0 20.00 10.00 4.00 26.00 84
Percentage of Renewable Energy: 42.16%
Maximum Storage Used: 146.22 MWh
Storage Utilization: 97.48%
Generator Dispatch Pattern
Looking at the hourly power generation chart, we can observe several key insights:
Base Load Operation: Coal plants, with their higher minimum output requirements but high operational costs, are used primarily as base load during peak demand hours.
Mid-merit Operation: Natural gas generators, with more flexibility and moderate costs, are ramped up and down to follow the demand curve.
Renewable Integration: Solar energy is maximized during daylight hours, while wind power is used whenever available, as they have the lowest operational costs.
Peak Management: The combination of all sources plus storage is optimized to meet peak demand periods in the morning and evening.
Storage Utilization
The storage level chart shows:
Charge/Discharge Cycles: The storage system charges during low-demand periods (especially when renewable generation is high) and discharges during peak demand periods.
Peak Shaving: The storage helps “shave” demand peaks by providing additional power, reducing the need for expensive peaking generators.
Renewable Smoothing: Storage helps balance the intermittent nature of renewable sources, storing excess generation for later use.
Cost Distribution
The cost breakdown reveals:
Generation Mix Economics: Despite their lower capacity factors, renewables contribute significantly to cost savings.
Storage Economics: The cost of storage operations is offset by the system’s ability to shift energy from low-cost generation periods to high-demand periods.
Practical Applications
This optimization approach can be applied in several real-world scenarios:
Microgrid Management: Isolated communities can optimize their local generation mix.
Utility-Scale Operations: Power companies can optimize their generation fleet.
Renewable Integration Planning: Grid operators can determine optimal storage capacity to accommodate increasing renewable penetration.
Demand Response Programs: Utilities can quantify the value of flexible demand.
Conclusion
Our Python model demonstrates how mathematical optimization can effectively balance power supply and demand in a smart grid environment.
By accounting for the specific characteristics of different generation sources, demand patterns, and storage capabilities, grid operators can minimize costs while ensuring reliable electricity supply.
As renewable energy sources continue to grow in importance, these optimization techniques will become increasingly valuable for managing the complex dynamics of modern power systems.

























