-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_following_with_dqn_test.py
72 lines (58 loc) · 1.57 KB
/
path_following_with_dqn_test.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
import torch
from carla_env import CarlaEnv
from DQN.DQN import QLearningAgent, ReplayBuffer
# action mapping
def action_mapping(action):
throttle, steering, brake = 0, 0, 0
# throttle mapping
if action[0] == 0:
throttle = 0.3
elif action[0] == 1:
throttle = 0.5
elif action[0] == 2:
throttle = 0.7
else:
print("throttle error!")
# steering mapping
if action[1] == 0:
steering = -0.4
elif action[1] == 1:
steering = -0.2
elif action[1] == 2:
steering = 0
elif action[1] == 3:
steering = 0.2
elif action[1] == 4:
steering = 0.4
else:
print("steering error!")
# brake mapping
if action[2] == 0:
brake = 0
elif action[2] == 1:
brake = 0.4
elif action[2] == 2:
brake = 0.8
else:
print("brake error!")
return throttle, steering, brake
# parameters
state_space_dims = 5
action_space_dims = (3, 5, 3)
buffer_maxlen = int(1e4)
# initialize env and agent
env = CarlaEnv()
agent = QLearningAgent(state_space_dims, action_space_dims)
agent.q_net.load_state_dict(torch.load("weight/DQN_Path_Following_3250e.pth", weights_only=True))
buffer = ReplayBuffer(buffer_maxlen)
# test
state = env.reset(scene='path_following')
state = buffer.convert_state(state)
done = False
while not done:
action = agent.sample_action(state, training=False)
action = action_mapping(action)
next_state, reward, done = env.step_pf(action)
next_state = buffer.convert_state(next_state)
state = next_state
env.close()