-
Notifications
You must be signed in to change notification settings - Fork 13
/
testing_with_rome_traces.py
306 lines (262 loc) · 15.1 KB
/
testing_with_rome_traces.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import tensorflow as tf
import numpy as np
import utils.logger as logger
from sampler.migration_sampler import EvaluationSampler
from sampler.migration_sampler import EvaluationSamplerForDRQN
from sampler.migration_sampler import MigrationSampler
from sampler.migration_sampler import MigrationSamplerProcess
from baselines.rnn_critic_network_baseline import RNNCriticNetworkBaseline
from policies.random_solution import random_solution
from policies.always_migration_solution import always_migration_solution
from policies.optimal_solution import optimal_solution_for_batch_system_infos
from policies.no_migration_solution import no_migration_solution
from policies.fc_categorical_policy import FCCategoricalPolicyWithValue
from baselines.critic_network_baseline import CriticNetworkBaseline
from policies.q_network import QNetwork
from algorithms.dracm import DRACM
from dracm_trainer import Trainer
from algorithms.mab_ts import MABTSGuassianServiceMigration
import os
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
class Tester(object):
def __init__(self,
testing_env,
policy,
fc_policy,
rnn_q_net_policy,
q_net_policy,
eval_sampler,
fc_eval_sampler,
rnn_q_net_eval_sampler,
q_net_eval_sampler):
self.eval_env = testing_env
self.policy = policy
self.fc_policy = fc_policy
self.rnn_q_net_policy = rnn_q_net_policy
self.q_net_policy = q_net_policy
self.eval_sampler = eval_sampler
self.fc_eval_sampler = fc_eval_sampler
self.rnn_q_net_eval_sampler = rnn_q_net_eval_sampler
self.q_net_eval_sampler = q_net_eval_sampler
self.mab_ts_algo = MABTSGuassianServiceMigration(testing_env)
def run_test(self, rnn_model_path=None, fc_model_path=None, rnn_q_net_model_path=None, q_net_model_path=None, rnn_policy=True):
if rnn_model_path != None:
self.policy.load_weights(rnn_model_path)
logger.log("Load rnn model successfully....")
if fc_model_path !=None:
self.fc_policy.load_weights(fc_model_path)
logger.log("Load fc model successfully....")
if rnn_q_net_model_path != None:
self.rnn_q_net_policy.load_weights(q_net_model_path)
logger.log("Load rnn q network model successfully ....")
if q_net_model_path != None:
self.q_net_policy.load_weights(q_net_model_path)
logger.log("Load q network model successfully ....")
avg_ppo_rewards = 0.0
avg_fc_ppo_rewards = 0.0
avg_random_rewards = 0.0
avg_always_migrate_rewards = 0.0
avg_optimal_rewards = 0.0
avg_no_migration_rewards = 0.0
avg_ppo_sample_rewards = 0.0
avg_drqn_rewards = 0.0
avg_dqn_rewards = 0.0
avg_mab_ts_rewards = 0.0
iter_number = 3
for i in range(iter_number):
dqn_rewards_collects, _ = self.q_net_eval_sampler.obtain_samples(is_rnn=False)
drqn_rewards_collects, _ = self.rnn_q_net_eval_sampler.obtain_samples(is_rnn=rnn_policy)
sample_reward_collects, _ = self.eval_sampler.obtain_samples(is_rnn=rnn_policy, is_greedy_sample=False)
fc_reward_collects, _ = self.fc_eval_sampler.obtain_samples(is_rnn=False, is_greedy_sample=False)
reward_collects, system_info_collects = self.eval_sampler.obtain_samples(is_rnn=rnn_policy,
is_greedy_sample=True)
dqn_rewards = np.mean(np.sum(dqn_rewards_collects, axis=-1))
drqn_rewards = np.mean(np.sum(drqn_rewards_collects, axis=-1))
fc_ppo_rewards = np.mean(np.sum(fc_reward_collects, axis=-1))
ppo_rewards = np.mean(np.sum(reward_collects, axis=-1))
ppo_sample_rewards = np.mean(np.sum(sample_reward_collects, axis=-1))
random_rewards = random_solution(self.eval_sampler.env, system_info_collects)
always_migrate_rewards = always_migration_solution(self.eval_sampler.env, system_info_collects)
optimal_rewards = optimal_solution_for_batch_system_infos(self.eval_sampler.env, system_info_collects)
# optimal_rewards = 0.0
no_migration_rewards = no_migration_solution(self.eval_sampler.env, system_info_collects)
self.eval_env.reset()
totoal_rewards = self.mab_ts_algo.train(num_iteration=3)
# totoal_rewards = np.array([0.0])
mab_ts_rewards = totoal_rewards[-1]
logger.log("---- round " + str(i) + " ----")
logger.log("eval sample reward", ppo_sample_rewards)
logger.log("eval fc ppo reward", fc_ppo_rewards)
logger.log("eval dqn reward", dqn_rewards)
logger.log("eval drqn reward", drqn_rewards)
logger.log("eval reward", ppo_rewards)
logger.log("eval random reward", -random_rewards)
logger.log("eval always migration reward", -always_migrate_rewards)
logger.log("eval optimal reward", -optimal_rewards)
logger.log("eval mab-ts reward", -mab_ts_rewards)
logger.log("no_migration_solution", -no_migration_rewards)
avg_dqn_rewards += dqn_rewards
avg_ppo_rewards += ppo_rewards
avg_fc_ppo_rewards += fc_ppo_rewards
avg_drqn_rewards += drqn_rewards
avg_random_rewards += random_rewards
avg_always_migrate_rewards += always_migrate_rewards
avg_optimal_rewards += optimal_rewards
avg_no_migration_rewards += no_migration_rewards
avg_ppo_sample_rewards += ppo_sample_rewards
avg_mab_ts_rewards += mab_ts_rewards
logger.logkv("eval sample reward", avg_ppo_sample_rewards / iter_number)
logger.logkv("eval reward", avg_ppo_rewards / iter_number)
logger.logkv("eval fc ppo reward", avg_fc_ppo_rewards / iter_number)
logger.logkv("eval dqn reward", avg_dqn_rewards / iter_number)
logger.logkv("eval drqn reward", avg_drqn_rewards / iter_number)
logger.logkv("eval random reward", -(avg_random_rewards / iter_number))
logger.logkv("eval always migration reward", -(avg_always_migrate_rewards / iter_number))
logger.logkv("eval optimal reward", -(avg_optimal_rewards / iter_number))
logger.logkv("eval no migration reward", -(avg_no_migration_rewards / iter_number))
logger.logkv("eval mab-ts reward", -(avg_mab_ts_rewards / iter_number))
logger.dumpkvs()
if __name__ == "__main__":
from environment.batch_migration_env import EnvironmentParameters
from environment.batch_migration_env import BatchMigrationEnv
from policies.rnn_q_network import RNNQNetwork
from policies.q_network import QNetwork
from policies.rnn_policy_with_action_input import RNNPolicyWithValue
from algorithms.mab_ts import MABTSGuassianServiceMigration
logger.configure(dir="./log/test_with_trace_number_30", format_strs=['stdout', 'log', 'csv'])
number_of_base_state = 64
x_base_state = 8
y_base_state = 8
# possion_rate_vector = np.random.randint(15, 31, size=number_of_base_state)
# print("possion_rate_vector is: ", repr(possion_rate_vector))
possion_rate_vector = [7, 10, 8, 14, 15, 6, 20, 18, 11, 17, 20, 9, 8, 14, 9, 15, 8, 17, 9, 9, 10, 7, 17, 10,
13, 12, 5, 8, 10, 13, 19, 15, 10, 9, 10, 18, 12, 13, 5, 11, 7, 8, 8, 19, 15, 15, 6, 10,
5, 20, 17, 5, 5, 16, 5, 19, 19, 19, 9, 20, 17, 14, 17, 17]
# start point (41.856, 12.442), end point (41.928,12.5387), a region in Roman, Italy.
env_eval_parameters = EnvironmentParameters(trace_start_index=120,
num_traces=30,
server_frequency=128.0, # GHz
num_base_station=number_of_base_state,
optical_fiber_trans_rate=500.0,
backhaul_coefficient=0.02,
migration_coefficient_low=1.0,
migration_coefficient_high=3.0,
server_poisson_rate=possion_rate_vector,
client_poisson_rate=2,
server_task_data_lower_bound=(0.05 * 1000.0 * 1000.0 * 8),
server_task_data_higher_bound=(5 * 1000.0 * 1000.0 * 8),
client_task_data_lower_bound=(0.05 * 1000.0 * 1000.0 * 8),
client_task_data_higher_bound=(5 * 1000.0 * 1000.0 * 8),
migration_size_low=0.5,
migration_size_high=100.0,
ratio_lower_bound=200.0,
ratio_higher_bound=10000.0,
map_width=8000.0, map_height=8000.0,
num_horizon_servers=x_base_state, num_vertical_servers=y_base_state,
traces_file_path='./environment/rome_traces_coordinate.txt',
transmission_rates=[60.0, 48.0, 36.0, 24.0, 12.0], # Mbps
trace_length=100,
trace_interval=12,
is_full_observation=False,
is_full_action=True)
env = BatchMigrationEnv(env_eval_parameters)
eval_sample_size = 30
print("action dim of the environment: ", env._action_dim)
rnn_policy = RNNPolicyWithValue(observation_dim=env._state_dim,
action_dim=env._action_dim,
rnn_parameter=256,
embbeding_size=2)
vf_baseline = RNNCriticNetworkBaseline(rnn_policy)
sampler = MigrationSampler(env,
policy=rnn_policy,
batch_size=400,
num_environment_per_core=2,
max_path_length=100,
parallel=True,
num_process=5,
is_norm_reward=False)
eval_sampler = EvaluationSampler(env,
policy=rnn_policy,
batch_size=eval_sample_size,
max_path_length=100)
sampler_process = MigrationSamplerProcess(baseline=vf_baseline,
discount=0.99,
gae_lambda=0.95,
normalize_adv=True,
positive_adv=False)
fc_policy = FCCategoricalPolicyWithValue(observation_dim=env._state_dim,
action_dim=env._action_dim,
fc_parameters=[256])
vf_baseline = CriticNetworkBaseline(fc_policy)
fc_sampler = MigrationSampler(env,
policy=fc_policy,
batch_size=4800,
num_environment_per_core=2,
max_path_length=100,
parallel=True,
num_process=8,
is_norm_reward=True) # 2 * 4 * 30
fc_eval_sampler = EvaluationSampler(env,
policy=fc_policy,
batch_size=10,
max_path_length=100)
algo = DRACM(policy=rnn_policy,
value_function=rnn_policy,
policy_optimizer=tf.keras.optimizers.Adam(5e-4),
value_optimizer=tf.keras.optimizers.Adam(5e-4),
is_rnn=True,
is_shared_critic_net=True,
num_inner_grad_steps=4,
clip_value=0.2,
vf_coef=0.5,
max_grad_norm=0.5,
entropy_coef=0.01)
trainer = Trainer(train_env=env,
eval_env=env,
algo=algo,
sampler=sampler,
sample_processor=sampler_process,
update_batch_size=100,
policy=rnn_policy,
n_itr=10,
save_interval=5,
eval_sampler=eval_sampler,
test_interval=10,
save_path=None)
rnn_q_network = RNNQNetwork(observation_dim=env._state_dim,
action_dim=env._action_dim,
rnn_parameter=256,
fc_parameters=128,
epsilon=0.1)
rnn_q_net_sampler = EvaluationSamplerForDRQN(env,
policy=rnn_q_network,
batch_size=eval_sample_size,
max_path_length=100)
q_network = QNetwork(observation_dim=env._state_dim,
action_dim=env._action_dim,
hidden_parameter=256,
fc_parameters=128,
epsilon=0.1)
q_network_eval_sampler = EvaluationSamplerForDRQN(env,
policy=q_network,
batch_size=30,
max_path_length=100,
is_rnn=False)
tester = Tester(testing_env=env,
policy=rnn_policy,
fc_policy=fc_policy,
rnn_q_net_policy=rnn_q_network,
q_net_policy=q_network,
eval_sampler=eval_sampler,
fc_eval_sampler=fc_eval_sampler,
rnn_q_net_eval_sampler=rnn_q_net_sampler,
q_net_eval_sampler=q_network_eval_sampler)
dracm_model_path = "./checkpoints_rome/checkpoints_ppo_64-bs-new-2/model_checkpoint_epoch_115"
fc_dracm_model_path = "./checkpoints_rome/checkpoints_ppo_64-bs-new-no-rnn/model_checkpoint_epoch_115"
drqn_model_path = "./checkpoints_rome/checkpoints_drqn_rome_64-bs-new/model_checkpoint_3800"
dqn_model_path = "./checkpoints_rome/checkpoints_dqn_rome_64-bs-new/model_checkpoint_3800"
tester.run_test(rnn_model_path=dracm_model_path,
fc_model_path=fc_dracm_model_path,
rnn_q_net_model_path=drqn_model_path,
q_net_model_path=dqn_model_path,
rnn_policy=True)