[
  {
    "path": "BPNet1.py",
    "content": "# BP ，更新阈值和权重，回归预测问题最后一层不带激活函数\r\n# coding: UTF-8\r\nimport numpy as np\r\nfrom sklearn.model_selection import train_test_split\r\nimport matplotlib.pyplot as plt\r\nfrom tkinter import _flatten\r\n# 读取txt中的数据，预处理去“，”\r\ndef load_data_wrapper(filename):\r\n    lineData = []\r\n    with open(filename) as txtData:\r\n        lines = txtData.readlines()\r\n        for line in lines:\r\n            linedata = line.strip().split(',')\r\n            lineData.append(linedata)\r\n    return lineData\r\n# 提出特征和标签，特征做输入，标签为输出\r\ndef splitData(dataset):\r\n    Character= []\r\n    Label = []\r\n    for i in range(len(dataset)):\r\n        Character.append([float(tk) for tk in dataset[i][1:-1]])\r\n        Label.append(float(dataset[i][-1]))\r\n    return Character, Label\r\n#输入特征数据归一化\r\ndef max_min_norm_x(dataset):\r\n    min_data = []\r\n    for i in range(len(dataset)):\r\n        min_data.append(min(dataset[i]))\r\n    new_min = min(min_data)\r\n    max_data = []\r\n    for i in range(len(dataset)):\r\n        max_data.append(max(dataset[i]))\r\n    new_max = max(max_data)\r\n    data = np.array(dataset)\r\n    data_x =[]\r\n    for x in np.nditer(data, op_flags=['readwrite']):\r\n        #x[...] = 2 * (x -new_min)/(new_max-new_min)-1\r\n        x[...] = (x - new_min) / (new_max - new_min)\r\n        #print('x[...]:',x[...])\r\n        data_x.append(x[...])\r\n    data_x3 = []\r\n    for index in range(0, len(data_x), 3):\r\n        data_x3.append([data_x[index], data_x[index+1], data_x[index+2]])\r\n    #print(\"data_x3:\",data_x3)\r\n    return data_x3\r\n#输入特征数据归一化\r\ndef max_min_norm_y(dataset):\r\n    new_min = min(dataset)\r\n    new_max = max(dataset)\r\n    data_y = []\r\n    for i in range(len(dataset)):\r\n        y = (dataset[i] -new_min)/(new_max-new_min)\r\n        #y = 2 * (dataset[i] - new_min) / (new_max - new_min) - 1\r\n        data_y.append(y)\r\n        #print(y)\r\n    return data_y\r\n#输出特征归一化\r\ndef de_max_min_norm_y(dataset1,dataset2):\r\n    new_min = min(dataset1)\r\n    new_max = max(dataset1)\r\n    de_data_y = []\r\n    for i in range(len(dataset2)):\r\n        y = dataset2[i] * (new_max - new_min) + new_min\r\n        de_data_y.append(y)\r\n    return de_data_y\r\n# 初始化参数\r\n# x为输入层神经元个数，y为隐层神经元个数，z输出层神经元个数\r\ndef parameter_initialization(x, y, z):\r\n    # 隐层阈值从（-5,5）之间的随机数\r\n    value1 = np.random.randint(-5, 5, (1, y)).astype(np.float64)\r\n\r\n    # 输出层阈值\r\n    value2 = np.random.randint(-5, 5, (1, z)).astype(np.float64)\r\n\r\n    # 输入层与隐层的连接权重\r\n    weight1 = np.random.randint(-5, 5, (x, y)).astype(np.float64)\r\n\r\n    # 隐层与输出层的连接权重\r\n    weight2 = np.random.randint(-5, 5, (y, z)).astype(np.float64)\r\n\r\n    return weight1, weight2, value1, value2\r\n\r\n#定义激活函数\r\ndef sigmoid(z):\r\n    return 1 / (1 + np.exp(-z))\r\ndef relu(z):\r\n    return np.where(z < 0, 0, z)\r\n\r\n'''\r\nweight1:输入层与隐层的连接权重\r\nweight2:隐层与输出层的连接权重\r\nvalue1:隐层阈值\r\nvalue2:输出层阈值\r\n'''\r\n#训练过程\r\ndef train_process(dataset, labelset, weight1, weight2, value1, value2):\r\n    # x为步长\r\n    x = 0.05\r\n    for i in range(len(dataset)):\r\n        # 输入数据\r\n        inputset = np.mat(dataset[i]).astype(np.float64)\r\n        # 数据标签\r\n        outputset = np.mat(labelset[i]).astype(np.float64)\r\n        # 隐层输入\r\n        input1 = np.dot(inputset, weight1).astype(np.float64)\r\n        # 隐层输出\r\n        output2 = sigmoid(input1 - value1).astype(np.float64)\r\n        # 输出层输入\r\n        input2 = np.dot(output2, weight2).astype(np.float64)\r\n        # 输出层输出\r\n        output3 = input2 - value2\r\n\r\n        # 更新公式由矩阵运算表示 用的是平方误差\r\n        g = outputset - output3 #最后一层直接求导 ，为输出层阈值求导\r\n        b = np.dot(g, np.transpose(weight2))\r\n        c = np.multiply(output2, 1 - output2)\r\n        e = np.multiply(b, c)  # 隐藏层之间阈值\r\n\r\n        value1_change = -x * e\r\n        value2_change = -x * g\r\n        weight1_change = x * np.dot(np.transpose(inputset), e)\r\n        weight2_change = x * np.dot(np.transpose(output2), g)\r\n\r\n        # 更新参数\r\n        value1 += value1_change\r\n        value2 += value2_change\r\n        weight1 += weight1_change\r\n        weight2 += weight2_change\r\n    return weight1, weight2, value1, value2\r\n\r\ndef test_process(dataset, labelset, weight1, weight2, value1, value2):\r\n    pre_data = []\r\n    for i in range(len(dataset)):\r\n        # 计算每一个样例通过该神经网路后的预测值\r\n        inputset = np.mat(dataset[i]).astype(np.float64)\r\n        outputset = np.mat(labelset[i]).astype(np.float64)\r\n        output2 = sigmoid(np.dot(inputset, weight1) - value1)\r\n        output3 = np.dot(output2, weight2) - value2\r\n        output3 = output3.tolist()\r\n        pre_data.append(output3)\r\n        pre_data = list(_flatten(pre_data))\r\n        # 返回预测值\r\n    return pre_data\r\n\r\nif __name__ == '__main__':\r\n    #要打开的文件名\r\n    iris_file = 'advertise.txt'\r\n    #数据预处理\r\n    Data = load_data_wrapper(iris_file)\r\n    #分离特征标签值，x为数据集的feature数据，y为label.\r\n    x, y = splitData(Data)\r\n    #数据归一化\r\n    x_norm = max_min_norm_x(x)\r\n    y_norm = max_min_norm_y(y)\r\n    #分训练和测试集\r\n    x_train, x_test, y_train, y_test = train_test_split(x_norm, y_norm, test_size=0.3)\r\n    #初始化权重\r\n    weight1, weight2, value1, value2 = parameter_initialization(len(x_train[0]), 2, 1)\r\n    #训练\r\n    for i in range(700):\r\n        weight1, weight2, value1, value2 = train_process(x_train, y_train, weight1, weight2, value1, value2)\r\n    #预测\r\n    pre = test_process(x_test, y_test, weight1, weight2, value1, value2)\r\n    #归一化还原均方误差\r\n    errors_std = np.std(np.array(pre) - np.array(y_test))\r\n    pre_org = np.array(pre) * (max(y) - min(y)) + min(y)\r\n    y_test_org = np.array(y_test) * (max(y) - min(y)) + min(y)\r\n    errors_std_org = np.std(pre_org - y_test_org)\r\n    \r\n    print(\"errors_std:\\n\", errors_std)\r\n    print(\"errors_std_org\\n\", errors_std_org)\r\n    #显示测试图像\r\n    plt.rcParams['font.sans-serif'] = ['SimHei']\r\n    plt.rcParams['axes.unicode_minus'] = False\r\n    x = np.linspace(0, 60, 60)\r\n    plt.figure(num=3, figsize=(8, 5))\r\n    plt.plot(x, y_test)\r\n    plt.plot(x, pre, color='red', linewidth=1, linestyle='--')\r\n    plt.xlim((0, 60))\r\n    plt.ylim((0, 1))\r\n    plt.xlabel('测试样本个数')\r\n    plt.ylabel('归一化后预测的数值')\r\n    plt.show()\r\n\r\n\r\n"
  },
  {
    "path": "GABPNet1.py",
    "content": "import numpy as np\r\nfrom sklearn.model_selection import train_test_split\r\nimport random\r\nfrom scipy.optimize import fsolve\r\nimport matplotlib.pyplot as plt\r\nimport heapq\r\nfrom tkinter import _flatten\r\n# 读取txt中的数据，预处理去“，”\r\ndef load_data_wrapper(filename):\r\n    lineData = []\r\n    with open(filename) as txtData:\r\n        lines = txtData.readlines()\r\n        for line in lines:\r\n            linedata = line.strip().split(',')\r\n            lineData.append(linedata)\r\n    return lineData\r\n# 提出特征和标签，特征做输入，标签为输出\r\ndef splitData(dataset):\r\n    Character= []\r\n    Label = []\r\n    for i in range(len(dataset)):\r\n        Character.append([float(tk) for tk in dataset[i][1:-1]])\r\n        Label.append(float(dataset[i][-1]))\r\n    return Character, Label\r\n#输入特征数据归一化\r\ndef max_min_norm_x(dataset):\r\n    min_data = []\r\n    for i in range(len(dataset)):\r\n        min_data.append(min(dataset[i]))\r\n    new_min = min(min_data)\r\n    max_data = []\r\n    for i in range(len(dataset)):\r\n        max_data.append(max(dataset[i]))\r\n    new_max = max(max_data)\r\n    data = np.array(dataset)\r\n    data_x =[]\r\n    for x in np.nditer(data, op_flags=['readwrite']):\r\n        #x[...] = 2 * (x -new_min)/(new_max-new_min)-1\r\n        x[...] = (x - new_min) / (new_max - new_min)\r\n        data_x.append(x[...])\r\n    data_x3 = []\r\n    for index in range(0, len(data_x), 3):\r\n        data_x3.append([data_x[index], data_x[index+1], data_x[index+2]])\r\n    return data_x3\r\n#输出特征归一化\r\ndef max_min_norm_y(dataset):\r\n    new_min = min(dataset)\r\n    new_max = max(dataset)\r\n    data_y = []\r\n    for i in range(len(dataset)):\r\n        y = (dataset[i] -new_min)/(new_max-new_min)\r\n        #y = 2 * (dataset[i] - new_min) / (new_max - new_min) - 1\r\n        data_y.append(y)\r\n    return data_y\r\n\r\n# 求染色体长度\r\ndef getEncodeLength(decisionvariables, delta):\r\n     # 将每个变量的编码长度放入数组\r\n     lengths = []\r\n     uper = decisionvariables[0][1]\r\n     low = decisionvariables[0][0]\r\n     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 6)\r\n     length0 = int(np.ceil(res[0]))\r\n     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 2)\r\n     length1 = int(np.ceil(res[0]))\r\n     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 2)\r\n     length2 = int(np.ceil(res[0]))\r\n     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 1)\r\n     length3= int(np.ceil(res[0]))\r\n     lengths.append(length0)\r\n     lengths.append(length1)\r\n     lengths.append(length2)\r\n     lengths.append(length3)\r\n     return lengths,length0,length1,length2,length3\r\n# 随机生成初始化种群\r\ndef getinitialPopulation(length,length0,length1,length2,length3, populationSize):\r\n    chromsomes = np.zeros((populationSize, length), dtype=np.int)\r\n    #print(\"len\",length)  # 每个变量的值相加 11\r\n    chromsomes0 = np.zeros((populationSize, length0), dtype=np.int)\r\n    chromsomes1 = np.zeros((populationSize, length1), dtype=np.int)\r\n    chromsomes2 = np.zeros((populationSize, length2), dtype=np.int)\r\n    chromsomes3 = np.zeros((populationSize, length3), dtype=np.int)\r\n    for popusize in range(populationSize):\r\n        # np.random.randit()产生[0,2)之间的随机整数，第三个参数表示随机数的数量\r\n        chromsomes[popusize, :] = np.random.randint(0, 2, length)\r\n        chromsomes0[popusize, :] = chromsomes[popusize, :][0:6]\r\n        chromsomes1[popusize, :] = chromsomes[popusize, :][6:8]\r\n        chromsomes2[popusize, :] = chromsomes[popusize, :][8:10]\r\n        chromsomes3[popusize, :] =chromsomes[popusize, :][10:11]\r\n    return chromsomes,chromsomes0,chromsomes1,chromsomes2,chromsomes3\r\n# 生成新种群\r\ndef getPopulation(population, populationSize):\r\n    population0 = np.zeros((populationSize, 6), dtype=np.int)\r\n    population1 = np.zeros((populationSize, 2), dtype=np.int)\r\n    population2 = np.zeros((populationSize, 2), dtype=np.int)\r\n    population3 = np.zeros((populationSize, 1), dtype=np.int)\r\n    for popusize in range(populationSize):\r\n        # np.random.randit()产生[0,2)之间的随机整数，第三个参数表示随机数的数量\r\n        population0[popusize, :] = population[popusize, :][0:6]\r\n        population1[popusize, :] = population[popusize, :][6:8]\r\n        population2[popusize, :] = population[popusize, :][8:10]\r\n        population3[popusize, :] = population[popusize, :][10:11]\r\n    return population0, population1, population2, population3\r\n\r\n # 染色体解码得到表现形的解\r\ndef getDecode(population,population0,population1,population2,population3, encodelength, decisionvariables):\r\n    population_decimal = (\r\n                (population.dot(np.power(2, np.arange(sum(encodelength))[::-1])) / np.power(2, len(encodelength)) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] +decisionvariables[0][1]))\r\n    for i in range(population0.shape[1]):\r\n        population_w1 = (\r\n                (population0.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] +decisionvariables[0][1]))\r\n    for i in range(population1.shape[1]):\r\n        population_v1 = (\r\n                (population1.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))\r\n    for i in range(population2.shape[1]):\r\n        population_w2 = (\r\n                (population2.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))\r\n    for i in range(population3.shape[1]):\r\n        population_v2 = (\r\n                (population3.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * ( decisionvariables[0][0] + decisionvariables[0][1]))\r\n    return population_decimal,population_w1,population_v1,population_w2,population_v2  #(100,2) 100个种群中的两个变量转化成10进制\r\n\r\ndef getDecode1(p0,p1,p2,p3, decisionvariables):\r\n    for i in range(len(p0)):\r\n        population_w1 = (\r\n                (p0.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] +decisionvariables[0][1]))\r\n    for i in range(len(p1)):\r\n        population_v1 = (\r\n                (p1.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))\r\n    for i in range(len(p2)):\r\n        population_w2 = (\r\n                (p2.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))\r\n    for i in range(len(p3)):\r\n        population_v2 = (\r\n                (p3.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *\r\n                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * ( decisionvariables[0][0] + decisionvariables[0][1]))\r\n    return population_w1,population_v1,population_w2,population_v2\r\n\r\n # 得到每个个体的适应度值及累计概率\r\ndef getFitnessValue(func, decode,w1,v1,w2,v2):\r\n    # 得到种群的规模和决策变量的个数\r\n    popusize = decode.shape[0]  #100\r\n    # 初始化适应度值空间\r\n    fitnessValue = np.zeros((popusize, 1)) #(100,1)\r\n    for popunum in range(popusize):\r\n        fitnessValue[popunum] = func(x_train,y_train,w1,v1,w2,v2,3,2,1,popusize)[-1]\r\n     # 得到每个个体被选择的概率\r\n    probability = fitnessValue / np.sum(fitnessValue)\r\n    # 得到每个染色体被选中的累积概率，用于轮盘赌算子使用\r\n    cum_probability = np.cumsum(probability)\r\n    return fitnessValue, cum_probability\r\n\r\n\r\n # 选择新的种群\r\ndef selectNewPopulation(decodepopu, cum_probability):\r\n    # 获取种群的规模和\r\n    m, n = decodepopu.shape  #100,33\r\n    # 初始化新种群\r\n    newPopulation = np.zeros((m, n))\r\n    for i in range(m):\r\n         # 产生一个0到1之间的随机数\r\n        randomnum = np.random.random()\r\n        # 轮盘赌选择\r\n        for j in range(m):\r\n            if (randomnum < cum_probability[j]):\r\n                newPopulation[i] = decodepopu[j]\r\n                break\r\n    return newPopulation\r\n\r\n\r\n # 新种群交叉\r\ndef crossNewPopulation(newpopu, prob):\r\n    m, n = newpopu.shape\r\n    # uint8将数值转换为无符号整型\r\n    numbers = np.uint8(m * prob)\r\n     # 如果选择的交叉数量为奇数，则数量加1\r\n    if numbers % 2 != 0:\r\n        numbers = numbers + 1     # 初始化新的交叉种群\r\n    updatepopulation = np.zeros((m, n), dtype=np.uint8)     # 随机生成需要交叉的染色体的索引号\r\n    index = random.sample(range(m), numbers)     # 不需要交叉的染色体直接复制到新的种群中\r\n    for i in range(m):\r\n        if not index.__contains__(i):\r\n            updatepopulation[i] = newpopu[i]\r\n     # 交叉操作\r\n    j = 0\r\n    while j < numbers:\r\n         # 随机生成一个交叉点，np.random.randint()返回的是一个列表\r\n        crosspoint = np.random.randint(0, n, 1)\r\n        crossPoint = crosspoint[0]\r\n        # a = index[j]\r\n        # b = index[j+1]\r\n        updatepopulation[index[j]][0:crossPoint] = newpopu[index[j]][0:crossPoint]\r\n        updatepopulation[index[j]][crossPoint:] = newpopu[index[j + 1]][crossPoint:]\r\n        updatepopulation[index[j + 1]][0:crossPoint] = newpopu[j + 1][0:crossPoint]\r\n        updatepopulation[index[j + 1]][crossPoint:] = newpopu[index[j]][crossPoint:]\r\n        j = j + 2\r\n    return updatepopulation\r\n\r\n\r\n # 变异操作\r\ndef mutation(crosspopulation, mutaprob):\r\n     # 初始化变异种群\r\n     mutationpopu = np.copy(crosspopulation)\r\n     m, n = crosspopulation.shape\r\n     # 计算需要变异的基因数量\r\n     mutationnums = np.uint8(m * n * mutaprob)\r\n     # 随机生成变异基因的位置\r\n     mutationindex = random.sample(range(m * n), mutationnums)\r\n     # 变异操作\r\n     for geneindex in mutationindex:\r\n         # np.floor()向下取整返回的是float型\r\n         row = np.uint8(np.floor(geneindex / n))\r\n         colume = geneindex % n\r\n         if mutationpopu[row][colume] == 0:\r\n             mutationpopu[row][colume] = 1\r\n         else:\r\n             mutationpopu[row][colume] = 0\r\n     return mutationpopu\r\n\r\n # 找到重新生成的种群中适应度值最大的染色体生成新种群\r\ndef findMinPopulation(population, minevaluation, minSize):\r\n     #将数组转换为列表\r\n     minevalue = minevaluation.flatten()\r\n     #maxevaluelist = maxevalue.tolist()\r\n     minevaluelist = minevalue.tolist()\r\n     # 找到前10个适应度最小的染色体的索引\r\n     #maxIndex = map(maxevaluelist.index, heapq.nlargest(10, maxevaluelist))\r\n     minIndex = map(minevaluelist.index, heapq.nsmallest(10, minevaluelist))\r\n\r\n     index = list(minIndex)\r\n     print(\"index\",index)\r\n     colume = population.shape[1] #11\r\n     #print(\"colume\",colume)\r\n     # 根据索引生成新的种群\r\n     minPopulation = np.zeros((minSize, colume))\r\n     i = 0\r\n     for ind in index:\r\n         minPopulation[i] = population[ind]\r\n         i = i + 1\r\n     return np.uint8(minPopulation)\r\n\r\n # 适应度函数，神经网络训练误差最小为\r\ndef fitnessFunction(dataset, labelset, temp1,temp2,temp3,temp4,inputnum , hiddennum, outputnum,num):\r\n    # x为步长\r\n    x = 0.05\r\n    if num !=0:#（输入为三维矩阵，第一维是种群数量，后两维是种群维度）\r\n        Value1 = temp2.reshape(num, 1, hiddennum)\r\n        Value2 = temp4.reshape(num, outputnum, outputnum)\r\n        Weight1 = temp1.reshape(num, inputnum, hiddennum)\r\n        Weight2 = temp3.reshape(num, hiddennum, outputnum)\r\n        errors_net = []\r\n        for v1, v2, w1, w2 in zip(Value1, Value2, Weight1, Weight2):\r\n            errors = []\r\n            for i in range(len(dataset)):\r\n                # 输入数据\r\n                inputset = np.mat(dataset[i]).astype(np.float64)\r\n                # 数据标签\r\n                outputset = np.mat(labelset[i]).astype(np.float64)\r\n                # 隐层输入\r\n                input1 = np.dot(inputset, w1).astype(np.float64)\r\n                # 隐层输出\r\n                output2 = sigmoid(input1 - v1).astype(np.float64)\r\n                # 输出层输入\r\n                input2 = np.dot(output2, w2).astype(np.float64)\r\n                # 输出层输出，回归预测不带激活函数\r\n                output3 = input2 - v2\r\n                #误差绝对值\r\n                error = abs(output3 - y_train[i])\r\n                errors.append(error)\r\n\r\n                # 更新公式由矩阵运算表示 用的是平方误差               \r\n                g = outputset - output3  # 最后一层直接求导 ，为输出层阈值求导\r\n                b = np.dot(g, np.transpose(w2))\r\n                c = np.multiply(output2, 1 - output2)\r\n                e = np.multiply(b, c)  # 隐藏层之间阈值\r\n\r\n                value1_change = -x * e\r\n                value2_change = -x * g\r\n                weight1_change = x * np.dot(np.transpose(inputset), e)\r\n                weight2_change = x * np.dot(np.transpose(output2), g) \r\n                v1 += value1_change\r\n                v2 += value2_change\r\n                w1 += weight1_change\r\n                w2 += weight2_change\r\n            error_net = np.array(min(errors)).flatten()\r\n            errors_net.append(error_net)  \r\n        return w1, w2, v1, v2, output3, error_net\r\n    else: #输入是二维矩阵只有权重的维度\r\n        Value1 = temp2.reshape(1, hiddennum)\r\n        Value2 = temp4.reshape(outputnum, outputnum)\r\n        Weight1 = temp1.reshape(inputnum, hiddennum)\r\n        Weight2 = temp3.reshape(hiddennum, outputnum)\r\n        for i in range(len(dataset)):\r\n            # 输入数据\r\n            inputset = np.mat(dataset[i]).astype(np.float64)\r\n            # 数据标签\r\n            outputset = np.mat(labelset[i]).astype(np.float64)\r\n            # 隐层输入\r\n            input1 = np.dot(inputset, Weight1).astype(np.float64)\r\n            # 隐层输出\r\n            output2 = sigmoid(input1 - Value1).astype(np.float64)\r\n            # 输出层输入\r\n            input2 = np.dot(output2, Weight2).astype(np.float64)\r\n            # 输出层输出\r\n            output3 = input2 - Value2\r\n\r\n            # 更新公式由矩阵运算表示 用的是平方误差\r\n            g = outputset - output3  # 最后一层直接求导 ，为输出层阈值求导\r\n            b = np.dot(g, np.transpose(Weight2))\r\n            c = np.multiply(output2, 1 - output2)\r\n            e = np.multiply(b, c)  # 隐藏层之间阈值\r\n\r\n            value1_change = -x * e\r\n            value2_change = -x * g\r\n            weight1_change = x * np.dot(np.transpose(inputset), e)\r\n            weight2_change = x * np.dot(np.transpose(output2), g)\r\n\r\n            # 更新参数\r\n            Value1 += value1_change\r\n            Value2 += value2_change\r\n            Weight1 += weight1_change\r\n            Weight2 += weight2_change\r\n    return Weight1,Weight2,Value1,Value2\r\n\r\ndef parameter_initialization(opt):\r\n    # 输入层与隐层的连接权重\r\n    weight1 =opt[0:6]\r\n    # 隐层与输出层的连接权重\r\n    weight2 =opt[6:8]\r\n    # 隐层阈值\r\n    value1 = opt[8:10]\r\n    # 输出层阈值\r\n    value2 = opt[10:11]\r\n    return weight1, weight2, value1, value2\r\n\r\ndef sigmoid(z):\r\n    return 1 / (1 + np.exp(-z))\r\n\r\ndef test_process(dataset, labelset, weight1, weight2, value1, value2):\r\n    pre_data = []\r\n    for i in range(len(dataset)):\r\n        # 计算每一个样例通过该神经网路后的预测值\r\n        inputset = np.mat(dataset[i]).astype(np.float64)\r\n        outputset = np.mat(labelset[i]).astype(np.float64)\r\n        output2 = sigmoid(np.dot(inputset, weight1) - value1)\r\n        output3 = np.dot(output2, weight2) - value2\r\n        output3 = output3.tolist()\r\n        pre_data.append(output3)\r\n        pre_data = list(_flatten(pre_data))\r\n    return pre_data\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    #要打开的文件名\r\n    iris_file = 'advertise.txt'\r\n    #数据预处理\r\n    Data = load_data_wrapper(iris_file)\r\n    #分离特征标签值，x为数据集的feature数据，y为label.\r\n    x, y = splitData(Data)\r\n    #数据归一化\r\n    x_norm = max_min_norm_x(x)\r\n    #数据归一化\r\n    y_norm = max_min_norm_y(y)    \r\n    #分训练和测试集\r\n    x_train, x_test, y_train, y_test = train_test_split(x_norm, y_norm, test_size=0.3)\r\n    optimalvalue = []\r\n    optimalvariables = []\r\n\r\n    # 两个决策变量的上下界，多维数组之间必须加逗号\r\n    decisionVariables = [[-5.0, 5.0]] * 4  # 神经网络参数： W1, W2, V1, V2\r\n    # 精度\r\n    delta = 0.0001\r\n    # 获取染色体长度\r\n    EncodeLength, L0, L1, L2, L3 = getEncodeLength(decisionVariables, delta)\r\n    # 种群数量\r\n    initialPopuSize = 10\r\n    # 初始生成100个种群\r\n    population, population0, population1, population2, population3 = getinitialPopulation(sum(EncodeLength), L0, L1, L2,L3, initialPopuSize)\r\n    # 最大进化代数\r\n    maxgeneration = 80\r\n    # 交叉概率\r\n    prob = 0.8\r\n    # 变异概率\r\n    mutationprob = 0.01\r\n    # 新生成的种群数量\r\n    maxPopuSize = 10\r\n\r\n    for generation in range(maxgeneration):\r\n        # 对种群解码得到表现形\r\n        decode, W1, V1, W2, V2 = getDecode(population, population0, population1, population2, population3, EncodeLength, decisionVariables)\r\n        # 得到适应度值和累计概率值\r\n        evaluation, cum_proba = getFitnessValue(fitnessFunction, decode, W1, V1, W2, V2)\r\n        # 选择新的种群\r\n        newpopulations = selectNewPopulation(population, cum_proba)\r\n        # 新种群交叉\r\n        crossPopulations = crossNewPopulation(newpopulations, prob)\r\n        # 变异操作\r\n        mutationpopulation = mutation(crossPopulations, mutationprob)\r\n        # 将父母和子女合并为新的种群\r\n        totalpopulation = np.vstack((population, mutationpopulation))\r\n        w11, v11, w22, v22 = getPopulation(totalpopulation, totalpopulation.shape[0])\r\n\r\n        # 最终解码\r\n        final_decode, W11, V11, W22, V22 = getDecode(totalpopulation, w11, v11, w22, v22, EncodeLength,decisionVariables)\r\n        # 适应度评估\r\n        final_evaluation, final_cumprob = getFitnessValue(fitnessFunction, final_decode, W11, V11, W22, V22)\r\n\r\n        # 选出适应度最大的100个重新生成种群\r\n        population = findMinPopulation(totalpopulation, final_evaluation, maxPopuSize)\r\n        # 找到本轮中适应度最小的值\r\n        optimalvalue.append(np.min(final_evaluation))\r\n        index = np.where(final_evaluation == min(final_evaluation))\r\n        optimalvariables.append((totalpopulation[index[0][0]]).tolist())\r\n    #找出适应度的最小值\r\n    optimalval = np.min(optimalvalue)\r\n    #找出适应最小值所对应的索引\r\n    index = np.where(optimalvalue == min(optimalvalue))\r\n    optimalvar = optimalvariables[index[0][0]]\r\n    optimalvar = np.array(optimalvar)\r\n    #把这个个体还原成神经网络的权重、阈值\r\n    weight11, weight21, value11, value21 = parameter_initialization(optimalvar)\r\n    weight1, weight2, value1, value2 = getDecode1(weight11, weight21, value11, value21, decisionVariables)\r\n    #训练\r\n    for i in range(700):\r\n        Weight1, Weight2, Value1, Value2 = fitnessFunction(x_train, y_train, weight1, weight2, value1, value2, 3, 2, 1, 0)\r\n    #预测\r\n    pre = test_process(x_test, y_test, Weight1, Weight2, Value1, Value2)\r\n   \r\n    #均方误差\r\n    errors_std = np.std(np.array(pre) - np.array(y_test))\r\n    #归一化还原均方误差\r\n    pre_org = np.array(pre) * (max(y)-min(y)) + min(y)\r\n    y_test_org = np.array(y_test) * (max(y) - min(y)) + min(y)\r\n    errors_std_org = np.std(pre_org - y_test_org)\r\n    print(\"errors_std:\\n\", errors_std)\r\n    print(\"errors_std_org\\n\", errors_std_org)\r\n    #显示测试图像\r\n    plt.rcParams['font.sans-serif'] = ['SimHei']\r\n    plt.rcParams['axes.unicode_minus'] = False\r\n    x = np.linspace(0, 60, 60)\r\n    plt.figure(num=3, figsize=(8, 5))\r\n    plt.plot(x, y_test)\r\n    plt.plot(x, pre, color='red', linewidth=1, linestyle='--')\r\n    plt.xlim((0, 60))\r\n    plt.ylim((0, 1))\r\n    plt.xlabel('测试样本个数')\r\n    plt.ylabel('归一化后预测的数值')\r\n    plt.show()\r\n"
  },
  {
    "path": "README.md",
    "content": "# GA-optimized-neural-network\npython 用GA算法优化BP神经网络，预测回归问题\n\n神经网络部分：\n网络结构三层：（3,2,1）\n\n数据集：\n实验的数据集为：advertise.txt （三个特征输入，一个输出）\n其数据形式如下所示：（即求前三个数与最后一个数的关系）\n一共有200条数据，训练集和测试集的比例为7:3\n\n1,230.1,37.8,69.2,22.1\n\n2,44.5,39.3,45.1,10.4\n\n3,17.2,45.9,69.3,9.3\n\n4,151.5,41.3,58.5,18.5\n\n5,180.8,10.8,58.4,12.9\n\n用GA算法优化BP神经网络的权值和阈值：\n种群数量10，迭代80次，交叉概率0.8，变异概率0.01，BP神经网络学习率：0.05，迭代500次：\n测试样本60个的平均无误差，errors_std_org： 1.5342603366697878\n![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/500%E6%AC%A1.png)\n\n迭代700次\n测试样本60个的平均无误差：errors_std_org：1.0408958068854353\n![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/700%E6%AC%A1.png)\n\n单独用BP神经网络，学习率：0.05，迭代500次：\n测试样本60个的平均无误差，errors_std_org：3.2695353501231272\n![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/BP500.png)\n\n单独用BP神经网络，学习率：0.05，迭代700次：\n测试样本60个的平均无误差，errors_std_org：1.812\n![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/BP700%E6%AC%A14.png)\n\n"
  },
  {
    "path": "advertise.txt",
    "content": "1,230.1,37.8,69.2,22.1\r\n2,44.5,39.3,45.1,10.4\r\n3,17.2,45.9,69.3,9.3\r\n4,151.5,41.3,58.5,18.5\r\n5,180.8,10.8,58.4,12.9\r\n6,8.7,48.9,75,7.2\r\n7,57.5,32.8,23.5,11.8\r\n8,120.2,19.6,11.6,13.2\r\n9,8.6,2.1,1,4.8\r\n10,199.8,2.6,21.2,10.6\r\n11,66.1,5.8,24.2,8.6\r\n12,214.7,24,4,17.4\r\n13,23.8,35.1,65.9,9.2\r\n14,97.5,7.6,7.2,9.7\r\n15,204.1,32.9,46,19\r\n16,195.4,47.7,52.9,22.4\r\n17,67.8,36.6,114,12.5\r\n18,281.4,39.6,55.8,24.4\r\n19,69.2,20.5,18.3,11.3\r\n20,147.3,23.9,19.1,14.6\r\n21,218.4,27.7,53.4,18\r\n22,237.4,5.1,23.5,12.5\r\n23,13.2,15.9,49.6,5.6\r\n24,228.3,16.9,26.2,15.5\r\n25,62.3,12.6,18.3,9.7\r\n26,262.9,3.5,19.5,12\r\n27,142.9,29.3,12.6,15\r\n28,240.1,16.7,22.9,15.9\r\n29,248.8,27.1,22.9,18.9\r\n30,70.6,16,40.8,10.5\r\n31,292.9,28.3,43.2,21.4\r\n32,112.9,17.4,38.6,11.9\r\n33,97.2,1.5,30,9.6\r\n34,265.6,20,0.3,17.4\r\n35,95.7,1.4,7.4,9.5\r\n36,290.7,4.1,8.5,12.8\r\n37,266.9,43.8,5,25.4\r\n38,74.7,49.4,45.7,14.7\r\n39,43.1,26.7,35.1,10.1\r\n40,228,37.7,32,21.5\r\n41,202.5,22.3,31.6,16.6\r\n42,177,33.4,38.7,17.1\r\n43,293.6,27.7,1.8,20.7\r\n44,206.9,8.4,26.4,12.9\r\n45,25.1,25.7,43.3,8.5\r\n46,175.1,22.5,31.5,14.9\r\n47,89.7,9.9,35.7,10.6\r\n48,239.9,41.5,18.5,23.2\r\n49,227.2,15.8,49.9,14.8\r\n50,66.9,11.7,36.8,9.7\r\n51,199.8,3.1,34.6,11.4\r\n52,100.4,9.6,3.6,10.7\r\n53,216.4,41.7,39.6,22.6\r\n54,182.6,46.2,58.7,21.2\r\n55,262.7,28.8,15.9,20.2\r\n56,198.9,49.4,60,23.7\r\n57,7.3,28.1,41.4,5.5\r\n58,136.2,19.2,16.6,13.2\r\n59,210.8,49.6,37.7,23.8\r\n60,210.7,29.5,9.3,18.4\r\n61,53.5,2,21.4,8.1\r\n62,261.3,42.7,54.7,24.2\r\n63,239.3,15.5,27.3,15.7\r\n64,102.7,29.6,8.4,14\r\n65,131.1,42.8,28.9,18\r\n66,69,9.3,0.9,9.3\r\n67,31.5,24.6,2.2,9.5\r\n68,139.3,14.5,10.2,13.4\r\n69,237.4,27.5,11,18.9\r\n70,216.8,43.9,27.2,22.3\r\n71,199.1,30.6,38.7,18.3\r\n72,109.8,14.3,31.7,12.4\r\n73,26.8,33,19.3,8.8\r\n74,129.4,5.7,31.3,11\r\n75,213.4,24.6,13.1,17\r\n76,16.9,43.7,89.4,8.7\r\n77,27.5,1.6,20.7,6.9\r\n78,120.5,28.5,14.2,14.2\r\n79,5.4,29.9,9.4,5.3\r\n80,116,7.7,23.1,11\r\n81,76.4,26.7,22.3,11.8\r\n82,239.8,4.1,36.9,12.3\r\n83,75.3,20.3,32.5,11.3\r\n84,68.4,44.5,35.6,13.6\r\n85,213.5,43,33.8,21.7\r\n86,193.2,18.4,65.7,15.2\r\n87,76.3,27.5,16,12\r\n88,110.7,40.6,63.2,16\r\n89,88.3,25.5,73.4,12.9\r\n90,109.8,47.8,51.4,16.7\r\n91,134.3,4.9,9.3,11.2\r\n92,28.6,1.5,33,7.3\r\n93,217.7,33.5,59,19.4\r\n94,250.9,36.5,72.3,22.2\r\n95,107.4,14,10.9,11.5\r\n96,163.3,31.6,52.9,16.9\r\n97,197.6,3.5,5.9,11.7\r\n98,184.9,21,22,15.5\r\n99,289.7,42.3,51.2,25.4\r\n100,135.2,41.7,45.9,17.2\r\n101,222.4,4.3,49.8,11.7\r\n102,296.4,36.3,100.9,23.8\r\n103,280.2,10.1,21.4,14.8\r\n104,187.9,17.2,17.9,14.7\r\n105,238.2,34.3,5.3,20.7\r\n106,137.9,46.4,59,19.2\r\n107,25,11,29.7,7.2\r\n108,90.4,0.3,23.2,8.7\r\n109,13.1,0.4,25.6,5.3\r\n110,255.4,26.9,5.5,19.8\r\n111,225.8,8.2,56.5,13.4\r\n112,241.7,38,23.2,21.8\r\n113,175.7,15.4,2.4,14.1\r\n114,209.6,20.6,10.7,15.9\r\n115,78.2,46.8,34.5,14.6\r\n116,75.1,35,52.7,12.6\r\n117,139.2,14.3,25.6,12.2\r\n118,76.4,0.8,14.8,9.4\r\n119,125.7,36.9,79.2,15.9\r\n120,19.4,16,22.3,6.6\r\n121,141.3,26.8,46.2,15.5\r\n122,18.8,21.7,50.4,7\r\n123,224,2.4,15.6,11.6\r\n124,123.1,34.6,12.4,15.2\r\n125,229.5,32.3,74.2,19.7\r\n126,87.2,11.8,25.9,10.6\r\n127,7.8,38.9,50.6,6.6\r\n128,80.2,0,9.2,8.8\r\n129,220.3,49,3.2,24.7\r\n130,59.6,12,43.1,9.7\r\n131,0.7,39.6,8.7,1.6\r\n132,265.2,2.9,43,12.7\r\n133,8.4,27.2,2.1,5.7\r\n134,219.8,33.5,45.1,19.6\r\n135,36.9,38.6,65.6,10.8\r\n136,48.3,47,8.5,11.6\r\n137,25.6,39,9.3,9.5\r\n138,273.7,28.9,59.7,20.8\r\n139,43,25.9,20.5,9.6\r\n140,184.9,43.9,1.7,20.7\r\n141,73.4,17,12.9,10.9\r\n142,193.7,35.4,75.6,19.2\r\n143,220.5,33.2,37.9,20.1\r\n144,104.6,5.7,34.4,10.4\r\n145,96.2,14.8,38.9,11.4\r\n146,140.3,1.9,9,10.3\r\n147,240.1,7.3,8.7,13.2\r\n148,243.2,49,44.3,25.4\r\n149,38,40.3,11.9,10.9\r\n150,44.7,25.8,20.6,10.1\r\n151,280.7,13.9,37,16.1\r\n152,121,8.4,48.7,11.6\r\n153,197.6,23.3,14.2,16.6\r\n154,171.3,39.7,37.7,19\r\n155,187.8,21.1,9.5,15.6\r\n156,4.1,11.6,5.7,3.2\r\n157,93.9,43.5,50.5,15.3\r\n158,149.8,1.3,24.3,10.1\r\n159,11.7,36.9,45.2,7.3\r\n160,131.7,18.4,34.6,12.9\r\n161,172.5,18.1,30.7,14.4\r\n162,85.7,35.8,49.3,13.3\r\n163,188.4,18.1,25.6,14.9\r\n164,163.5,36.8,7.4,18\r\n165,117.2,14.7,5.4,11.9\r\n166,234.5,3.4,84.8,11.9\r\n167,17.9,37.6,21.6,8\r\n168,206.8,5.2,19.4,12.2\r\n169,215.4,23.6,57.6,17.1\r\n170,284.3,10.6,6.4,15\r\n171,50,11.6,18.4,8.4\r\n172,164.5,20.9,47.4,14.5\r\n173,19.6,20.1,17,7.6\r\n174,168.4,7.1,12.8,11.7\r\n175,222.4,3.4,13.1,11.5\r\n176,276.9,48.9,41.8,27\r\n177,248.4,30.2,20.3,20.2\r\n178,170.2,7.8,35.2,11.7\r\n179,276.7,2.3,23.7,11.8\r\n180,165.6,10,17.6,12.6\r\n181,156.6,2.6,8.3,10.5\r\n182,218.5,5.4,27.4,12.2\r\n183,56.2,5.7,29.7,8.7\r\n184,287.6,43,71.8,26.2\r\n185,253.8,21.3,30,17.6\r\n186,205,45.1,19.6,22.6\r\n187,139.5,2.1,26.6,10.3\r\n188,191.1,28.7,18.2,17.3\r\n189,286,13.9,3.7,15.9\r\n190,18.7,12.1,23.4,6.7\r\n191,39.5,41.1,5.8,10.8\r\n192,75.5,10.8,6,9.9\r\n193,17.2,4.1,31.6,5.9\r\n194,166.8,42,3.6,19.6\r\n195,149.7,35.6,6,17.3\r\n196,38.2,3.7,13.8,7.6\r\n197,94.2,4.9,8.1,9.7\r\n198,177,9.3,6.4,12.8\r\n199,283.6,42,66.2,25.5\r\n200,232.1,8.6,8.7,13.4"
  }
]