Python 遗传算法为什么有效?他们什么时候停止工作?

Python 遗传算法为什么有效?他们什么时候停止工作?,python,algorithm,time-complexity,genetic-algorithm,Python,Algorithm,Time Complexity,Genetic Algorithm,我一直在学习一些化学发展的算法,我遇到了遗传算法。所以我写了一个简单的遗传算法,试图从一组给定的符号(基因)中找出目标字符串 所以, genes=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvwxyz 1234567890,.-;:\”#%&/()=?@${[]} 而target=生存还是毁灭,这就是问题所在 这是我的头文件: #a chromosome in a genetic algorithm is a possible solution

我一直在学习一些化学发展的算法,我遇到了遗传算法。所以我写了一个简单的遗传算法,试图从一组给定的符号(基因)中找出目标字符串

所以,
genes=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvwxyz 1234567890,.-;:\”#%&/()=?@${[]}
target=生存还是毁灭,这就是问题所在

这是我的头文件:

#a chromosome in a genetic algorithm is a possible solution to the problem

import random 

genes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP \
QRSTUVWXYZ 1234567890, .-;:_!\"#%&/()=?@${[]}"


class Individual: 
    #define some properties 
    def __init__(self, chromosome):
        self.chromosome = chromosome #the actual solution 
        self.fitness = self.calc_fitness() 
        
    def mutated_genes(self): 
        #mutate the genes you have in an Individual 
        gene = random.choice(genes) 
        return gene 
    
    def create_gnome(self, target):
        gnome_len = len(target) 
        return [self.mutated_genes() for _ in range(gnome_len)]
    
    def mate(self, par2):
        #mate with another individual 
        #child chromosome 
        child_chromosome = [] 
        for gp1, gp2 in zip(self.chromosome, par2.chromosome):
            #generate a random number 
            prob = random.random() 
            
            #if prob is less than 0.45, accept gene from parent 1 
            if prob<0.45:
                child_chromosome.append(gp1)
            elif prob < 0.9:
                child_chromosome.append(gp2) #if between 0.45 and 0.9, accept gene from parent 2
            else: 
                child_chromosome.append(self.mutated_genes()) 
                
        return Individual(child_chromosome)
                
    def calc_fitness(self):
        #calculate a fitness score
        #this is the number of characters in the string which 
        #match the target 
        fitness = 0
        for gs,gt in zip(self.chromosome, target):
            if gs!=gt:
                fitness += 1
        return fitness 
这是我的结果:

.
.
.
Generation: 14465    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14466    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14467    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14468    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14469    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14470    String: to be or not to be that is the question     Fitness: 1
我有点着迷于这个算法是如何将字符串设置为最多一个字符的(
question
is
8uestion

我的问题是,为什么遗传算法工作得这么好? 为什么它们在实际收敛到真实解之前在
Fitness=1
处稳定了很长一段时间?为什么增加总体规模会提高收敛速度?如果我通过适应度得分改变接受父1而不是父2的概率,算法会变得更好吗


我一直在试图理解这一点,但大多数博客只是实现了一些代码和经验状态,即人口越多,收敛时间越长。我非常感谢你给我的任何建议。

在我看来,这项任务非常适合遗传算法,因为在适应度、交叉和变异方面有明显的选择。您可以保留具有最正确字符的个体,并对其进行重新组合,这使得最终得到非常相似字符串的几率非常高。每个字符都独立地对适合度作出贡献,而“基因”之间没有交互作用,这意味着每个字符都可以独立地进行优化


最后一点适应度损失会持续很长一段时间,这是由于你实施了随机突变。如果双亲只有一个与目标不同的字符,那么后代与目标完全匹配的几率很高。然而,每个新染色体的10%是随机产生的,可能会引入额外的基因个人损失。虽然变异对于探索至关重要,但您可能希望在优化过程接近尾声时降低变异率,至少对一部分群体而言,这样可以更具开发性,更少探索性。这可能会让您达到所需的
0

适应度。这几乎肯定更适合r@juanpa.arrivillaga哦,好吧。对不起,我不知道这一点。如果“基因型”中的微小变化对应于“表型”中的微小变化,那么它们通常会起作用(在你的例子中,这些变化是相同的,因此可以检验)。在这里,它们在适应度=1时趋于稳定,因为随机突变最后一个错误的基因需要一些运气(首先选择正确的基因进行突变,然后以正确的方式进行突变)。一开始,更多的随机突变会带来进步。
.
.
.
Generation: 14465    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14466    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14467    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14468    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14469    String: to be or not to be that is the 8uestion     Fitness: 1
Generation: 14470    String: to be or not to be that is the question     Fitness: 1