-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.py
384 lines (300 loc) · 16.4 KB
/
env.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
MIT License from https://github.com/marmotlab/CAtNIPP/
Copyright (c) 2022 MARMot Lab @ NUS-ME
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import numpy as np
import sklearn.metrics as metrics
from scipy.stats import entropy
from matplotlib import pyplot as plt
from copy import deepcopy
import imageio
from itertools import product
import warnings
warnings.filterwarnings("ignore")
from classes.Fruit import Fruit_info
from classes.Plants import Obstacle
from classes.Controller import obsController
from classes.Sensor import cam_sensor
from gp_ipp import gp_3d
from parameters import *
from test_parameters import TEST_TYPE
PATH = 'gifs\k20_novec_final\ ' #################################################################################################################################
if not os.path.exists(PATH):
os.makedirs(PATH)
class Env():
def __init__(self, ep_num, num_plants,k_size, budget_range, start=None, dest=None, save_image=False, seed=None):
self.seed = None
if seed is not None:
self.seed = seed
np.random.seed(seed)
self.path = logs_path + '/gp_lpg.txt'
self.ep_num = ep_num
self.num_plants = num_plants
self.obs = Obstacle(self.num_plants, TEST_TYPE)
self.plants = self.obs.get_plants() # Generate plants
self.fruit_plants = Fruit_info(self.plants, TEST_TYPE) # Generate fruits on plants
self.all_fruit_coords = self.fruit_plants.get_all_coords() # Ground truth
self.k_size = k_size
self.sensor = cam_sensor(depth=DEPTH, shift=0.5)
if start is None:
self.start = np.array([0.0, 0.0, 0.0])
else:
self.start = start
self.budget_range = budget_range
self.budget = np.random.uniform(low = self.budget_range[0], high = self.budget_range[1])
self.node_utils, self.node_std = None, None
self.node_utils0, self.node_std0, self.budget0 = deepcopy((self.node_utils, self.node_std,self.budget))
self.RMSE = None
self.F1score = None
self.cov_trace = None
self.MI = None
self.entropy = None
# start point
self.current_node_index = 4
self.sample = self.start
self.dist_residual = 0
self.route = [4]
self.save_image = save_image
self.frame_files = []
self.route_coords = [self.start]
# Graph
self.controller = obsController(self.start, self.k_size)
self.obs_list = []
self.node_coords, self.action_coords, self.graph = self.controller.gen_graph(self.start, SAMPLING_SIZE, GEN_RANGE)
def reset(self, seed=None):
np.random.seed(self.seed)
self.budget_arr = []
self.detected_arr = []
self.obs = Obstacle(self.num_plants, TEST_TYPE)
self.plants = self.obs.get_plants() # Generate plants
self.fruit_plants = Fruit_info(self.plants, TEST_TYPE) # Generate fruits on plants
self.all_fruit_coords = self.fruit_plants.get_all_coords() # Ground truth
self.ground_truth = self.obs.get_3d_gt(self.all_fruit_coords)
self.gt_occupancy_grid = self.obs.get_gt_occupancy_grid(self.all_fruit_coords, is_ground_truth=True)
self.occupancy_grid = np.zeros((50, 50, 50))
self.total_fruits = self.fruit_plants.num_fruits
self.detected_fruits = 0
# initialize gp
self.utils_gp = gp_3d(self.action_coords, 4)
self.high_info_area = self.utils_gp.get_high_info_area() if ADAPTIVE_AREA else None
self.node_utils = np.zeros((len(self.action_coords),1))
self.tree_binary = np.zeros(((K_SIZE+1)*4, 1))
self.node_std = np.ones((len(self.action_coords),1))
self.prev_utility_avg = 0.0
# initialize evaluations
self.RMSE = metrics.mean_squared_error(self.gt_occupancy_grid.flatten(), self.occupancy_grid.flatten(), squared=False)
self.F1score = metrics.f1_score(self.gt_occupancy_grid.flatten(), self.occupancy_grid.flatten(), average='weighted')
self.MI = metrics.mutual_info_score(self.gt_occupancy_grid.flatten(), self.occupancy_grid.flatten())
self.cov_trace = self.utils_gp.evaluate_cov_trace(self.high_info_area)
self.entropy = entropy(self.occupancy_grid.flatten(), self.gt_occupancy_grid.flatten())
self.cov_trace0 = deepcopy(self.cov_trace)
# save initial state
self.node_utils0, self.node_std0, self.budget = deepcopy((self.node_utils, self.node_std, self.budget0))
# start point
self.current_node_index = 0
self.prev_node_index = self.current_node_index
self.sample = self.start
self.dist_residual = 0
self.route = [self.current_node_index]
np.random.seed(None)
self.plane_2D = np.zeros((50, 50))
self.obs_list = []
self.current_coord = self.action_coords[self.current_node_index]
return self.action_coords, self.node_coords, self.graph, self.node_utils, self.node_std, self.budget
def step_sample(self, next_node_index, sample_length=SAMPLE_LENGTH, measurement=True, save_img=False, given_path=None):
self.tree_binary = np.zeros(len(self.action_coords))
tree = 0
dist = np.linalg.norm(self.action_coords[int(self.current_node_index)][0:3] - self.action_coords[int(next_node_index)][0:3])
facing = self.action_coords[int(next_node_index)][3]
facing = FACING_ACTIONS[int(facing)]
remain_length = dist
next_length = sample_length - self.dist_residual
utils_rew = 0
reward = 0
done = False # Budget not exhausted
no_sample = True
while remain_length > next_length:
if no_sample:
self.sample = (self.action_coords[next_node_index][0:3] - self.action_coords[self.current_node_index][0:3]) * next_length / dist + self.action_coords[self.current_node_index][0:3]
else:
self.sample = (self.action_coords[next_node_index][0:3] - self.action_coords[self.current_node_index][0:3]) * next_length / dist + self.sample
remain_length -= next_length
next_length = sample_length
no_sample = False
grid_idx = self.obs.find_grid_idx(self.sample)
utility, utils_rew, observed_fruits, obstacles, tree = self.sensor.get_utility(grid_idx, self.gt_occupancy_grid, facing)
utility = utility/self.total_fruits
# Detected fruits
self.detected_fruits += utils_rew / self.total_fruits
# Update agent belief
self.occupancy_grid = self.obs.get_gt_occupancy_grid(observed_fruits, obstacles, self.occupancy_grid)
obs_pt = np.array([self.sample[0], self.sample[1], self.sample[2], self.action_coords[int(next_node_index)][3]])
self.utils_gp.add_observed_point(obs_pt, utility)
#2.86e-6 sec
self.dist_residual = self.dist_residual + remain_length if no_sample else remain_length
self.tree_binary[int(next_node_index)] = tree
self.utils_gp.update_gp()
pred_util = self.utils_gp.gp.predict(self.action_coords[int(next_node_index)].reshape(1, -1), return_std=False)
self.utilities, self.node_std = self.utils_gp.update_node()
self.prev_utility_avg = np.average(self.utilities)
if measurement:
self.high_info_area = self.utils_gp.get_high_info_area() if ADAPTIVE_AREA else None
self.RMSE = metrics.mean_squared_error(self.gt_occupancy_grid.flatten(), self.occupancy_grid.flatten(), squared=False)
self.F1score = metrics.f1_score(self.gt_occupancy_grid.flatten(), self.occupancy_grid.flatten(), average='weighted')
self.MI = metrics.mutual_info_score(self.gt_occupancy_grid.flatten(), self.occupancy_grid.flatten())
self.entropy = entropy(self.occupancy_grid.flatten(), self.gt_occupancy_grid.flatten())
cov_trace = self.utils_gp.evaluate_cov_trace(self.high_info_area)
# REWARD
reward = 0
if next_node_index in self.route[-1:]: # if revisiting
reward += -0.01
if self.cov_trace > cov_trace: # if reducing uncertainty
reward += (self.cov_trace - cov_trace) / self.cov_trace
self.cov_trace = cov_trace
if not EXPLORATION_ONLY:
reward += utils_rew / 100.0 # if detecting fruit
self.prev_node_index = self.current_node_index
self.current_node_index = next_node_index
self.route.append(int(next_node_index))
self.route_coords.append(self.action_coords[int(next_node_index)][0:3])
try:
assert self.budget >= 0.1 # Dijsktra filter
except:
done = True
reward -= self.cov_trace / (50*50*50*4) # Correction factor
reward -= self.total_fruits*(1-self.detected_fruits)
if save_img:
self.visualize(reward, facing, path=given_path)
print('remain budget: {:.2f}, step: {}, detected targets - {:.2f} %'.format(self.budget, len(self.route), 100*self.detected_fruits))
if (int(self.current_node_index) - int(next_node_index)) // 4 == 0:
dist += 0.05
self.budget -= dist
self.budget_arr.append(self.budget0 - self.budget)
self.detected_arr.append(100*self.detected_fruits)
self.node_coords, self.action_coords, self.graph = self.controller.gen_graph(self.action_coords[next_node_index][0:3], SAMPLING_SIZE, GEN_RANGE)
utilities, std = self.utils_gp.gp.predict(self.action_coords, return_std=True)
if TREE_BINARY:
self.pred_tree_binary()
self.prev_utility_avg = np.average(utilities)
return reward, done, utilities, std, self.budget, utils_rew, pred_util, self.tree_binary[:len(self.action_coords)]
def pred_tree_binary(self):
for i, action_coord in enumerate(self.action_coords):
grid_idx = self.obs.find_grid_idx(action_coord[0:3])
facing = FACING_ACTIONS[int(action_coord[3])]
tree = self.sensor.check_tree(9, grid_idx, facing, self.occupancy_grid)
self.tree_binary[i] = tree
def visualize(self, reward, facing, path=None):
fig_s = (10,5.5)
fig = plt.figure(figsize=fig_s)
# GROUND TRUTH PLOTTING
l = 1.0 / self.obs.dim
ax = fig.add_subplot(121, projection='3d', label='Ground Truth')
lw = 0.25
for each_plant in self.plants:
bl = each_plant.bottom_left
tr = [bl[0] + self.obs.width*l, bl[1] + self.obs.width*l]
h = each_plant.height
ax.plot( [bl[0], bl[0]], [bl[1], bl[1]], [0.0, h], color='blue', linewidth=lw)
ax.plot([bl[0] + self.obs.width*l, bl[0] + self.obs.width*l], [bl[1], bl[1]], [0.0, h], color='blue', linewidth=lw)
ax.plot([tr[0], tr[0]], [tr[1], tr[1]], [0.0, h], color='blue', linewidth=lw)
ax.plot([bl[0], bl[0]], [bl[1]+self.obs.width*l, bl[1]+self.obs.width*l], [0.0, h], color='blue', linewidth=lw)
ax.plot([bl[0], tr[0], bl[0]+self.obs.width*l, bl[0], bl[0]], [bl[1]+self.obs.width*l, tr[1], bl[1], bl[1], bl[1]+self.obs.width*l], [h, h, h, h, h], color='blue', linewidth=lw)
ax.plot([bl[0], tr[0], bl[0]+self.obs.width*l, bl[0], bl[0]], [bl[1]+self.obs.width*l, tr[1], bl[1], bl[1], bl[1]+self.obs.width*l], [0.0, 0.0, 0.0, 0.0, 0.0], color='blue')
ax.plot([0.0, 0.0], [0.0, 0.0], [0.0, 1.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
# Plotting fruits
fruit_x = []
fruit_y = []
fruit_z = []
for coord in self.all_fruit_coords:
fruit_x.append(coord[0])
fruit_y.append(coord[1])
fruit_z.append(coord[2])
ax.plot(fruit_x, fruit_y, fruit_z, '*', color='green')
# ROBOT BELIEF PLOTTING
ax1 = fig.add_subplot(122, projection='3d')
for coord in self.utils_gp.observed_points:
ax1.plot(coord[0], coord[1], coord[2], '.', color='brown', alpha=0.5)
for k in range(self.obs.dim):
for j in range(self.obs.dim):
for i in range(self.obs.dim):
grid_cell = [i, j, k]
coords = self.obs.find_grid_coord(grid_cell)
val = self.occupancy_grid[k][i][j]
if val == 1:
ax1.plot(coords[0], coords[1], coords[2], '|', color='black', alpha=0.2)
if val == 2:
ax1.plot(coords[0], coords[1], coords[2], '*', color='green')
ax1.plot([0.0, 0.0], [0.0, 0.0], [0.0, 1.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
ax1.plot([0.0, 0.0], [0.0, 1.0], [0.0, 0.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
ax1.plot([0.0, 1.0], [0.0, 0.0], [0.0, 0.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
# ROBOT ROUTE, GRAPH AND SENSOR VIEW AREA PLOTTING
for i in range(len(self.route_coords)):
if i+1 == len(self.route) or len(self.route) == 1 or len(self.route) == 0:
break
try:
x_vals = [self.route_coords[i][0], self.route_coords[i+1][0]]
y_vals = [self.route_coords[i][1], self.route_coords[i+1][1]]
z_vals = [self.route_coords[i][2], self.route_coords[i+1][2]]
ax1.plot(x_vals, y_vals, z_vals, color='red')
except:
pass
try:
for obs in self.sensor.observed_indices:
coords = self.obs.find_grid_coord(obs)
ax1.plot(coords[0], coords[1], coords[2], '|', color='orchid', alpha=0.1)
except:
pass
for coord in self.node_coords:
ax1.plot(coord[0], coord[1], coord[2], '.', color='indigo', alpha=0.15)
ax1.plot([0.0, 0.0], [0.0, 0.0], [0.0, 1.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
ax1.plot([0.0, 0.0], [0.0, 1.0], [0.0, 0.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
ax1.plot([0.0, 1.0], [0.0, 0.0], [0.0, 0.0], c='k', marker=None, linestyle = '-', linewidth = 0.1)
plt.xlim(0.0, 1.0)
plt.ylim(0.0, 1.0)
if path is None:
name = PATH + 'episode_{}_step_{}.png'.format(self.ep_num, len(self.route))
else:
name = path + '/episode_{}_step_{}.png'.format(self.ep_num, len(self.route))
plt.suptitle('Remain Budget/Total Budget: {:.4g}/{:.4g} Detected: {:.4g}%'.format(self.budget, self.budget0, 100*self.detected_fruits))
plt.tight_layout()
plt.savefig(name)
self.frame_files.append(name)
def make_gif(self, ep, results=None):
if results is None:
with imageio.get_writer(PATH + 'uav_{}.gif'.format(ep), mode='I', duration=1000.5) as writer:
for frame in self.frame_files:
image = imageio.imread(frame)
writer.append_data(image)
print('gif complete\n')
print('Saved at - ', PATH + 'uav_{}.gif'.format(ep))
# Remove files
for filename in self.frame_files[:-1]:
os.remove(filename)
else:
with imageio.get_writer(results + '/uav_{}.gif'.format(ep), mode='I', duration=1000.5) as writer:
for frame in self.frame_files:
image = imageio.imread(frame)
writer.append_data(image)
print('gif complete\n')
# Remove files
for filename in self.frame_files[:-1]:
os.remove(filename)
if __name__=='__main__':
trial = Env(20)
trial.visualize()