示例#1
0
    def notify(self, algorithm):
        if algorithm.n_gen % 10 == 0:

            fig, ax = plt.subplots()

            x = algorithm.opt[0].get("X")
            visualize(problem, x, fig=fig, ax=ax, show=False)
            ax.set_title(f"Generation: {algorithm.n_gen}")
            self.video.record(fig=fig)
示例#2
0
 def notify(self, algorithm):
     if algorithm.n_gen % 10 == 0:
         x = algorithm.opt[0].get("X")
         visualize(problem, x, show=False)
         plt.title(f"Generation: {algorithm.n_gen}")
         self.vid.record()
示例#3
0
    sampling=RandomPermutationSampling(),
    mutation=InversionMutation(),
    # crossover=EdgeRecombinationCrossover(),
    crossover=OrderCrossover(),
    repair=StartFromZeroRepair(),
    eliminate_duplicates=True)

# if the algorithm did not improve the last 200 generations then it will terminate (and disable the max generations)
termination = SingleObjectiveDefaultTermination(n_last=200, n_max_gen=np.inf)

res = minimize(problem, algorithm, termination, seed=1, verbose=False)

print(res.F)
print(res.algorithm.evaluator.n_eval)

visualize(problem, res.X)


class PathVisualization(Callback):
    def __init__(self):
        super().__init__()
        self.vid = Video(File("tsp.mp4"))

    def notify(self, algorithm):
        if algorithm.n_gen % 10 == 0:
            x = algorithm.opt[0].get("X")
            visualize(problem, x, show=False)
            plt.title(f"Generation: {algorithm.n_gen}")
            self.vid.record()