iflow-mcp_xrds76354_sumo-mcp 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
workflows/sim_gen.py ADDED
@@ -0,0 +1,70 @@
1
+ import os
2
+ from mcp_tools.network import netgenerate
3
+ from mcp_tools.route import random_trips, duarouter
4
+ from mcp_tools.simulation import run_simple_simulation
5
+ from mcp_tools.analysis import analyze_fcd
6
+
7
+ def sim_gen_workflow(output_dir: str, grid_number: int = 3, steps: int = 100) -> str:
8
+ """
9
+ Executes the Simulation Generation & Evaluation workflow:
10
+ 1. Generate Grid Network
11
+ 2. Generate Random Trips
12
+ 3. Compute Routes
13
+ 4. Create Config
14
+ 5. Run Simulation
15
+ 6. Analyze Results
16
+ """
17
+ if not os.path.exists(output_dir):
18
+ os.makedirs(output_dir)
19
+
20
+ net_file = os.path.join(output_dir, "grid.net.xml")
21
+ trips_file = os.path.join(output_dir, "trips.xml")
22
+ route_file = os.path.join(output_dir, "routes.xml")
23
+ sumocfg_file = os.path.join(output_dir, "sim.sumocfg")
24
+ fcd_file = os.path.join(output_dir, "fcd.xml")
25
+
26
+ # 1. Generate Net
27
+ res = netgenerate(net_file, grid=True, grid_number=grid_number)
28
+ if "failed" in res.lower(): return f"Step 1 Failed: {res}"
29
+
30
+ # 2. Generate Trips
31
+ res = random_trips(net_file, trips_file, end_time=steps)
32
+ if "failed" in res.lower(): return f"Step 2 Failed: {res}"
33
+
34
+ # 3. Generate Routes
35
+ res = duarouter(net_file, trips_file, route_file)
36
+ if "failed" in res.lower(): return f"Step 3 Failed: {res}"
37
+
38
+ # 4. Create Config
39
+ # We use absolute paths for safety or relative if SUMO expects it.
40
+ # Usually relative to config file is best.
41
+ try:
42
+ with open(sumocfg_file, "w") as f:
43
+ f.write(f"""<configuration>
44
+ <input>
45
+ <net-file value="{os.path.basename(net_file)}"/>
46
+ <route-files value="{os.path.basename(route_file)}"/>
47
+ </input>
48
+ <time>
49
+ <begin value="0"/>
50
+ <end value="{steps}"/>
51
+ </time>
52
+ <output>
53
+ <fcd-output value="{os.path.basename(fcd_file)}"/>
54
+ </output>
55
+ </configuration>""")
56
+ except Exception as e:
57
+ return f"Step 4 Failed: Could not write config file. {e}"
58
+
59
+ # 5. Run Sim
60
+ res = run_simple_simulation(sumocfg_file, steps)
61
+ if "error" in res.lower(): return f"Step 5 Failed: {res}"
62
+
63
+ # 6. Analyze
64
+ # FCD file should exist after sim
65
+ if not os.path.exists(fcd_file):
66
+ return "Step 6 Failed: FCD file not generated."
67
+
68
+ res_analysis = analyze_fcd(fcd_file)
69
+
70
+ return f"Workflow Completed Successfully.\n\nSimulation Output:\n{res}\n\nAnalysis Result:\n{res_analysis}"