Exemplo n.º 1
0
def pso():
    swarm = Swarm()
    particle0 = Particle(my_function.min_val, my_function.max_val)
    swarm.particles.append(particle0)
    swarm.global_best_position = particle0.position
    particle0.best_value = my_function.my_function(particle0.position[0], particle0.position[1])
    swarm.global_best_value = particle0.best_value

    for i in range(0, max_particle - 1):
        particle = Particle(my_function.min_val, my_function.max_val)
        swarm.particles.append(particle)
        value = my_function.my_function(particle.position[0], particle.position[1])
        particle.best_value = value
        if value < swarm.global_best_value:
            swarm.global_best_value = value
            swarm.global_best_position = particle.position

    if to_plot:
        plot_graph(None, my_function.function_name, 'viridis', None)
        plot_graph(swarm.particles, 'initial state', 'Pastel1', swarm.global_best_position)

    # position(k+1) = position(k) + v(k+1)
    # v(k+1) = w(k+1) * v(k) +
    #           cp(k+1) * rand * (best_position(k) - position(k)) +
    #           cg(k+1) * rand * (global_best_position - position(k))
    for it in range(0, max_iteration):
        for particle in swarm.particles:
            v_kplus1 = w(it) * particle.prev_v + \
                       cp(it) * np.random.random() * (particle.best_position - particle.position) + \
                       cg(it) * np.random.random() * (swarm.global_best_position - particle.position)
            next_position = particle.position + v_kplus1
            value = my_function.my_function(particle.position[0], particle.position[1])
            if value < particle.best_value:
                particle.best_value = value
                particle.best_position = next_position
            particle.position = next_position
            particle.prev_v = v_kplus1

        for particle in swarm.particles:
            value = my_function.my_function(particle.position[0], particle.position[1])

            if value < swarm.global_best_value:
                swarm.global_best_value = value
                swarm.global_best_position = particle.position

        if it % (max_iteration // 10) == 0 and to_plot:
            plot_graph(swarm.particles, 'iteration ' + str(it + 1), 'Pastel1', swarm.global_best_position)

    if to_plot:
        plot_graph(swarm.particles, 'final state', 'Pastel1', swarm.global_best_position)

    print('x = {0}'.format(swarm.global_best_position[0]))
    print('y = {0}'.format(swarm.global_best_position[1]))
    print('Global best = {0}'.format(swarm.global_best_value))

    analytic_xy, analytic_best = my_function.analytic_result()
    print('\nPontos eredmény:')
    print('x = {0}'.format(analytic_xy[0]))
    print('y = {0}'.format(analytic_xy[1]))
    print('Global best = {0}'.format(analytic_best))
Exemplo n.º 2
0
def plot_graph(particles, title, color, global_postion):
    fig = plt.figure(figsize=(5, 5), dpi=200)
    ax = plt.axes()

    x = np.linspace(my_function.min_val - 1, my_function.max_val + 1, 100)
    y = np.linspace(my_function.min_val - 1, my_function.max_val + 1, 100)

    X, Y = np.meshgrid(x, y)
    z = my_function.my_function(X, Y)

    if color == 'Pastel1':
        for particle in particles:
            if my_function.min_val < particle.position[
                    0] < my_function.max_val and my_function.min_val < particle.position[
                        1] < my_function.max_val:
                ax.scatter(particle.position[0],
                           particle.position[1],
                           c='#32CAF6',
                           zorder=20)

        ax.scatter(global_postion[0], global_postion[1], c='r', zorder=21)

    ax.contour(x, y, z, cmap=color)
    plt.title(title)
    ax.set_xlabel('x')
    ax.set_ylabel('y')

    plt.savefig(my_function.function_name + '/' + title + '.png')
Exemplo n.º 3
0
    def create_png1(self, player):

        fig, ax = matplotlib.pyplot.subplots()
        ax.set_title(player + "のスタッツ・・・")
        obj = func.my_function()
        obj.getCorr(player)
        matplotlib.pyplot.savefig(self.file_name)
Exemplo n.º 4
0
    def create_png(self, player, itemName):

        fig, ax = matplotlib.pyplot.subplots()
        ax.set_title(player + "のスタッツ・・・" + itemName)
        obj = func.my_function()
        obj.getData(itemName, player)
        canvas = FigureCanvasAgg(fig)
        canvas.print_figure(self.file_name)
Exemplo n.º 5
0
def occr(playerName):
    obj = func.my_function()
    obj.delFiles("static/img/")

    title = constans.playerList[playerName]
    my_list = []
    my_dic = {}
    my_dic["message"] = "相関係数(勝敗と各項目)"
    obj = tempImage.TempImage("")
    my_dic["imagePath"] = "../" + obj.getPng1(playerName)
    my_list.append(my_dic)
    return render_template('stats.html', title=title, itemList=my_list)
Exemplo n.º 6
0
def stats(playerName):
    obj = func.my_function()
    obj.delFiles("static/img/")

    title = constans.playerList[playerName] + "のスタッツ"
    my_list = []

    for item in constans.items:
        my_dic = {}
        my_dic["message"] = item
        obj = tempImage.TempImage("")
        my_dic["imagePath"] = "../" + obj.getPng(playerName, item)
        my_list.append(my_dic)

    return render_template('stats.html', title=title, itemList=my_list)
Exemplo n.º 7
0
def plot_function_3d(function_name):
    fig = plt.figure(figsize=(5, 5), dpi=200)
    ax = plt.axes(projection='3d')

    x = np.linspace(my_function.min_val - 1, my_function.max_val + 1, 100)
    y = np.linspace(my_function.min_val - 1, my_function.max_val + 1, 100)

    X, Y = np.meshgrid(x, y)
    z = my_function.my_function(X, Y)

    #ax.contour3D(X, Y, z, 200)
    ax.plot_surface(X, Y, z, cmap="plasma", lw=0.5, rstride=1, cstride=1)
    ax.contour(X, Y, z, 10, lw=3, cmap="Pastel2", linestyles="solid", offset=0)

    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')

    ax.view_init(45, 45)

    plt.savefig(function_name + '/' + function_name + '3D.png')
Exemplo n.º 8
0
# -*- coding: utf-8 -*-
from my_function import my_function

if __name__ == '__main__':
    print(my_function('a', 3))