-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel_test.py
125 lines (101 loc) · 4.51 KB
/
model_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import numpy as np
import os
import torch
from environment import Env
from utils import seed_torch
from ppo_agent import PPOAgent
import csv
def model_test(args, env_args):
seed_torch(args.seed)
print('in test process')
os.environ['OMP_NUM_THREADS'] = '1'
if args.test_device_num == -1:
test_device_name = 'cpu'
else:
test_device_name = 'cuda:' + str(args.test_device_num)
torch.cuda.set_device(args.test_device_num)
# -------------get environment information------------
ppo_agent = []
for i in range(args.user_num):
ppo_agent.append(
PPOAgent(args.state_dim, test_device_name, args.lr, args.exploration_steps,
args.mini_batch_num, args.use_gae, args.gamma, args.gae_param, args.ppo_epoch,
args.clip, args.value_coeff, args.clip_coeff, args.ent_coeff))
ori_device_name = 'cuda:' + str(args.device_num)
model_path = os.path.join(args.root_path, 'model')
for i, agent in enumerate(ppo_agent):
ppo_model_path = os.path.join(model_path, 'ppo_model' + str(i) + '.pt')
agent.load_model(ppo_model_path, ori_device_name, test_device_name)
agent.local_ppo_model.eval()
done_time = 0
episode_length = 0
user_num = args.user_num
env = Env(user_num, args.state_dim, test_device_name, env_args)
action = torch.zeros(user_num, dtype=torch.long)
final_av_reward = 0
final_av_server_reward = 0
test_file_path = os.path.join(args.root_path, 'test_file')
if not os.path.exists(test_file_path):
os.mkdir(test_file_path)
test_result_profile = open(test_file_path + '/test_result.csv', 'w', newline='')
test_writer = csv.writer(test_result_profile)
av_ext_reward = 0
av_int_rewards = 0
av_completion_ratio = 0
result_path = test_file_path + '/test_result.npz'
# -----------------------------------------
all_remaining_energy = []
while True:
if episode_length >= args.max_test_length:
print('training over')
break
print('---------------in episode ', episode_length, '-----------------------')
step = 0
done = True
av_reward = 0
av_action = torch.zeros(user_num)
obs = env.reset()
interact_time = 0
remaining_energy = []
remaining_energy.append(env.remain_energy.mean())
while step < args.exploration_steps:
interact_time += 1
# ----------------sample actions(no grad)------------------------
with torch.no_grad():
for i, agent in enumerate(ppo_agent):
_, action[i], _ = agent.act(obs[i])
obs, reward, done = env.step(action)
remaining_energy.append(env.remain_energy.mean())
av_reward += np.mean(reward.numpy())
av_action += 0.2 * action.float()
step = step + 1
done = interact_time >= args.max_interact_time
if done:
# env.draw_remain_energy(done_time)
done_time += 1
interact_time = 0
obs = env.reset()
if len(remaining_energy) == args.max_interact_time + 1 and len(all_remaining_energy) < 100:
all_remaining_energy.append(remaining_energy)
remaining_energy = []
remaining_energy.append(env.remain_energy.mean())
for i, agent in enumerate(ppo_agent):
agent.reset(obs[i])
continue
av_reward /= args.exploration_steps
ext_reward, int_reward = env.get_reward()
av_ext_reward += ext_reward / args.exploration_steps
av_int_rewards += int_reward / args.exploration_steps
completion_ratio = env.get_completion_ratio()
av_completion_ratio += completion_ratio
final_av_reward += av_reward
final_av_server_reward += env.plot_server_reward(episode_length) / args.exploration_steps
episode_length += 1
test_writer.writerow(
['vehicle reward', 'server reward', 'extrinsic reward', 'intrinsic reward', 'completion ratio'])
test_writer.writerow([final_av_reward / args.max_test_length, final_av_server_reward / args.max_test_length,
av_ext_reward / args.max_test_length, av_int_rewards / args.max_test_length,
av_completion_ratio / args.max_test_length])
test_result_profile.close()
np.savez(result_path, np.asarray(all_remaining_energy))
print('Finish! Results saved in ', args.root_path)