def mutate(self, probability=.3): content = '' for c in self.gtype: if rng.random() < probability: c = rng.choice(alphabet) content += c b = Board(*SIZE, content=content) return Agent(board=b)
def crossover_with(self, other, swap_prob=.4): content = '' swapper = 0 for c1, c2 in zip(self.gtype, other.gtype): c = [c1, c2][swapper] content += c swapper = 1 - swapper if rng.random() < swap_prob else swapper b = Board(*SIZE, content=content) return Agent(board=b)
def __init__(self, board=None): if not board: # Iniate random self.ftype = Board(*SIZE) #Fenotype else: self.ftype = board self.gtype = ''.join([''.join(line) for line in self.ftype.letters]) #Genotype self.score = self.calc_score() #Fitness
def setUp(self): self.board = Board([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']])
class BoardTest(unittest.TestCase): def setUp(self): self.board = Board([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]) def test_get_letter(self): self.assertEquals('A', self.board.get_letter(0, 0)) self.assertEquals('B', self.board.get_letter(1, 0)) self.assertEquals('I', self.board.get_letter(2, 2)) def test_neighbours_corner(self): self.assertEquals(set(self.board.get_neighbours(0, 0)), set([(1, 0), (1, 1), (0, 1)])) self.assertEquals(set(self.board.get_neighbours(2, 0)), set([(1, 0), (1, 1), (2, 1)])) self.assertEquals(set(self.board.get_neighbours(2, 2)), set([(2, 1), (1, 1), (1, 2)])) self.assertEquals(set(self.board.get_neighbours(0, 2)), set([(0, 1), (1, 1), (1, 2)])) def test_neighbours_edge(self): self.assertEquals(set(self.board.get_neighbours(1, 0)), set([(0, 0), (0, 1), (1, 1), (2, 1), (2, 0)])) self.assertEquals(set(self.board.get_neighbours(2, 1)), set([(1, 0), (2, 0), (1, 1), (1, 2), (2, 2)])) self.assertEquals(set(self.board.get_neighbours(1, 2)), set([(0, 1), (1, 1), (2, 1), (0, 2), (2, 2)])) self.assertEquals(set(self.board.get_neighbours(0, 1)), set([(0, 0), (1, 0), (1, 1), (1, 2), (0, 2)])) def test_neighbours_center(self): self.assertEquals( set(self.board.get_neighbours(1, 1)), set([(0, 0), (1, 0), (2, 0), (0, 1), (2, 1), (0, 2), (1, 2), (2, 2)]))
def board(): return Board([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']])
remove_index = set() for i, p in enumerate(population): if p.gtype in gtype_set: if rng.random() <= remove_duplicate_prob: remove_index.add(i) else: gtype_set.add(p.gtype) return [p for i, p in enumerate(population) if i not in remove_index], len(gtype_set) if __name__ == "__main__": population = [Agent() for i in range(pop_size // 2)] population += [ Agent(Board(*SIZE, content='iledversofylcerp')).mutate() for i in range(pop_size // 2) ] unique = pop_size for i in range(iter_count): # Sort new pop, trim or append to pop_size population = sorted(population, key=lambda p: -p.score) # Diversify by killing off some duplicates population, unique = diversify(population, remove_duplicate_prob=0.33) # Complete or trim to pop size population += [Agent() for i in range(pop_size - len(population))] population = population[:pop_size] assert len(population) == pop_size # Report stats
#!/usr/bin/env python from boggle import Board, traverse_board if __name__ == '__main__': board = Board(4, 4) # neighbors = board.get_neighbors(8) input_string = 'thecatisntthehat' board.fill_board(input_string) print(board) target = 'tis' found = traverse_board(board, input_string, target, 'breadth') print(found)