-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathagents.py
More file actions
474 lines (327 loc) · 14 KB
/
Copy pathagents.py
File metadata and controls
474 lines (327 loc) · 14 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
"""
This file contains implementation of all the agents.
"""
from abc import ABC, abstractmethod
from util import *
import random
from game import CHECKERS_FEATURE_COUNT, checkers_features, checkers_reward
import numpy as np
class Agent(ABC):
def __init__(self, is_learning_agent=False):
self.is_learning_agent = is_learning_agent
self.has_been_learning_agent = is_learning_agent
@abstractmethod
def get_action(self, state):
"""
state: the state in which to take action
Returns: the single action to take in this state
"""
pass
class KeyBoardAgent(Agent):
def __init__(self):
Agent.__init__(self)
def get_action(self, state):
"""
state: the current state from which to take action
Returns: list of starting position, ending position
"""
start = [int(pos) for pos in input("Enter start position (e.g. x y): ").split(" ")]
end = [int(pos) for pos in input("Enter end position (e.g. x y): ").split(" ")]
ends = []
i=1
while i < len(end):
ends.append([end[i-1], end[i]])
i += 2
action = [start] + ends
return action
class AlphaBetaAgent(Agent):
def __init__(self, depth):
Agent.__init__(self, is_learning_agent=False)
self.depth = depth
def evaluation_function(self, state, agent=True):
"""
state: the state to evaluate
agent: True if the evaluation function is in favor of the first agent and false if
evaluation function is in favor of second agent
Returns: the value of evaluation
"""
agent_ind = 0 if agent else 1
other_ind = 1 - agent_ind
if state.is_game_over():
if agent and state.is_first_agent_win():
return 500
if not agent and state.is_second_agent_win():
return 500
return -500
pieces_and_kings = state.get_pieces_and_kings()
return pieces_and_kings[agent_ind] + 2 * pieces_and_kings[agent_ind + 2] - \
(pieces_and_kings[other_ind] + 2 * pieces_and_kings[other_ind + 2])
def get_action(self, state):
def mini_max(state, depth, agent, A, B):
if agent >= state.get_num_agents():
agent = 0
depth += 1
if depth == self.depth or state.is_game_over():
return [None, self.evaluation_function(state, max_agent)]
elif agent == 0:
return maximum(state, depth, agent, A, B)
else:
return minimum(state, depth, agent, A, B)
def maximum(state, depth, agent, A, B):
output = [None, -float("inf")]
actions_list = state.get_legal_actions()
if not actions_list:
return [None, self.evaluation_function(state, max_agent)]
for action in actions_list:
current = state.generate_successor(action)
val = mini_max(current, depth, agent + 1, A, B)
check = val[1]
if check > output[1]:
output = [action, check]
if check > B:
return [action, check]
A = max(A, check)
return output
def minimum(state, depth, agent, A, B):
output = [None, float("inf")]
actions_list = state.get_legal_actions()
if not actions_list:
return [None, self.evaluation_function(state, max_agent)]
for action in actions_list:
current = state.generate_successor(action)
val = mini_max(current, depth, agent+1, A, B)
check = val[1]
if check < output[1]:
output = [action, check]
if check < A:
return [action, check]
B = min(B, check)
return output
# max_agent is true meaning it is the turn of first player at the state in
# which to choose the action
max_agent = state.is_first_agent_turn()
output = mini_max(state, -1, 0, -float("inf"), float("inf"))
return output[0]
class ReinforcementLearningAgent(Agent):
def __init__(self, is_learning_agent=True):
Agent.__init__(self, is_learning_agent)
self.episodes_so_far = 0
@abstractmethod
def get_action(self, state):
"""
state: the current state from which to take action
Returns: the action to perform
"""
# TODO call do_action from this method
pass
@abstractmethod
def update(self, state, action, next_state, reward):
"""
performs update for the learning agent
state: the state (s) in which action was taken
action: the action (a) taken in the state (s)
next_state: the next state (s'), in which agnet will perform next action,
that resulted from state (s) and action (a)
reward: reward obtained for taking action (a) in state (s) and going to next state (s')
"""
pass
def start_episode(self):
# Accumulate rewards while training for each episode and show total rewards
# at the end of each episode i.e. when stop episode
self.prev_state = None
self.prev_action = None
self.episode_rewards = 0.0
def stop_episode(self):
# print('reward this episode', self.episode_rewards)
pass
@abstractmethod
def start_learning(self):
pass
@abstractmethod
def stop_learning(self):
pass
@abstractmethod
def observe_transition(self, state, action, next_state, reward, next_action=None):
pass
@abstractmethod
def observation_function(self, state):
pass
# TODO
def reward_function(self, state, action, next_state):
# make a reward function for the environment
return checkers_reward(state, action, next_state)
def do_action(self, state, action):
"""
called by get_action to update previous state and action
"""
self.prev_state = state
self.prev_action = action
class QLearningAgent(ReinforcementLearningAgent):
def __init__(self, alpha=0.01, gamma=0.1, epsilon=0.5, is_learning_agent=True, weights=None):
"""
alpha: learning rate
gamma: discount factor
epsilon: exploration constant
is_learning_agent: whether to treat this agent as learning agent or not
weights: default weights
"""
ReinforcementLearningAgent.__init__(self, is_learning_agent=is_learning_agent)
self.original_alpha = alpha
self.original_epsilon = epsilon
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
if not is_learning_agent:
self.epsilon = 0.0
self.alpha = 0.0
if weights is None:
# initialize weights for the features
self.weights = np.zeros(CHECKERS_FEATURE_COUNT)
else:
if len(weights) != CHECKERS_FEATURE_COUNT:
raise Exception("Invalid weights " + weights)
self.weights = np.array(weights, dtype=float)
def start_learning(self):
"""
called by environment to notify agent of starting new episode
"""
self.alpha = self.original_alpha
self.epsilon = self.original_epsilon
self.is_learning_agent = True
def stop_learning(self):
"""
called by environment to notify agent about end of episode
"""
self.alpha = 0.0
self.epsilon = 0.0
self.is_learning_agent = False
def get_q_value(self, state, action, features):
"""
Returns: Q(state,action)
"""
q_value = np.dot(self.weights, features)
return q_value
def compute_value_from_q_values(self, state):
"""
Returns: max_action Q(state, action) where the max is over legal actions.
If there are no legal actions, which is the case at the terminal state,
return a value of 0.0.
"""
actions = state.get_legal_actions()
if not actions:
return 0.0
q_values = \
[self.get_q_value(state, action, checkers_features(state, action)) for action in actions]
return max(q_values)
def compute_action_from_q_values(self, state, actions):
"""
Returns: the best action to take in a state. If there are no legal actions,
which is the case at the terminal state, return None.
"""
if not actions:
return None
# if max_value < 0:
# return random.choice(actions)
arg_max = np.argmax([self.get_q_value(state, action, checkers_features(state, action))
for action in actions])
return actions[arg_max]
def get_action(self, state):
"""
Returns: the action to take in the current state. With probability self.epsilon,
take a random action and take the best policy action otherwise. If there are
no legal actions, which is the case at the terminal state, returns None.
"""
# Pick Action
legal_actions = state.get_legal_actions()
action = None
if not legal_actions:
return None
if flip_coin(self.epsilon):
action = random.choice(legal_actions)
else:
action = self.compute_action_from_q_values(state, legal_actions)
self.do_action(state, action)
return action
def update(self, state, action, next_state, reward):
features = checkers_features(state, action)
expected = reward + self.gamma * self.compute_value_from_q_values(next_state)
current = self.get_q_value(state, action, features)
temporal_difference = expected - current
for i in range(CHECKERS_FEATURE_COUNT):
self.weights[i] = self.weights[i] + self.alpha * (temporal_difference) * features[i]
def getPolicy(self, state):
return self.compute_action_from_q_values(state, state.get_legal_actions())
def getValue(self, state):
return self.compute_value_from_q_values(state)
def observe_transition(self, state, action, next_state, reward, next_action=None):
"""
state: the state (s) in which action was taken
action: the action (a) taken in the state (s)
next_state: the next state (s'), in which agnet will perform next action,
that resulted from state (s) and action (a)
reward: reward obtained for taking action (a) in state (s) and going to next state (s')
"""
self.episode_rewards += reward
self.update(state, action, next_state, reward)
def observation_function(self, state):
if self.prev_state is not None:
reward = self.reward_function(self.prev_state, self.prev_action, state)
# print('reward is', reward)
self.observe_transition(self.prev_state, self.prev_action, state, reward)
def update_parameters(self, freq, num_games):
if num_games % freq == 0:
self.original_alpha /= 2.0
self.original_epsilon /= 2.0
class SarsaLearningAgent(QLearningAgent):
def __init__(self, alpha=0.01, gamma=0.1, epsilon=0.5, is_learning_agent=True, weights=None):
QLearningAgent.__init__(self, alpha, gamma, epsilon, is_learning_agent, weights)
def update(self, state, action, next_state, next_action, reward):
features = checkers_features(state, action)
if next_action is None:
next_q_value = 0.0
else:
next_q_value = \
self.get_q_value(next_state, next_action, checkers_features(next_state, next_action))
expected = reward + self.gamma * next_q_value
current = self.get_q_value(state, action, features)
temporal_difference = expected - current
for i in range(CHECKERS_FEATURE_COUNT):
self.weights[i] = self.weights[i] + self.alpha * (temporal_difference) * features[i]
def observe_transition(self, state, action, next_state, next_action, reward):
"""
state: the state (s) in which action was taken
action: the action (a) taken in the state (s)
next_state: the next state (s'), in which agnet will perform next action,
that resulted from state (s) and action (a)
reward: reward obtained for taking action (a) in state (s) and going to next state (s')
"""
self.episode_rewards += reward
self.update(state, action, next_state, next_action, reward)
def observation_function(self, state):
if self.prev_state is not None:
reward = self.reward_function(self.prev_state, self.prev_action, state)
# print('reward is', reward)
action = self.get_action(state)
self.observe_transition(self.prev_state, self.prev_action, state, action, reward)
return action
class SarsaSoftmaxAgent(SarsaLearningAgent):
def __init__(self, alpha=0.01, gamma=0.1, t=1.0, is_learning_agent=True, weights=None):
SarsaLearningAgent.__init__(self, alpha=alpha, gamma=gamma,
is_learning_agent=is_learning_agent, weights=weights)
self.t = t
def get_action(self, state):
legal_actions = state.get_legal_actions()
if not legal_actions:
return None
if self.epsilon == 0.0:
return self.compute_action_from_q_values(state, legal_actions)
q_values = [self.get_q_value(state, action, checkers_features(state, action))
for action in legal_actions]
exps = np.exp(q_values) / self.t
probs = exps / np.sum(exps)
action_ind = np.random.choice(len(legal_actions), p=probs)
self.do_action(state, legal_actions[action_ind])
return legal_actions[action_ind]
def update_parameters(self, freq, num_games):
if num_games % freq == 0:
self.t /= 2.0