-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgem_eval.py
96 lines (95 loc) · 4 KB
/
gem_eval.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
import argparse
import torch
import torch.nn as nn
import numpy as np
import os
import pickle
from torch.autograd import Variable
import math
import time
from plan_general import *
def eval_tasks(mpNet, test_data, folder, filename, IsInCollision, normalize_func = lambda x:x, unnormalize_func=lambda x: x, time_flag=False):
obc, obs, paths, path_lengths = test_data
obs = obs.astype(np.float32)
obs = torch.from_numpy(obs)
fes_env = [] # list of list
valid_env = []
time_env = []
time_total = []
for i in range(len(paths)):
time_path = []
fes_path = [] # 1 for feasible, 0 for not feasible
valid_path = [] # if the feasibility is valid or not
# save paths to different files, indicated by i
# feasible paths for each env
for j in range(len(paths[0])):
time0 = time.time()
time_norm = 0.
fp = 0 # indicator for feasibility
print ("step: i="+str(i)+" j="+str(j))
p1_ind=0
p2_ind=0
p_ind=0
if path_lengths[i][j]<2:
# invalid, feasible = 0, and path count = 0
fp = 0
valid_path.append(0)
if path_lengths[i][j]>=2:
fp = 0
valid_path.append(1)
path = [torch.from_numpy(paths[i][j][0]).type(torch.FloatTensor),\
torch.from_numpy(paths[i][j][path_lengths[i][j]-1]).type(torch.FloatTensor)]
step_sz = DEFAULT_STEP
MAX_NEURAL_REPLAN = 11
for t in range(MAX_NEURAL_REPLAN):
# adaptive step size on replanning attempts
# 1.2, 0.5, 0.1 are for simple env
# 0.04, 0.03, 0.02 are for home env
if (t == 2):
#step_sz = 1.2
step_sz = 0.04
elif (t == 3):
#step_sz = 0.5
step_sz = 0.03
elif (t > 3):
#step_sz = 0.1
step_sz = 0.02
if time_flag:
path, time_norm = neural_replan(mpNet, path, obc[i], obs[i], IsInCollision, \
normalize_func, unnormalize_func, t==0, step_sz=step_sz, time_flag=time_flag)
else:
path = neural_replan(mpNet, path, obc[i], obs[i], IsInCollision, \
normalize_func, unnormalize_func, t==0, step_sz=step_sz, time_flag=time_flag)
print('after neural replan:')
print(path)
path = lvc(path, obc[i], IsInCollision, step_sz=step_sz)
#print('after lvc:')
#print(path)
if feasibility_check(path, obc[i], IsInCollision, step_sz=0.01):
fp = 1
print('feasible, ok!')
break
if fp:
# only for successful paths
time1 = time.time() - time0
time1 -= time_norm
time_path.append(time1)
print('test time: %f' % (time1))
# write the path
#print('planned path:')
#print(path)
path = [p.numpy() for p in path]
path = np.array(path)
np.savetxt('path_%d.txt' % (j), path, fmt='%f')
fes_path.append(fp)
print('env %d accuracy up to now: %f' % (i, (float(np.sum(fes_path))/ np.sum(valid_path))))
time_env.append(time_path)
time_total += time_path
print('average test time up to now: %f' % (np.mean(time_total)))
fes_env.append(fes_path)
valid_env.append(valid_path)
print('accuracy up to now: %f' % (float(np.sum(fes_env)) / np.sum(valid_env)))
if filename is not None:
pickle.dump(time_env, open(filename, "wb" ))
#print(fp/tp)
return np.array(fes_env), np.array(valid_env)