Esempio n. 1
0
    def do(self, problem, algorithm):
        import matplotlib.pyplot as plt

        if problem.n_var != 2 or problem.n_obj != 1:
            raise Exception(
                "This visualization can only be used for problems with two variables and one objective!")

        # draw the problem surface
        FitnessLandscape(problem,
                         _type="contour",
                         kwargs_contour=dict(alpha=0.3),
                         n_samples=self.n_samples_for_surface,
                         close_on_destroy=False).do()

        # get the population
        off = algorithm.pop
        pop = algorithm.pop if self.last_pop is None else self.last_pop
        pbest = Population.create(*off.get("pbest"))

        for i in range(len(pop)):
            plt.plot([off[i].X[0], pop[i].X[0]], [off[i].X[1], pop[i].X[1]], color="blue", alpha=0.5)
            plt.plot([pbest[i].X[0], pop[i].X[0]], [pbest[i].X[1], pop[i].X[1]], color="red", alpha=0.5)
            plt.plot([pbest[i].X[0], off[i].X[0]], [pbest[i].X[1], off[i].X[1]], color="red", alpha=0.5)

        X, F, CV = pbest.get("X", "F", "CV")
        plt.scatter(X[:, 0], X[:, 1], edgecolors="red", marker="*", s=70, facecolors='none', label="pbest")

        X, F, CV = off.get("X", "F", "CV")
        plt.scatter(X[:, 0], X[:, 1], color="blue", marker="o", s=30, label="particle")

        X, F, CV = pop.get("X", "F", "CV")
        plt.scatter(X[:, 0], X[:, 1], color="blue", marker="o", s=30, alpha=0.5)

        opt = algorithm.opt
        X, F, CV = opt.get("X", "F", "CV")
        plt.scatter(X[:, 0], X[:, 1], color="black", marker="x", s=100, label="gbest")

        xl, xu = problem.bounds()
        plt.xlim(xl[0], xu[0])
        plt.ylim(xl[1], xu[1])

        plt.title(f"Generation: %s \nf: %.5E" % (algorithm.n_gen, opt[0].F[0]))
        plt.legend()

        self.last_pop = off.copy(deep=True)
Esempio n. 2
0
    def do(self, problem, algorithm):

        # check whether the visualization can be done or not - throw exception or simply do nothing
        if problem.n_var != 2 or problem.n_obj != 1:
            raise Exception("This visualization can only be used for problems with two variables and one objective!")

        # draw the problem surface
        FitnessLandscape(problem, _type="contour", kwargs_contour=dict(alpha=0.5)).do()

        # get the population
        pop = algorithm.pop

        X, F, CV = pop.get("X", "F", "CV")
        plt.scatter(X[:, 0], X[:, 1], color="blue", marker="o", s=70)

        is_new = np.full(len(pop), True)
        if self.last_pop is not None:
            for k, ind in enumerate(pop):
                if ind in self.last_pop:
                    is_new[k] = False

        # plot the new population
        if is_new.sum() > 0:
            X, F, CV = pop[is_new].get("X", "F", "CV")
            plt.scatter(X[:, 0], X[:, 1], color="red", marker="*", s=70)

        if hasattr(algorithm, "off") and algorithm.off is not None:
            X, F, CV = algorithm.off.get("X", "F", "CV")
            plt.scatter(X[:, 0], X[:, 1], color="purple", marker="*", s=40)

        xl, xu = problem.bounds()
        plt.xlim(xl[0], xu[0])
        plt.ylim(xl[1], xu[1])

        plt.title(f"Generation: {algorithm.n_gen}")
        plt.legend()

        # store the current population as the last
        self.last_pop = set(pop)
Esempio n. 3
0
    def do(self, problem, algorithm):

        # check whether the visualization can be done or not - throw exception or simply do nothing
        if problem.n_var != 2 or problem.n_obj != 1:
            raise Exception(
                "This visualization can only be used for problems with two variables and one objective!"
            )

        # draw the problem surface
        # if algorithm.surrogate.targets["F"].doe is not None:
        #     problem = algorithm.surrogate
        plot = FitnessLandscape(problem,
                                _type="contour",
                                kwargs_contour=dict(alpha=0.5))
        plot.do()

        # get the population
        pop = algorithm.pop

        X, F, CV = pop.get("X", "F", "CV")
        plt.scatter(X[:, 0],
                    X[:, 1],
                    facecolor="none",
                    edgecolors="black",
                    marker="o",
                    s=50,
                    label="Solutions")

        if hasattr(algorithm, "off") and algorithm.off is not None:
            X, F, CV = algorithm.off.get("X", "F", "CV")
            plt.scatter(X[:, 0],
                        X[:, 1],
                        color="green",
                        marker="D",
                        s=30,
                        label="Offsprings")

        is_new = np.full(len(pop), True)
        if self.last_pop is not None:
            for k, ind in enumerate(pop):
                if ind in self.last_pop:
                    is_new[k] = False

        # plot the new population
        if is_new.sum() > 0:
            X, F, CV = pop[is_new].get("X", "F", "CV")
            plt.scatter(X[:, 0],
                        X[:, 1],
                        color="red",
                        marker="*",
                        s=70,
                        label="Survivors")

        xl, xu = problem.bounds()
        plt.xlim(xl[0], xu[0])
        plt.ylim(xl[1], xu[1])

        plt.title(f"Generation: {algorithm.n_gen}")
        plt.legend()

        # store the current population as the last
        self.last_pop = set(pop)

        plt.show()

        return plt.gcf()
Esempio n. 4
0
                   algorithm,
                   termination=('n_gen', 10000),
                   seed=1,
                   save_history=True,
                   verbose=False)

    print(ret.F)

    with Video(File("mm.mp4")) as vid:
        for algorithm in ret.history:

            if algorithm.n_gen % 100 == 0:

                fl = FitnessLandscape(problem,
                                      _type="contour",
                                      kwargs_contour=dict(linestyles="solid",
                                                          offset=-1,
                                                          alpha=0.4))
                fl.do()

                for k, solver in enumerate(algorithm.solvers):
                    X = solver.pop.get("X")
                    plt.scatter(X[:, 0], X[:, 1], marker="x", label=k)

                for k, solver in enumerate(algorithm.niches):
                    X = solver.opt.get("X")
                    plt.scatter(X[:, 0],
                                X[:, 1],
                                marker="x",
                                color="black",
                                label="Niche %s" % k,