-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheval_single_env.py
More file actions
165 lines (128 loc) · 5.76 KB
/
eval_single_env.py
File metadata and controls
165 lines (128 loc) · 5.76 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# @file eval_single_env.py
# @brief Evaluate a single dataset with multiple controllers
import os
import sys
import coverage_control as cc
import numpy as np
from coverage_control import CoverageSystem
from coverage_control import IOUtils
from coverage_control import WorldIDF
from coverage_control.algorithms import ControllerCVT
from coverage_control.algorithms import ControllerNN
# @ingroup python_api
class EvaluatorSingle:
"""
Class to evaluate a single environment with multiple controllers
"""
def __init__(self, in_config):
self.config = in_config
self.eval_dir = IOUtils.sanitize_path(self.config["EvalDir"]) + "/"
self.env_dir = IOUtils.sanitize_path(self.config["EnvironmentDataDir"]) + "/"
if not os.path.exists(self.env_dir):
os.makedirs(self.env_dir)
self.num_controllers = len(self.config["Controllers"])
self.controllers_configs = self.config["Controllers"]
self.env_config_file = IOUtils.sanitize_path(self.config["EnvironmentConfig"])
self.env_config = IOUtils.load_toml(self.env_config_file)
self.cc_params = cc.Parameters(self.env_config_file)
self.num_steps = self.config["NumSteps"]
self.plot_map = self.config["PlotMap"]
self.generate_video = self.config["GenerateVideo"]
self.feature_file = None
self.pos_file = None
file_exists = False
if "FeatureFile" in self.config and "RobotPosFile" in self.config:
self.feature_file = self.env_dir + self.config["FeatureFile"]
self.pos_file = self.env_dir + self.config["RobotPosFile"]
print(self.feature_file)
print(self.pos_file)
if os.path.isfile(self.feature_file):
feature_file_exists = True
else:
print(f"Feature file {self.feature_file} not found")
feature_file_exists = False
if os.path.isfile(self.pos_file):
pos_file_exists = True
else:
print(f"Position file {self.pos_file} not found")
pos_file_exists = False
file_exists = feature_file_exists and pos_file_exists
if file_exists is True:
self.world_idf = WorldIDF(self.cc_params, self.feature_file)
self.env_main = CoverageSystem(
self.cc_params, self.world_idf, self.pos_file
)
else:
self.env_main = CoverageSystem(self.cc_params)
self.world_idf = self.env_main.GetWorldIDF()
if self.feature_file is not None and self.pos_file is not None:
self.env_main.WriteEnvironment(self.pos_file, self.feature_file)
def evaluate(self, save=True):
cost_data = np.zeros((self.num_controllers, self.num_steps))
robot_init_pos = self.env_main.GetRobotPositions(force_no_noise=True)
if self.plot_map:
map_dir = self.eval_dir + "/plots/"
os.makedirs(map_dir, exist_ok=True)
self.env_main.PlotInitMap(map_dir, "InitMap")
for controller_id in range(self.num_controllers):
step_count = 0
env = CoverageSystem(self.cc_params, self.world_idf, robot_init_pos)
controller_name = self.controllers_configs[controller_id]["Name"]
if self.generate_video:
env.RecordPlotData()
if self.controllers_configs[controller_id]["Type"] == "Learning":
Controller = ControllerNN
else:
Controller = ControllerCVT
controller = Controller(
self.controllers_configs[controller_id], self.cc_params, env
)
initial_objective_value = env.GetObjectiveValue()
cost_data[controller_id, step_count] = (
env.GetObjectiveValue() / initial_objective_value
)
step_count = step_count + 1
while step_count < self.num_steps:
converged = controller.step(env)
objective_value = env.GetObjectiveValue()
cost_data[controller_id, step_count] = (
objective_value / initial_objective_value
)
if converged and not self.generate_video:
cost_data[controller_id, step_count:] = (
objective_value / initial_objective_value
)
break
if self.generate_video:
env.RecordPlotData()
step_count = step_count + 1
if step_count % 100 == 0:
print(
f"{controller.name} "
f"Step {step_count} "
f"Obj Value {cost_data[controller_id, step_count - 1]:.3e}"
)
print(
f"{controller.name} "
f"final step {step_count} "
f"Obj Value {cost_data[controller_id, step_count - 1]:.3e}"
)
if save is True:
controller_dir = self.eval_dir + "/" + controller_name
if not os.path.exists(controller_dir):
os.makedirs(controller_dir)
controller_data_file = controller_dir + "/" + "eval.csv"
np.savetxt(
controller_data_file, cost_data[controller_id, :], delimiter=","
)
if self.generate_video:
controller_dir = self.eval_dir + "/" + controller_name
env.RenderRecordedMap(controller_dir, "video.mp4")
del controller
del env
return cost_data
if __name__ == "__main__":
config_file = sys.argv[1]
config = IOUtils.load_toml(config_file)
evaluator = EvaluatorSingle(config)
evaluator.evaluate()