diff --git a/main.py b/main.py index 31c756f..eb31115 100644 --- a/main.py +++ b/main.py @@ -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( diff --git a/mip.py b/mip.py index e887ed1..0170ea4 100644 --- a/mip.py +++ b/mip.py @@ -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) # 预计算距离矩阵