Пример #1
0
def optimize(n_clicks, func, method, max_iter, seed, popsize, constrain, w, c1,
             c2, gamma, strategy, CR, F, sigma, mu_perc, sync):
    bf = BenchmarkFunction(str(func), n_dim=2)
    solver = Evolutionary(
        popsize=int(popsize),
        max_iter=int(max_iter),
        constrain="constrain" in constrain,
        snap=True,
        random_state=int(seed),
        **bf.get(),
    )
    solver.optimize(
        solver=method,
        sync="sync" in sync,
        w=float(w),
        c1=float(c1),
        c2=float(c2),
        gamma=float(gamma),
        strategy=str(strategy),
        CR=float(CR),
        F=float(F),
        sigma=10**float(sigma),
        mu_perc=float(mu_perc),
    )
    energy = solver.energy
    gfit = [energy[:, 0].min()]
    for i in range(1, energy.shape[1]):
        gfit.append(min(gfit[i - 1], energy[:, i].min()))
    return json.dumps([solver.models.tolist()] + [gfit])
 def Solve(self, problem):
     objective_function = lambda x: 50 + problem.Calculate(x)
     lb, ub = problem.GetBounds()
     ea = Evolutionary(objective_function, lower=lb, upper=ub, popsize=self.popsize, \
         max_iter=int(self.max_iters/self.popsize), eps1=1e-16, eps2=1e-16)
     xopt, gfit = ea.optimize(solver='cpso', sync=False, CR=0.4, F=0.5)
     n_evals = problem.GetCalculationsStatistics()
     return xopt, gfit, n_evals
Пример #3
0
    def run(self):
        if self.check_variables():
            # To avoid errors when clicking in the window
            if self.first_run:
                self.first_run = False

            # To ensure repeatability if needed
            if not self.fix_seed.get():
                self.seed.set(numpy.random.randint(self.MAX_SEED))
            numpy.random.seed(self.seed.get())

            # Initialize function
            func = "_".join(self.function.get().split()).lower()
            self.bf = BenchmarkFunction(func, n_dim=2)

            # Solve
            solver_name = self.solver_name.get().lower()
            if solver_name in ["hastings", "hamiltonian"]:
                if solver_name == "hastings":
                    stepsize = [
                        self.mcmc_stepsize_x1.get(),
                        self.mcmc_stepsize_x2.get(),
                    ]
                else:
                    stepsize = self.hmc_stepsize.get()
                self.solver = MonteCarlo(
                    max_iter=self.max_iter.get(),
                    constrain=bool(self.constrain.get()),
                    **self.bf.get(),
                )
                self.solver.sample(sampler=solver_name,
                                   stepsize=stepsize,
                                   n_leap=self.n_leap.get())
            elif solver_name in ["cpso", "pso", "de", "cmaes", "vdcma"]:
                self.solver = Evolutionary(
                    popsize=self.popsize.get(),
                    max_iter=self.max_iter.get(),
                    constrain=bool(self.constrain.get()),
                    snap=True,
                    **self.bf.get(),
                )
                self.solver.optimize(
                    solver=solver_name,
                    sync=bool(self.sync.get()),
                    w=self.w.get(),
                    c1=self.c1.get(),
                    c2=self.c2.get(),
                    gamma=self.gamma.get(),
                    CR=self.CR.get(),
                    F=self.F.get(),
                    strategy=self.strategy.get().lower(),
                    sigma=self.sigma.get(),
                    mu_perc=self.mu_perc.get(),
                )

            # Animate
            self.animate(interval=self.interval.get(), yscale="log")
Пример #4
0
def test_evolutionary(solver, solver_kws, xopt_ref):
    ea = Evolutionary(
        func=lambda x: 100.0 * numpy.sum((x[1:] - x[:-1]**2)**2) + numpy.sum(
            (1.0 - x[:-1])**2),
        lower=numpy.full(2, -5.12),
        upper=numpy.full(2, 5.12),
        popsize=int(4 + numpy.floor(3.0 * numpy.log(2))),
        max_iter=50,
        random_state=42,
    )
    xopt, _ = ea.optimize(solver=solver, **solver_kws)

    assert numpy.allclose(xopt_ref, xopt)
