コード例 #1
0
    def evolve_same_pop(self, iteration):
        """Step algorithm using 'Sampe population' method."""
        bestTwo = self.get_best_tours(self.tours)
        worstTwo = self.get_worst_tours(self.tours)

        children1, children2 = self.crossover(self.tours[bestTwo[0][0]].cities,
                                              self.tours[bestTwo[1][0]].cities)

        child1 = Tour(self.lx, self.ly)
        child2 = Tour(self.lx, self.ly)

        child1.set_tour(children1)
        child2.set_tour(children2)

        if self.tours[worstTwo[0][0]].get_cost() > child1.get_cost():
            self.tours[worstTwo[0][0]] = child1

        if self.tours[worstTwo[1][0]].get_cost() > child2.get_cost():
            self.tours[worstTwo[1][0]] = child2

        for i in range(len(self.tours)):
            self.tours[i] = self.mutate(self.tours[i])
コード例 #2
0
    def evolve_new_pop(self, iteration):
        """Step algorithm using 'New population' method."""
        new_tours = []

        # Save best tour
        _, best_tuple = self.get_best_tours(self.tours)
        best = self.tours[best_tuple[0]]
        new_tours.append(best)

        for i in range(1, len(self.tours)):
            parent1 = self.tournament_selection()
            parent2 = self.tournament_selection()
            child1, child2 = self.crossover(parent1.cities, parent2.cities)

            t = Tour(self.lx, self.ly)
            t.set_tour(child1)
            new_tours.append(t)

        self.tours = new_tours
        for i in range(1, len(self.tours)):
            self.tours[i] = self.mutate(self.tours[i])

        _, best_tuple = self.get_best_tours(self.tours)
        best = self.tours[best_tuple[0]]