-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_optimizer.py
More file actions
309 lines (257 loc) · 10.2 KB
/
grid_optimizer.py
File metadata and controls
309 lines (257 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
Improved Distribution Grid Optimization using Genetic Algorithm
"""
import pandas as pd
import numpy as np
import random
from typing import Tuple, List
from utils import run_time_series
class GridOptimizer:
"""
Genetic Algorithm optimizer for DER and load allocation in distribution grids.
"""
def __init__(
self,
gen_data: pd.DataFrame,
load_data: pd.DataFrame,
net,
population_size: int = 20,
mutation_rate: float = 0.15,
elite_size: int = 2,
max_generations: int = 50,
convergence_threshold: float = 0.001,
verbose: bool = True
):
"""
Initialize the grid optimizer.
Parameters:
-----------
gen_data : pd.DataFrame
Generation data for all households
load_data : pd.DataFrame
Load data for all households
net : pandapower network
The pandapower network object
population_size : int
Number of individuals in population
mutation_rate : float
Probability of mutation (0-1)
elite_size : int
Number of best individuals to preserve
max_generations : int
Maximum number of generations to run
convergence_threshold : float
Stop if improvement is less than this threshold
verbose : bool
Print progress information
"""
self.gen_data = gen_data
self.load_data = load_data
self.net = net
self.population_size = population_size
self.mutation_rate = mutation_rate
self.elite_size = elite_size
self.max_generations = max_generations
self.convergence_threshold = convergence_threshold
self.verbose = verbose
self.n_households = len(gen_data.columns)
# Best solution tracking
self.best_gen_order = None
self.best_load_order = None
self.best_fitness = float('inf')
self.fitness_history = []
def evaluate_fitness(self, gen_order: List[int], load_order: List[int]) -> float:
"""
Evaluate fitness of a solution (lower is better).
Returns sum of maximum line loading across all time steps.
"""
try:
gen_order_idx = pd.Index(gen_order)
load_order_idx = pd.Index(load_order)
res_ext, res_lines = run_time_series(
self.gen_data,
self.load_data,
self.net,
index_order_gen=gen_order_idx,
index_order_load=load_order_idx
)
# Fitness = sum of max loading per time step (lower is better)
fitness = res_lines.max(axis=1).sum()
return fitness
except Exception as e:
if self.verbose:
print(f"Error evaluating fitness: {e}")
return float('inf')
def initialize_population(self) -> Tuple[List[List[int]], List[List[int]]]:
"""
Create initial random population.
"""
gen_population = []
load_population = []
base_order = list(range(self.n_households))
for _ in range(self.population_size):
gen_order = base_order.copy()
load_order = base_order.copy()
random.shuffle(gen_order)
random.shuffle(load_order)
gen_population.append(gen_order)
load_population.append(load_order)
return gen_population, load_population
def crossover(self, parent1: List[int], parent2: List[int]) -> List[int]:
"""
Order crossover (OX) - preserves relative order and ensures all elements present.
"""
size = len(parent1)
start = random.randint(0, size - 2)
end = random.randint(start + 1, size)
# Take substring from parent1
child = [-1] * size
child[start:end] = parent1[start:end]
# Fill remaining positions from parent2
current_pos = end % size
for gene in parent2:
if gene not in child:
child[current_pos] = gene
current_pos = (current_pos + 1) % size
return child
def mutate(self, order: List[int]) -> List[int]:
"""
Swap mutation - randomly swap two positions.
"""
order = order.copy()
if random.random() < self.mutation_rate:
idx1, idx2 = random.sample(range(len(order)), 2)
order[idx1], order[idx2] = order[idx2], order[idx1]
return order
def select_parents(
self,
population_gen: List[List[int]],
population_load: List[List[int]],
fitness_scores: List[float]
) -> Tuple[List[int], List[int], List[int], List[int]]:
"""
Tournament selection - select 2 parents.
"""
tournament_size = 3
def tournament():
tournament_indices = random.sample(range(len(population_gen)), tournament_size)
tournament_fitness = [fitness_scores[i] for i in tournament_indices]
winner_idx = tournament_indices[tournament_fitness.index(min(tournament_fitness))]
return winner_idx
parent1_idx = tournament()
parent2_idx = tournament()
return (
population_gen[parent1_idx],
population_load[parent1_idx],
population_gen[parent2_idx],
population_load[parent2_idx]
)
def evolve_generation(
self,
population_gen: List[List[int]],
population_load: List[List[int]],
fitness_scores: List[float]
) -> Tuple[List[List[int]], List[List[int]]]:
"""
Create next generation using elitism, crossover, and mutation.
"""
# Sort by fitness (lower is better)
sorted_indices = np.argsort(fitness_scores)
new_pop_gen = []
new_pop_load = []
# Elitism - keep best individuals
for i in range(self.elite_size):
idx = sorted_indices[i]
new_pop_gen.append(population_gen[idx].copy())
new_pop_load.append(population_load[idx].copy())
# Create offspring
while len(new_pop_gen) < self.population_size:
# Select parents
p1_gen, p1_load, p2_gen, p2_load = self.select_parents(
population_gen, population_load, fitness_scores
)
# Crossover
child_gen = self.crossover(p1_gen, p2_gen)
child_load = self.crossover(p1_load, p2_load)
# Mutation
child_gen = self.mutate(child_gen)
child_load = self.mutate(child_load)
new_pop_gen.append(child_gen)
new_pop_load.append(child_load)
return new_pop_gen[:self.population_size], new_pop_load[:self.population_size]
def optimize(self) -> Tuple[List[int], List[int], float]:
"""
Run genetic algorithm optimization.
Returns:
--------
best_gen_order : List[int]
Optimal generator allocation
best_load_order : List[int]
Optimal load allocation
best_fitness : float
Best fitness value achieved
"""
if self.verbose:
print(f"Starting genetic algorithm optimization...")
print(f"Population size: {self.population_size}")
print(f"Max generations: {self.max_generations}")
print(f"Mutation rate: {self.mutation_rate}")
print("-" * 60)
# Initialize population
population_gen, population_load = self.initialize_population()
# Track best solution
self.best_fitness = float('inf')
generations_without_improvement = 0
for generation in range(self.max_generations):
# Evaluate fitness for entire population
fitness_scores = []
for gen_order, load_order in zip(population_gen, population_load):
fitness = self.evaluate_fitness(gen_order, load_order)
fitness_scores.append(fitness)
# Find best in current generation
min_fitness_idx = np.argmin(fitness_scores)
current_best_fitness = fitness_scores[min_fitness_idx]
# Update global best
improvement = self.best_fitness - current_best_fitness
if current_best_fitness < self.best_fitness:
self.best_fitness = current_best_fitness
self.best_gen_order = population_gen[min_fitness_idx].copy()
self.best_load_order = population_load[min_fitness_idx].copy()
generations_without_improvement = 0
else:
generations_without_improvement += 1
self.fitness_history.append(current_best_fitness)
if self.verbose and generation % 5 == 0:
print(f"Generation {generation:3d}: Best Fitness = {current_best_fitness:.4f}, "
f"Improvement = {improvement:.6f}")
# Check convergence
if improvement < self.convergence_threshold and generations_without_improvement >= 10:
if self.verbose:
print(f"\nConverged at generation {generation}")
break
# Evolve to next generation
population_gen, population_load = self.evolve_generation(
population_gen, population_load, fitness_scores
)
if self.verbose:
print("-" * 60)
print(f"Optimization complete!")
print(f"Best fitness: {self.best_fitness:.4f}")
print(f"Best gen order: {self.best_gen_order}")
print(f"Best load order: {self.best_load_order}")
return self.best_gen_order, self.best_load_order, self.best_fitness
def plot_fitness_history(self):
"""
Plot fitness evolution over generations.
"""
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plt.plot(self.fitness_history, linewidth=2)
plt.xlabel('Generation', fontsize=12)
plt.ylabel('Best Fitness (Total Line Loading)', fontsize=12)
plt.title('Genetic Algorithm Convergence', fontsize=14, fontweight='bold')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('optimization_history.png', dpi=150)
plt.show()
return plt.gcf()