-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
182 lines (140 loc) · 4.28 KB
/
agent.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
#!/usr/bin/env python3
"""
Framework for 2048 & 2048-like Games (Python 3)
agent.py: Define the behavior of variants of agents including players and environments
Author: Hung Guei (moporgic)
Computer Games and Intelligence (CGI) Lab, NYCU, Taiwan
https://cgilab.nctu.edu.tw/
"""
from board import board
from action import action
from weight import weight
from array import array
import random
import sys
class agent:
""" base agent """
def __init__(self, options = ""):
self.info = {}
options = "name=unknown role=unknown " + options
for option in options.split():
data = option.split("=", 1) + [True]
self.info[data[0]] = data[1]
return
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return
def open_episode(self, flag = ""):
return
def close_episode(self, flag = ""):
return
def take_action(self, state):
return action()
def check_for_win(self, state):
return False
def property(self, key):
return self.info[key] if key in self.info else None
def notify(self, message):
data = message.split("=", 1) + [True]
self.info[data[0]] = data[1]
return
def name(self):
return self.property("name")
def role(self):
return self.property("role")
class random_agent(agent):
""" base agent for agents with random behavior """
def __init__(self, options = ""):
super().__init__(options)
seed = self.property("seed")
if seed is not None:
random.seed(int(seed))
return
def choice(self, seq):
target = random.choice(seq)
return target
def shuffle(self, seq):
random.shuffle(seq)
return
class weight_agent(agent):
""" base agent for agents with weight tables """
def __init__(self, options = ""):
super().__init__(options)
self.net = []
init = self.property("init")
if init is not None:
self.init_weights(init)
load = self.property("load")
if load is not None:
self.load_weights(load)
return
def __exit__(self, exc_type, exc_value, traceback):
save = self.property("save")
if save is not None:
self.save_weights(save)
return
def init_weights(self, info):
# self.net += [weight(65536)]
# self.net += [weight(65536)]
return
def load_weights(self, path):
input = open(path, 'rb')
size = array('L')
size.fromfile(input, 1)
size = size[0]
for i in range(size):
self.net += [weight()]
self.net[-1].load(input)
return
def save_weights(self, path):
output = open(path, 'wb')
array('L', [len(self.net)]).tofile(output)
for w in self.net:
w.save(output)
return
class learning_agent(agent):
""" base agent for agents with a learning rate """
def __init__(self, options = ""):
super().__init__(options)
self.alpha = 0.1
alpha = self.property("alpha")
if alpha is not None:
self.alpha = float(alpha)
return
class rndenv(random_agent):
"""
random environment
add a new random tile to an empty cell
2-tile: 90%
4-tile: 10%
"""
def __init__(self, options = ""):
super().__init__("name=random role=environment " + options)
return
def take_action(self, state):
empty = [pos for pos, tile in enumerate(state.state) if not tile]
if empty:
pos = self.choice(empty)
tile = self.choice([1] * 9 + [2])
return action.place(pos, tile)
else:
return action()
class player(random_agent):
"""
dummy player
select a legal action randomly
"""
def __init__(self, options = ""):
super().__init__("name=dummy role=player " + options)
return
def take_action(self, state):
legal = [op for op in range(4) if board(state).slide(op) != -1]
if legal:
op = self.choice(legal)
return action.slide(op)
else:
return action()
if __name__ == '__main__':
print('2048 Demo: agent.py\n')
pass