-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathactor_critic_test.py
128 lines (91 loc) · 2.91 KB
/
actor_critic_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
125
126
127
128
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import matplotlib
matplotlib.use('agg')
import time
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import environments as envs
from utils import *
from param import *
from load_balance.heuristic_agents import *
from actor_agent import *
def run_test(agent, num_exp=100):
# set up environment
env = envs.make(args.env)
all_total_reward = []
# run experiment
for ep in range(num_exp):
env.set_random_seed(100000000 + ep)
env.reset()
total_reward = 0
state = env.observe()
done = False
while not done:
act = agent.get_action(state)
state, reward, done = env.step(act)
total_reward += reward
all_total_reward.append(total_reward)
return all_total_reward
def main():
np.random.seed(args.seed)
tf.set_random_seed(args.seed)
# create result and model folder
create_folder_if_not_exists(args.result_folder)
# different agents for different environments
if args.env == 'load_balance':
schemes = ['shortest_processing_time', 'learn']
else:
schemes = ['learn']
# tensorflow session
sess = tf.Session()
# store results
all_performance = {scheme: [] for scheme in schemes}
# create environment
env = envs.make(args.env)
# initialize all agents
agents = {}
for scheme in schemes:
if scheme == 'learn':
agents[scheme] = ActorAgent(sess)
# saver for loading trained model
saver = tf.train.Saver(max_to_keep=args.num_saved_models)
# initialize parameters
sess.run(tf.global_variables_initializer())
# load trained model
if args.saved_model is not None:
saver.restore(sess, args.saved_model)
elif scheme == 'leat_work':
agents[scheme] = LeastWorkAgent()
elif scheme == 'shortest_processing_time':
agents[scheme] = ShortestProcessingTimeAgent()
else:
print('invalid scheme', scheme)
exit(1)
# store results
all_performance = {}
# plot job duration cdf
fig = plt.figure()
title = 'average: '
for scheme in schemes:
all_total_reward = run_test(agents[scheme], num_exp=args.num_ep)
all_performance[scheme] = all_total_reward
x, y = compute_CDF(all_total_reward)
plt.plot(x, y)
title += ' ' + scheme + ' '
title += '%.2f' % np.mean(all_total_reward)
plt.xlabel('Total reward')
plt.ylabel('CDF')
plt.title(title)
plt.legend(schemes)
fig.savefig(args.result_folder + \
args.env + '_all_performance.png')
plt.close(fig)
# save all job durations
np.save(args.result_folder + \
args.env + '_all_performance.npy', \
all_performance)
sess.close()
if __name__ == '__main__':
main()