Skip to content

Commit 31e8206

Browse files
committed
Simulate large dataset
1 parent c042f95 commit 31e8206

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-28
lines changed

mesa/examples/basic/ships_hybrid_algorithm/config/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"simulation_steps": 1000,
2+
"simulation_steps": 10000,
33
"width": 100,
44
"height": 100,
5-
"num_ships": 100,
5+
"num_ships": 1000,
66
"max_speed_range": [1.0, 1.5],
77
"resolution": 1,
88
"obstacle_threshold": 2,

mesa/examples/basic/ships_hybrid_algorithm/ships.ipynb

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@
122122
"execution_count": null,
123123
"metadata": {},
124124
"outputs": [],
125-
"source": []
125+
"source": [
126+
"#tc = mpd.TrajectoryCollection(df, traj_id_col='AgentID', t='t', x='x', y='y', crs=31287)\n",
127+
"tc"
128+
]
126129
},
127130
{
128131
"cell_type": "code",
@@ -171,7 +174,9 @@
171174
"metadata": {},
172175
"outputs": [],
173176
"source": [
174-
"tc.add_speed(overwrite=True)\n",
177+
"import os\n",
178+
"\n",
179+
"tc.add_speed(overwrite=True, n_threads=os.cpu_count())\n",
175180
"tc.trajectories[0].df#.hvplot(c='speed')"
176181
]
177182
},
@@ -216,7 +221,7 @@
216221
"metadata": {},
217222
"outputs": [],
218223
"source": [
219-
"sim_out_file_name = 'sim_20250517_100643.csv'\n",
224+
"sim_out_file_name = 'sim_20250517_164810.csv'\n",
220225
"PERCENTAGE_OF_ANOMALIES = 0.03"
221226
]
222227
},
@@ -236,8 +241,9 @@
236241
"metadata": {},
237242
"outputs": [],
238243
"source": [
239-
"unique_ids = sims.AgentID.unique()\n",
240-
"unique_ids"
244+
"#sims = sims[sims.AgentID<29].copy()\n",
245+
"sims['is_anomaly'] = False\n",
246+
"sims['anomaly_type'] = 'n/a'\n"
241247
]
242248
},
243249
{
@@ -246,8 +252,8 @@
246252
"metadata": {},
247253
"outputs": [],
248254
"source": [
249-
"sample_ids_pd = pd.Series(unique_ids).sample(frac=PERCENTAGE_OF_ANOMALIES).tolist()\n",
250-
"print(\"Subset we'll insert anomalies in: \", sample_ids_pd)"
255+
"tc = mpd.TrajectoryCollection(sims, t='t', traj_id_col='AgentID', x='x', y='y', crs=31287)\n",
256+
"tc"
251257
]
252258
},
253259
{
@@ -256,21 +262,18 @@
256262
"metadata": {},
257263
"outputs": [],
258264
"source": [
259-
"#sims = sims[sims.AgentID<29].copy()\n",
260-
"sims['is_anomaly'] = False\n",
261-
"sims['anomaly_type'] = 'n/a'\n",
262-
"tc = mpd.TrajectoryCollection(sims, t='t', traj_id_col='AgentID', x='x', y='y', crs=31287)\n",
265+
"# Remove stopped segments (with speed < 0.0001)\n",
266+
"tc = mpd.SpeedSplitter(tc).split(speed=0.0001, duration=timedelta(seconds=5))\n",
263267
"tc"
264268
]
265269
},
266270
{
267-
"cell_type": "code",
268-
"execution_count": null,
271+
"cell_type": "markdown",
269272
"metadata": {},
270-
"outputs": [],
271273
"source": [
272-
"tc.add_speed(overwrite=True)\n",
273-
"tc.plot(column='speed', legend=True)"
274+
"### Speed\n",
275+
"\n",
276+
"Change the timestamps to make a subsection of the trajectory faster"
274277
]
275278
},
276279
{
@@ -279,17 +282,18 @@
279282
"metadata": {},
280283
"outputs": [],
281284
"source": [
282-
"# Remove stopped segments (with speed < 0.01)\n",
283-
"tc = mpd.SpeedSplitter(tc).split(speed=0.01, duration=timedelta(seconds=5))"
285+
"unique_ids = sims.AgentID.unique()\n",
286+
"unique_ids"
284287
]
285288
},
286289
{
287-
"cell_type": "markdown",
290+
"cell_type": "code",
291+
"execution_count": null,
288292
"metadata": {},
293+
"outputs": [],
289294
"source": [
290-
"### Speed\n",
291-
"\n",
292-
"Change the timestamps to make a subsection of the trajectory faster"
295+
"sample_ids_pd = pd.Series(unique_ids).sample(frac=PERCENTAGE_OF_ANOMALIES).tolist()\n",
296+
"print(\"Subset we'll insert anomalies in: \", sample_ids_pd)"
293297
]
294298
},
295299
{
@@ -327,8 +331,9 @@
327331
"outputs": [],
328332
"source": [
329333
"for i, traj in enumerate(tc.trajectories):\n",
330-
" traj = inject_speed_anomaly(traj)\n",
331-
" tc.trajectories[i] = traj\n",
334+
" if i in sample_ids_pd: \n",
335+
" traj = inject_speed_anomaly(traj)\n",
336+
" tc.trajectories[i] = traj\n",
332337
" \n",
333338
"tc.add_speed(overwrite=True)\n",
334339
"tc.plot(column='speed', legend=True)"
@@ -376,7 +381,9 @@
376381
"source": [
377382
"### Location\n",
378383
"\n",
379-
"#### Generate trajectories that pass through obstacles"
384+
"#### Generate trajectories that pass through obstacles\n",
385+
"\n",
386+
"Note that is not guaranteed that each resulting trajectory will pass through an obtacle! Ships simply ignore the obstacles."
380387
]
381388
},
382389
{
@@ -408,7 +415,7 @@
408415
"model = ShipModel(\n",
409416
" width=config[\"width\"],\n",
410417
" height=config[\"height\"],\n",
411-
" num_ships=config[\"num_ships\"]*PERCENTAGE_OF_ANOMALIES, \n",
418+
" num_ships=int(config[\"num_ships\"]*2*PERCENTAGE_OF_ANOMALIES), # doubling the number, assuming that 50% will violate obstacles\n",
412419
" max_speed_range=config[\"max_speed_range\"],\n",
413420
" ports=config[\"ports\"],\n",
414421
" speed_limit_zones=config.get(\"speed_limit_zones\", []),\n",
@@ -607,6 +614,60 @@
607614
{
608615
"cell_type": "markdown",
609616
"metadata": {},
617+
"source": [
618+
"## Merge files"
619+
]
620+
},
621+
{
622+
"cell_type": "markdown",
623+
"metadata": {},
624+
"source": []
625+
},
626+
{
627+
"cell_type": "code",
628+
"execution_count": null,
629+
"metadata": {},
630+
"outputs": [],
631+
"source": [
632+
"df1 = pd.read_csv(\"anomalies_speed_20250517_172830.csv\")\n",
633+
"df2 = pd.read_csv(\"anomalies_loc_20250517_184842.csv\")\n",
634+
"out = pd.concat([df1,df2])[['t','AgentID','speed','is_anomaly','anomaly_type','x','y']]\n",
635+
"out.to_csv(\"synthetic_vessel_tracks_with_anomalies_20250517.csv\", index=False)"
636+
]
637+
},
638+
{
639+
"cell_type": "code",
640+
"execution_count": null,
641+
"metadata": {},
642+
"outputs": [],
643+
"source": [
644+
"tmp = pd.read_csv(\"synthetic_vessel_tracks_with_anomalies_20250517.csv\")\n",
645+
"tmp"
646+
]
647+
},
648+
{
649+
"cell_type": "code",
650+
"execution_count": null,
651+
"metadata": {},
652+
"outputs": [],
653+
"source": [
654+
"tmp[tmp.anomaly_type==\"location\"]"
655+
]
656+
},
657+
{
658+
"cell_type": "code",
659+
"execution_count": null,
660+
"metadata": {},
661+
"outputs": [],
662+
"source": [
663+
"tmp[tmp.anomaly_type==\"location\"].AgentID.unique()"
664+
]
665+
},
666+
{
667+
"cell_type": "code",
668+
"execution_count": null,
669+
"metadata": {},
670+
"outputs": [],
610671
"source": []
611672
}
612673
],
14.3 KB
Loading

0 commit comments

Comments
 (0)