Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
341 lines (261 loc) · 6.39 KB
/
Copy pathcode.py
File metadata and controls
341 lines (261 loc) · 6.39 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import random
import os
import time
MAP = [ [1,1,1,1,1,1,1,1],
[1,9,9,9,9,9,9,1],
[1,9,0,0,0,0,9,1],
[1,9,0,0,0,9,1,1],
[1,9,0,0,0,9,1,1],
[1,9,0,0,0,0,9,1],
[1,9,9,9,9,9,9,1],
[1,1,1,1,1,1,1,1]]
def fill_hpos():
arr = []
for y in range(8):
for x in range(8):
if MAP[y][x] == 9:
arr.append([y,x])
return arr
H_POS = fill_hpos()
NORTH = [-1, 0]
SOUTH = [1, 0]
EAST = [0, 1]
WEST = [0, -1]
CHAR_NORTH = '^'
CHAR_SOUTH = 'v'
CHAR_EAST = '>'
CHAR_WEST = '<'
NOP = 0b00
FORWARD = 0b01
ROT_RIGHT = 0b10
ROT_LEFT = 0b11
MAX_CYCLES = 28
MAX_GENERATIONS = 50
MAX_POP = 20
MUT_RATE = 50
class Agent:
x = 1
y = 1
vec = EAST
char = CHAR_EAST
def sense(self):
sensors = 0
if self.vec == EAST:
#left
if MAP[self.y-1][self.x] == 1:
sensors = sensors | 0x01
#left-diag
if MAP[self.y-1][self.x+1] == 1:
sensors = sensors | 0x02
#front
if MAP[self.y][self.x+1] == 1:
sensors = sensors | 0x04
#right-diag
if MAP[self.y+1][self.x+1] == 1:
sensors = sensors | 0x08
#right
if MAP[self.y+1][self.x] == 1:
sensors = sensors | 0x10
elif self.vec == WEST:
#left
if MAP[self.y+1][self.x] == 1:
sensors = sensors | 0x01
#left-diag
if MAP[self.y+1][self.x-1] == 1:
sensors = sensors | 0x02
#front
if MAP[self.y][self.x-1] == 1:
sensors = sensors | 0x04
#right-diag
if MAP[self.y-1][self.x-1] == 1:
sensors = sensors | 0x08
#right
if MAP[self.y-1][self.x] == 1:
sensors = sensors | 0x10
elif self.vec == NORTH:
#left
if MAP[self.y][self.x-1] == 1:
sensors = sensors | 0x01
#left-diag
if MAP[self.y-1][self.x-1] == 1:
sensors = sensors | 0x02
#front
if MAP[self.y-1][self.x] == 1:
sensors = sensors | 0x04
#right-diag
if MAP[self.y-1][self.x+1] == 1:
sensors = sensors | 0x08
#right
if MAP[self.y][self.x+1] == 1:
sensors = sensors | 0x10
elif self.vec == SOUTH:
#left
if MAP[self.y][self.x+1] == 1:
sensors = sensors | 0x01
#left-diag
if MAP[self.y+1][self.x+1] == 1:
sensors = sensors | 0x02
#front
if MAP[self.y+1][self.x] == 1:
sensors = sensors | 0x04
#right-diag
if MAP[self.y+1][self.x-1] == 1:
sensors = sensors | 0x08
#right
if MAP[self.y][self.x-1] == 1:
sensors = sensors | 0x10
return sensors
def act(self, action):
if action == FORWARD:
new_x = self.x + self.vec[1]
new_y = self.y + self.vec[0]
if MAP[new_y][new_x] == 1:
return
self.x = new_x
self.y = new_y
elif action == ROT_RIGHT:
if self.vec == NORTH:
self.vec = EAST
self.char = CHAR_EAST
elif self.vec == SOUTH:
self.vec = WEST
self.char = CHAR_WEST
elif self.vec == EAST:
self.vec = SOUTH
self.char = CHAR_SOUTH
elif self.vec == WEST:
self.vec = NORTH
self.char = CHAR_NORTH
elif action == ROT_LEFT:
if self.vec == NORTH:
self.vec = WEST
self.char = CHAR_WEST
elif self.vec == SOUTH:
self.vec = EAST
self.char = CHAR_EAST
elif self.vec == EAST:
self.vec = NORTH
self.char = CHAR_NORTH
elif self.vec == WEST:
self.vec = SOUTH
self.char = CHAR_SOUTH
elif action == NOP:
return
def init_population(sz):
arr = []
for i in range(sz):
arr.append(random.randint(0x8000000000000000, 0xffffffffffffffff))
return arr
def calc_fitness(gene, cycles):
agent = Agent()
score = 0
visited = []
for i in range(cycles):
last_pos = [agent.y, agent.x]
visited.append(last_pos)
wall_config = agent.sense()
action = (gene >> (wall_config * 2)) & 0x03
agent.act(action)
curr_pos = [agent.y, agent.x]
if curr_pos != last_pos and curr_pos not in visited and curr_pos in H_POS:
score += 1
return score
def sort_by_fitness(individuals, gen):
fitness_list = []
sorted_fittest = []
for i in range(len(individuals)):
fitness_list.append((individuals[i], calc_fitness(individuals[i], MAX_CYCLES), gen))
sorted_fittest = sorted(fitness_list, key = lambda ind: ind[1], reverse = True)
return sorted_fittest
MASK = 0xffffffffffffffff
def mutate(gene):
prob_mut = MUT_RATE
mut = 0
new_gene = gene
for i in range (64):
if random.randint(1, 1000) > prob_mut:
mut = 0
else:
mut = 1
new_gene = new_gene ^ (mut << i & 0xffffffffffffffff)
return new_gene
def crossover_mutate(gene1, gene2, p):
head_mask = (MASK << p) & MASK
tail_mask = MASK - head_mask
g1_head = gene1 & head_mask
g2_head = gene2 & head_mask
g1_tail = gene1 & tail_mask
g2_tail = gene2 & tail_mask
return [mutate(g1_head | g2_tail), mutate(g2_head | g1_tail)]
def breed(individuals):
children = []
length = len(individuals)
for i in range(length):
cross_point = random.randint(1, 63)
p1 = individuals[i]
p2 = individuals[(i+1) % length]
children += crossover_mutate(p1, p2, cross_point)
return children
def print_gen(pop, gen):
print "---------GEN "+ str(gen) +"------------------------"
for i in range(len(pop)):
print bin(pop[i][0]),
print "\t" + str(pop[i][1])
def render(m, agent):
for y in range(8):
for x in range(8):
if [x, y] == [agent.x, agent.y]:
print agent.char,
else:
print m[y][x],
print ""
def create_printable_map(m):
char_map = []
row = ""
for y in range(8):
for x in range(8):
if m[y][x] == 1:
row += 'O'
else:
row += ' '
char_map.append(row)
row = ""
return char_map
def sim(gene, cycles):
m = create_printable_map(MAP)
agent = Agent()
for i in range(cycles):
os.system('clear')
render(m, agent)
wall_config = agent.sense()
action = (gene >> (wall_config * 2)) & 0x03
agent.act(action)
time.sleep(0.5)
#-----------------------------------------------------------------------
def main():
pop_size = MAX_POP
curr_gen = []
new_gen = []
mating_pool = []
fittest = (0x0000000000000000, 0, 0)
n_gen = 0
curr_gen = init_population(pop_size)
while fittest[1] != 19:
fittest_first = sort_by_fitness(curr_gen, n_gen)
print_gen(fittest_first, n_gen)
if fittest_first[0][1] > fittest[1]:
fittest = fittest_first[0]
mating_pool = fittest_first[:pop_size/2]
new_gen = breed([i[0] for i in mating_pool])
curr_gen = new_gen
n_gen += 1
return fittest
optimal = main()
print ""
print "OPTIMAL SOLUTION FOUND IN GENERATION " + str(optimal[2])
print "(solution, fitness, generation) = " + str(optimal)
for i in range(10):
print "SIMULATING OPTIMAL SOLUTION IN " + str(10-i) + " SECONDS"
time.sleep(1)
test_gene = optimal[0]
sim(test_gene, MAX_CYCLES+2)