forked from MiaoDragon/Hybrid-MPNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility_r2d.py
40 lines (40 loc) · 1.25 KB
/
utility_r2d.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
import torch
from torch.autograd import Variable
import copy
import numpy as np
import time
def normalize(x, bound, time_flag=False):
# normalize to -1 ~ 1 (bound can be a tensor)
#return x
time_0 = time.time()
bound = torch.tensor(bound)
if len(x[0]) != len(bound):
# then the proceding is obstacle
# don't normalize obstacles
x[:,:-2*len(bound)] = x[:,:-2*len(bound)] / bound[0]
x[:,-2*len(bound):-len(bound)] = x[:,-2*len(bound):-len(bound)] / bound
x[:,-len(bound):] = x[:,-len(bound):] / bound
else:
x = x / bound
if time_flag:
return x, time.time() - time_0
else:
return x
def unnormalize(x, bound, time_flag=False):
# normalize to -1 ~ 1 (bound can be a tensor)
# x only one dim
#return x
time_0 = time.time()
bound = torch.tensor(bound)
if len(x) != len(bound):
# then the proceding is obstacle
# don't normalize obstacles
x[:,:-2*len(bound)] = x[:,:-2*len(bound)] * bound[0]
x[:,-2*len(bound):-len(bound)] = x[:,-2*len(bound):-len(bound)] * bound
x[:,-len(bound):] = x[:,-len(bound):] * bound
else:
x = x * bound
if time_flag:
return x, time.time() - time_0
else:
return x