-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_graphs.py
More file actions
133 lines (106 loc) · 5.44 KB
/
plot_graphs.py
File metadata and controls
133 lines (106 loc) · 5.44 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
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
from header_import import *
class plot_graphs(Windy_World):
def __init__(self, action_type, wind_type):
super().__init__(action_type, wind_type)
self.path = "graphs_charts/"
self.chart_path = self.path + "charts/"
self.enviroment = self.path + "enviroment/"
def action_path(self, q_value):
x, y = (0, 3)
path = [(0,3)]
for _ in range(100):
best_action = np.argmax([q_value[(x,y), a] for a in self.action_space])
x, y = self.transition(x, y, best_action)
path.append((x,y))
if x == self.goal[0] and y == self.goal[1]:
break
return path
def plot_windy(self, q_value, type_graph = "reward", type_graph_name = "default"):
action_path = self.action_path(q_value)
fig = plt.figure()
axis = fig.add_subplot(111)
axis.set_xlim(-0.5, 9.5)
axis.set_ylim(-0.5, 6.5)
axis.set_yticks([])
axis.set_xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
axis.set_xticklabels([0, 0, 0, 1, 1, 1, 2, 2, 1, 0])
axis.text(0, 3, 'S', fontsize=18, horizontalalignment='center', verticalalignment='center')
axis.text(7, 3, 'G', fontsize=18, horizontalalignment='center', verticalalignment='center')
for x in range(10):
for y in range(7):
axis.add_patch(patches.Rectangle([x-0.5, y-0.5], 1, 1, fill=False))
params = {'head_width':0.2, 'head_length':0.2, 'color':'gray', 'alpha':.2}
if self.action_type == "regular":
action = np.argmax([q_value[(x,y), a] for a in self.action_space])
if action == 0:
axis.arrow(x, y, -0.1, 0, **params)
elif action == 1:
axis.arrow(x, y, 0, -0.1, **params)
elif action == 2:
axis.arrow(x, y, 0.1, 0, **params)
elif action == 3:
axis.arrow(x, y, 0, 0.1, **params)
elif self.action_type == "king":
action = np.argmax([q_value[(x,y), a] for a in self.action_space])
if action == 0:
axis.arrow(x, y, -0.1, 0, **params)
elif action == 1:
axis.arrow(x, y, 0, -0.1, **params)
elif action == 2:
axis.arrow(x, y, 0.1, 0, **params)
elif action == 3:
axis.arrow(x, y, 0, 0.1, **params)
elif action ==4:
axis.arrow(x, y, -0.1, -0.1, **params)
elif action == 5:
axis.arrow(x, y, -0.1, 0.1, **params)
elif action == 6:
axis.arrow(x, y, 0.1, -0.1, **params)
elif action == 7:
axis.arrow(x, y, 0.1, 0.1, **params)
elif self.action_type == "king_zero":
action = np.argmax([q_value[(x,y), a] for a in self.action_space])
if action == 0:
axis.arrow(x, y, -0.1, 0, **params)
elif action == 1:
axis.arrow(x, y, 0, -0.1, **params)
elif action == 2:
axis.arrow(x, y, 0.1, 0, **params)
elif action == 3:
axis.arrow(x, y, 0, 0.1, **params)
elif action ==4:
axis.arrow(x, y, -0.1, -0.1, **params)
elif action == 5:
axis.arrow(x, y, -0.1, 0.1, **params)
elif action == 6:
axis.arrow(x, y, 0.1, -0.1, **params)
elif action == 7:
axis.arrow(x, y, 0.1, 0.1, **params)
elif action == 8:
axis.arrow(x, y, 0, 0, **params)
for i in range(len(action_path)-1):
x, y = action_path[i]
next_x, next_y = action_path[i+1]
axis.plot([x, next_x], [y, next_y], color='blue', alpha=1.0)
plt.savefig((str(self.enviroment) + type_graph_name + "_" + type_graph + "_paths.png"), dpi =500)
def plot_episode_time_step(self, data, type_graph = "reward", type_graph_name = "default"):
fig = plt.figure()
axis = fig.add_subplot(111)
if type_graph == "reward":
axis.plot(data, color='blue')
plt.axhline(y=0.2, color='red', linestyle='-')
axis.set_title(type_graph_name+ " Reward vs Time Step")
axis.set_xlabel("Time Steps")
axis.set_ylabel("Reward per Step")
elif type_graph == "action":
axis.plot(data, color='blue')
axis.set_title(type_graph_name + " Maximal Action vs Time Step")
plt.axhline(y=0.36, color='red', linestyle='-')
axis.set_xlabel("Time Steps")
axis.set_ylabel("Maximal Action Value")
elif type_graph == "time_step":
axis.plot(data, color='blue')
axis.set_title(type_graph_name +" Episode vs Time Step")
axis.set_xlabel("Time Steps")
axis.set_ylabel("Episodes")
plt.savefig((str(self.chart_path) + type_graph_name + "_" + type_graph + "_.png"), dpi =500)