-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturn_left_with_ppo_test.py
98 lines (80 loc) · 2.52 KB
/
turn_left_with_ppo_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
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
import torch
import argparse
from carla_env import CarlaEnv
from DQN.DQN import QLearningAgent
from PPO.PPO import PPO_Agent
# 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
# parser
parser = argparse.ArgumentParser()
parser.add_argument('--scene', type=str, default='normal')
args = parser.parse_args()
# 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))
brake_agent = PPO_Agent()
if args.scene == 'normal':
brake_agent.actor_net.load_state_dict(torch.load('weight/PPO_Turn_Left_Actor_780e.pth', weights_only=True))
elif args.scene == 'difficult':
brake_agent.actor_net.load_state_dict(torch.load('weight/PPO_Turn_Left_Actor_Difficult_720e.pth', weights_only=True))
else:
print('Please choose normal or difficult.')
exit()
# test
success = 0
for i in range(10):
state = env.reset(scene=args.scene)
state_follow, state_brake = brake_agent.convert_state(state)
done = False
while not done:
action_follow = agent.sample_action(state_follow, training=False)
action_brake = brake_agent.get_action(state_brake, training=False)
action = (action_follow[0], action_follow[1], action_brake)
action = action_mapping(action)
next_state, reward, terminated, truncated = env.step(action)
next_state_follow, next_state_brake = brake_agent.convert_state(next_state)
state_follow = next_state_follow
state_brake = next_state_brake
done = terminated or truncated
if terminated:
success += 1
print(f"Success Rate: {success / 10}")
env.close()