-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvisualization.py
More file actions
96 lines (71 loc) · 2.94 KB
/
Copy pathvisualization.py
File metadata and controls
96 lines (71 loc) · 2.94 KB
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
import matplotlib.pyplot as plt
Q_LEARNING = 'q_learning'
Q_LEARNING_5 = 'q_learning_5'
Q_LEARNING_10 = 'q_learning_10'
Q_LEARNING_DOUBLE = 'q_learning_double'
NUM_1000 = 1000
NUM_10000 = 10000
NUM_20000 = 20000
BASE_LINE = 150
def read_train_data(algo):
file_path = 'saved_networks/' + algo + '/data/scores.txt'
file_score = open(file_path, 'r')
scores = []
for line in file_score.readlines():
score = map(int, line.strip()[1:-1].split(','))
scores.extend(score)
file_score.close()
return scores
def read_evaluation_data(algo):
if algo == 'dqn':
file_path = 'saved_networks/dqn.txt'
else:
file_path = 'saved_networks/' + algo + '/data/evaluations.txt'
file_data = open(file_path, 'r')
score = [0] + map(float, file_data.readline().strip().split(' '))
file_data.close()
return score
def scatter_plot(algo, num):
train_data = read_train_data(algo)
eval_data = read_evaluation_data(algo)
plt.scatter(range(1, len(train_data)+1) ,train_data, marker='.', label='Training')
plt.plot(range(0, len(eval_data)*num, num) , eval_data, color='r', linewidth=2.5, label='Evaluation')
plt.plot([0, len(train_data),], [BASE_LINE, BASE_LINE,], 'k--', color='black', linewidth=2.5, label='Goal 150')
plt.title('Q Learning 10 * 10')
plt.xlabel('Episode')
plt.ylabel('Score')
plt.legend()
plt.show()
def display_plot():
eval_data_1 = read_evaluation_data(Q_LEARNING)
plt.plot(range(0, len(eval_data_1)*NUM_10000, NUM_10000) , eval_data_1, color='r', linewidth=2.5, label='1*1')
eval_data_5 = read_evaluation_data(Q_LEARNING_5)
plt.plot(range(0, len(eval_data_5)*NUM_1000, NUM_1000) , eval_data_5, color='g', linewidth=2.5, label='5*5')
eval_data_10 = read_evaluation_data(Q_LEARNING_10)
plt.plot(range(0, len(eval_data_10)*NUM_1000, NUM_1000) , eval_data_10, color='b', linewidth=2.5, label='10*10')
eval_data_double = read_evaluation_data(Q_LEARNING_DOUBLE)
plt.plot(range(0, len(eval_data_double)*NUM_1000, NUM_1000) , eval_data_double, color='c', linewidth=2.5, label='Double')
plt.plot([0, len(eval_data_double)*NUM_1000, ], [BASE_LINE, BASE_LINE, ], 'k--', color='black', linewidth=2.5, label='Goal 150')
plt.title('Q Learning')
plt.xlabel('Episode')
plt.ylabel('Score')
plt.legend()
plt.show()
def dqn_plot(algo, num):
eval_data = read_evaluation_data(algo)
plt.plot(range(0, len(eval_data)*num, num) , eval_data, color='r', linewidth=2.5, label='Evaluation')
plt.plot([0, len(eval_data)*num,], [BASE_LINE, BASE_LINE,], 'k--', color='black', linewidth=2.5, label='Goal 150')
plt.title('DQN')
plt.xlabel('Update')
plt.ylabel('Score')
plt.legend()
plt.show()
if __name__ == '__main__':
# q_learning & sarsa scatter plot
# algo = "q_learning_10"
# scatter_plot(algo, NUM_1000)
# # compare q_learning methods
# display_plot()
#
# # dqn
dqn_plot('dqn', NUM_20000)