This repository has been archived by the owner on Feb 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__cm.py
316 lines (228 loc) · 7.5 KB
/
__cm.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
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
# myTeam.py
# ---------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to
# http://inst.eecs.berkeley.edu/~cs188/pacman/pacman.html
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
from functools import wraps
from captureAgents import CaptureAgent
from _player_state import PlayerState
from game import AgentState, Directions
# extension
PlayerState.extend_agent_state(AgentState)
#################
# Team creation #
#################
def createTeam(firstIndex, secondIndex, isRed,
first='MainAgent', second='MainAgent'):
return [eval(first)(firstIndex), eval(second)(secondIndex)]
################
# Util Classes #
################
class FeatureSet:
def __init__(self, groups=None):
self.fns = dict()
self.ws = dict()
self.vs = dict()
self.names = list()
if groups is not None:
self.group_append(groups)
def append(self, w, fn):
n = fn.__name__
self.names.append(n)
self.fns[n] = fn
self.ws[n] = w
def group_append(self, groups):
for (w, fn) in groups:
self.append(w, fn)
def calc(self, player):
for n in self.names:
self.vs[n] = self.fns[n](player)
def eval(self, player):
self.calc(player)
values = [self.vs[n] * self.ws[n] if self.vs[n] is not None else 0 for n in self.names]
# print self.vs
# print self.ws
return sum([0] + values)
class StateMachine:
def __init__(self):
self.state_dict = dict()
self.state = None
self.state_name = None
@property
def state_names(self):
reserved_names = ['state_names', 'state_dict', 'act', 'state', 'state_name', 'default', 'reset']
return [name for name in dir(self) if not name.startswith('_') and name not in reserved_names]
def act(self, *args):
results = [(getattr(self, name)(*args), name) for name in self.state_names]
for (fn, need_transfer, priority), name in sorted(results, key=lambda x: x[0][2]):
if need_transfer:
# print "-> " + name
self._transfer(fn, name)
def _transfer(self, fn, name):
# print "-> " + name
self.state_name = name
if name not in self.state_dict:
self.state_dict[name] = fn()
self.state = self.state_dict[name]
def reset(self):
self.state = None
self.state_name = None
if 'default' in dir(self):
a_class, name = getattr(self, 'default')()
self._transfer(a_class, name)
def state(fn, priority=0):
def transfer_decorator(transfer_fn):
@wraps(transfer_fn)
def wrapper(*args, **kwargs):
need_transfer = transfer_fn(*args, **kwargs)
return fn, need_transfer, priority
return wrapper
return transfer_decorator
##############
# Heuristics #
##############
def successor_score(p):
return p.score
# start
def d_center(p):
return p.get_maze_d(p.center)
def reach_boundary(p):
if p.pos == p.center:
return 1
# hunt and defend
def num_invaders(p):
return p.num_invaders
def d_remote_invader(p):
ds = [p.get_maze_d(p.source.most_likely_pos_dict[idx]) for idx in p.invaders_indexes]
if len(ds) > 0:
return min(ds)
def d_ally_rev(p):
if p.in_opponent_territory and p.d_ally is not None:
return 1.0 / p.d_ally
def stop_punish(p):
return 1 if p.from_action == Directions.STOP else 0
def reverse_action(p):
return 1 if p.is_from_reverse_action else 0
# defend
def d_nearby_invader(p):
return p.d_nearest_invader if len(p.nearby_invaders) > 0 else 0
def danger_dfd(p):
d = p.d_nearest_nearby_opponent
if d <= 1 and p.scared_timer > 0:
return -1
if d <= 5:
return 1
# attack
def d_nearest_food(p):
return p.d_nearest_food if p.food_num > 0 else 0
def collect_food(p):
v = -p.food_num + 100 * p.score if p.food_num > 0 else 0
if p.strong:
v = 100 * v
return v
def danger_atk(p):
d = p.d_nearest_nearby_opponent
if d is None:
return None
if d <= 2:
return 4 / d
elif d <= 4:
return 1
def hold_food(p):
if not p.strong:
return p.carrying_food_num * p.min_d_opponent_boundary_locations
def store_food(p):
if p.source is not None:
return p.returned_food_num - p.source.returned_food_num
def collect_capsule(p):
if p.capsule_num > 0:
return -p.capsule_num
def d_capsule_rev(p):
if p.capsule_num > 0:
return 1.0 / p.d_nearest_capsule
return 1.0 / .1
def strong_level(p):
if p.strong:
return p.capsule_timer / p.CAPSULE_LAST_TIME
def path_end(p):
return 1 if len(p.actions) <= 2 else 0
##########
# Agents #
##########
def get_invade_feature_set():
return FeatureSet([
(1000, successor_score),
(-10, d_nearest_food),
(-1000, danger_atk),
(5000, collect_food),
(1000, d_capsule_rev),
(-1000, stop_punish),
(-200, path_end),
(500 * 10000, strong_level),
(5000, collect_capsule),
# (-2500, d_ally_rev),
])
class StageSM(StateMachine):
def default(self):
return get_invade_feature_set, self.invade.__name__
@state(lambda: FeatureSet([
(-1, d_center),
(1000, reach_boundary)
]))
def start(self, player):
return not player.reach_boundary
@state(get_invade_feature_set, 1)
def invade(self, player):
if player.pos == player.center and not player.reach_boundary:
player.reach_boundary = True
return True
return False
@state(lambda: FeatureSet([
(-5000, num_invaders),
(-10, d_remote_invader),
(-5000, stop_punish),
(-5000, reverse_action),
(-2500, d_ally_rev)
]), 2)
def chase(self, player):
return player.invader_exists
@state(lambda: FeatureSet([
(-10000, num_invaders),
(-1000, d_nearby_invader),
(-5000, stop_punish),
(-100, reverse_action),
(3000, danger_dfd),
(-4000, d_ally_rev)
]), 3)
def guard(self, player):
for o in player.nearby_opponents:
if player.get_maze_d(o.pos) < 5 and not player.in_opponent_territory:
return True
return False
# noinspection PyAttributeOutsideInit,PyAttributeOutsideInit
class MainAgent(CaptureAgent):
def registerInitialState(self, gameState):
CaptureAgent.registerInitialState(self, gameState)
self.player = PlayerState(self, gameState)
self.stage = StageSM()
PlayerState.init_beliefs(self.player, gameState)
def chooseAction(self, gameState):
p = self.player
p.update_game_state(gameState)
p.update_most_likely_opponents_pos()
self.stage.reset()
self.stage.act(p)
fs = self.stage.state
values = [fs.eval(succ_p) for succ_p in p.succs]
max_v = max(values)
best_actions = [a for a, v in zip(p.actions, values) if v == max_v]
return best_actions[0]
#