Skip to content

Commit d11c891

Browse files
author
razvancruceanu
committed
Add initial parallel execution setup with batch_run
1 parent e463f17 commit d11c891

File tree

5 files changed

+448
-234
lines changed

5 files changed

+448
-234
lines changed

ships_hybrid_algorithm/config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"min_lon": 0.0,
6464
"max_lon": 20.0
6565
},
66-
"time_step_seconds": 0.001,
66+
"time_step_seconds": 0.002,
6767
"dwa_config": {
6868
"max_speed": 1.0,
6969
"min_speed": 0.1,

ships_hybrid_algorithm/main.py

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import logging
22
from datetime import datetime
33
import json
4+
from mesa.batchrunner import batch_run, _collect_data
5+
from multiprocessing import freeze_support
6+
import pandas as pd
47

58
from model import ShipModel
69
from visualization import plot_simulation
@@ -13,40 +16,43 @@
1316
format="%(asctime)s - %(levelname)s - %(message)s"
1417
)
1518

16-
def run_simulation(config_file="config/config.json"):
19+
if __name__ == "__main__":
20+
freeze_support()
21+
22+
config_file="config/config.json"
1723
with open(config_file) as f:
1824
config = json.load(f)
1925

2026
steps = config["simulation_steps"]
2127

22-
model = ShipModel(
23-
width=config["width"],
24-
height=config["height"],
25-
num_ships=config["num_ships"],
26-
max_speed_range=config["max_speed_range"],
27-
speed_variation=config["speed_variation"],
28-
directional_variation=config["directional_variation"],
29-
ports=config["ports"],
30-
speed_limit_zones=config.get("speed_limit_zones", []),
31-
obstacles=config["obstacles"],
32-
dwa_config=config["dwa_config"],
33-
resolution=config["resolution"],
34-
obstacle_threshold=config["obstacle_threshold"],
35-
lookahead=config["lookahead"]
36-
)
37-
38-
# Print agent counts by type
39-
for agent_type, agents in model.agents_by_type.items():
40-
logging.info(f'{agent_type}: {len(agents)}')
28+
model_params = {
29+
"width": [config["width"]],
30+
"height": [config["height"]],
31+
"num_ships": [config["num_ships"]],
32+
"max_speed_range": [config["max_speed_range"]],
33+
"speed_variation": [config["speed_variation"]],
34+
"directional_variation": [config["directional_variation"]],
35+
"ports": [config["ports"]],
36+
"speed_limit_zones": [config.get("speed_limit_zones", [])],
37+
"obstacles": [config["obstacles"]],
38+
"dwa_config": [config["dwa_config"]],
39+
"resolution": [config["resolution"]],
40+
"obstacle_threshold": [config["obstacle_threshold"]],
41+
"lookahead": [config["lookahead"]]
42+
}
4143

42-
# Run the simulation
43-
logging.info(f"{datetime.now()} Starting ...")
44-
for t in range(steps):
45-
logging.info(f"Step {t}")
46-
model.step()
47-
logging.info(f"{datetime.now()} Finished.")
44+
results = batch_run(
45+
ShipModel,
46+
parameters=model_params,
47+
iterations=1,
48+
max_steps=steps,
49+
number_processes=None,
50+
data_collection_period=1,
51+
display_progress=True,
52+
)
4853

49-
agent_df = model.datacollector.get_agent_vars_dataframe().dropna()
54+
results_df = pd.DataFrame(results)
55+
agent_df = results_df[["Step", "AgentID", "x", "y", "AStarPath"]].dropna()
5056
df = agent_df.reset_index()
5157

5258
# Save to CSV
@@ -56,6 +62,39 @@ def run_simulation(config_file="config/config.json"):
5662
# Run visualization
5763
plot_simulation(df, config)
5864

65+
# model = ShipModel(
66+
# width=config["width"],
67+
# height=config["height"],
68+
# num_ships=config["num_ships"],
69+
# max_speed_range=config["max_speed_range"],
70+
# speed_variation=config["speed_variation"],
71+
# directional_variation=config["directional_variation"],
72+
# ports=config["ports"],
73+
# speed_limit_zones=config.get("speed_limit_zones", []),
74+
# obstacles=config["obstacles"],
75+
# dwa_config=config["dwa_config"],
76+
# resolution=config["resolution"],
77+
# obstacle_threshold=config["obstacle_threshold"],
78+
# lookahead=config["lookahead"]
79+
# )
5980

60-
if __name__ == "__main__":
61-
run_simulation()
81+
# # Print agent counts by type
82+
# for agent_type, agents in model.agents_by_type.items():
83+
# logging.info(f'{agent_type}: {len(agents)}')
84+
85+
# # Run the simulation
86+
# logging.info(f"{datetime.now()} Starting ...")
87+
# for t in range(steps):
88+
# logging.info(f"Step {t}")
89+
# model.step()
90+
# logging.info(f"{datetime.now()} Finished.")
91+
92+
# agent_df = model.datacollector.get_agent_vars_dataframe().dropna()
93+
# df = agent_df.reset_index()
94+
95+
# # Save to CSV
96+
# df.to_csv("ship_simulation_output.csv", index=False)
97+
# logging.info("Saved simulation data to ship_simulation_output.csv")
98+
99+
# # Run visualization
100+
# plot_simulation(df, config)

ships_hybrid_algorithm/model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from mesa import Model
23
from mesa.space import ContinuousSpace
34
from mesa.datacollection import DataCollector
@@ -15,6 +16,7 @@
1516
class ShipModel(Model):
1617
def __init__(self, width, height, num_ships, max_speed_range, speed_variation, directional_variation, ports, speed_limit_zones, obstacles, dwa_config, resolution=1, obstacle_threshold=0, lookahead=3.0):
1718
super().__init__()
19+
# self.step = 0
1820
self.width = width
1921
self.height = height
2022
self.max_speed_range = max_speed_range
@@ -110,5 +112,7 @@ def step(self):
110112
"""Run one step of the model.
111113
112114
All agents are activated in random order."""
115+
# logging.info(f"Step {self.step}")
113116
self.schedule.step()
114-
self.datacollector.collect(self)
117+
self.datacollector.collect(self)
118+
# self.step += 1

0 commit comments

Comments
 (0)