Пример #5
0
class GUI:
    """
    GUI for StochOPy.

    Parameters
    ----------
    master : tkinter object
        tkinter root window.

    """

    master = None
    anim_running = False
    first_run = True

    MAX_SEED = 999999
    FUNCOPT = (
        "Ackley",
        "Quartic",
        "Quartic noise",
        "Rastrigin",
        "Rosenbrock",
        "Sphere",
        "Styblinski-Tang",
    )
    EAOPT = ("CPSO", "PSO", "DE", "CMAES", "VDCMA")
    MCOPT = (
        "Hastings",
        "Hamiltonian",
    )
    STRATOPT = ("rand1", "rand2", "best1", "best2")
    MIN_POPSIZE = {"cpso": 2, "pso": 2, "de": 4, "cmaes": 4, "vdcma": 4}

    def __init__(self, master):
        self.master = master
        master.title("StochOPy Viewer")
        master.protocol("WM_DELETE_WINDOW", self.close_window)
        master.geometry("900x600")
        master.minsize(900, 600)
        master.maxsize(900, 600)

        default_font = font.nametofont("TkDefaultFont")
        default_font.configure(family="Helvetica", size=9)
        master.option_add("*Font", default_font)

        self.define_variables()
        self.trace_variables()
        self.init_variables()
        self.menubar()
        self.frame1()
        self.frame2()
        self.footer()
        self.select_widget(self.solver_name.get())

    def about(self):
        about = "StochOPy Viewer {}".format(__version__)
        about += "\nCreated by Keurfon Luu"
        tkmessage.showinfo("About", about)

    def menubar(self):
        menubar = tk.Menu(self.master)

        # File
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Export models", command=self.export_models)
        filemenu.add_command(label="Export fitness",
                             command=self.export_fitness)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.close_window)

        # Help
        helpmenu = tk.Menu(menubar, tearoff=0)
        helpmenu.add_command(label="About", command=self.about)

        # Display menu bar
        menubar.add_cascade(label="File", menu=filemenu)
        menubar.add_cascade(label="Help", menu=helpmenu)
        self.master.config(menu=menubar)

    def frame1(self):
        self._frame1 = ttk.LabelFrame(self.master,
                                      text="Parameters",
                                      borderwidth=2,
                                      relief="groove")
        self._frame1.place(
            bordermode="outside",
            relwidth=0.99,
            relheight=0.21,
            relx=0,
            x=5,
            y=5,
            anchor="nw",
        )
        self._frame1.first_run = True

        # function
        function_label = ttk.Label(self._frame1, text="Function")
        function_option_menu = ttk.OptionMenu(self._frame1, self.function,
                                              self.function.get(),
                                              *sorted(self.FUNCOPT))

        # max_iter
        max_iter_label = ttk.Label(self._frame1,
                                   text="Maximum number of iterations")
        max_iter_spinbox = Spinbox(
            self._frame1,
            from_=2,
            to_=9999,
            increment=1,
            textvariable=self.max_iter,
            width=6,
            justify="right",
            takefocus=True,
        )

        # fps
        fps_label = ttk.Label(self._frame1, text="Delay between frames (ms)")
        fps_spinbox = Spinbox(
            self._frame1,
            from_=1,
            to_=1000,
            increment=1,
            textvariable=self.interval,
            width=6,
            justify="right",
            takefocus=True,
        )

        # seed
        seed_button = ttk.Checkbutton(self._frame1,
                                      text="Fix seed",
                                      variable=self.fix_seed,
                                      takefocus=False)
        seed_spinbox = Spinbox(
            self._frame1,
            from_=0,
            to_=self.MAX_SEED,
            increment=1,
            textvariable=self.seed,
            width=6,
            justify="right",
            takefocus=True,
        )

        # solver
        solver_label = ttk.Label(self._frame1, text="Solver")

        solver_option_menu = ttk.OptionMenu(
            self._frame1,
            self.solver_name,
            self.solver_name.get(),
            *(self.EAOPT + self.MCOPT),
            command=self.select_widget,
        )

        # constrain
        constrain_button = ttk.Checkbutton(self._frame1,
                                           text="Constrain",
                                           variable=self.constrain,
                                           takefocus=False)

        # Layout
        function_label.place(relx=0.0, x=5, y=5, anchor="nw")
        function_option_menu.place(relx=0.0, x=75, y=3, anchor="nw")
        max_iter_label.place(relx=0.0, x=5, y=30, anchor="nw")
        max_iter_spinbox.place(width=80, relx=0.0, x=220, y=30, anchor="nw")
        fps_label.place(relx=0.0, x=5, y=55, anchor="nw")
        fps_spinbox.place(width=80, relx=0.0, x=220, y=55, anchor="nw")
        seed_button.place(relx=0.0, x=5, y=80, anchor="nw")
        seed_spinbox.place(width=80, relx=0.0, x=220, y=80, anchor="nw")
        solver_label.place(relx=0.35, x=0, y=5, anchor="nw")
        solver_option_menu.place(relx=0.35, x=50, y=3, anchor="nw")
        constrain_button.place(relx=0.35, x=0, y=80, anchor="nw")

    def frame1_pop(self):
        if not self._frame1.first_run:
            self._frame1.pop.forget()
        self._frame1.pop = ttk.Frame(self._frame1, borderwidth=0)
        self._frame1.pop.place(width=170,
                               height=25,
                               relx=0.35,
                               y=30,
                               anchor="nw")
        if self.solver_name.get() in self.EAOPT:
            # popsize
            popsize_label = ttk.Label(self._frame1.pop, text="Population size")
            popsize_spinbox = Spinbox(
                self._frame1.pop,
                from_=1,
                to_=999,
                increment=1,
                textvariable=self.popsize,
                width=3,
                justify="right",
                takefocus=True,
            )

            # Layout
            popsize_label.place(relx=0, x=0, y=0, anchor="nw")
            popsize_spinbox.place(width=60, relx=0, x=110, y=0, anchor="nw")

    def frame1_sync(self):
        if not self._frame1.first_run:
            self._frame1.sync.forget()
        self._frame1.sync = ttk.Frame(self._frame1, borderwidth=0)
        self._frame1.sync.place(width=170,
                                height=25,
                                relx=0.35,
                                y=55,
                                anchor="nw")
        if self.solver_name.get() in ["CPSO", "PSO", "DE"]:
            # sync
            sync_button = ttk.Checkbutton(
                self._frame1.sync,
                text="Synchronize",
                variable=self.sync,
                takefocus=False,
            )

            # Layout
            sync_button.place(relx=0, x=0, y=0, anchor="nw")

    def frame2(self):
        from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

        self._frame2 = ttk.Frame(self.master, borderwidth=2, relief="groove")
        self._frame2.place(
            bordermode="outside",
            relwidth=0.99,
            relheight=0.72,
            relx=0,
            rely=0.22,
            x=5,
            y=5,
            anchor="nw",
        )
        self._frame2.canvas = ttk.Frame(self._frame2, borderwidth=0)
        self._frame2.canvas.place(relwidth=1, relheight=1, relx=0, anchor="nw")
        self.fig = Figure(figsize=(13, 6), facecolor="white")
        self.canvas = FigureCanvasTkAgg(self.fig, master=self._frame2.canvas)
        self.fig.canvas.mpl_connect("button_press_event", self._onClick)
        self.canvas.get_tk_widget().pack()

    def init_widget(self):
        if not self._frame1.first_run:
            self._frame1.sliders.forget()
        else:
            self._frame1.first_run = False
        self._frame1.sliders = ttk.Frame(self._frame1, borderwidth=0)
        self._frame1.sliders.place(relwidth=0.45,
                                   relheight=1.0,
                                   relx=0.55,
                                   anchor="nw")

    def select_widget(self, solver):
        self.frame1_pop()
        self.frame1_sync()
        if solver == "CPSO":
            self.cpso_widget()
        elif solver == "PSO":
            self.pso_widget()
        elif solver == "DE":
            self.de_widget()
        elif solver == "CMAES":
            self.cmaes_widget()
        elif solver == "VDCMA":
            self.cmaes_widget()
        elif solver == "Hastings":
            self.hastings_widget()
        elif solver == "Hamiltonian":
            self.hamiltonian_widget()

    def _label(self, text, position, kwargs={}):
        label = ttk.Label(self._frame1.sliders, text=text, **kwargs)
        if position == 1:
            label.place(relx=0, x=0, y=5, anchor="nw")
        elif position == 2:
            label.place(relx=0, x=0, y=50, anchor="nw")
        elif position == 3:
            label.place(relx=0.5, x=0, y=5, anchor="nw")
        elif position == 4:
            label.place(relx=0.5, x=0, y=50, anchor="nw")
        return label

    def _scale(self,
               from_,
               to,
               resolution,
               variable,
               position,
               command=None,
               kwargs={}):
        if command is None:
            command = lambda s: variable.set(round(float(s), 3))
        scale = ttk.Scale(
            self._frame1.sliders,
            from_=from_,
            to=to,
            variable=variable,
            orient="horizontal",
            length=20,
            command=command,
            takefocus=False,
            **kwargs,
        )
        if position == 1:
            scale.place(relwidth=0.35, relx=0, x=0, y=25, anchor="nw")
        elif position == 2:
            scale.place(relwidth=0.35, relx=0, x=0, y=70, anchor="nw")
        elif position == 3:
            scale.place(relwidth=0.35, relx=0.5, x=0, y=25, anchor="nw")
        elif position == 4:
            scale.place(relwidth=0.35, relx=0.5, x=0, y=70, anchor="nw")
        return scale

    def _entry(self, variable, position, kwargs={}):
        entry = ttk.Entry(
            self._frame1.sliders,
            textvariable=variable,
            justify="right",
            takefocus=True,
            **kwargs,
        )
        if position == 1:
            entry.place(width=45, relx=0.35, x=-3, y=26, anchor="nw")
        elif position == 2:
            entry.place(width=45, relx=0.35, x=-3, y=71, anchor="nw")
        elif position == 3:
            entry.place(width=45, relx=0.85, x=-3, y=26, anchor="nw")
        elif position == 4:
            entry.place(width=45, relx=0.85, x=-3, y=71, anchor="nw")
        return entry

    def pso_widget(self):
        # Initialize widget
        self.init_widget()

        # Omega
        self._label("Inertial weight", 1)
        self._scale(0.0, 1.0, 0.01, self.w, 1)
        self._entry(self.w, 1)

        # C1
        self._label("Cognition parameter", 2)
        self._scale(0.0, 4.0, 0.01, self.c1, 2)
        self._entry(self.c1, 2)

        # C2
        self._label("Sociability parameter", 3)
        self._scale(0.0, 4.0, 0.01, self.c2, 3)
        self._entry(self.c2, 3)

    def cpso_widget(self):
        # Initialize widget
        self.pso_widget()

        # Gamma
        self._label("Competitivity parameter", 4)
        self._scale(0.0, 2.0, 0.01, self.gamma, 4)
        self._entry(self.gamma, 4)

    def de_widget(self):
        # Initialize widget
        self.init_widget()

        # strategy
        self.strategy_label = ttk.Label(self._frame1.sliders, text="Strategy")
        self.strategy_option_menu = ttk.OptionMenu(
            self._frame1.sliders,
            self.strategy,
            self.strategy.get(),
            *sorted(self.STRATOPT),
        )
        self.strategy_label.place(relx=0, x=0, y=5, anchor="nw")
        self.strategy_option_menu.place(relx=0, x=70, y=3, anchor="nw")

        # CR
        self._label("Crossover probability", 2)
        self._scale(0.0, 1.0, 0.01, self.CR, 2)
        self._entry(self.CR, 2)

        # F
        self._label("Differential weight", 4)
        self._scale(0.0, 2.0, 0.01, self.F, 4)
        self._entry(self.F, 4)

    def cmaes_widget(self):
        # Initialize widget
        self.init_widget()

        # sigma
        self._label("Step size", 1)
        self._scale(0.01, 10.0, 0.01, self.sigma, 1)
        self._entry(self.sigma, 1)

        # mu_perc
        self._label("Percentage of offsprings", 2)
        self._scale(0.01, 1.0, 0.01, self.mu_perc, 2)
        self._entry(self.mu_perc, 2)

    def hastings_widget(self):
        # Initialize widget
        self.init_widget()
        vcmd1 = self._frame1.pop.register(self._validate_mcmc_stepsize_x1)
        vcmd2 = self._frame1.pop.register(self._validate_mcmc_stepsize_x2)

        # stepsize x1
        self._label("Step size X1", 1)
        ss = self._scale(
            -4.0,
            0.0,
            0.01,
            self.log_mcmc_stepsize_x1,
            1,
            lambda val: self.mcmc_stepsize_x1.set(round(10.0**float(val), 4)),
        )
        ss.set(numpy.log10(self.mcmc_stepsize_x1.get()))
        self._entry(
            self.mcmc_stepsize_x1,
            1,
            kwargs=dict(validate="key", validatecommand=(vcmd1, "%P")),
        )

        # stepsize x2
        self._label("Step size X2", 2)
        ss = self._scale(
            -4.0,
            0.0,
            0.01,
            self.log_mcmc_stepsize_x2,
            2,
            lambda val: self.mcmc_stepsize_x2.set(round(10.0**float(val), 4)),
        )
        ss.set(numpy.log10(self.mcmc_stepsize_x2.get()))
        self._entry(
            self.mcmc_stepsize_x2,
            2,
            kwargs=dict(validate="key", validatecommand=(vcmd2, "%P")),
        )

    def hamiltonian_widget(self):
        # Initialize widget
        self.init_widget()
        vcmd = self._frame1.pop.register(self._validate_hmc_stepsize)

        # stepsize
        self._label("Step size", 1)
        ss = self._scale(
            -4.0,
            0.0,
            0.01,
            self.log_hmc_stepsize,
            1,
            lambda val: self.hmc_stepsize.set(round(10.0**float(val), 4)),
        )
        ss.set(numpy.log10(self.hmc_stepsize.get()))
        self._entry(
            self.hmc_stepsize,
            1,
            kwargs=dict(validate="key", validatecommand=(vcmd, "%P")),
        )

        # Leap
        self._label("Number of leap frog steps", 2)
        self._scale(
            1,
            100,
            1,
            self.n_leap,
            2,
            lambda val: self.n_leap.set(int(numpy.floor(float(val)))),
        )
        self._entry(self.n_leap, 2)

    def _validate_mcmc_stepsize_x1(self, val):
        try:
            val = float(val)
            if val > 0.0:
                self.log_mcmc_stepsize_x1.set(numpy.log10(val))
        except ValueError:
            pass
        return True

    def _validate_mcmc_stepsize_x2(self, val):
        try:
            val = float(val)
            if val > 0.0:
                self.log_mcmc_stepsize_x2.set(numpy.log10(val))
        except ValueError:
            pass
        return True

    def _validate_hmc_stepsize(self, val):
        try:
            val = float(val)
            if val > 0.0:
                self.log_hmc_stepsize.set(numpy.log10(val))
        except ValueError:
            pass
        return True

    def footer(self):
        # Run button
        run_button = ttk.Button(self.master, text="Run", command=self.run)

        # Exit button
        exit_button = ttk.Button(self.master,
                                 text="Exit",
                                 command=self.close_window)

        # Layout
        run_button.place(relwidth=0.1,
                         relx=0.9,
                         rely=1,
                         x=-5,
                         y=-5,
                         anchor="se")
        exit_button.place(relwidth=0.1,
                          relx=1,
                          rely=1,
                          x=-5,
                          y=-5,
                          anchor="se")

    def run(self):
        if self.check_variables():
            # To avoid errors when clicking in the window
            if self.first_run:
                self.first_run = False

            # To ensure repeatability if needed
            if not self.fix_seed.get():
                self.seed.set(numpy.random.randint(self.MAX_SEED))
            numpy.random.seed(self.seed.get())

            # Initialize function
            func = "_".join(self.function.get().split()).lower()
            self.bf = BenchmarkFunction(func, n_dim=2)

            # Solve
            solver_name = self.solver_name.get().lower()
            if solver_name in ["hastings", "hamiltonian"]:
                if solver_name == "hastings":
                    stepsize = [
                        self.mcmc_stepsize_x1.get(),
                        self.mcmc_stepsize_x2.get(),
                    ]
                else:
                    stepsize = self.hmc_stepsize.get()
                self.solver = MonteCarlo(
                    max_iter=self.max_iter.get(),
                    constrain=bool(self.constrain.get()),
                    **self.bf.get(),
                )
                self.solver.sample(sampler=solver_name,
                                   stepsize=stepsize,
                                   n_leap=self.n_leap.get())
            elif solver_name in ["cpso", "pso", "de", "cmaes", "vdcma"]:
                self.solver = Evolutionary(
                    popsize=self.popsize.get(),
                    max_iter=self.max_iter.get(),
                    constrain=bool(self.constrain.get()),
                    snap=True,
                    **self.bf.get(),
                )
                self.solver.optimize(
                    solver=solver_name,
                    sync=bool(self.sync.get()),
                    w=self.w.get(),
                    c1=self.c1.get(),
                    c2=self.c2.get(),
                    gamma=self.gamma.get(),
                    CR=self.CR.get(),
                    F=self.F.get(),
                    strategy=self.strategy.get().lower(),
                    sigma=self.sigma.get(),
                    mu_perc=self.mu_perc.get(),
                )

            # Animate
            self.animate(interval=self.interval.get(), yscale="log")

    def animate(
        self,
        interval=100,
        nx=101,
        ny=101,
        n_levels=10,
        yscale="linear",
        repeat=True,
        kwargs={},
    ):
        # Clear figure
        if self.anim_running:
            self.anim.event_source.stop()
        self.fig.clear()

        # Initialize parameters
        models = self.solver.models
        if self.solver._solver in ["hastings", "hamiltonian"]:
            func = self._update_monte_carlo
            gfit = self.solver.energy
            frames = models.shape[0]
            linestyle = "--"
            ylabel = "Fitness"
        elif self.solver._solver in ["cpso", "pso", "de", "cmaes", "vdcma"]:
            func = self._update_evolutionary
            gfit = self._gfit(self.solver.energy)
            frames = models.shape[-1]
            linestyle = "none"
            ylabel = "Global best fitness"
        max_iter = len(gfit)
        it = numpy.linspace(1, max_iter, max_iter)

        # Initialize axis
        ax1 = self.fig.add_subplot(1, 2, 1)
        ax2 = self.fig.add_subplot(1, 2, 2)
        self.bf.plot(axes=ax1, cont_kws=kwargs)
        (self.scatplot, ) = ax1.plot(
            [],
            [],
            linestyle=linestyle,
            color="black",
            marker="o",
            markersize=12,
            markerfacecolor="white",
            markeredgecolor="black",
            animated=True,
        )
        ax2.plot(it, gfit, linestyle="-.", linewidth=1, color="black")
        (self.enerplot, ) = ax2.plot([], [],
                                     linestyle="-",
                                     linewidth=2,
                                     color="red")
        ax1.set_xlabel("X1", fontsize=12)
        ax1.set_ylabel("X2", fontsize=12)
        ax1.set_xlim(self.bf._lower[0], self.bf._upper[0])
        ax1.set_ylim(self.bf._lower[1], self.bf._upper[1])
        ax2.set_xlim((1, max_iter))
        ax2.set_yscale(yscale)
        ax2.set_ylabel(ylabel, fontsize=12)
        ax2.grid(True, linestyle=":")
        self.iter = ax2.text(
            0.99,
            0.99,
            "",
            va="top",
            ha="right",
            fontsize=10,
            transform=ax2.transAxes,
            animated=True,
        )

        # Animate
        self.anim_running = True
        self.anim = animation.FuncAnimation(
            self.fig,
            func,
            fargs=(models, gfit),
            frames=frames,
            interval=interval,
            repeat=repeat,
            blit=True,
        )
        self.fig.tight_layout()

    def _update_monte_carlo(self, i, models, gfit):
        self.scatplot.set_data(models[:i, 0], models[:i, 1])
        self.enerplot.set_xdata(numpy.linspace(1, i + 1, i + 1))
        self.enerplot.set_ydata(gfit[:i + 1])
        self.iter.set_text("Sample %d" % (i + 1))
        return (
            self.scatplot,
            self.enerplot,
            self.iter,
        )

    def _update_evolutionary(self, i, models, gfit):
        self.scatplot.set_data(models[:, 0, i], models[:, 1, i])
        self.enerplot.set_xdata(numpy.linspace(1, i + 1, i + 1))
        self.enerplot.set_ydata(gfit[:i + 1])
        self.iter.set_text("Iteration %d" % (i + 1))
        return (
            self.scatplot,
            self.enerplot,
            self.iter,
        )

    def _gfit(self, energy):
        gfit = [energy[:, 0].min()]
        for i in range(1, energy.shape[1]):
            gfit.append(min(gfit[i - 1], energy[:, i].min()))
        return numpy.array(gfit)

    def _onClick(self, event):
        if not self.first_run:
            if self.anim_running:
                self.anim.event_source.stop()
                self.anim_running = False
            else:
                self.anim.event_source.start()
                self.anim_running = True

    def export_models(self):
        if self._check_run():
            filename = tkfile.asksaveasfilename(
                title="Export models",
                filetypes=[("Pickle", ".pickle")],
                defaultextension=".pickle",
            )
            if len(filename) > 0:
                with open(filename, "wb") as f:
                    pickle.dump(self.solver.models,
                                f,
                                protocol=pickle.HIGHEST_PROTOCOL)

    def export_fitness(self):
        if self._check_run():
            filename = tkfile.asksaveasfilename(
                title="Export fitness",
                filetypes=[("Pickle", ".pickle")],
                defaultextension=".pickle",
            )
            if len(filename) > 0:
                with open(filename, "wb") as f:
                    pickle.dump(self.solver.energy,
                                f,
                                protocol=pickle.HIGHEST_PROTOCOL)

    def _check_run(self):
        if self.first_run:
            tkmessage.showerror("Error", "No optimization performed yet.")
            return False
        else:
            return True

    def close_window(self):
        yes = tkmessage.askyesno("Exit", "Do you really want to quit?")
        if yes:
            self.close()

    def define_variables(self):
        self.solver_name = tk.StringVar(self.master)
        self.function = tk.StringVar(self.master)
        self.popsize = tk.IntVar(self.master)
        self.max_iter = tk.IntVar(self.master)
        self.interval = tk.IntVar(self.master)
        self.mcmc_stepsize_x1 = tk.DoubleVar(self.master)
        self.mcmc_stepsize_x2 = tk.DoubleVar(self.master)
        self.hmc_stepsize = tk.DoubleVar(self.master)
        self.log_mcmc_stepsize_x1 = tk.DoubleVar(self.master)
        self.log_mcmc_stepsize_x2 = tk.DoubleVar(self.master)
        self.log_hmc_stepsize = tk.DoubleVar(self.master)
        self.n_leap = tk.IntVar(self.master)
        self.w = tk.DoubleVar(self.master)
        self.c1 = tk.DoubleVar(self.master)
        self.c2 = tk.DoubleVar(self.master)
        self.gamma = tk.DoubleVar(self.master)
        self.CR = tk.DoubleVar(self.master)
        self.F = tk.DoubleVar(self.master)
        self.strategy = tk.StringVar(self.master)
        self.sigma = tk.DoubleVar(self.master)
        self.mu_perc = tk.DoubleVar(self.master)
        self.seed = tk.IntVar(self.master)
        self.fix_seed = tk.BooleanVar(self.master)
        self.constrain = tk.BooleanVar(self.master)
        self.sync = tk.BooleanVar(self.master)

    def trace_variables(self):
        self.solver_name.trace("w", self.callback)
        self.function.trace("w", self.callback)
        self.popsize.trace("w", self.callback)
        self.max_iter.trace("w", self.callback)
        self.interval.trace("w", self.callback)
        self.mcmc_stepsize_x1.trace("w", self.callback)
        self.mcmc_stepsize_x2.trace("w", self.callback)
        self.hmc_stepsize.trace("w", self.callback)
        self.log_mcmc_stepsize_x1.trace("w", self.callback)
        self.log_mcmc_stepsize_x2.trace("w", self.callback)
        self.log_hmc_stepsize.trace("w", self.callback)
        self.n_leap.trace("w", self.callback)
        self.w.trace("w", self.callback)
        self.c1.trace("w", self.callback)
        self.c2.trace("w", self.callback)
        self.gamma.trace("w", self.callback)
        self.CR.trace("w", self.callback)
        self.F.trace("w", self.callback)
        self.strategy.trace("w", self.callback)
        self.sigma.trace("w", self.callback)
        self.mu_perc.trace("w", self.callback)
        self.seed.trace("w", self.callback)
        self.fix_seed.trace("w", self.callback)
        self.constrain.trace("w", self.callback)
        self.sync.trace("w", self.callback)

    def init_variables(self):
        self.solver_name.set("CPSO")
        self.function.set("Rosenbrock")
        self.popsize.set(10)
        self.max_iter.set(200)
        self.interval.set(60)
        self.mcmc_stepsize_x1.set(0.01)
        self.mcmc_stepsize_x2.set(0.01)
        self.hmc_stepsize.set(0.001)
        self.log_mcmc_stepsize_x1.set(numpy.log10(self.mcmc_stepsize_x1.get()))
        self.log_mcmc_stepsize_x2.set(numpy.log10(self.mcmc_stepsize_x2.get()))
        self.log_hmc_stepsize.set(numpy.log10(self.hmc_stepsize.get()))
        self.n_leap.set(10)
        self.w.set(0.73)
        self.c1.set(1.496)
        self.c2.set(1.496)
        self.gamma.set(1.0)
        self.CR.set(0.1)
        self.F.set(0.5)
        self.strategy.set("best2")
        self.sigma.set(0.5)
        self.mu_perc.set(0.5)
        self.seed.set(42)
        self.fix_seed.set(False)
        self.constrain.set(True)
        self.sync.set(False)

    def check_variables(self):
        # Check popsize
        solver_name = self.solver_name.get().lower()
        if (solver_name.upper() in self.EAOPT
                and self.popsize.get() < self.MIN_POPSIZE[solver_name]):
            tkmessage.showerror(
                "Error",
                "For %s, population size should be greater than %d." %
                (solver_name.upper(), self.MIN_POPSIZE[solver_name] - 1),
            )
            return False
        return True

    def close(self):
        self.master.quit()
        self.master.destroy()

    def callback(self, *args):
        pass
