Fix MIP toggle bug: handle PuLP import gracefully

This commit is contained in:
dmy
2026-01-08 10:01:46 +08:00
parent 4230d2221d
commit 579f8866c4
2 changed files with 17 additions and 10 deletions

View File

@@ -14,11 +14,7 @@ from sklearn.cluster import KMeans
from esau_williams import design_with_esau_williams
from ga import design_with_ga
try:
from mip import design_with_mip
except ImportError:
design_with_mip = None
from mip import design_with_mip
# 设置matplotlib支持中文显示
plt.rcParams["font.sans-serif"] = ["Microsoft YaHei", "SimHei", "Arial"]
@@ -1715,7 +1711,7 @@ def compare_design_methods(
f" [GA] Cost: ¥{eval_ga['total_cost']:,.2f} | Loss: {eval_ga['total_loss']:.2f} kW | Circuits: {n_circuits_ga}"
)
if use_mip and design_with_mip:
if use_mip:
# --- Run 5: Mixed Integer Programming ---
mip_name = f"{name} (MIP)"
conns_mip, turbines_mip = design_with_mip(

17
mip.py
View File

@@ -3,7 +3,12 @@ import pandas as pd
from scipy.spatial import distance_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
from collections import defaultdict
import pulp
import random
try:
import pulp
except ImportError:
pulp = None
def design_with_mip(
@@ -32,13 +37,19 @@ def design_with_mip(
:param get_max_capacity_func: 获取最大容量函数
:return: 连接列表和带有簇信息的turbines
"""
if pulp is None:
print(
"WARNING: PuLP library not available. MIP optimization skipped, falling back to MST."
)
from main import design_with_mst
return design_with_mst(turbines, substation)
if get_max_capacity_func:
max_mw = get_max_capacity_func(cable_specs, voltage, power_factor)
else:
max_mw = 100.0 # 默认值
total_power = turbines["power"].sum()
if max_clusters is None:
max_clusters = int(np.ceil(total_power / max_mw))
n_turbines = len(turbines)