Commit 59b532a4 authored by wang hongli's avatar wang hongli

Update __init__.py, best.py, calfitValue.py, calobjValue.py, crossover.py,...

Update __init__.py, best.py, calfitValue.py, calobjValue.py, crossover.py, ga.py, geneEncoding.py, selection.py, mutation.py files
parents
# 0.0 coding:utf-8 0.0
# 找出最优解和最优解的基因编码
def best(pop, fit_value):
px = len(pop)
best_individual = []
best_fit = fit_value[0]
for i in range(1, px):
if(fit_value[i] > best_fit):
best_fit = fit_value[i]
best_individual = pop[i]
return best_individual, best_fit
if __name__ == '__main__':
pass
# 0.0 coding:utf-8 0.0
# 淘汰(去除负值)
def calfitValue(obj_value):
fit_value = []
c_min = 0
for i in range(len(obj_value)):
if(obj_value[i] + c_min > 0):
temp = c_min + obj_value[i]
else:
temp = 0.0
fit_value.append(temp)
return fit_value
if __name__ == '__main__':
pass
# 0.0 coding:utf-8 0.0
# 解码并计算值
import math
def decodechrom(pop, chrom_length):
temp = []
for i in range(len(pop)):
t = 0
for j in range(chrom_length):
t += pop[i][j] * (math.pow(2, j))
temp.append(t)
return temp
def calobjValue(pop, chrom_length, max_value, ):
picture_sizeList = [] # 图片大小列表
picture_sizeList.append(1.41)
picture_sizeList.append(1.85)
picture_sizeList.append(2.69)
picture_sizeList.append(2.16)
picture_sizeList.append(1.77)
picture_sizeList.append(1.19)
picture_sizeList.append(1.91)
picture_sizeList.append(1.19)
picture_sizeList.append(1.91)
picture_sizeList.append(2.16)
picture_sizeList.append(1.77)
#1.85 2.69 2.16 1.77 1.19 1.91
# 0,1权重
w0 = 0.05
w1 = 0.01
#temp1 = []
obj_value = []
sum0 = sum1 = 0
# temp1 = decodechrom(pop, chrom_length)
# for i in range(len(temp1)):
for i in range(len(pop)):
sum0 = sum1 = 0
# x = temp1[i] * max_value / (math.pow(2, chrom_length) - 1)
# obj_value.append(10 * math.sin(5 * x) + 7 * math.cos(4 * x))
for j in range(chrom_length):
if pop[i][j] == 0:
sum0 = sum0 + w0 * picture_sizeList[j] + 0.04
else:
sum1 = sum1 + w1 * picture_sizeList[j] + 0.08
obj_value.append(1/max(sum0,sum1))
return obj_value
if __name__ == '__main__':
pass
# 0.0 coding:utf-8 0.0
# 交配
import random
def crossover(pop, pc):
pop_len = len(pop)
for i in range(pop_len - 1):
if(random.random() < pc):
cpoint = random.randint(0,len(pop[0]))
temp1 = []
temp2 = []
temp1.extend(pop[i][0:cpoint])
temp1.extend(pop[i+1][cpoint:len(pop[i])])
temp2.extend(pop[i+1][0:cpoint])
temp2.extend(pop[i][cpoint:len(pop[i])])
pop[i] = temp1
pop[i+1] = temp2
if __name__ == '__main__':
pass
# 0.0 coding:utf-8 0.0
import matplotlib.pyplot as plt
import math
from calobjValue import calobjValue
#from calfitValue import calfitValue
from selection import selection
from crossover import crossover
from mutation import mutation
from best import best
from geneEncoding import geneEncoding
from time import *
# 计算2进制序列代表的数值
# def b2d(b, max_value, chrom_length):
# t = 0
# for j in range(len(b)):
# t += b[j] * (math.pow(2, j))
# t = t * max_value / (math.pow(2, chrom_length) - 1)
# return t
inum = 20 # 迭代次数
pop_size = 15 # 种群数量
max_value = 20 # 基因中允许出现的最大值
chrom_length = 7 # 染色体长度
pc = 0.6 # 交配概率
pm = 0.0001 # 变异概率
results = [] # 存储每一代的最优解,N个二元组
fit_value = [] # 个体适应度
fit_mean = [] # 平均适应度
pop = geneEncoding(pop_size, chrom_length)
start=time()
for i in range(inum):
obj_value = calobjValue(pop, chrom_length, max_value) # 个体评价
# fit_value = calfitValue(obj_value) # 淘汰
best_individual, best_fit = best(pop, obj_value) # 第一个存储最优的解, 第二个存储最优基因
# results.append([best_fit, b2d(best_individual, max_value, chrom_length)])
results.append([best_fit, best_individual])
selection(pop, obj_value) # 新种群复制
crossover(pop, pc) # 交配
mutation(pop, pm) # 变异
end=time()
print(end-start)
#print(len(results))
#results = results[1:]
#print(len(results))
#print(inum)
# results.sort(reverse=True)
#print(results[-1])
print(best_individual)
print(results)
#print(best_fit)
#print(obj_value[1])
# print (results)
# print ("y = %f, x = %f" % (results[-1][0], results[-1][1]))
X = []
Y = []
for i in range(inum):
X.append(i)
t = 1/results[i][0]
Y.append(t)
plt.plot(X, Y)
plt.show()
# 0.0 coding:utf-8 0.0
import random
def geneEncoding(pop_size, chrom_length):
pop = [[]]
for i in range(pop_size):
temp = []
for j in range(chrom_length):
temp.append(random.randint(0, 1))
pop.append(temp)
return pop[1:]
if __name__ == '__main__':
pop_size = 10 # 种群数量
chrom_length = 10 # 染色体长度
pop = geneEncoding(pop_size, chrom_length)
print (pop)
print (len(pop))
# 0.0 coding:utf-8 0.0
# 基因突变
import random
def mutation(pop, pm):
px = len(pop)
py = len(pop[0])
for i in range(px):
if(random.random() < pm):
mpoint = random.randint(0, py-1)
if(pop[i][mpoint] == 1):
pop[i][mpoint] = 0
else:
pop[i][mpoint] = 1
if __name__ == '__main__':
pass
# 0.0 coding:utf-8 0.0
# 选择
import random
def sum(fit_value):
total = 0
for i in range(len(fit_value)):
total += fit_value[i]
return total
def cumsum(fit_value):
for i in range(len(fit_value)-2, -1, -1):
t = 0
j = 0
while(j <= i):
t += fit_value[j]
j += 1
fit_value[i] = t
fit_value[len(fit_value)-1] = 1
return fit_value
def selection(pop, fit_value):
newfit_value = []
# 适应度总和
total_fit = sum(fit_value)
for i in range(len(fit_value)):
newfit_value.append(fit_value[i] / total_fit)
# 计算累计概率
newfit_value=cumsum(newfit_value)
ms = []
pop_len = len(pop)
for i in range(pop_len):
ms.append(random.random())
ms.sort()
fitin = 0
newin = 0
newpop = pop
# 转轮盘选择法
while newin < pop_len:
if(ms[newin] < newfit_value[fitin]):
newpop[newin] = pop[fitin]
newin = newin + 1
else:
fitin = fitin + 1
pop = newpop
if __name__ == '__main__':
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment