问题描述
因此,我为TSP使用了GA,其中初始种群是使用具有不同起点的最近邻方法生成的。 当我的变异率> 0时,总体上的初始最佳仍是最佳 当突变率是0时,会发生改善,但仅在短时间内发生,如下图所示:
我试图迭代地提高突变率,但这没有好处
这是我的突变代码:
def mutate(route,rate):
for i in range(len(route)):
#if (random.uniform(0,1) < rate):
if (random.uniform(0,1) < rate):
j=int(random.random()*len(route))
cityA=route[i]
cityB=route[j]
route[i]=cityB
route[j]=cityA
return route
这是我的育儿代码:
def make_babies(parent1,parent2):
child = []
child_1 = []
half1_1=[]
half1_2=[]
child_2 = []
half2_1=[]
half2_2=[]
splicepoint1 = int(random.random() * len(parent1))
splicepoint2 = int(random.random() * len(parent1))
startsplice = min(splicepoint1,splicepoint2)
endsplice = max(splicepoint1,splicepoint2)
for i in range(startsplice,endsplice):
half1_1.append(parent1[i])
#print(parent2)
half2_1.append(parent2[i])
half1_2 = [j for j in parent2 if j not in half1_1]
half2_2 = [j for j in parent1 if j not in half2_1]
child_1 = half1_1+half1_2
child_2 = half2_1+half2_2
return [child_1,child_2]
即使在此之后,也有交叉的边缘 请帮助我
EDIT1:-我确保最好的路线都是精英化的 这是我的健身函数,以防出现错误:
sum=0
fitness_sum=0
fitness=[]
for i in range(len(sol)):
fitness_sum=fitness_sum+(1/sol[i][1])**2
for j in range(len(sol)):
a=((1/(sol[j][1])**2)/(fitness_sum))
fitness.append(a)
return fitness
解决方法
您的最佳表现必定会根据候选总体的大小而有所提高,但不一定取决于解释“短脉冲”的每次迭代(因为最佳表现可能会转化为较差的解决方案,而替代方法则显示出有限的改进)
此外,您的突变功能是一种交叉功能,因为突变通常会从路由表型中潜在的未经探索的候选资格中引入新的候选资格。
因此,您有2个交叉函数,并且根据对下一个迭代的候选对象的选择,将由初始种群中定义的局部最优约束。
最后,纵横交错的边是无关紧要的,因为图形不受约束到欧几里得空间,并且边是单向或双向的。