Пример #6
0
"""

from time import time
try:
    from stochopy import Evolutionary, BenchmarkFunction
except ImportError:
    import sys
    sys.path.append("../")
    from stochopy import Evolutionary, BenchmarkFunction

if __name__ == "__main__":
    # Parameters
    func = "sphere"

    # Initialize function
    bf = BenchmarkFunction(func, n_dim=10)

    # Initialize solver
    ea = Evolutionary(popsize=10,
                      max_iter=200,
                      constrain=True,
                      random_state=-1,
                      **bf.get())

    # Solve
    starttime = time()
    ea.optimize(solver="pso", sync=False)

    # Print solution
    print(ea)
    print("Elapsed time: %.2f seconds" % (time() - starttime))
Пример #7
0
    def invert(self,
               dcurves,
               beta,
               thickness,
               ny=100,
               dtype="float32",
               n_threads=1,
               evo_kws=dict(popsize=10, max_iter=100, constrain=True),
               opt_kws=dict(solver="cpso")):
        """
        Invert the different modes of the dispersion curve for a layered
        velocity model. Layers' P-wave velocities are determined by the Vp/Vs
        ratio ranging in [ 1.5, 2.2 ]. High uncertainties should be expected
        for P-wave velocities as surface waves are not much sensitive to Vp.
        Layers' densities are determined using the Nafe-Drake's equation as
        they only affect the amplitudes of the dispersion, not the location of
        the zero-crossing.
        
        Parameters
        ----------
        dcurves : list of DispersionCurve
            Dispersion curves to invert.
        beta : ndarray (beta_min, beta_max)
            S-wave velocity boundaries in m/s.
        thickness : ndarray (d_min, d_max)
            Layer thickness boundaries in m.
        ny : int, default 100
            Number of samples on the Y axis.
        dtype : {'float32', 'float64'}, default 'float32'
            Models data type.
        n_threads : int, default 1
            Number of threads to pass to OpenMP for forward modelling.
        evo_kws : dict
            Keywords to pass to evolutionary algorithm initialization.
        opt_kws : dict
            Keywords to pass to evolutionary algorithm optimizer.
        """
        # Check inputs
        if not isinstance(dcurves, (list, tuple)) or not np.all(
            [isinstance(dcurve, DispersionCurve) for dcurve in dcurves]):
            raise ValueError(
                "dcurves must be a list of DispersionCurve objects")
        else:
            self._dcurves = dcurves
        if not isinstance(beta, np.ndarray) or beta.ndim != 2:
            raise ValueError("beta must be a 2-D ndarray")
        else:
            self._n_layers = beta.shape[0]
        if np.any(beta[:, 1] < beta[:, 0]):
            raise ValueError(
                "elements in beta_max must be greater than beta_min")
        if not isinstance(thickness, np.ndarray) or thickness.ndim != 2:
            raise ValueError("thickness must be a 2-D ndarray")
        if thickness.shape[0] != self._n_layers:
            raise ValueError("inconsistent number of layers in thickness, got %d instead of %d" \
                             % (thickness.shape[0], self._n_layers))
        if np.any(thickness[:, 1] < thickness[:, 0]):
            raise ValueError("elements in d_max must be greater than d_min")
        if not isinstance(ny, int) or ny < 1:
            raise ValueError("ny must be a positive integer")
        if not isinstance(n_threads, int) or n_threads < 1:
            raise ValueError("n_threads must be a positive integer")
        if not isinstance(opt_kws, dict):
            raise ValueError("opt_kws must be a dictionary")
        if not isinstance(evo_kws, dict):
            raise ValueError("evo_kws must be a dictionary")
        if "constrain" not in evo_kws:
            evo_kws.update(constrain=True)
        else:
            evo_kws["constrain"] = True
        if "eps2" not in evo_kws:
            evo_kws.update(eps2=-1e30)
        else:
            evo_kws["eps2"] = -1e30
        if "snap" not in evo_kws:
            evo_kws.update(snap=True)
        else:
            evo_kws["snap"] = True

        args = (ny, n_threads)
        # get the lowest, highest value from index 0,1
        lower = np.concatenate(
            (beta[:, 0], thickness[:, 0], np.full(self._n_layers, 1.51)))
        upper = np.concatenate(
            (beta[:, 1], thickness[:, 1], np.full(self._n_layers, 2.19)))
        ea = Evolutionary(self._costfunc, lower, upper, args=args, **evo_kws)
        # no starting model
        xopt, gfit = ea.optimize(**opt_kws)
        self._misfit = gfit
        # output model is 1D list
        self._model = np.array(xopt, dtype=dtype)
        self._misfits = np.array(ea.energy, dtype=dtype)
        self._models = np.array(ea.models, dtype=dtype)
        self._n_iter = ea.n_iter
        self._n_eval = ea.n_eval
        return self
                           callback=None,
                           options=None)

        elif method == 'DE':

            ea = DE(simulator.objfunction,
                    bounds,
                    mutation=(0.5, 1.0),
                    maxiters=num)
            p, f = ea.solve(show_progress=True)

        elif method == 'GA':

            ea = Evolutionary(simulator.objfunction,
                              lower,
                              upper,
                              popsize=15,
                              max_iter=num,
                              random_state=3)
            xopt, gfit = ea.optimize(solver="cpso")

        # the simulator object stores all the data itself
        df_X = pd.DataFrame(
            simulator.X, columns=['Solar (kW)', 'Wind (kW)', 'Battery (kWh)'])
        df_Y = pd.DataFrame(
            simulator.Y,
            columns=['LCOE', 'Cost', 'Income', 'Income_Cost_Ratio', 'Benefit'])

        experiment_name = 'Optimization_mapping/' + method + '/Space_mapping_' + method + '_' + str(
            num)

        if not os.path.exists(experiment_name):
Пример #9
0
 -0.1252477303982147E+01   1   1   0   0
 -0.4759344611440753E+00   2   2   0   0
  0.7137758743754461E+00   0   0   0   0
""")


