-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgreedyMouse.py
192 lines (156 loc) · 5.71 KB
/
greedyMouse.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
# coding:utf-8
import random
import setup
import qlearn
import config as cfg
from Queue import Queue
reload(setup)
reload(qlearn)
def pick_random_location():
while 1:
x = random.randrange(world.width)
y = random.randrange(world.height)
cell = world.get_cell(x, y)
if not (cell.wall or len(cell.agents) > 0):
return cell
class Cat(setup.Agent):
def __init__(self, filename):
self.cell = None
self.catWin = 0
self.color = cfg.cat_color
f = file(filename)
lines = f.readlines()
lines = [x.rstrip() for x in lines]
self.fh = len(lines)
self.fw = max([len(x) for x in lines])
self.grid_list = [[1 for x in xrange(self.fw)] for y in xrange(self.fh)]
self.move = [(0, -1), (1, -1), (
1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]
for y in xrange(self.fh):
line = lines[y]
for x in xrange(min(self.fw, len(line))):
t = 1 if (line[x] == 'X') else 0
self.grid_list[y][x] = t
print 'cat init success......'
# using BFS algorithm to move quickly to target.
def bfs_move(self, target):
if self.cell == target:
return
for n in self.cell.neighbors:
if n == target:
self.cell = target # if next move can go towards target
return
best_move = None
q = Queue()
start = (self.cell.y, self.cell.x)
end = (target.y, target.x)
q.put(start)
step = 1
V = {}
preV = {}
V[(start[0], start[1])] = 1
print 'begin BFS......'
while not q.empty():
grid = q.get()
for i in xrange(8):
ny, nx = grid[0] + self.move[i][0], grid[1] + self.move[i][1]
if nx < 0 or ny < 0 or nx > (self.fw-1) or ny > (self.fh-1):
continue
if self.get_value(V, (ny, nx)) or self.grid_list[ny][nx] == 1: # has visit or is wall.
continue
preV[(ny, nx)] = self.get_value(V, (grid[0], grid[1]))
if ny == end[0] and nx == end[1]:
V[(ny, nx)] = step + 1
seq = []
last = V[(ny, nx)]
while last > 1:
k = [key for key in V if V[key] == last]
seq.append(k[0])
assert len(k) == 1
last = preV[(k[0][0], k[0][1])]
seq.reverse()
print seq
best_move = world.grid[seq[0][0]][seq[0][1]]
q.put((ny, nx))
step += 1
V[(ny, nx)] = step
if best_move is not None:
self.cell = best_move
else:
dir = random.randrange(cfg.directions)
self.go_direction(dir)
print "!!!!!!!!!!!!!!!!!!"
def get_value(self, mdict, key):
try:
return mdict[key]
except KeyError:
return 0
def update(self):
print 'cat update begin..'
if self.cell != mouse.cell:
self.bfs_move(mouse.cell)
print 'cat move..'
class Cheese(setup.Agent):
def __init__(self):
self.color = cfg.cheese_color
def update(self):
print 'cheese update...'
pass
class Mouse(setup.Agent):
def __init__(self):
self.ai = None
self.ai = qlearn.QLearn(actions=xrange(cfg.directions), alpha=0.1, gamma=0.9, epsilon=0.1)
self.catWin = 0
self.mouseWin = 0
self.lastState = None
self.lastAction = None
self.color = cfg.mouse_color
print 'mouse init...'
def update(self):
print 'mouse update begin...'
state = self.calculate_state()
reward = cfg.MOVE_REWARD
if self.cell == cat.cell:
print 'eaten by cat...'
self.catWin += 1
reward = cfg.EATEN_BY_CAT
if self.lastState is not None:
self.ai.learn(self.lastState, self.lastAction, state, reward)
print 'mouse learn...'
self.lastState = None
self.cell = pick_random_location()
print 'mouse random generate..'
return
if self.cell == cheese.cell:
self.mouseWin += 1
reward = 50
cheese.cell = pick_random_location()
if self.lastState is not None:
self.ai.learn(self.lastState, self.lastAction, state, reward)
# choose a new action and execute it
action = self.ai.choose_action(state)
self.lastState = state
self.lastAction = action
self.go_direction(action)
def calculate_state(self):
def cell_value(cell):
if cat.cell is not None and (cell.x == cat.cell.x and cell.y == cat.cell.y):
return 3
elif cheese.cell is not None and (cell.x == cheese.cell.x and cell.y == cheese.cell.y):
return 2
else:
return 1 if cell.wall else 0
dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
return tuple([cell_value(world.get_relative_cell(self.cell.x + dir[0], self.cell.y + dir[1])) for dir in dirs])
if __name__ == '__main__':
mouse = Mouse()
cat = Cat(filename='resources/world.txt')
cheese = Cheese()
world = setup.World(filename='resources/world.txt')
world.add_agent(mouse)
world.add_agent(cheese, cell=pick_random_location())
world.add_agent(cat, cell=pick_random_location())
world.display.activate()
world.display.speed = cfg.speed
while 1:
world.update(mouse.mouseWin, mouse.catWin)