-
Notifications
You must be signed in to change notification settings - Fork 1
/
learner.py
227 lines (185 loc) · 10.1 KB
/
learner.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
from header_import import *
class sarsa_algorithm(Grid_World_Enviroment_with_Wind_Obstacle):
def __init__(self, episode, gamma, alpha, epsilon, max_time_step, grid_world_size):
super().__init__(grid_world_size)
self.alpha = alpha
self.epsilon = epsilon
self.gamma = gamma
self.number_episode = episode
self.max_time_step = max_time_step
self.q_value = defaultdict(float)
self.cumulative_reward = []
self.step_number = []
def argmax_rand(self, max_act):
return np.random.choice(np.flatnonzero(max_act == np.max(max_act)))
def policy(self, state):
if np.random.rand() > self.epsilon:
return self.argmax_rand([self.q_value[state, a] for a in self.action_space])
else:
return np.random.choice(self.action_space)
def sarsa(self, state):
if state != "all":
for _ in range(self.number_episode):
state = self.reset(start=(0,0))
count = 0
reward_list = 0
action = self.policy(state)
while True:
done, reward, next_state = self.step(action)
next_action = self.policy(next_state)
self.q_value[state, action] = self.q_value[state, action] + self.alpha * (reward + self.gamma * self.q_value[next_state, next_action] - self.q_value[state, action])
state, action = next_state, next_action
reward_list += reward
count += 1
if done:
break
self.step_number.append(count)
self.cumulative_reward.append(reward_list)
return self.cumulative_reward, self.step_number
else:
for _ in range(self.number_episode):
for i in range(self.grid_world_size):
for ii in range(self.grid_world_size):
state = self.reset(start=(i,ii))
action = self.policy(state)
while True:
done, reward, next_state = self.step(action)
next_action = self.policy(next_state)
self.q_value[state, action] = self.q_value[state, action] + self.alpha * (reward + self.gamma * self.q_value[next_state, next_action] - self.q_value[state, action])
state, action = next_state, next_action
if i == 17 and ii == 12:
break
if done:
break
return self.q_value
class learner(Grid_World_Enviroment_with_Wind_Obstacle):
def __init__(self, episode, gamma, alpha, epsilon, max_time_step, grid_world_size):
super().__init__(grid_world_size)
self.alpha = alpha
self.epsilon = epsilon
self.gamma = gamma
self.number_episode = episode
self.max_time_step = max_time_step
self.q_value = defaultdict(float)
self.cumulative_reward = []
self.step_number = []
def argmax_rand(self, max_act):
pass
def policy(self, state):
pass
def learn(self):
for i in range(self.number_episode):
state = self.reset()
while True:
action = np.random.choice(self.action_space)
done, reward, next_state = self.step(action)
state = next_state
if done:
break
class q_learning_algorithm(Grid_World_Enviroment_with_Wind_Obstacle):
def __init__(self, episode, gamma, alpha, epsilon, max_time_step, grid_world_size):
super().__init__(grid_world_size)
self.alpha = alpha
self.epsilon = epsilon
self.gamma = gamma
self.epsilon = epsilon
self.number_episode = episode
self.max_time_step = max_time_step
self.q_value = defaultdict(float)
self.q_value_2 = defaultdict(float)
self.q_value_sum = defaultdict(float)
self.cumulative_reward = []
self.step_number = []
def argmax_rand(self, max_act):
return np.random.choice(np.flatnonzero(max_act == np.max(max_act)))
def policy(self, state):
if np.random.rand() > self.epsilon:
return self.argmax_rand([self.q_value[state, a] for a in self.action_space])
else:
return np.random.choice(self.action_space)
def q_learning(self, state):
if state != "all":
for _ in range(self.number_episode):
state = self.reset(start=(0,0))
count = 0
reward_list = 0
while True:
action = self.policy(state)
done, reward, next_state = self.step(action)
next_best_action = self.argmax_rand([self.q_value[next_state, a] for a in self.action_space])
self.q_value[state, action] = self.q_value[state, action] + self.alpha * (reward + self.gamma * self.q_value[next_state, next_best_action] - self.q_value[state, action])
reward_list += reward
state = next_state
count += 1
if done:
break
self.step_number.append(count)
self.cumulative_reward.append(reward_list)
return self.cumulative_reward, self.step_number
else:
for i in range(self.grid_world_size):
for ii in range(self.grid_world_size):
for _ in range(self.number_episode):
state = self.reset(start=(i,ii))
while True:
action = self.policy(state)
done, reward, next_state = self.step(action)
next_best_action = self.argmax_rand([self.q_value[next_state, a] for a in self.action_space])
self.q_value[state, action] = self.q_value[state, action] + self.alpha * (reward + self.gamma * self.q_value[next_state, next_best_action] - self.q_value[state, action])
state = next_state
if i == 17 and ii == 12:
break
if done:
break
return self.q_value
def double_q_learning(self, state):
next_best_action1 = 0
next_best_action2 = 0
if state != "all":
for _ in range(self.number_episode):
state = self.reset(start=(0,0))
count = 0
reward_list = 0
action = self.policy(state)
while True:
done, reward, next_state = self.step(action)
if random.random()<0.5:
next_best_action1 = self.argmax_rand([self.q_value[next_state, a] for a in self.action_space])
self.q_value[state, action] = self.q_value[state, action] + self.alpha * (reward + self.gamma * self.q_value_2[next_state, next_best_action1] - self.q_value[state, action])
else:
next_best_action2 = self.argmax_rand([self.q_value_2[next_state, a] for a in self.action_space])
self.q_value_2[state, action] = self.q_value_2[state, action] + self.alpha * (reward + self.gamma * self.q_value[next_state, next_best_action2] - self.q_value_2[state, action])
self.q_value_sum[state, action] = self.q_value[state, action] + self.q_value_2[state, action]
action = self.argmax_rand([self.q_value_sum[next_state, a] for a in self.action_space])
reward_list += reward
state = next_state
count += 1
if done:
break
self.step_number.append(count)
self.cumulative_reward.append(reward_list)
self.q_value_sum[state, action] = self.q_value[state, action] + self.q_value_2[state, action]
return self.cumulative_reward, self.step_number
else:
for _ in range(self.number_episode):
for i in range(self.grid_world_size):
for ii in range(self.grid_world_size):
state = self.reset(start=(i,ii))
action = self.policy(state)
while True:
done, reward, next_state = self.step(action)
if random.random()<0.5:
next_best_action1 = self.argmax_rand([self.q_value[next_state, a] for a in self.action_space])
self.q_value[state, action] = self.q_value[state, action] + self.alpha * (reward + self.gamma * self.q_value_2[next_state, next_best_action1] - self.q_value[state, action])
else:
next_best_action2 = self.argmax_rand([self.q_value_2[next_state, a] for a in self.action_space])
self.q_value_2[state, action] = self.q_value_2[state, action] + self.alpha * (reward + self.gamma * self.q_value[next_state, next_best_action2] - self.q_value_2[state, action])
self.q_value_sum[state, action] = self.q_value[state, action] + self.q_value_2[state, action]
action = self.argmax_rand([self.q_value_sum[next_state, a] for a in self.action_space])
state = next_state
if i == 17 and ii == 12:
break
if done:
break
self.q_value_sum[state, action] = self.q_value[state, action] + self.q_value_2[state, action]
return self.q_value_sum