Implement genetic algorithm for collector layout optimization
This commit is contained in:
257
main.py
257
main.py
@@ -1,6 +1,7 @@
|
||||
import argparse
|
||||
import math
|
||||
import os
|
||||
import random
|
||||
from collections import defaultdict
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -12,6 +13,7 @@ from scipy.spatial import distance_matrix
|
||||
from sklearn.cluster import KMeans
|
||||
|
||||
from esau_williams import design_with_esau_williams
|
||||
from ga import design_with_ga
|
||||
|
||||
# 设置matplotlib支持中文显示
|
||||
plt.rcParams["font.sans-serif"] = ["Microsoft YaHei", "SimHei", "Arial"]
|
||||
@@ -654,9 +656,163 @@ def design_with_rotational_sweep(
|
||||
|
||||
return final_connections, turbines
|
||||
|
||||
# 预计算距离矩阵
|
||||
all_coords = np.vstack([substation, turbines[["x", "y"]].values])
|
||||
dist_matrix_full = distance_matrix(all_coords, all_coords)
|
||||
|
||||
def fitness(chromosome):
|
||||
cluster_assign = chromosome
|
||||
clusters = defaultdict(list)
|
||||
for i, c in enumerate(cluster_assign):
|
||||
clusters[c].append(i)
|
||||
|
||||
connections = []
|
||||
for c, members in clusters.items():
|
||||
if len(members) == 0:
|
||||
continue
|
||||
coords = turbines.iloc[members][["x", "y"]].values
|
||||
if len(members) > 1:
|
||||
dm = distance_matrix(coords, coords)
|
||||
mst = minimum_spanning_tree(dm).toarray()
|
||||
for i in range(len(members)):
|
||||
for j in range(len(members)):
|
||||
if mst[i, j] > 0:
|
||||
connections.append(
|
||||
(
|
||||
f"turbine_{members[i]}",
|
||||
f"turbine_{members[j]}",
|
||||
mst[i, j],
|
||||
)
|
||||
)
|
||||
# 连接到升压站
|
||||
dists = [dist_matrix_full[0, m + 1] for m in members]
|
||||
closest = members[np.argmin(dists)]
|
||||
connections.append((f"turbine_{closest}", "substation", min(dists)))
|
||||
|
||||
eval_res = evaluate_design(
|
||||
turbines,
|
||||
connections,
|
||||
substation,
|
||||
cable_specs,
|
||||
is_offshore=False,
|
||||
method_name="GA",
|
||||
voltage=voltage,
|
||||
power_factor=power_factor,
|
||||
)
|
||||
if system_params:
|
||||
res_list = total_investment(
|
||||
[
|
||||
{
|
||||
"cost": eval_res["total_cost"],
|
||||
"loss": eval_res["total_loss"],
|
||||
"eval": eval_res,
|
||||
}
|
||||
],
|
||||
system_params,
|
||||
)
|
||||
return res_list[0]["total_cost_npv"]
|
||||
return eval_res["total_cost"]
|
||||
|
||||
def init_individual():
|
||||
assign = np.zeros(n_turbines, dtype=int)
|
||||
cluster_powers = np.zeros(max_clusters)
|
||||
for i in range(n_turbines):
|
||||
p = turbines.iloc[i]["power"]
|
||||
possible = [
|
||||
c for c in range(max_clusters) if cluster_powers[c] + p <= max_mw
|
||||
]
|
||||
if possible:
|
||||
c = random.choice(possible)
|
||||
else:
|
||||
c = random.randint(0, max_clusters - 1)
|
||||
assign[i] = c
|
||||
cluster_powers[c] += p
|
||||
return assign.tolist()
|
||||
|
||||
population = [init_individual() for _ in range(pop_size)]
|
||||
best = None
|
||||
best_fitness = float("inf")
|
||||
|
||||
for gen in range(generations):
|
||||
fitnesses = [fitness(ind) for ind in population]
|
||||
min_fit = min(fitnesses)
|
||||
if min_fit < best_fitness:
|
||||
best_fitness = min_fit
|
||||
best = population[fitnesses.index(min_fit)].copy()
|
||||
|
||||
def tournament(size=3):
|
||||
candidates = random.sample(list(zip(population, fitnesses)), size)
|
||||
return min(candidates, key=lambda x: x[1])[0]
|
||||
|
||||
selected = [tournament() for _ in range(pop_size)]
|
||||
|
||||
new_pop = []
|
||||
for i in range(0, pop_size, 2):
|
||||
p1 = selected[i]
|
||||
p2 = selected[i + 1] if i + 1 < pop_size else selected[0]
|
||||
if random.random() < 0.8:
|
||||
point = random.randint(1, n_turbines - 1)
|
||||
child1 = p1[:point] + p2[point:]
|
||||
child2 = p2[:point] + p1[point:]
|
||||
else:
|
||||
child1, child2 = p1.copy(), p2.copy()
|
||||
new_pop.extend([child1, child2])
|
||||
|
||||
for ind in new_pop:
|
||||
if random.random() < 0.1:
|
||||
idx = random.randint(0, n_turbines - 1)
|
||||
old_c = ind[idx]
|
||||
new_c = random.randint(0, max_clusters - 1)
|
||||
ind[idx] = new_c
|
||||
cluster_powers = defaultdict(float)
|
||||
for j, c in enumerate(ind):
|
||||
cluster_powers[c] += turbines.iloc[j]["power"]
|
||||
if max(cluster_powers.values()) > max_mw:
|
||||
ind[idx] = max_clusters
|
||||
max_clusters += 1
|
||||
|
||||
elites = sorted(zip(population, fitnesses), key=lambda x: x[1])[
|
||||
: int(0.1 * pop_size)
|
||||
]
|
||||
new_pop[: len(elites)] = [e[0] for e in elites]
|
||||
population = new_pop[:pop_size]
|
||||
|
||||
# 解码最佳个体
|
||||
cluster_assign = best
|
||||
clusters = defaultdict(list)
|
||||
for i, c in enumerate(cluster_assign):
|
||||
clusters[c].append(i)
|
||||
|
||||
connections = []
|
||||
for c, members in clusters.items():
|
||||
if len(members) == 0:
|
||||
continue
|
||||
coords = turbines.iloc[members][["x", "y"]].values
|
||||
if len(members) > 1:
|
||||
dm = distance_matrix(coords, coords)
|
||||
mst = minimum_spanning_tree(dm).toarray()
|
||||
for i in range(len(members)):
|
||||
for j in range(len(members)):
|
||||
if mst[i, j] > 0:
|
||||
connections.append(
|
||||
(
|
||||
f"turbine_{members[i]}",
|
||||
f"turbine_{members[j]}",
|
||||
mst[i, j],
|
||||
)
|
||||
)
|
||||
dists = [dist_matrix_full[0, m + 1] for m in members]
|
||||
closest = members[np.argmin(dists)]
|
||||
connections.append((f"turbine_{closest}", "substation", min(dists)))
|
||||
|
||||
turbines["cluster"] = cluster_assign
|
||||
return connections, turbines
|
||||
|
||||
|
||||
# 4. 获取电缆最大容量(MW)
|
||||
def get_max_cable_capacity_mw(cable_specs, voltage=VOLTAGE_LEVEL, power_factor=POWER_FACTOR):
|
||||
def get_max_cable_capacity_mw(
|
||||
cable_specs, voltage=VOLTAGE_LEVEL, power_factor=POWER_FACTOR
|
||||
):
|
||||
"""
|
||||
根据电缆规格计算最大承载功率
|
||||
:param cable_specs: 电缆规格列表 list of tuples,或者直接是最大功率数值(MW)
|
||||
@@ -991,7 +1147,11 @@ def export_to_excel(connections_details, filename):
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
# 汇总统计
|
||||
n_circuits = sum(1 for conn in connections_details if conn["source"] == "substation" or conn["target"] == "substation")
|
||||
n_circuits = sum(
|
||||
1
|
||||
for conn in connections_details
|
||||
if conn["source"] == "substation" or conn["target"] == "substation"
|
||||
)
|
||||
summary = {
|
||||
"Total Cost (¥)": df["Cost (¥)"].sum(),
|
||||
"Total Effective Length (m)": df["Effective Length (m)"].sum(),
|
||||
@@ -1023,7 +1183,11 @@ def export_all_scenarios_to_excel(results, filename):
|
||||
summary_data = []
|
||||
for res in results:
|
||||
# 获取回路数 (通过统计从升压站发出的连接)
|
||||
n_circuits = sum(1 for conn in res["eval"]["details"] if conn["source"] == "substation" or conn["target"] == "substation")
|
||||
n_circuits = sum(
|
||||
1
|
||||
for conn in res["eval"]["details"]
|
||||
if conn["source"] == "substation" or conn["target"] == "substation"
|
||||
)
|
||||
|
||||
summary_data.append(
|
||||
{
|
||||
@@ -1230,7 +1394,11 @@ def visualize_design(
|
||||
|
||||
# 7. 主函数:比较两种设计方法
|
||||
def compare_design_methods(
|
||||
excel_path=None, n_clusters_override=None, interactive=True, plot_results=True
|
||||
excel_path=None,
|
||||
n_clusters_override=None,
|
||||
interactive=True,
|
||||
plot_results=True,
|
||||
use_ga=False,
|
||||
):
|
||||
"""
|
||||
比较MST和三种电缆方案下的K-means设计方法
|
||||
@@ -1411,7 +1579,11 @@ def compare_design_methods(
|
||||
voltage=voltage,
|
||||
power_factor=power_factor,
|
||||
)
|
||||
n_circuits_base = sum(1 for d in eval_base["details"] if d["source"] == "substation" or d["target"] == "substation")
|
||||
n_circuits_base = sum(
|
||||
1
|
||||
for d in eval_base["details"]
|
||||
if d["source"] == "substation" or d["target"] == "substation"
|
||||
)
|
||||
comparison_results.append(
|
||||
{
|
||||
"name": base_name,
|
||||
@@ -1441,7 +1613,11 @@ def compare_design_methods(
|
||||
voltage=voltage,
|
||||
power_factor=power_factor,
|
||||
)
|
||||
n_circuits_rot = sum(1 for d in eval_rot["details"] if d["source"] == "substation" or d["target"] == "substation")
|
||||
n_circuits_rot = sum(
|
||||
1
|
||||
for d in eval_rot["details"]
|
||||
if d["source"] == "substation" or d["target"] == "substation"
|
||||
)
|
||||
comparison_results.append(
|
||||
{
|
||||
"name": rot_name,
|
||||
@@ -1471,7 +1647,11 @@ def compare_design_methods(
|
||||
voltage=voltage,
|
||||
power_factor=power_factor,
|
||||
)
|
||||
n_circuits_ew = sum(1 for d in eval_ew["details"] if d["source"] == "substation" or d["target"] == "substation")
|
||||
n_circuits_ew = sum(
|
||||
1
|
||||
for d in eval_ew["details"]
|
||||
if d["source"] == "substation" or d["target"] == "substation"
|
||||
)
|
||||
comparison_results.append(
|
||||
{
|
||||
"name": ew_name,
|
||||
@@ -1486,6 +1666,49 @@ def compare_design_methods(
|
||||
f" [Esau-Williams] Cost: ¥{eval_ew['total_cost']:,.2f} | Loss: {eval_ew['total_loss']:.2f} kW | Circuits: {n_circuits_ew}"
|
||||
)
|
||||
|
||||
if use_ga:
|
||||
# --- Run 4: Genetic Algorithm ---
|
||||
ga_name = f"{name} (GA)"
|
||||
conns_ga, turbines_ga = design_with_ga(
|
||||
turbines.copy(),
|
||||
substation,
|
||||
current_specs,
|
||||
voltage,
|
||||
power_factor,
|
||||
system_params,
|
||||
evaluate_func=evaluate_design,
|
||||
total_invest_func=total_investment,
|
||||
get_max_capacity_func=get_max_cable_capacity_mw,
|
||||
)
|
||||
eval_ga = evaluate_design(
|
||||
turbines,
|
||||
conns_ga,
|
||||
substation,
|
||||
cable_specs=current_specs,
|
||||
is_offshore=is_offshore,
|
||||
method_name=ga_name,
|
||||
voltage=voltage,
|
||||
power_factor=power_factor,
|
||||
)
|
||||
n_circuits_ga = sum(
|
||||
1
|
||||
for d in eval_ga["details"]
|
||||
if d["source"] == "substation" or d["target"] == "substation"
|
||||
)
|
||||
comparison_results.append(
|
||||
{
|
||||
"name": ga_name,
|
||||
"cost": eval_ga["total_cost"],
|
||||
"loss": eval_ga["total_loss"],
|
||||
"eval": eval_ga,
|
||||
"turbines": turbines_ga,
|
||||
"specs": current_specs,
|
||||
}
|
||||
)
|
||||
print(
|
||||
f" [GA] Cost: ¥{eval_ga['total_cost']:,.2f} | Loss: {eval_ga['total_loss']:.2f} kW | Circuits: {n_circuits_ga}"
|
||||
)
|
||||
|
||||
# 记录最佳
|
||||
if eval_rot["total_cost"] < best_cost:
|
||||
best_cost = eval_rot["total_cost"]
|
||||
@@ -1500,7 +1723,11 @@ def compare_design_methods(
|
||||
# 可视化 (只画 Base 版本)
|
||||
ax_idx = i + 1
|
||||
if plot_results and ax_idx < 4:
|
||||
n_circuits = sum(1 for d in eval_base["details"] if d["source"] == "substation" or d["target"] == "substation")
|
||||
n_circuits = sum(
|
||||
1
|
||||
for d in eval_base["details"]
|
||||
if d["source"] == "substation" or d["target"] == "substation"
|
||||
)
|
||||
title = f"{base_name} ({n_circuits} circuits)\nCost: ¥{eval_base['total_cost'] / 10000:.2f}万"
|
||||
visualize_design(
|
||||
turbines_base, substation, eval_base["details"], title, ax=axes[ax_idx]
|
||||
@@ -1540,11 +1767,17 @@ def compare_design_methods(
|
||||
for i, res in enumerate(comparison_results):
|
||||
if res["cost"] < comparison_results[best_idx]["cost"]:
|
||||
best_idx = i
|
||||
|
||||
|
||||
# 获取回路数 (通过统计从升压站发出的连接)
|
||||
n_circuits = sum(1 for conn in res["eval"]["details"] if conn["source"] == "substation" or conn["target"] == "substation")
|
||||
|
||||
print(f" {i + 1}. {res['name']} - Cost: ¥{res['cost']:,.2f} | Circuits: {n_circuits}")
|
||||
n_circuits = sum(
|
||||
1
|
||||
for conn in res["eval"]["details"]
|
||||
if conn["source"] == "substation" or conn["target"] == "substation"
|
||||
)
|
||||
|
||||
print(
|
||||
f" {i + 1}. {res['name']} - Cost: ¥{res['cost']:,.2f} | Circuits: {n_circuits}"
|
||||
)
|
||||
|
||||
print(f"推荐方案: {comparison_results[best_idx]['name']} (默认)")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user