公司网站开发模板,邮箱检测网站,广西南宁市有哪些网络公司,公司网站制作公司倒闭目录实例#xff1a;求解函数的最大值yxsin(10x)xsin(2x),自变量取值#xff1a;0--5#xff0c;用Python画出的图像如下(注#xff1a;此代码好像有一些感觉不对的地方#xff0c;首先:没有保留那些适应度低的个体pop select(pop, fitness) 这一行代码#xff0c;压根就…目录实例求解函数的最大值yxsin(10x)xsin(2x),自变量取值0--5用Python画出的图像如下(注此代码好像有一些感觉不对的地方首先:没有保留那些适应度低的个体pop select(pop, fitness) 这一行代码压根就是把适应度低的个体给干没了。for parent in pop:child crossover(parent, pop_copy)child mutate(child)parent[:] child 这个for循环没有对交叉变异后的个体进行适应度筛选啊)代码讲解1初始化种群返回一个元素为 二进制的矩阵每一行代表一个个体每个个体由二进制数字表示x值2代表二进制POP_SIZE矩阵行数DNA_SIZE矩阵列数。pop np.random.randint(2, size(POP_SIZE, DNA_SIZE))2计算适应度并且原则优良种群(有放回的抽取)循环很多代pop:代表二进制种群,translateDNA(pop):将其转化十进制在计算适应度在select选择优良种群F_values F(translateDNA(pop)) #求函数值fitness get_fitness(F_values) #得到使用度这里例子函数值就是适应度pop select(pop, fitness)#此时的pop是经过筛选的好的总群也是一个二进制表现方式里面有很多一样的个体因为使用放回抓取3,经过一波筛选后接下来该交叉变异了为了得到更好的x值因为初始化的x值使得个体分布不均匀pop_copy pop.copy() #复制一份优良种群用于交叉变异for parent in pop:child crossover(parent, pop_copy) #交叉child mutate(child) #交叉后的种群在进行变异parent[:] child#将child赋值给parent难度较大的代码crossover() mutate() parent[:] childcrossover交叉parent表示每一个二进制个体pop优良的种群如100个个体返回交叉后的一个个体也是二进制表示方式这个函数的功能就是parent[cross_points] pop[i_, cross_points]def crossover(parent, pop): # mating process (genes crossover)if np.random.rand() CROSS_RATE:i_ np.random.randint(0, POP_SIZE, size1) # select another individual from pop# 从0-POP_SIZE随机选择一个数字cross_points np.random.randint(0, 2, sizeDNA_SIZE).astype(np.bool)#array([ True, False, False, True, True, False, False, True, True,#True])#随机生成一个10个元素的数组元素为0和1在转化成bool型# choose crossover pointsparent[cross_points] pop[i_, cross_points]#这个语句有难度将parent为True的元素将被改变False的元素不变.将被改变的元素改变成 pop矩阵第 i_ 行里面被选择为true的元素注意parent的是cross_points, pop 也是cross_points必须保持一致才可以实现交叉生成一个新的个体这个个体是父母基因的交叉结果# mating and produce one childreturn parent #将新个体返回出去变异将交叉后得到的个体(这个个体不一定就是好的个体很可能是不好的适应度不高的个体)进行变异核心代码child[point] 1 if child[point] 0 else 0def mutate(child):for point in range(DNA_SIZE):if np.random.rand() MUTATION_RATE:child[point] 1 if child[point] 0 else 0point是把child这个个体基因遍历一遍按照几率进行将0变成1注意:并不是将所有的0变成1而是有几率的 if np.random.rand() MUTATION_RATE:return child全部代码Visualize Genetic Algorithm to find a maximum point in a function.Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/import numpy as npimport matplotlib.pyplot as pltDNA_SIZE 10 # DNA lengthPOP_SIZE 100 # population sizeCROSS_RATE 0.8 # mating probability (DNA crossover)MUTATION_RATE 0.003 # mutation probabilityN_GENERATIONS 200X_BOUND [0, 5] # x upper and lower boundsdef F(x): return np.sin(10*x)*x np.cos(2*x)*x # to find the maximum of this function# find non-zero fitness for selectiondef get_fitness(pred): return pred 1e-3 - np.min(pred)# convert binary DNA to decimal and normalize it to a range(0, 5)def translateDNA(pop): return pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2**DNA_SIZE-1) * X_BOUND[1]def select(pop, fitness): # nature selection wrt pops fitnessidx np.random.choice(np.arange(POP_SIZE), sizePOP_SIZE, replaceTrue,pfitness/fitness.sum())return pop[idx]def crossover(parent, pop): # mating process (genes crossover)if np.random.rand() CROSS_RATE:i_ np.random.randint(0, POP_SIZE, size1) # select another individual from popcross_points np.random.randint(0, 2, sizeDNA_SIZE).astype(np.bool) # choose crossover pointsparent[cross_points] pop[i_, cross_points] # mating and produce one childreturn parentdef mutate(child):for point in range(DNA_SIZE):if np.random.rand() MUTATION_RATE:child[point] 1 if child[point] 0 else 0return childpop np.random.randint(2, size(POP_SIZE, DNA_SIZE)) # initialize the pop DNAplt.ion() # something about plottingx np.linspace(*X_BOUND, 200)plt.plot(x, F(x))for _ in range(N_GENERATIONS):F_values F(translateDNA(pop)) # compute function value by extracting DNA# something about plottingif sca in globals(): sca.remove()sca plt.scatter(translateDNA(pop), F_values, s200, lw0, cred, alpha0.5); plt.pause(0.05)# GA part (evolution)fitness get_fitness(F_values)print(Most fitted DNA: , pop[np.argmax(fitness), :])pop select(pop, fitness)pop_copy pop.copy()for parent in pop:child crossover(parent, pop_copy)child mutate(child)parent[:] child # parent is replaced by its childplt.ioff(); plt.show()