-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmy_team.py
183 lines (147 loc) · 6.88 KB
/
my_team.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
# my_team.py
# ---------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero ([email protected]) and Dan Klein ([email protected]).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
import random
import contest.util as util
from contest.capture_agents import CaptureAgent
from contest.game import Directions
from contest.util import nearest_point
#################
# Team creation #
#################
def create_team(first_index, second_index, is_red,
first='OffensiveReflexAgent', second='DefensiveReflexAgent', num_training=0):
"""
This function should return a list of two agents that will form the
team, initialized using firstIndex and secondIndex as their agent
index numbers. isRed is True if the red team is being created, and
will be False if the blue team is being created.
As a potentially helpful development aid, this function can take
additional string-valued keyword arguments ("first" and "second" are
such arguments in the case of this function), which will come from
the --red_opts and --blue_opts command-line arguments to capture.py.
For the nightly contest, however, your team will be created without
any extra arguments, so you should make sure that the default
behavior is what you want for the nightly contest.
"""
return [eval(first)(first_index), eval(second)(second_index)]
##########
# Agents #
##########
class ReflexCaptureAgent(CaptureAgent):
"""
A base class for reflex agents that choose score-maximizing actions
"""
def __init__(self, index, time_for_computing=.1):
super().__init__(index, time_for_computing)
self.start = None
def register_initial_state(self, game_state):
self.start = game_state.get_agent_position(self.index)
CaptureAgent.register_initial_state(self, game_state)
def choose_action(self, game_state):
"""
Picks among the actions with the highest Q(s,a).
"""
actions = game_state.get_legal_actions(self.index)
# You can profile your evaluation time by uncommenting these lines
# start = time.time()
values = [self.evaluate(game_state, a) for a in actions]
# print 'eval time for agent %d: %.4f' % (self.index, time.time() - start)
max_value = max(values)
best_actions = [a for a, v in zip(actions, values) if v == max_value]
food_left = len(self.get_food(game_state).as_list())
if food_left <= 2:
best_dist = 9999
best_action = None
for action in actions:
successor = self.get_successor(game_state, action)
pos2 = successor.get_agent_position(self.index)
dist = self.get_maze_distance(self.start, pos2)
if dist < best_dist:
best_action = action
best_dist = dist
return best_action
return random.choice(best_actions)
def get_successor(self, game_state, action):
"""
Finds the next successor which is a grid position (location tuple).
"""
successor = game_state.generate_successor(self.index, action)
pos = successor.get_agent_state(self.index).get_position()
if pos != nearest_point(pos):
# Only half a grid position was covered
return successor.generate_successor(self.index, action)
else:
return successor
def evaluate(self, game_state, action):
"""
Computes a linear combination of features and feature weights
"""
features = self.get_features(game_state, action)
weights = self.get_weights(game_state, action)
return features * weights
def get_features(self, game_state, action):
"""
Returns a counter of features for the state
"""
features = util.Counter()
successor = self.get_successor(game_state, action)
features['successor_score'] = self.get_score(successor)
return features
def get_weights(self, game_state, action):
"""
Normally, weights do not depend on the game state. They can be either
a counter or a dictionary.
"""
return {'successor_score': 1.0}
class OffensiveReflexAgent(ReflexCaptureAgent):
"""
A reflex agent that seeks food. This is an agent
we give you to get an idea of what an offensive agent might look like,
but it is by no means the best or only way to build an offensive agent.
"""
def get_features(self, game_state, action):
features = util.Counter()
successor = self.get_successor(game_state, action)
food_list = self.get_food(successor).as_list()
features['successor_score'] = -len(food_list) # self.getScore(successor)
# Compute distance to the nearest food
if len(food_list) > 0: # This should always be True, but better safe than sorry
my_pos = successor.get_agent_state(self.index).get_position()
min_distance = min([self.get_maze_distance(my_pos, food) for food in food_list])
features['distance_to_food'] = min_distance
return features
def get_weights(self, game_state, action):
return {'successor_score': 100, 'distance_to_food': -1}
class DefensiveReflexAgent(ReflexCaptureAgent):
"""
A reflex agent that keeps its side Pacman-free. Again,
this is to give you an idea of what a defensive agent
could be like. It is not the best or only way to make
such an agent.
"""
def get_features(self, game_state, action):
features = util.Counter()
successor = self.get_successor(game_state, action)
my_state = successor.get_agent_state(self.index)
my_pos = my_state.get_position()
# Computes whether we're on defense (1) or offense (0)
features['on_defense'] = 1
if my_state.is_pacman: features['on_defense'] = 0
# Computes distance to invaders we can see
enemies = [successor.get_agent_state(i) for i in self.get_opponents(successor)]
invaders = [a for a in enemies if a.is_pacman and a.get_position() is not None]
features['num_invaders'] = len(invaders)
if len(invaders) > 0:
dists = [self.get_maze_distance(my_pos, a.get_position()) for a in invaders]
features['invader_distance'] = min(dists)
if action == Directions.STOP: features['stop'] = 1
rev = Directions.REVERSE[game_state.get_agent_state(self.index).configuration.direction]
if action == rev: features['reverse'] = 1
return features
def get_weights(self, game_state, action):
return {'num_invaders': -1000, 'on_defense': 100, 'invader_distance': -10, 'stop': -100, 'reverse': -2}