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

19
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,14 +37,20 @@ 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))
max_clusters = int(np.ceil(total_power / max_mw))
n_turbines = len(turbines)
# 预计算距离矩阵