def generate_genome(self): genome = list() for x in range(self.nstates): state = dict() for key in itertools.product(tiles.all(), repeat=4): action = random.choice(self.choices) newstate = random.randint(0, self.nstates - 1) state[key] = (action, newstate) genome.append(state) return genome
def initialize_evolver(self): # get all tiles keys = list(itertools.product(tiles.all(), repeat=4)) # create sample with all possible genome keys sample = GMap(keys, Peater.choices, nstates=8) sample.evaluator.set = GMapEvaluator # initialize evolver ga = GSimulationGA(sample) ga.setPopulationSize(o.ga.general.population) ga.setGenerations(o.ga.general.generations) ga.setCrossoverRate(o.ga.crossover.rate) ga.setMutationRate(o.ga.mutation.rate) ga.setElitismReplacement(o.ga.crossover.elites) ga.setElitism(o.ga.crossover.elites > 0) ga.selector.set(Selectors.GTournamentSelector) ga.minimax = Consts.minimaxType[o.ga.evaluator.minimax] self.ga = ga
def get_neighbors(self, world): neighbors = list() if self.heading: neighbors.append(self.neighbor_forward(world)) neighbors.append(self.neighbor_right(world)) neighbors.append(self.neighbor_backward(world)) neighbors.append(self.neighbor_left(world)) else: neighbors.append(self.neighbor_north(world)) neighbors.append(self.neighbor_east(world)) neighbors.append(self.neighbor_south(world)) neighbors.append(self.neighbor_west(world)) for idx, n in enumerate(neighbors): for cls in tiles.all(): if cls.char == n.char: neighbors[idx] = cls break return tuple(neighbors)