Implement genetic algorithm for collector layout optimization
This commit is contained in:
46
gui.py
46
gui.py
@@ -90,6 +90,7 @@ def index():
|
||||
"run_btn": None,
|
||||
"current_file_container": None, # 替换 label 为 container
|
||||
"info_container": None, # 新增信息展示容器
|
||||
"ga_switch": None, # 遗传算法开关
|
||||
}
|
||||
|
||||
def update_info_panel():
|
||||
@@ -347,7 +348,9 @@ def index():
|
||||
except Exception as ex:
|
||||
ui.notify(f"上传处理失败: {ex}", type="negative")
|
||||
|
||||
async def save_file_with_dialog(filename, callback, file_filter="All files (*.*)", sender=None):
|
||||
async def save_file_with_dialog(
|
||||
filename, callback, file_filter="All files (*.*)", sender=None
|
||||
):
|
||||
"""
|
||||
跨平台文件保存助手。
|
||||
如果是原生模式,弹出系统保存对话框。
|
||||
@@ -372,7 +375,7 @@ def index():
|
||||
ps_filter = file_filter.replace("(", "|").replace(")", "")
|
||||
if "|" not in ps_filter:
|
||||
ps_filter += f"|{os.path.splitext(filename)[1] or '*.*'}"
|
||||
|
||||
|
||||
# 简单清洗 filter 字符串以适应 PowerShell (e.g., "Excel Files *.xlsx" -> "Excel Files|*.xlsx")
|
||||
# 这里做一个简化的映射,确保格式正确
|
||||
if "Excel" in file_filter:
|
||||
@@ -399,9 +402,9 @@ def index():
|
||||
# 使用 startupinfo 隐藏控制台窗口 (防止黑框闪烁)
|
||||
startupinfo = subprocess.STARTUPINFO()
|
||||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||
|
||||
|
||||
print("DEBUG: invoking PowerShell SaveFileDialog...")
|
||||
|
||||
|
||||
# 使用 run.io_bound 在后台线程执行,避免阻塞主事件循环
|
||||
# 这样按钮的禁用状态可以立即同步到前端
|
||||
result = await run.io_bound(
|
||||
@@ -409,7 +412,7 @@ def index():
|
||||
["powershell", "-Command", ps_script],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
startupinfo=startupinfo
|
||||
startupinfo=startupinfo,
|
||||
)
|
||||
save_path = result.stdout.strip()
|
||||
if save_path:
|
||||
@@ -420,7 +423,7 @@ def index():
|
||||
else:
|
||||
print("DEBUG: PowerShell dialog cancelled or empty result.")
|
||||
# 用户取消,直接返回,不回退
|
||||
return
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
print(f"PowerShell dialog failed: {e}")
|
||||
@@ -477,7 +480,10 @@ def index():
|
||||
|
||||
async def on_click_excel(e):
|
||||
await save_file_with_dialog(
|
||||
default_excel_name, save_excel, "Excel Files (*.xlsx)", sender=e.sender
|
||||
default_excel_name,
|
||||
save_excel,
|
||||
"Excel Files (*.xlsx)",
|
||||
sender=e.sender,
|
||||
)
|
||||
|
||||
ui.button(
|
||||
@@ -607,7 +613,9 @@ def index():
|
||||
except:
|
||||
pass
|
||||
|
||||
await save_file_with_dialog(default_name, save_zip, "ZIP Files (*.zip)", sender=e.sender)
|
||||
await save_file_with_dialog(
|
||||
default_name, save_zip, "ZIP Files (*.zip)", sender=e.sender
|
||||
)
|
||||
|
||||
ui.button("导出全部方案 DXF (ZIP)", on_click=on_click_all_dxf).props(
|
||||
"icon=folder_zip color=secondary"
|
||||
@@ -669,6 +677,9 @@ def index():
|
||||
refs["log_box"].clear()
|
||||
log_queue = queue.Queue()
|
||||
|
||||
# 获取遗传算法开关状态
|
||||
use_ga = refs["ga_switch"].value if refs["ga_switch"] else False
|
||||
|
||||
class QueueLogger(io.StringIO):
|
||||
def write(self, message):
|
||||
if message and message.strip():
|
||||
@@ -716,6 +727,7 @@ def index():
|
||||
n_clusters_override=None,
|
||||
interactive=False,
|
||||
plot_results=False,
|
||||
use_ga=use_ga,
|
||||
)
|
||||
|
||||
# 在后台线程运行计算任务
|
||||
@@ -782,7 +794,11 @@ def index():
|
||||
total_length_km = total_length_m / 1000
|
||||
|
||||
# 获取回路数 (通过统计从升压站发出的连接)
|
||||
n_circuits = sum(1 for d in res["eval"]["details"] if d["source"] == "substation" or d["target"] == "substation")
|
||||
n_circuits = sum(
|
||||
1
|
||||
for d in res["eval"]["details"]
|
||||
if d["source"] == "substation" or d["target"] == "substation"
|
||||
)
|
||||
|
||||
row_dict = {
|
||||
"name": name_display,
|
||||
@@ -875,7 +891,10 @@ def index():
|
||||
raise FileNotFoundError("无法生成模板文件")
|
||||
|
||||
await save_file_with_dialog(
|
||||
"coordinates.xlsx", save_template, "Excel Files (*.xlsx)", sender=e.sender
|
||||
"coordinates.xlsx",
|
||||
save_template,
|
||||
"Excel Files (*.xlsx)",
|
||||
sender=e.sender,
|
||||
)
|
||||
|
||||
ui.button("导出 Excel 模板", on_click=export_template).classes(
|
||||
@@ -901,6 +920,8 @@ def index():
|
||||
# with refs["current_file_container"]:
|
||||
# ui.label("未选择文件").classes("text-xs text-gray-500 italic ml-1")
|
||||
|
||||
|
||||
|
||||
# 3. 运行按钮
|
||||
refs["run_btn"] = (
|
||||
ui.button(
|
||||
@@ -910,6 +931,11 @@ def index():
|
||||
.classes("flex-1 py-4")
|
||||
.props("icon=play_arrow color=secondary")
|
||||
)
|
||||
# 4. 遗传算法开关
|
||||
with ui.column().classes("flex-1 gap-0 justify-center items-center"):
|
||||
refs["ga_switch"] = ui.switch("启用遗传算法", value=False).props(
|
||||
"color=orange"
|
||||
)
|
||||
|
||||
with ui.column().classes("w-full gap-4"):
|
||||
# 新增:信息展示卡片
|
||||
|
||||
Reference in New Issue
Block a user