-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagents.py
87 lines (65 loc) · 2.56 KB
/
agents.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
import numpy as np
import collections
class Agent:
'''Agent Base.'''
def __init__(self, game, display=None):
self.game = game
self.display = display
def play(self, max_iter=np.inf, verbose=False):
n_iter = 0
while (n_iter < max_iter) and (not self.game.end):
direction = self.step()
self.game.move(direction)
n_iter += 1
if verbose:
print("Iter: {}".format(n_iter))
print("======Direction: {}======".format(
["left", "down", "right", "up"][direction]))
if self.display is not None:
self.display.display(self.game)
# added to keep track of exploration for the learning agent
def play_learn(self, max_iter=np.inf, verbose=False):
n_iter = 0
transitions = []
while (n_iter < max_iter) and (not self.game.end):
direction = self.step()
state = convert_state(self.game.board)
self.game.move(direction)
new_state = convert_state(self.game.board)
n_iter += 1
transition = (state, new_state, direction)
transitions.append(transition)
if verbose:
print("Iter: {}".format(n_iter))
print("======Direction: {}======".format(
["left", "down", "right", "up"][direction]))
if self.display is not None:
self.display.display(self.game)
counter = collections.Counter(transitions)
return counter
def step(self):
direction = int(input("0: left, 1: down, 2: right, 3: up = ")) % 4
return direction
# in order to represent states more simply, convert from matrix to string
# required for use of Counter()
def convert_state(state):
numbers = ""
for row in range(len(state)):
for col in range(len(state[row])):
numbers += str(state[row][col])
return numbers
class RandomAgent(Agent):
def step(self):
direction = np.random.randint(0, 4)
return direction
class ExpectiMaxAgent(Agent):
def __init__(self, game, display=None):
if game.size != 4:
raise ValueError(
"`%s` can only work with game of `size` 4." % self.__class__.__name__)
super().__init__(game, display)
from .expectimax import board_to_move
self.search_func = board_to_move
def step(self):
direction = self.search_func(self.game.board)
return direction