diff --git a/main.py b/main.py index b31186d..a9c5c85 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ from collections import defaultdict import networkx as nx import math import argparse +import os # 设置matplotlib支持中文显示 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'SimHei', 'Arial'] @@ -66,6 +67,7 @@ def generate_wind_farm_data(n_turbines=30, seed=42, layout='random', spacing=800 'x': x_coords, 'y': y_coords, 'power': power_ratings, + 'platform_height': np.zeros(n_turbines), 'cumulative_power': np.zeros(n_turbines) # 用于后续计算 }) @@ -410,12 +412,22 @@ def get_max_cable_capacity_mw(cable_specs=None): 异常: Exception: 当未提供 cable_specs 时抛出,提示截面不满足。 """ - if cable_specs: - # 从所有电缆规格中找到最大的额定电流容量 - max_current_capacity = max(spec[1] for spec in cable_specs) - else: - # 如果没有传入电缆规格,通常意味着已尝试过所有规格但仍不满足需求 - raise Exception("没有提供电缆参数") + if cable_specs is None: + # Default cable specs if not provided (same as in evaluate_design) + cable_specs = [ + (35, 150, 0.524, 80), + (70, 215, 0.268, 120), + (95, 260, 0.193, 150), + (120, 295, 0.153, 180), + (150, 330, 0.124, 220), + (185, 370, 0.0991, 270), + (240, 425, 0.0754, 350), + (300, 500, 0.0601, 450), + (400, 580, 0.0470, 600) + ] + + # 从所有电缆规格中找到最大的额定电流容量 + max_current_capacity = max(spec[1] for spec in cable_specs) # 计算最大功率:P = √3 * U * I * cosφ # 这里假设降额系数为 1 (不降额) @@ -593,15 +605,32 @@ def export_to_dxf(turbines, substation, connections_details, filename): # 1. 建立图层 doc.layers.add('Substation', color=1) # Red doc.layers.add('Turbines', color=5) # Blue - doc.layers.add('Cable_35mm', color=3) # Green - doc.layers.add('Cable_70mm', color=130) - doc.layers.add('Cable_95mm', color=150) - doc.layers.add('Cable_120mm', color=4) # Cyan - doc.layers.add('Cable_150mm', color=6) # Magenta - doc.layers.add('Cable_185mm', color=30) # Orange - doc.layers.add('Cable_240mm', color=1) # Red - doc.layers.add('Cable_300mm', color=250) - doc.layers.add('Cable_400mm', color=7) # White/Black + + # 动态确定电缆颜色 + # 提取所有使用到的电缆截面 + used_sections = sorted(list(set(conn['cable']['cross_section'] for conn in connections_details))) + + # 定义颜色映射规则 (AutoCAD Color Index) + # 1=Red, 2=Yellow, 3=Green, 4=Cyan, 5=Blue, 6=Magenta + color_rank = [ + 2, # 1st (Smallest): Yellow + 3, # 2nd: Green + 1, # 3rd: Red + 5, # 4th: Blue + 6 # 5th: Magenta + ] + default_color = 3 # Others: Green + + # 创建电缆图层 + for i, section in enumerate(used_sections): + if i < len(color_rank): + c = color_rank[i] + else: + c = default_color + + layer_name = f'Cable_{section}mm' + if layer_name not in doc.layers: + doc.layers.add(layer_name, color=c) # 2. 绘制升压站 sx, sy = substation[0, 0], substation[0, 1] @@ -856,16 +885,24 @@ def compare_design_methods(excel_path=None, n_clusters_override=None): ax=axes[1]) plt.tight_layout() - output_filename = 'wind_farm_design_imported.png' if excel_path else 'offshore_wind_farm_design.png' + output_filename = 'wind_farm_design_comparison.png' plt.savefig(output_filename, dpi=300) - # 导出DXF - dxf_filename = 'wind_farm_design.dxf' - # 默认导出更优的方案(通常K-means扇区聚类在海上更合理,或者成本更低者) - # 这里我们导出Sector Clustering的结果 + # 导出最佳方案 DXF + if excel_path: + base_name = os.path.splitext(os.path.basename(excel_path))[0] + dir_name = os.path.dirname(excel_path) + # 如果 dir_name 为空(即当前目录),则直接使用文件名,或者使用 os.path.join('', ...) 也是安全的 + dxf_filename = os.path.join(dir_name, f"{base_name}.dxf") + else: + dxf_filename = 'wind_farm_design.dxf' + + # Use the connection details from the best evaluation + # Note: We need the turbine DataFrame corresponding to the best method to get cluster IDs if possible, + # but export_to_dxf mainly needs coordinates which are constant. export_to_dxf(clustered_turbines, substation, kmeans_evaluation['details'], dxf_filename) - # plt.show() + # plt.show() # 打印详细结果 print(f"\n===== 海上风电场设计方案比较 ({'导入数据' if excel_path else '自动生成'}) =====") @@ -921,14 +958,10 @@ def compare_design_methods(excel_path=None, n_clusters_override=None): if __name__ == "__main__": # 解析命令行参数 parser = argparse.ArgumentParser(description='Wind Farm Collector System Design') + parser.add_argument('--excel', help='Path to the Excel coordinates file', default=None) parser.add_argument('--clusters', type=int, help='Specify the number of clusters (circuits) manually', default=None) args = parser.parse_args() - # 2. 读取 Excel 坐标数据 (如果存在) - excel_path = 'coordinates2.xlsx' - # 尝试从命令行参数获取文件路径 (可选扩展) - # if len(sys.argv) > 1: excel_path = sys.argv[1] - # 3. 运行比较 - # 如果本地没有excel文件,将自动回退到生成数据模式 - compare_design_methods(excel_path, n_clusters_override=args.clusters) \ No newline at end of file + # 如果没有提供excel文件,将自动回退到生成数据模式 + compare_design_methods(args.excel, n_clusters_override=args.clusters) \ No newline at end of file