-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfig_simulation.py
More file actions
59 lines (51 loc) · 2.48 KB
/
fig_simulation.py
File metadata and controls
59 lines (51 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import argparse
from config import *
from Simulation.Env import *
from Tools.statistic import *
from Tools.utils import write_results_to_text
from Tools.plotting import *
args = argparse.ArgumentParser()
args.add_argument('--env', type=str, default='grid',
choices=['grid', 'polygon', 'maze', 'dynamics'], help='environment to simulate')
if __name__ == '__main__':
args = args.parse_args()
# folder
ratios = SimulationConfig()
project_folder = os.path.dirname(os.path.abspath(__file__))
figure_folder = os.path.join(project_folder, 'data', 'simulation', 'figure')
if not os.path.exists(figure_folder):
os.makedirs(figure_folder)
figure_appendix = os.path.join(figure_folder, f'{args.env}_')
path_to_show = []
stat_group_value = []
for ratio in ratios:
data_folder = os.path.join(project_folder, 'data', 'simulation', f'ratio={ratio}', args.env)
if not os.path.exists(data_folder):
raise FileNotFoundError('Data folder not found, please run simulation first!')
# configuration functions
config_map = {'grid': GridConfig, 'polygon': PolygonConfig, 'maze': MazeConfig, 'dynamics': DynamicConfig}
algoConfig = config_map[args.env]()
# trajectory
paths = {}
for algo_name, _ in algoConfig.items():
file_name = os.path.join(data_folder, f'{algo_name}.npy')
if not os.path.exists(file_name):
raise FileNotFoundError(f'Data file not found: {file_name}')
data = np.load(file_name, allow_pickle=True).item()
paths[algo_name] = data['path_to_show']
path_to_show.append(paths)
# statistic
file_name = os.path.join(data_folder, 'statistic_results.npy')
results = np.load(file_name, allow_pickle=True)
statistic_values = {}
for result in results: # algo_name, mean_path_len, mean_turn_angle, unnorm_turn, mean_duration
statistic_values[str(result[0])] = result[1].astype(float)/ratio
stat_group_value.append(statistic_values)
# plotting
env_map = {'grid': GridEnv, 'polygon': PolygonEnv, 'maze': MazeEnv, 'dynamics': DynamicsEnv}
env_func = env_map[args.env]
plotting_map = {'grid': Plotting_Grid_Map, 'polygon': Plotting_Polygon_Map,
'maze': Plotting_Maze_Map, 'dynamics': Plotting_Dynamic_Map}
plotting = plotting_map[args.env](env_func, path_to_show, stat_group_value, ratios, figure_appendix)
plotting.showing()