遗传算法工厂模式封装:二进制编码和实数编码
遗传算法工厂模式封装:二进制编码和实数编码
本文将介绍如何使用工厂模式封装两种遗传算法,分别为二进制基因编码和实数编码。通过工厂模式,可以根据需要创建不同的遗传算法对象,方便代码管理和扩展。
代码示例
# 工厂类
class GeneticAlgorithmFactory:
def __init__(self, algorithm_type):
self.algorithm_type = algorithm_type
def create_algorithm(self):
if self.algorithm_type == 'binary':
return BinaryGeneticAlgorithm()
elif self.algorithm_type == 'real':
return RealGeneticAlgorithm()
else:
return None
# 二进制基因编码
class BinaryGeneticAlgorithm:
def __init__(self):
self.PI = 3.1415926
self.NUMIND = 25
self.LENIND = 20
self.FUN = 1
self.crossPro = 0.9
self.mutaPro = 0.1
class Indivi:
def __init__(self):
self.gene = ['0'] * (BinaryGeneticAlgorithm.LENIND+1)
self.fitness = 0.0
self.x = 0.0
self.upLim = 0.0
class Popu:
def __init__(self):
self.indv = [BinaryGeneticAlgorithm.Indivi() for n in range(BinaryGeneticAlgorithm.NUMIND)]
self.bestInd = BinaryGeneticAlgorithm.Indivi()
self.generan = 0
def ranF(self, x, y):
return (((random.random() + 90.0) * (y - x) / 33956.0) + x)
def fit(self, x, i):
if i == 1:
return x * math.sin(10 * self.PI * x) + 2.0
else:
return x
def bin2dou(self, bp):
retuV = 0
for i in range(BinaryGeneticAlgorithm.LENIND):
tem = 0 if bp[i] == '0' else 1
retuV += tem * pow(2, BinaryGeneticAlgorithm.LENIND-i-1)
return(retuV)
def dou2ZD(self, x, lowBef, upBef, lowAft, upAft):
return lowAft + (x - lowBef) * (upAft - lowAft) / (upBef - lowBef)
def initPop(self, pop):
pop.generan = 0
for indv in pop.indv:
indv.gene = [str(random.randint(0, 1)) for _ in range(BinaryGeneticAlgorithm.LENIND)]
indv.gene.append('�')
def calFit(self, pop):
for indv in pop.indv:
indv.x = self.dou2ZD(self.bin2dou(indv.gene), 0, pow(2, BinaryGeneticAlgorithm.LENIND), -1, 2)
indv.fitness = self.fit(indv.x, BinaryGeneticAlgorithm.FUN)
def calUp(self, pop):
sum = 0.0
for indv in pop.indv:
sum += indv.fitness
pop.indv[0].upLim = pop.indv[0].fitness / sum
for i in range(BinaryGeneticAlgorithm.NUMIND-1):
temp = pop.indv[i+1].fitness / sum
pop.indv[i+1].upLim = temp + pop.indv[i].upLim
if pop.indv[i+1].upLim > 1.0 and (i+1) < (BinaryGeneticAlgorithm.NUMIND-1):
for j in range(BinaryGeneticAlgorithm.NUMIND):
print('generation is:{},fitness={},UpLim={}'.format(pop.generan,pop.indv[j].fitness,pop.indv[j].upLim))
print('
Error,{}'s upLim is greater than 1=============
'.format(i+1))
exit(0)
pop.indv[BinaryGeneticAlgorithm.NUMIND-1].upLim = 1
def HalfSear(self, value, LowBo, UpBo, InA):
if LowBo >= UpBo:
return UpBo
Mid = (LowBo + UpBo) // 2
if Mid == 0:
return 0
if value <= InA[Mid] and value > InA[Mid-1]:
return Mid
else:
if value >= InA[Mid]:
return self.HalfSear(value, Mid, UpBo, InA)
else:
return self.HalfSear(value, LowBo, Mid, InA)
def calSub(self, pop):
poptem = BinaryGeneticAlgorithm.Popu()
TemFitn = [indv.upLim for indv in pop.indv]
for i in range(BinaryGeneticAlgorithm.NUMIND):
rnd = self.ranF(0, 1)
NumTemp = self.HalfSear(rnd, 0, BinaryGeneticAlgorithm.NUMIND, TemFitn)
poptem.indv[i] = pop.indv[NumTemp]
poptem.generan = pop.generan
poptem.bestInd = pop.bestInd
pop = poptem
def swithstr(self, s, t):
i = random.randint(1, BinaryGeneticAlgorithm.LENIND-1)
for j in range(i, BinaryGeneticAlgorithm.LENIND):
temp = s[j]
s[j] = t[j]
t[j] = temp
def crossove(self,
使用方法
# 创建工厂对象
factory = GeneticAlgorithmFactory('binary')
# 创建二进制遗传算法对象
binary_algorithm = factory.create_algorithm()
# 创建种群
pop = binary_algorithm.Popu()
# 初始化种群
binary_algorithm.initPop(pop)
# 运行遗传算法
# ...
总结
使用工厂模式可以将不同类型的遗传算法代码进行封装,提高代码的可维护性和可扩展性。通过工厂类,我们可以根据需要创建不同的遗传算法对象,而无需直接创建具体的算法类,简化代码逻辑,使代码更加清晰易懂。
原文地址: https://www.cveoy.top/t/topic/nyDs 著作权归作者所有。请勿转载和采集!