|
GA遗传算法的函数寻优--Python:
链接:https://pan.baidu.com/s/1EuV2HkC_5oR7G6G-S1bXeA 提取码:cwmo
链接:https://pan.baidu.com/s/1yECy3SICh_8a1QZzmtu5ww 提取码:n4ip
- from PIL import Image
- from pylab import *
- import string
- import matplotlib.pyplot as plt
- import numpy as np
- from numpy import random
- import fun
- #print random.random()
- #
- popmax = np.array( [1,1] )
- popmin = np.array( [-1,-1] )
- pc = 0.75
- pm = 0.05
- nVar = 2
- maxg = 30
- sizepop = 30
- #Intial
- pop = [[0 for x in range(nVar)] for y in range(sizepop)]
- gbest = [[0 for x in range(nVar)] for y in range(sizepop)]
- fitness = [0 for x in range(sizepop)]
- fitnessgbest =[0 for x in range(sizepop)]
- zbest = [0 for x in range(nVar)]
- #init pop
- for i in range(sizepop):
- pop[i] = fun.code(popmin,popmax);
- fitness[i] =fun.funfitness(pop[i][0],pop[i][1]);
- gbest[i][0] = pop[i][0]
- gbest[i][1] = pop[i][1]
- fitnessgbest[i] = fitness[i]
- print(fitness[i])
- fitnesszbest =min(fitness)
- index = fun.minIndex(fitness,fitnesszbest)
- zbest[0] = pop[index][0]
- zbest[1] = pop[index][1]
- print (zbest)
- # plot (range(sizepop),fitness,'r*-')
- # plot(fitness,'r*-')
- # show
- fitness_iter = [0 for x in range(maxg)]
- # main loop
- for i in range(maxg):
- # update position
- pop = fun.select(sizepop,pop,fitness,popmin,popmax)
-
- pop = fun.cross(sizepop,pc,pop,popmin,popmax)
- pop = fun.mut(sizepop,pm,pop,popmin,popmax)
- for j in range(sizepop):
- fitness[j] = fun.funfitness(pop[j][0],pop[j][1])
- if (fitness[j] < fitnessgbest[j]):
- fitnessgbest[j] = fitness[j]
- gbest[j][0] = pop[j][0]
- gbest[j][1] = pop[j][1]
- if (fitness[j] < fitnesszbest):
- fitnesszbest = fitness[j]
- zbest[0] = pop[j][0];
- zbest[1] = pop[j][1];
- fitness_iter[i] = fitnesszbest
- print ("最优解:")
- print (zbest)
- plt.plot(fitness_iter,'ro-')
- plt.show()
复制代码
相应的fun.py函数如下:包含选择、交叉、变异:
- import numpy as np
- from numpy import random
- import math
- def funfitness(x1,x2):
- y = (x1-0.5)**2+(x2-0.6)**2;
- return y;
- def minIndex(fitness, fitnesszbest):
- for i in range(len(fitness)):
- if(fitness[i] == fitnesszbest):
- break;
- return i;
- def select(sizepop,pop,fitness,popmin,popmax):
- #fitness_d =sorted(fitness,reverse=True);
- fitness_Index = np.argsort( -np.array( fitness) )
- c = np.int32(sizepop/4);
- for i in range(c):
- pop[fitness_Index[i]][0] = popmin[0] + (popmax[0]-popmin[0])*random.random();
- pop[fitness_Index[i]][1] = popmin[1] + (popmax[1]-popmin[1])*random.random();
- return pop
- def mut(sizepop,pm,pop,popmin,popmax):
- for i in range(sizepop*2):
- if random.random() > pm:
- a = 0;
- while a == 0:
- a = np.int32( random.random()*sizepop );
- b=[0,0]
- b[0] = pop[a][0]
- b[1] = pop[a][1]
- if random.random() > 0.5:
- pop[a][0] = pop[a][0] + (popmax[0]-pop[a][0])*random.random();
- pop[a][1] = pop[a][1] + (popmax[1]-pop[a][1])*random.random();
- else:
- pop[a][0] = pop[a][0] + (popmin[0]-pop[a][0])*random.random();
- pop[a][1] = pop[a][1] + (popmin[1]-pop[a][1])*random.random();
- if pop[a][0] > popmax[0] or pop[a][0] < popmin[0]:
- pop[a][0] = b[0];
- if pop[a][1] > popmax[1] or pop[a][1] < popmin[1]:
- pop[a][1] = b[1];
- return pop
- def cross(sizepop,pc,pop,popmin,popmax):
- for j in range(sizepop*2):
- if random.random() > pc:
- a = np.array( [0,0] );
- while np.min(a.tolist()) == 0:
- a = np.int32( random.random([1,2])*sizepop );
- pop1 = [0 for x in range(2)]
- pop2 = [0 for x in range(2)]
- pop1 = pop[a[0][0]]
- pop2 = pop[a[0][1]]
- k = random.random()
- pop[a[0][0]] = (1-k) * np.array( pop1 ) + k * np.array( pop2 );
- pop[a[0][1]] = (1-k)* np.array( pop2 ) + k * np.array( pop1 );
- if pop[a[0][0]][0] > popmax[0] or pop[a[0][0]][0] < popmin[0]:
- pop[a[0][0]] = pop1;
- if pop[a[0][1]][1] > popmax[1] or pop[a[0][1]][1] < popmin[1]:
- pop[a[0][1]] = pop2;
- return pop
- def code(popmin,popmax):
- flag = 1
- pop = [0,0]
- while flag ==1:
- pop[0] = popmin[0] + (popmax[0]-popmin[0])*random.random();
- pop[1] = popmin[1] + (popmax[1]-popmin[1])*random.random();
- flag = test(pop,popmin,popmax)
- return pop
- def test(pop,popmin,popmax):
- if pop[0] < popmin[0] or pop[0] > popmax[0] or pop[1] < popmin[1] or pop[1] > popmax[1]:
- flag = 1
- else:
- flag = 0
- return flag
复制代码
|
|