mirror of
https://github.com/dptech-corp/Uni-Lab-OS.git
synced 2026-02-06 23:15:10 +00:00
优化了全protocol的运行时间,除了pumptransfer相关的还没
This commit is contained in:
@@ -3,6 +3,11 @@ from typing import List, Dict, Any
|
||||
from .pump_protocol import generate_pump_protocol_with_rinsing
|
||||
|
||||
|
||||
def debug_print(message):
|
||||
"""调试输出"""
|
||||
print(f"🔄 [RESET_HANDLING] {message}", flush=True)
|
||||
|
||||
|
||||
def find_solvent_vessel(G: nx.DiGraph, solvent: str) -> str:
|
||||
"""
|
||||
查找溶剂容器,支持多种匹配模式
|
||||
@@ -14,7 +19,7 @@ def find_solvent_vessel(G: nx.DiGraph, solvent: str) -> str:
|
||||
Returns:
|
||||
str: 溶剂容器ID
|
||||
"""
|
||||
print(f"RESET_HANDLING: 正在查找溶剂 '{solvent}' 的容器...")
|
||||
debug_print(f"🔍 正在查找溶剂 '{solvent}' 的容器... 🧪")
|
||||
|
||||
# 构建可能的容器名称
|
||||
possible_names = [
|
||||
@@ -28,23 +33,30 @@ def find_solvent_vessel(G: nx.DiGraph, solvent: str) -> str:
|
||||
f"vessel_{solvent}", # vessel_methanol
|
||||
]
|
||||
|
||||
debug_print(f"📋 候选容器名称: {possible_names[:3]}... (共{len(possible_names)}个) 📝")
|
||||
|
||||
# 第一步:通过容器名称匹配
|
||||
debug_print(" 🎯 步骤1: 精确名称匹配...")
|
||||
for vessel_name in possible_names:
|
||||
if vessel_name in G.nodes():
|
||||
print(f"RESET_HANDLING: 通过名称匹配找到容器: {vessel_name}")
|
||||
debug_print(f" 🎉 通过名称匹配找到容器: {vessel_name} ✨")
|
||||
return vessel_name
|
||||
debug_print(" 😞 精确名称匹配失败,尝试模糊匹配... 🔍")
|
||||
|
||||
# 第二步:通过模糊匹配
|
||||
debug_print(" 🔍 步骤2: 模糊名称匹配...")
|
||||
for node_id in G.nodes():
|
||||
if G.nodes[node_id].get('type') == 'container':
|
||||
node_name = G.nodes[node_id].get('name', '').lower()
|
||||
|
||||
# 检查是否包含溶剂名称
|
||||
if solvent.lower() in node_id.lower() or solvent.lower() in node_name:
|
||||
print(f"RESET_HANDLING: 通过模糊匹配找到容器: {node_id}")
|
||||
debug_print(f" 🎉 通过模糊匹配找到容器: {node_id} ✨")
|
||||
return node_id
|
||||
debug_print(" 😞 模糊匹配失败,尝试液体类型匹配... 🧪")
|
||||
|
||||
# 第三步:通过液体类型匹配
|
||||
debug_print(" 🧪 步骤3: 液体类型匹配...")
|
||||
for node_id in G.nodes():
|
||||
if G.nodes[node_id].get('type') == 'container':
|
||||
vessel_data = G.nodes[node_id].get('data', {})
|
||||
@@ -56,10 +68,11 @@ def find_solvent_vessel(G: nx.DiGraph, solvent: str) -> str:
|
||||
reagent_name = vessel_data.get('reagent_name', '').lower()
|
||||
|
||||
if solvent.lower() in liquid_type or solvent.lower() in reagent_name:
|
||||
print(f"RESET_HANDLING: 通过液体类型匹配找到容器: {node_id}")
|
||||
debug_print(f" 🎉 通过液体类型匹配找到容器: {node_id} ✨")
|
||||
return node_id
|
||||
|
||||
# 列出可用容器帮助调试
|
||||
debug_print(" 📊 显示可用容器信息...")
|
||||
available_containers = []
|
||||
for node_id in G.nodes():
|
||||
if G.nodes[node_id].get('type') == 'container':
|
||||
@@ -75,13 +88,17 @@ def find_solvent_vessel(G: nx.DiGraph, solvent: str) -> str:
|
||||
'reagent_name': vessel_data.get('reagent_name', '')
|
||||
})
|
||||
|
||||
print(f"RESET_HANDLING: 可用容器列表:")
|
||||
for container in available_containers:
|
||||
print(f" - {container['id']}: {container['name']}")
|
||||
print(f" 液体: {container['liquids']}")
|
||||
print(f" 试剂: {container['reagent_name']}")
|
||||
debug_print(f" 📋 可用容器列表 (共{len(available_containers)}个):")
|
||||
for i, container in enumerate(available_containers[:5]): # 只显示前5个
|
||||
debug_print(f" {i+1}. 🥽 {container['id']}: {container['name']}")
|
||||
debug_print(f" 💧 液体: {container['liquids']}")
|
||||
debug_print(f" 🧪 试剂: {container['reagent_name']}")
|
||||
|
||||
raise ValueError(f"找不到溶剂 '{solvent}' 对应的容器。尝试了: {possible_names}")
|
||||
if len(available_containers) > 5:
|
||||
debug_print(f" ... 还有 {len(available_containers)-5} 个容器 📦")
|
||||
|
||||
debug_print(f"❌ 找不到溶剂 '{solvent}' 对应的容器 😭")
|
||||
raise ValueError(f"找不到溶剂 '{solvent}' 对应的容器。尝试了: {possible_names[:3]}...")
|
||||
|
||||
|
||||
def generate_reset_handling_protocol(
|
||||
@@ -104,35 +121,49 @@ def generate_reset_handling_protocol(
|
||||
|
||||
# 固定参数
|
||||
target_vessel = "main_reactor" # 默认目标容器
|
||||
volume = 100.0 # 默认体积 100 mL
|
||||
|
||||
print(f"RESET_HANDLING: 开始生成重置处理协议")
|
||||
print(f" - 溶剂: {solvent}")
|
||||
print(f" - 目标容器: {target_vessel}")
|
||||
print(f" - 体积: {volume} mL")
|
||||
volume = 50.0 # 默认体积 50 mL
|
||||
|
||||
debug_print("🔄" * 20)
|
||||
debug_print("🚀 开始生成重置处理协议 ✨")
|
||||
debug_print(f"📝 输入参数:")
|
||||
debug_print(f" 🧪 溶剂: {solvent}")
|
||||
debug_print(f" 🥽 目标容器: {target_vessel}")
|
||||
debug_print(f" 💧 体积: {volume} mL")
|
||||
debug_print(f" ⚙️ 其他参数: {kwargs}")
|
||||
debug_print("🔄" * 20)
|
||||
|
||||
# 1. 验证目标容器存在
|
||||
debug_print("📍 步骤1: 验证目标容器... 🔧")
|
||||
if target_vessel not in G.nodes():
|
||||
debug_print(f"❌ 目标容器 '{target_vessel}' 不存在于系统中! 😱")
|
||||
raise ValueError(f"目标容器 '{target_vessel}' 不存在于系统中")
|
||||
debug_print(f"✅ 目标容器 '{target_vessel}' 验证通过 🎯")
|
||||
|
||||
# 2. 查找溶剂容器
|
||||
debug_print("📍 步骤2: 查找溶剂容器... 🔍")
|
||||
try:
|
||||
solvent_vessel = find_solvent_vessel(G, solvent)
|
||||
print(f"RESET_HANDLING: 找到溶剂容器: {solvent_vessel}")
|
||||
debug_print(f"🎉 找到溶剂容器: {solvent_vessel} ✨")
|
||||
except ValueError as e:
|
||||
debug_print(f"❌ 溶剂容器查找失败: {str(e)} 😭")
|
||||
raise ValueError(f"无法找到溶剂 '{solvent}': {str(e)}")
|
||||
|
||||
# 3. 验证路径存在
|
||||
debug_print("📍 步骤3: 验证传输路径... 🛤️")
|
||||
try:
|
||||
path = nx.shortest_path(G, source=solvent_vessel, target=target_vessel)
|
||||
print(f"RESET_HANDLING: 找到路径: {' → '.join(path)}")
|
||||
debug_print(f"🛤️ 找到路径: {' → '.join(path)} ✅")
|
||||
except nx.NetworkXNoPath:
|
||||
debug_print(f"❌ 路径不可达: {solvent_vessel} → {target_vessel} 😞")
|
||||
raise ValueError(f"从溶剂容器 '{solvent_vessel}' 到目标容器 '{target_vessel}' 没有可用路径")
|
||||
|
||||
# 4. 使用pump_protocol转移溶剂
|
||||
print(f"RESET_HANDLING: 开始转移溶剂 {volume} mL")
|
||||
debug_print("📍 步骤4: 转移溶剂... 🚰")
|
||||
debug_print(f" 🚛 开始转移: {solvent_vessel} → {target_vessel}")
|
||||
debug_print(f" 💧 转移体积: {volume} mL")
|
||||
|
||||
try:
|
||||
debug_print(" 🔄 生成泵送协议...")
|
||||
pump_actions = generate_pump_protocol_with_rinsing(
|
||||
G=G,
|
||||
from_vessel=solvent_vessel,
|
||||
@@ -150,21 +181,52 @@ def generate_reset_handling_protocol(
|
||||
)
|
||||
|
||||
action_sequence.extend(pump_actions)
|
||||
debug_print(f" ✅ 泵送协议已添加: {len(pump_actions)} 个动作 🚰✨")
|
||||
|
||||
except Exception as e:
|
||||
debug_print(f" ❌ 泵送协议生成失败: {str(e)} 😭")
|
||||
raise ValueError(f"生成泵协议时出错: {str(e)}")
|
||||
|
||||
# 5. 等待溶剂稳定
|
||||
debug_print("📍 步骤5: 等待溶剂稳定... ⏳")
|
||||
|
||||
# 🕐 模拟运行时间优化
|
||||
debug_print(" ⏱️ 检查模拟运行时间限制...")
|
||||
original_wait_time = 10.0 # 原始等待时间
|
||||
simulation_time_limit = 5.0 # 模拟运行时间限制:5秒
|
||||
|
||||
final_wait_time = min(original_wait_time, simulation_time_limit)
|
||||
|
||||
if original_wait_time > simulation_time_limit:
|
||||
debug_print(f" 🎮 模拟运行优化: {original_wait_time}s → {final_wait_time}s ⚡")
|
||||
debug_print(f" 📊 时间缩短: {original_wait_time}s → {final_wait_time}s 🚀")
|
||||
else:
|
||||
debug_print(f" ✅ 时间在限制内: {final_wait_time}s 保持不变 🎯")
|
||||
|
||||
action_sequence.append({
|
||||
"action_name": "wait",
|
||||
"action_kwargs": {
|
||||
"time": 10.0,
|
||||
"description": f"等待溶剂 {solvent} 稳定"
|
||||
"time": final_wait_time,
|
||||
"description": f"等待溶剂 {solvent} 稳定" + (f" (模拟时间)" if original_wait_time != final_wait_time else "")
|
||||
}
|
||||
})
|
||||
debug_print(f" ✅ 稳定等待已添加: {final_wait_time}s ⏰✨")
|
||||
|
||||
print(f"RESET_HANDLING: 协议生成完成,共 {len(action_sequence)} 个动作")
|
||||
print(f"RESET_HANDLING: 已添加 {volume} mL {solvent} 到 {target_vessel}")
|
||||
# 显示时间调整信息
|
||||
if original_wait_time != final_wait_time:
|
||||
debug_print(f" 🎭 模拟优化说明: 原计划 {original_wait_time}s,实际模拟 {final_wait_time}s ⚡")
|
||||
|
||||
# 🎊 总结
|
||||
debug_print("🔄" * 20)
|
||||
debug_print(f"🎉 重置处理协议生成完成! ✨")
|
||||
debug_print(f"📊 总动作数: {len(action_sequence)} 个")
|
||||
debug_print(f"🧪 溶剂: {solvent}")
|
||||
debug_print(f"🥽 源容器: {solvent_vessel}")
|
||||
debug_print(f"🥽 目标容器: {target_vessel}")
|
||||
debug_print(f"💧 转移体积: {volume} mL")
|
||||
debug_print(f"⏱️ 预计总时间: {(final_wait_time + 5):.0f} 秒 ⌛")
|
||||
debug_print(f"🎯 已添加 {volume} mL {solvent} 到 {target_vessel} 🚰✨")
|
||||
debug_print("🔄" * 20)
|
||||
|
||||
return action_sequence
|
||||
|
||||
@@ -172,8 +234,15 @@ def generate_reset_handling_protocol(
|
||||
# 测试函数
|
||||
def test_reset_handling_protocol():
|
||||
"""测试重置处理协议"""
|
||||
print("=== RESET HANDLING PROTOCOL 测试 ===")
|
||||
print("测试完成")
|
||||
debug_print("🧪 === RESET HANDLING PROTOCOL 测试 === ✨")
|
||||
|
||||
# 测试溶剂名称
|
||||
debug_print("🧪 测试常用溶剂名称...")
|
||||
test_solvents = ["methanol", "ethanol", "water", "acetone", "dmso"]
|
||||
for solvent in test_solvents:
|
||||
debug_print(f" 🔍 测试溶剂: {solvent}")
|
||||
|
||||
debug_print("✅ 测试完成 🎉")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user