11import logging
22from datetime import datetime
33import json
4+ from mesa .batchrunner import batch_run , _collect_data
5+ from multiprocessing import freeze_support
6+ import pandas as pd
47
58from model import ShipModel
69from visualization import plot_simulation
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- ports = config ["ports" ],
28- speed_limit_zones = config .get ("speed_limit_zones" , []),
29- obstacles = config ["obstacles" ],
30- dwa_config = config ["dwa_config" ],
31- resolution = config ["resolution" ],
32- obstacle_threshold = config ["obstacle_threshold" ],
33- lookahead = config ["lookahead" ]
34- )
35-
36- # Print agent counts by type
37- for agent_type , agents in model .agents_by_type .items ():
38- 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+ }
3943
40- # Run the simulation
41- logging .info (f"{ datetime .now ()} Starting ..." )
42- for t in range (steps ):
43- logging .info (f"Step { t } " )
44- model .step ()
45- 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+ )
4653
47- 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 ()
4856 df = agent_df .reset_index ()
4957
5058 # Save to CSV
@@ -54,6 +62,39 @@ def run_simulation(config_file="config/config.json"):
5462 # Run visualization
5563 plot_simulation (df , config )
5664
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+ # )
5780
58- if __name__ == "__main__" :
59- 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)
0 commit comments