我是python和学习阶段的新手 . 我想实现粒子群优化(PSO)算法,我通过在线材料和python教程的帮助来完成 . 在PSO中,推断出一个简单的微积分问题,即 100 * ((y - (x 2)) 2) + ((1 - (x 2))_ 1182447 . 此问题在 fitness 函数中定义 .

def fitness(x, y):
    return 100 * ((y - (x**2))**2) + ((1 - (x**2))**2)

现在,我希望通过简单的第一阶 Ordinary Differential Equation(ODE) 来替换这个简单的微积分问题,而无需更改现有函数参数 (x,y) ,并希望返回 dy_dx,y0 and t 的值以进行进一步处理 .

# Define a function which calculates the derivative
def dy_dx(y, x):
    return x - y

t = np.linspace(0,5,100)
y0 = 1.0  # the initial condition
ys = odeint(dy_dx, y0, t)`

在python odeint 中,函数用于 ODE ,这需要三个基本参数,即 func/model ,_ 1186057(y上的初始条件(可以是向量)和 t (为y求解的时间序列)Example of odeint parameters.我不想更改其参数因为我很难对算法进行更改 .

为简单起见,我粘贴了下面的完整代码,如果想要在 General Best, Personal Bestr[i] 中使用更多参数修改代码,我的问题对所有人开放 .

import numpy as np
    from scipy.integrate import odeint
    import random as rand
    from scipy.integrate import odeint
    from numpy import array
    import matplotlib.pyplot as plt

    def main():
    #Variables
    n = 40
    num_variables = 2

    a = np.empty((num_variables, n))
    v = np.empty((num_variables, n))
    Pbest = np.empty((num_variables, n))
    Gbest = np.empty((1, 2))
    r = np.empty((n))

    for i in range(0, num_variables):
        for j in range(0, n):
            Pbest[i][j] = rand.randint(-20, 20)
            a[i][j] = Pbest[i][j]
            v[i][j] = 0

    for i in range(0, n):
        r[i] = fitness(a[0][i], a[1][i])

    #Sort elements of Pbest
    Order(Pbest, r, n)

    Gbest[0][0] = Pbest[0][0]
    Gbest[0][1] = Pbest[1][0]

    generation = 0

    plt.ion()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.grid(True)

    while(generation < 1000):
        for i in range(n):
            #Get Personal Best
            if(fitness(a[0][i], a[1][i]) < fitness(Pbest[0][i], Pbest[1][i])):
                Pbest[0][i] = a[0][i]
                Pbest[1][i] = a[1][i]
            #Get General Best
            if(fitness(Pbest[0][i], Pbest[1][i]) < fitness(Gbest[0][0], Gbest[0][1])):
                Gbest[0][0] = Pbest[0][i]
                Gbest[0][1] = Pbest[1][i]
            #Calculate Velocity
            Vector_Velocidad(n, a, Pbest, Gbest, v)
        generation = generation + 1
        print 'Generacion: ' + str(generation) + ' - - - Gbest: ' +str(Gbest)

        line1 = ax.plot(a[0], a[1], 'r+')
        line2 = ax.plot(Gbest[0][0], Gbest[0][1], 'g*')

        ax.set_xlim(-10, 10)
        ax.set_ylim(-10, 10)

        fig.canvas.draw()

        ax.clear()
        ax.grid(True)

    print 'Gbest: '
    print Gbest

def Vector_Velocidad(n, a, Pbest, Gbest, v):
    for i in range(n):
        #Velocity in X
        v[0][i] = 0.7 * v[0][i] + (Pbest[0][i] - a[0][i]) * rand.random() * 1.47 + (Gbest[0][0] - a[0][i]) * rand.random() * 1.47
        a[0][i] = a[0][i] + v[0][i]

        v[1][i] = 0.7 * v[1][i] + (Pbest[1][i] - a[1][i]) * rand.random() * 1.47 + (Gbest[0][1] - a[1][i]) * rand.random() * 1.47
        a[1][i] = a[1][i] + v[1][i]

def fitness(x, y):
        return 100 * ((y - (x**2))**2) + ((1 - (x**2))**2)

def Order(Pbest, r, n):

    for i in range(1, n):
        for j in range(0, n - 1):
            if r[j] > r[j + 1]:
                #Order the fitness
                tempRes = r[j]
                r[j] = r[j + 1]
                r[j + 1] = tempRes

                #Order las X, Y
                tempX = Pbest[0][j]
                Pbest[0][j] = Pbest[0][j + 1]
                Pbest[0][j + 1] = tempX

                tempY = Pbest[1][j]
                Pbest[1][j] = Pbest[1][j + 1]
                Pbest[1][j + 1] = tempY

if '__main__' == main():
    main()