# Define PSO objective function
def f(params):
    # note 'no-mpi' at the compute energy, hamiltonian term
    # level, but we are using mpi at the higher PSO swarm level
    return vqe.execute(
        op, **{
            'mpi-provider': 'no-mpi',
            'task': 'compute-energy',
            'vqe-params': str(params[0]) + ',' + str(params[1])
        }).energy


# Construct Stochopy PSO algorithm and execute
ea = Evolutionary(f,
                  popsize=30,
                  lower=np.array([-np.pi, -np.pi]),
                  upper=np.array([np.pi, np.pi]),
                  n_dim=2,
                  mpi=True)
ea.optimize(solver="pso")

# Print result
if mpi_rank == 0:
    print('Our result = ', ea)
 def fit(self, X, y):
     """
     Fit the model to data matrix X and target y.
     
     Parameters
     ----------
     X : ndarray of shape (n_samples, n_features)
         Input data.
     y : ndarray of length n_samples
         Target values.
     
     Returns
     -------
     self : returns a trained ENNClassifier.
     """
     # Check inputs and initialize
     self._validate_hyperparameters()
     X, y = self._initialize(X, y)
     
     # Initialize boundaries
     n_dim = np.sum([ np.prod(i[-1]) for i in self.coef_indptr_ ])
     lower = np.full(n_dim, -self.bounds)
     upper = np.full(n_dim, self.bounds)
     
     # Optimize using DE, PSO, CPSO or CMA-ES
     ea = Evolutionary(self._loss,
                       lower = lower,
                       upper = upper,
                       max_iter = self.max_iter,
                       popsize = self.popsize,
                       eps1 = self.eps1,
                       eps2 = self.eps2,
                       constrain = False,
                       args = (X,))
     if self.solver == "de":
         packed_coefs, self.loss_ = ea.optimize(solver = "de",
                                                F = self.F,
                                                CR = self.CR,
                                                )
     elif self.solver == "pso":
         packed_coefs, self.loss_ = ea.optimize(solver = "pso",
                                                w = self.w,
                                                c1 = self.c1,
                                                c2 = self.c2,
                                                )
     elif self.solver == "cpso":
         packed_coefs, self.loss_ = ea.optimize(solver = "cpso",
                                                w = self.w,
                                                c1 = self.c1,
                                                c2 = self.c2,
                                                gamma = self.gamma,
                                                )
     elif self.solver == "cmaes":
         packed_coefs, self.loss_ = ea.optimize(solver = "cmaes",
                                                sigma = self.sigma,
                                                mu_perc = self.mu_perc)
     elif self.solver == "vdcma":
         packed_coefs, self.loss_ = ea.optimize(solver = "vdcma",
                                                sigma = self.sigma,
                                                mu_perc = self.mu_perc)
     self.coefs_ = self._unpack(packed_coefs)
     self.flag_ = ea.flag
     self.n_iter_ = ea.n_iter
     self.n_eval_ = ea.n_eval        
     return self
Пример #11
0
except ImportError:
    import sys
    sys.path.append("../")
    from stochopy import Evolutionary, BenchmarkFunction

if __name__ == "__main__":
    # Parameters
    func = "rastrigin"

    # Initialize MPI
    mpi_comm = MPI.COMM_WORLD
    mpi_rank = mpi_comm.Get_rank()

    # Initialize function
    bf = BenchmarkFunction(func, n_dim=30)

    # Initialize solver
    ea = Evolutionary(popsize=30,
                      max_iter=2000,
                      random_state=-1,
                      mpi=True,
                      **bf.get())

    # Solve
    starttime = time()
    ea.optimize(solver="cpso")

    # Print solution
    if mpi_rank == 0:
        print(ea)
        print("Elapsed time: %.2f seconds" % (time() - starttime))
Пример #12
0
License: MIT
"""

import matplotlib.pyplot as plt
try:
    from stochopy import Evolutionary, BenchmarkFunction
except ImportError:
    import sys
    sys.path.append("../")
    from stochopy import Evolutionary, BenchmarkFunction

if __name__ == "__main__":
    # Parameters
    func = "rastrigin"

    # Initialize function
    bf = BenchmarkFunction(func, n_dim=2)

    # Initialize solver
    ea = Evolutionary(popsize=5, max_iter=200, snap=True, **bf.get())

    # Solve
    ea.optimize(solver="cpso")

    # Plot in 3D
    ax1 = bf.plot(figsize=(8, 6), projection="3d")
    ax1.view_init(azim=-45, elev=74)
    plt.show()

    # Save figure
    plt.savefig("%s.png" % func)
# Communication
Xstart = comm.bcast(Xstart, root=0)
gmod_best = comm.bcast(gmod_best, root=0)
mod_best = comm.bcast(mod_best, root=0)
V_prev = comm.bcast(V_prev, root=0)

# RESTART
# ---> lower and upper should remain the exact same regardless of jobs
# ---> may be better to keep them in netcdf file or a coefficient
# ---> in netcdf file
lower = Xi - np.ones(n_dim) * cst_upper
upper = Xi + np.ones(n_dim) * cst_lower

# Initialize SOLVER
# Added + 1 to max_iter to have the desired number of iteration
ea = Evolutionary(F, lower = lower, upper = upper, popsize = popsize,
                  max_iter = n_iter_job+1, mpi = True, snap = True)

# SOLVE
xopt,gfit=ea.optimize(solver = "cpso", xstart = Xstart , sync = True,
                      mod_best = mod_best, gmod_best = gmod_best,
                      V_prev = V_prev, it_prev = it_start-1,
                      max_iter_tot = max_iter)


  ### #-----------------------------------------------#
  ### #                                               #
  ### #    COMPUTE FUNCTION DENSITY PROBABILITY       #
  ### #                                               #
  ### #-----------------------------------------------#
  ### if rank==0:
  ###     nparam=len(np.unique(model))
Xstart = np.zeros((popsize, len(Xi)))
div = popsize / np.float(n_dim)
for i in range(popsize):
    Xstart[i, :] = Xi

lower = Xi - np.ones(n_dim) * 2
upper = Xi + np.ones(n_dim) * 2
#print lower,upper
### lower=Xi
### upper=Xi

# Initialize SOLVER
ea = Evolutionary(F,
                  lower=lower,
                  upper=upper,
                  popsize=popsize,
                  max_iter=max_iter,
                  mpi=True,
                  snap=True)

# SOLVE
xopt, gfit = ea.optimize(solver="cpso", xstart=Xstart, sync=True)

# Jc write ea.model ea.energy xopt

### #-----------------------------------------------#
### #                                               #
### #    COMPUTE FUNCTION DENSITY PROBABILITY       #
### #                                               #
### #-----------------------------------------------#
### if rank==0:
Пример #15
0
import numpy as np
try:
    from stochopy import Evolutionary, BenchmarkFunction
except ImportError:
    import sys
    sys.path.append("../")
    from stochopy import Evolutionary, BenchmarkFunction

if __name__ == "__main__":
    # Parameters
    func = "rastrigin"
    n_run = 100

    # Initialize function
    bf = BenchmarkFunction(func, n_dim=20)

    # Initialize solver
    ea = Evolutionary(popsize=20, max_iter=1000, eps2=1., **bf.get())

    # Solve
    sync, async = [], []
    for i in range(n_run):
        ea.optimize(solver="de", sync=True)
        sync.append(ea._n_eval)
        ea.optimize(solver="de", sync=False)
        async .append(ea._n_eval)

    # Print results
    print("Mean number of fitness evaluations:")
    print("Synchronous: %d" % np.mean(sync))
    print("Asynchronous: %d" % np.mean(async))