-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
281 lines (241 loc) · 12.9 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
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
import logging
import random
from abc import ABCMeta, abstractmethod
from learner import QLearner
from world import QbertWorld
class Agent:
__metaclass__ = ABCMeta
@abstractmethod
def action(self):
"""
Perform an action.
"""
raise NotImplementedError
class QbertAgent(Agent):
"""
Obert agent which can be of multiple types.
"""
def __init__(self, agent_type='subsumption', random_seed=123, frame_skip=4, repeat_action_probability=0,
sound=True, display_screen=True, alpha=0.1, gamma=0.95,
epsilon=0.2, unexplored_threshold=1, unexplored_reward=100, exploration='combined',
distance_metric=None, combined_reward=True, state_representation='simple'):
if agent_type is 'block':
self.agent = QbertBlockAgent(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_representation)
elif agent_type is 'enemy':
self.agent = QbertEnemyAgent(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_representation)
elif agent_type is 'friendly':
self.agent = QbertFriendlyAgent(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_representation)
elif agent_type is 'subsumption':
self.agent = QbertSubsumptionAgent(random_seed, frame_skip, repeat_action_probability, sound,
display_screen, alpha, gamma, epsilon, unexplored_threshold,
unexplored_reward, exploration, distance_metric, combined_reward,
state_representation)
elif agent_type is 'combined_verbose':
self.agent = QbertCombinedVerboseAgent(random_seed, frame_skip, repeat_action_probability, sound,
display_screen, alpha, gamma, epsilon, unexplored_threshold,
unexplored_reward, exploration, distance_metric)
self.world = self.agent.world
def action(self):
return self.agent.action()
def q_size(self):
return self.agent.q_size()
def save(self, filename):
self.agent.save(filename)
def load(self, filename):
self.agent.load(filename)
class QbertBlockAgent(Agent):
"""
Obert agent which learns to explore blocks.
"""
def __init__(self, random_seed, frame_skip, repeat_action_probability, sound, display_screen, alpha,
gamma, epsilon, unexplored_threshold, unexplored_reward, exploration, distance_metric,
state_representation):
if state_representation is 'simple':
state_repr = 'along_direction'
else:
state_repr = 'verbose'
self.world = QbertWorld(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
block_state_repr=state_repr)
self.block_learner = QLearner(self.world, alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr)
def action(self):
s = self.world.to_state_blocks()
a = self.block_learner.get_best_single_action(s)
block_score, friendly_score, enemy_score, enemy_penalty = self.world.perform_action(a)
s_next = self.world.to_state_blocks()
self.block_learner.update(s, a, s_next, block_score)
return block_score + friendly_score + enemy_score
def q_size(self):
return len(self.block_learner.Q)
def save(self, filename):
self.block_learner.save(filename)
def load(self, filename):
self.block_learner.load(filename)
class QbertEnemyAgent(Agent):
"""
Obert agent which learns to avoid enemies (purple agents).
"""
def __init__(self, random_seed, frame_skip, repeat_action_probability, sound, display_screen, alpha,
gamma, epsilon, unexplored_threshold, unexplored_reward, exploration, distance_metric,
state_representation):
if state_representation is 'simple':
state_repr = 'adjacent_conservative'
else:
state_repr = 'verbose'
self.world = QbertWorld(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
enemy_state_repr=state_repr)
self.enemy_learner = QLearner(self.world, alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr)
def action(self):
s = self.world.to_state_enemies()
a = self.enemy_learner.get_best_single_action(s)
block_score, friendly_score, enemy_score, enemy_penalty = self.world.perform_action(a)
s_next = self.world.to_state_enemies()
self.enemy_learner.update(s, a, s_next, enemy_score + enemy_penalty)
return block_score + friendly_score + enemy_score
def q_size(self):
return len(self.enemy_learner.Q)
def save(self, filename):
self.enemy_learner.save(filename)
def load(self, filename):
self.enemy_learner.load(filename)
class QbertFriendlyAgent(Agent):
"""
Obert agent which learns to capture friendlies (green agents).
"""
def __init__(self, random_seed, frame_skip, repeat_action_probability, sound, display_screen, alpha,
gamma, epsilon, unexplored_threshold, unexplored_reward, exploration, distance_metric,
state_representation):
if state_representation is 'simple':
state_repr = 'simple'
else:
state_repr = 'verbose'
self.world = QbertWorld(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
friendly_state_repr=state_repr)
self.friendly_learner = QLearner(self.world, alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr)
def action(self):
s = self.world.to_state_friendlies()
a = self.friendly_learner.get_best_single_action(s)
block_score, friendly_score, enemy_score, enemy_penalty = self.world.perform_action(a)
s_next = self.world.to_state_friendlies()
self.friendly_learner.update(s, a, s_next, friendly_score)
return block_score + friendly_score + enemy_score
def q_size(self):
return len(self.friendly_learner.Q)
def save(self, filename):
self.friendly_learner.save(filename)
def load(self, filename):
self.friendly_learner.load(filename)
class QbertCombinedVerboseAgent(Agent):
"""
Obert agent which uses a verbose state for enemies, blocks and friendlies.
"""
def __init__(self, random_seed, frame_skip, repeat_action_probability, sound, display_screen, alpha,
gamma, epsilon, unexplored_threshold, unexplored_reward, exploration, distance_metric):
state_repr = 'verbose'
self.world = QbertWorld(random_seed, frame_skip, repeat_action_probability, sound, display_screen)
self.learner = QLearner(self.world, alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr)
def action(self):
s = self.world.to_state_combined_verbose()
a = self.learner.get_best_single_action(s)
block_score, friendly_score, enemy_score, enemy_penalty = self.world.perform_action(a)
s_next = self.world.to_state_combined_verbose()
self.learner.update(s, a, s_next, block_score + friendly_score + enemy_score + enemy_penalty)
return block_score + friendly_score + enemy_score
def q_size(self):
return len(self.learner.Q)
def save(self, filename):
self.learner.save(filename)
def load(self, filename):
self.learner.load(filename)
class QbertSubsumptionAgent(Agent):
"""
Obert agent which uses a subsumption model, separating learning block exploring, avoiding enemies and capturing
friendlies.
"""
def __init__(self, random_seed, frame_skip, repeat_action_probability, sound, display_screen, alpha,
gamma, epsilon, unexplored_threshold, unexplored_reward, exploration, distance_metric,
combined_reward, state_representation):
if state_representation is 'simple':
block_state_repr = 'adjacent'
enemy_state_repr = 'adjacent_dangerous'
friendly_state_repr = 'simple'
else:
block_state_repr = 'verbose'
enemy_state_repr = 'verbose'
friendly_state_repr = 'verbose'
self.world = QbertWorld(random_seed, frame_skip, repeat_action_probability, sound, display_screen,
block_state_repr=block_state_repr,
enemy_state_repr=enemy_state_repr,
friendly_state_repr=friendly_state_repr)
self.block_learner = QLearner(self.world, alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr=block_state_repr, tag='blocks')
self.friendly_learner = QLearner(self.world, alpha, gamma, epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr=friendly_state_repr,
tag='friendlies')
enemy_epsilon = 0
self.enemy_learner = QLearner(self.world, alpha, gamma, enemy_epsilon, unexplored_threshold, unexplored_reward,
exploration, distance_metric, state_repr=enemy_state_repr, tag='enemies')
self.combined_reward = combined_reward
def action(self):
enemy_present = self.world.is_enemy_nearby()
friendly_present = self.world.is_friendly_nearby()
a_enemies = None
a_friendlies = None
s_enemies = None
s_friendlies = None
if enemy_present:
logging.debug('Enemy present!')
s_enemies = self.world.to_state_enemies()
a_enemies = self.enemy_learner.get_best_actions(s_enemies)
if friendly_present:
logging.debug('Friendly present!')
s_friendlies = self.world.to_state_friendlies()
a_friendlies = self.friendly_learner.get_best_actions(s_friendlies)
s = self.world.to_state_blocks()
a = self.block_learner.get_best_actions(s)
if enemy_present and len(a_enemies) > 0:
logging.debug('Chose enemy action!')
if len(a_enemies) > 1:
logging.debug('Broke tie!')
chosen_action = self.block_learner.get_best_action(s, a_enemies)
else:
chosen_action = a_enemies[0]
elif friendly_present and len(a_friendlies) > 0:
logging.debug('Chose friendly action!')
chosen_action = a_friendlies[0]
else:
logging.debug('Chose block action!')
chosen_action = random.choice(a)
block_score, friendly_score, enemy_score, enemy_penalty = self.world.perform_action(chosen_action)
if enemy_present:
s_next_enemies = self.world.to_state_enemies()
self.enemy_learner.update(s_enemies, chosen_action, s_next_enemies, enemy_score + enemy_penalty)
if friendly_present:
s_next_friendlies = self.world.to_state_friendlies()
self.friendly_learner.update(s_friendlies, chosen_action, s_next_friendlies, friendly_score)
s_next = self.world.to_state_blocks()
combined_score = block_score if self.combined_reward else block_score
self.block_learner.update(s, chosen_action, s_next, combined_score)
return block_score + friendly_score + enemy_score
def q_size(self):
return len(self.block_learner.Q) + \
len(self.friendly_learner.Q) + \
len(self.enemy_learner.Q)
def save(self, filename):
self.block_learner.save('{}_{}'.format(filename, 'block'))
self.friendly_learner.save('{}_{}'.format(filename, 'friendly'))
self.enemy_learner.save('{}_{}'.format(filename, 'enemy'))
def load(self, filename):
self.block_learner.load('{}_{}'.format(filename, 'block'))
self.friendly_learner.load('{}_{}'.format(filename, 'friendly'))
self.enemy_learner.load('{}_{}'.format(filename, 'enemy'))
# Human high scores: 15825, 27000