def apply_evolution(self): tuples = [] total_score = 0.0 MultipleSessions.refresh_grids() for i in range(len(self.robots)): m = MultipleSessions(self.robots[i]) score = m.run() total_score = total_score + score tuples.append((self.robots[i], score)) tuples.sort(key=lambda x: x[1], reverse=True) normalized_score = total_score / len(self.robots) best_score = tuples[0][1] child_robots = [] roulette_size = (POPULATION_SIZE * (POPULATION_SIZE + 1) / 2) for i in range(POPULATION_SIZE / 2): parent_1 = tuples[self.get_roulette_wheel_selection(roulette_size)][0] parent_2 = tuples[self.get_roulette_wheel_selection(roulette_size)][0] child_1, child_2 = parent_1.give_birth(parent_2) child_robots.append(child_1) child_robots.append(child_2) self.num_stay_put = child_1.gene.count(5) + child_2.gene.count(5) return Generation(self.id + 1, child_robots), normalized_score, best_score
def applyEvolution(self): tuples = [] totalScore = 0.0 MultipleSessions.refreshGrids() for i in range(len(self.robots)): m = MultipleSessions(self.robots[i]) score = m.run() totalScore = totalScore + score tuples.append((self.robots[i], score)) tuples.sort(key=lambda x: x[1], reverse=True) normalizedScore = totalScore / len(self.robots) bestScore = tuples[0][1] childRobots = [] # (TODO #5) # fill the childRobots array with POPULATION_SIZE children # using getRouletteWheelSelection() and Robby.giveBirth() for new in range(POPULATION_SIZE): father = tuples[self.getRouletteWheelSelection(POPULATION_SIZE)][0] mother = tuples[self.getRouletteWheelSelection(POPULATION_SIZE)][0] [child1, child2] = father.giveBirth(mother) childRobots.append(child1) childRobots.append(child2) number_of_stay_puts = 0 for number in tuples: k = number[0].gene for nu in k: if nu == ACTIONS.STAY_PUT: number_of_stay_puts += 1 print tuples[0][0].gene return (Generation(self.id + 1, childRobots), normalizedScore, bestScore, number_of_stay_puts)
def get_score(self): r_val = 0.0 for i in range(len(self.robots)): m = MultipleSessions(self.robots[i]) r_val = r_val + m.run() return r_val / len(self.robots)
def getScore(self): rVal = 0.0 for i in range(len(self.robots)): m = MultipleSessions(self.robots[i]) rVal = rVal + m.run() return rVal / len(self.robots)