MCPcopy Create free account
hub / github.com/BIT-MCS/DRL-eFresh / test

Function test

test_agent.py:13–96  ·  view source on GitHub ↗
(local_time, model_path, device)

Source from the content-addressed store, hash-verified

11
12
13def test(local_time, model_path, device):
14 os.environ['CUDA_VISIBLE_DEVICES'] = "-1"
15 util = Util(device)
16 torch.manual_seed(params.seed)
17 torch.set_num_threads(4)
18 worker_id = 0
19
20 # ----------------make environment----------------------
21 env = Make_Env(device, params.max_time_steps, local_time, worker_id)
22 # -----------------load parameters----------------------
23 obs_shape = env.observ_shape
24 uav_num = params.uav_num
25
26 # ---------------create local model---------------------
27 local_ppo_model = Model(obs_shape, uav_num, device, trainable=False)
28 local_ppo_model.load_state_dict(torch.load(model_path, map_location='cpu'))
29 local_ppo_model.to(device)
30
31 episode_length = 0
32 interact_time = 0
33 # --------------define file writer-----------------------
34 file_root_path = os.path.join(params.root_path, str(local_time) + '/' + str(+worker_id) + '/file')
35 os.makedirs(file_root_path)
36
37 print('Starting a new TEST iterations...')
38 print("Log_dir:",file_root_path)
39
40 reward_file = open(os.path.join(file_root_path, 'test_reward.csv'), 'w', newline='')
41 reward_writer = csv.writer(reward_file)
42 while True:
43 if episode_length >= params.max_test_episode:
44 print('testing over')
45 break
46 print('---------------in episode ', episode_length, '-----------------------')
47 step = 0
48 av_reward = 0
49 cur_obs, uav_aoi, uav_snr, uav_tuse, uav_effort = env.reset()
50 temporal_hidden_states = torch.zeros(params.temporal_hidden_size).unsqueeze(0)
51 spatial_hidden_state=torch.zeros(params.spatial_hidden_size,8, 8).unsqueeze(0)
52 masks = torch.ones(1)
53
54 while step < params.max_time_steps:
55 interact_time += 1
56 # ----------------sample actions(no grad)------------------------
57 with torch.no_grad():
58 if params.use_rnn:
59 if params.use_spatial_att:
60 value, action, action_log_probs, temporal_hidden_states,spatial_hidden_state = local_ppo_model.act(cur_obs, uav_aoi,
61 uav_snr, uav_tuse,
62 uav_effort,
63 temporal_hidden_states,
64 masks,
65 spatial_hidden_state)
66 else:
67 value, action, action_log_probs, temporal_hidden_states = local_ppo_model.act(cur_obs, uav_aoi,
68 uav_snr, uav_tuse,
69 uav_effort,
70 temporal_hidden_states,

Callers 1

main.pyFile · 0.90

Calls 9

resetMethod · 0.95
actMethod · 0.95
stepMethod · 0.95
to_numpyMethod · 0.95
draw_pathMethod · 0.95
UtilClass · 0.90
Make_EnvClass · 0.90
ModelClass · 0.90
toMethod · 0.80

Tested by

no test coverage detected