forked from MiaoDragon/Hybrid-MPNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgem_eval.py
79 lines (78 loc) · 3.26 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
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, filename, IsInCollision, normalize_func = lambda x:x, unnormalize_func=lambda x: x, time_flag=False):
obc, obs, paths, path_lengths = test_data
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]==0:
# invalid, feasible = 0, and path count = 0
fp = 0
valid_path.append(0)
if path_lengths[i][j]>0:
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
if (t == 2):
step_sz = 1.2
elif (t == 3):
step_sz = 0.5
elif (t > 3):
step_sz = 0.1
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)
path = lvc(path, obc[i], IsInCollision, step_sz=step_sz)
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))
fes_path.append(fp)
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' % (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)