遗传算法(模型拟合)

时间:2016-12-17 16:35:02

标签: python-2.7

我已经运行了抗微生物抗性(amr)的遗传算法模型,但结果是这样的错误:amr()正好需要3个参数(给定2个)。希望有人可以帮助我追踪错误,因为我迫切需要在两天内解决这个问题。谢谢。我的代码嵌入在下面:

# First code to run
# imports relevant modules
# then defines functions for the Levenberg-Marquardt algorithm

import numpy as np
import matplotlib.pyplot as plt
#from scipy.optimize import leastsq
%matplotlib inline

time = np.arange(0.0, 3000.1,1.0)
pop = np.array([2,27,43,36,39,32,27,22,10,14,14,4,4,7,3,3,1])

def amr(pars,t):
    beta,gamma,sigma_A,A,H,MIC_S,MIC_R,H,r = pars  
    E_S = 1-Emax*A**H/(MIC_S**H + A**H)
    E_R = 1-Emax*A**H/(MIC_R**H + A**H)
    derivs = [r*(1-(R+S)/Nmax)*E_S*S - sigma_S*S - beta*S*R/(R+S),
             r*(1-gamma)*(1-(R+S)/Nmax)*E_R*R - sigma_R*R + beta*S*R/(R+S),
             -sigma_A*A]
    return derivs


def amr_resid(pars,t,data):
    return amr(pars,t)-data

# code for the genetic algorithm. Relies on data set up above

# define a sum of squares function that we will use as fitness
def amr_ss(pars,t,data):
    return sum(amr_resid(pars,t,data)**2)

# Parameter values
npars = 3
popsize = 60 # this needs to be a multiple of 3
elitism = popsize/3
# strength of mutation: the higher the number the larger the mutations can be, and vice versa
psigma = 0.08
ngenerations = 100

#set up initial population with parameters at log normal distribution around (1,1,1)
population = 10**np.random.normal(0,psigma,size=(popsize,npars))
newpop = population
# Matrices into which we put results
best = np.zeros(shape=(ngenerations,npars))
bestfitness = np.zeros(ngenerations)
fitnesses = np.zeros(shape=(popsize,ngenerations))

# Genetic algorithm code
for j in range(ngenerations):
    # work out fitness
    for i in range(popsize):
        fitnesses[i,j] = amr_ss(population[i,:],time,pop)

    # find the best and copy them into the next generation: we use the top 1/3rd
    newpop[range(elitism),:]=population[np.argsort(fitnesses[:,j])[range(elitism)],:]
    best[j,:]=newpop[0,:]
    bestfitness[j]=np.sort(fitnesses[:,j])[0]

    #create some mutants
    for i in range(elitism):
        # mutants have multiplicative change so that the change is a fixed proportion of the parameter value
        newpop[elitism+i,:] = newpop[i,:] * 10**np.random.normal(0,psigma,npars)

    # now create some recombinants: the gene values also mutate
    for i in range(elitism):
        parents = np.random.choice(elitism,2,replace=False)
        # first gene from first parent
        newpop[2*elitism+i,0]=newpop[parents[0],0] * 10**np.random.normal(0,psigma)
        # second gene at random from first or second parent: depends on recombination position
        if (np.random.rand()<0.5):
            newpop[2*elitism+i,1]=newpop[parents[0],1] * 10**np.random.normal(0,psigma)
        else:
            newpop[2*elitism+i,1]=newpop[parents[1],1] * 10**np.random.normal(0,psigma)
        # third gene from second parent
        newpop[2*elitism+i,2]=newpop[parents[1],2] * 10**np.random.normal(0,psigma)

    #update population   
    population = newpop

plt.boxplot(fitnesses) ;

0 个答案:

没有答案