-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
62 lines (56 loc) · 1.74 KB
/
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
import time
import torch
import numpy as np
from config import get_args, get_env_args
from environment.metro_env_eventbased import MetroEnvEventbased
from stable_baselines3 import PPO
def test():
args = get_args()
params = get_env_args(args)
metro_env = MetroEnvEventbased(params)
iterations = params['test_iterations']
# 随机动作test]
iter = 0
time1 = time.time()
rew_mean1 = 0
while iter < iterations:
rew_li = []
steps = 0
next_obs = metro_env.reset()
for _ in range(1000):
action = torch.FloatTensor([np.random.random()*2-1, np.random.random()*2-1])
next_obs, reward, done, _ = metro_env.step(action)
rew_li.append(reward)
steps += 1
if done:
print(f"iter: {iter} | ep_len:{steps} | ep_rew:{sum(rew_li)}")
break
iter += 1
rew_mean1 += sum(rew_li)/iterations
print(time.time()-time1)
print(rew_mean1)
# policy动作test
agent = PPO.load('./models/2023-0418-10-40-28-PPO-20-24/PPO/7280640.zip')
iter = 0
time2 = time.time()
rew_mean2 = 0
while iter < iterations:
rew_li = []
steps = 0
next_obs = metro_env.reset()
for _ in range(1000):
action = agent.predict(next_obs)[0]
next_obs, reward, done, _ = metro_env.step(action)
rew_li.append(reward)
steps += 1
if done:
print(f"iter: {iter} | ep_len:{steps} | ep_rew:{sum(rew_li)}")
break
iter += 1
rew_mean2 += sum(rew_li)/iterations
print(time.time()-time2)
print(rew_mean2)
# metro_env.render()
print('finish')
if __name__ == "__main__":
test()