遗传算法Python实现 瞎BB 代码 导入库以及参数设置 目标函数 生成C行R列的值在0-1的数组 混沌函数 二进制转十进制 个体按值从大到小排序 交叉变异 适应度函数 主函数 瞎BB 代码 导入库以及参数设置 import pandas as pd import numpy as np import matplotlib.pyplot as plt import math import random #range of variable bounds = np.array([-2,2]) #begin of variable boundsbegin = bounds[0] #end of variable boundsend = bounds[1] precision = 0.0001 #calc the BitLength BitLength = math.ceil(np.log2((boundsend - boundsbegin) / precision)) #init popsize = 100 Generationmax = 100 pmut = 0.09 目标函数 def targetfun(x): value = 200 * math.exp(-0.05 * x) * math.sin(x) return value 生成C行R列的值在0-1的数组 def random_random(C,R): rand=[] for i in range(C*R): rand.append(random.random()) rand=np.array(rand) return rand.reshape(C,R) 混沌函数 def chaos(size): chaos_cro = np.zeros(size) chaos_cro[0] = random.random() for j in range(1,size): chaos_cro[j] = 4 * chaos_cro[j - 1] * (1 - chaos_cro[j - 1]) return chaos_cro[-1] 二进制转十进制 def transform2to10(sample):