-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifter_vectors.py
146 lines (117 loc) · 4.28 KB
/
lifter_vectors.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
import world
import random
import math
from heapq import *
def wrc(weighted_choices):
total_weight = sum(w for (w, c) in weighted_choices)
x = random.random()*total_weight
for (w, c) in weighted_choices:
x -= w
if x < 0:
return c
return c
class AvgEpsilonChooser(object):
def __init__(self):
self.choice_stats = {} # maps choice to (count, total, max)
def choose(self, valid_choices):
if random.random() < 0.2:
return random.choice(valid_choices)
else:
scored_choices = []
for c in valid_choices:
if c in self.choice_stats:
(count, total, maximum) = self.choice_stats[c]
scored_choices.append((float(total)/count, c))
else:
scored_choices.append((None, c))
scored_choices.sort(reverse=True)
if scored_choices[0][0] is None:
return random.choice(valid_choices)
else:
return scored_choices[0][1]
def feedback(self, choice, score):
if choice in self.choice_stats:
count, total, maximum = self.choice_stats[choice]
else:
count, total, maximum = 0, 0, None
count += 1
total += score
if maximum is None or score > maximum:
maximum = score
self.choice_stats[choice] = (count, total, maximum)
class MaxEpsilonChooser(object):
def __init__(self):
self.choice_stats = {} # maps choice to (count, total, max)
def choose(self, valid_choices):
if random.random() < 0.5:
return random.choice(valid_choices)
else:
scored_choices = []
for c in valid_choices:
if c in self.choice_stats:
(count, total, maximum) = self.choice_stats[c]
scored_choices.append((maximum, c))
else:
scored_choices.append((None, c))
scored_choices.sort(reverse=True)
if scored_choices[0][0] is None:
return random.choice(valid_choices)
else:
return scored_choices[0][1]
def feedback(self, choice, score):
if choice in self.choice_stats:
count, total, maximum = self.choice_stats[choice]
else:
count, total, maximum = 0, 0, None
count += 1
total += score
if maximum is None or score > maximum:
maximum = score
self.choice_stats[choice] = (count, total, maximum)
if __name__ == "__main__":
initial_world = world.read_world([])
best_score = 0
best_commands = ''
chooser_factory = AvgEpsilonChooser
debug_mode = True
def debug(s):
if debug_mode:
print s
flow = {} # maps ((x, y), (prev_x, prev_y)) to Chooser
while True:
# start a random walk
debug('starting walk')
w = initial_world
prev_x, prev_y = None, None
path_choices = {} # map of ((x, y), (prev_x, prev_y)) to move we made
command_list = []
while True:
# walk another step
if w.is_done():
debug('world done')
break
valid_moves = list(w.valid_moves())
valid_moves.remove('A')
valid_moves.remove('W')
x, y = w.robot
#debug('robot at %d, %d' % (x, y))
transition = ((x, y), (prev_x, prev_y))
if transition in path_choices:
debug('repeated transition')
break # we repeated ourself, so stop the walk
chooser = flow.get(transition)
if chooser is None:
chooser = chooser_factory()
flow[transition] = chooser
command = chooser.choose(valid_moves)
#debug('command %s' % command)
w = w.move(command)
path_choices[transition] = command
command_list.append(command)
prev_x = x
prev_y = y
final_score = w.score()
command_str = ''.join(command_list)
debug('finished walk, score %d path [%s]' % (final_score, command_str))
for trans, command in path_choices.iteritems():
flow[trans].feedback(command, final_score)