在路上---学习篇,一Python 数据结构和算法

独白

  最近了解到一种算法叫遗传算法,对其比较感兴趣,研究了一下,是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。遗传算法是从代表问题可能潜在的解集的一个种群(population)开始的,而一个种群则由经过基因(gene)编码的一定数目的个体(individual)组成。每个个体实际上是染色体(chromosome)带有特征的实体。染色体作为遗传物质的主要载体,即多个基因的集合,其内部表现(即基因型)是某种基因组合,它决定了个体的形状的外部表现,如黑头发的特征是由染色体中控制这一特征的某种基因组合决定的。因此,在一开始需要实现从表现型到基因型的映射即编码工作。由于仿照基因编码的工作很复杂,我们往往进行简化,如二进制编码,初代种群产生之后,按照适者生存和优胜劣汰的原理,逐代(generation)演化产生出越来越好的近似解,在每一代,根据问题域中个体的适应度(fitness)大小选择(selection)个体,并借助于自然遗传学的遗传算子(genetic operators)进行组合交叉(crossover)和变异(mutation),产生出代表新的解集的种群。这个过程将导致种群像自然进化一样的后生代种群比前代更加适应于环境,末代种群中的最优个体经过解码(decoding),可以作为问题近似最优解

----->>载自百度百科

  自己研究了下,部分代码以及部分函数的功能暂未弄清楚。仍正在努力学习当中。附上一组代码,利用遗传算法匹配一组密码,最后达到正确输出密码的案例。代码如下:

import numpy as np


class GeneticAlgorithm(object):
    """遗传算法"""
    
    def __init__(self, cross_rate, mutation_rate, n_population, n_iterations, password):
        self.cross_rate = cross_rate                                    # 交叉可能性
        self.mutate_rate = mutation_rate                                # 变异的可能性
        self.n_population = n_population                                # 种群的大小
        self.n_iterations = n_iterations                                # 迭代次数
        self.password = password                                            # 要破解的密码
        self.password_size = len(self.password)                             # 要破解密码的长度
        self.password_ascii = np.fromstring(self.password, dtype=np.uint8)  # 将password转换成ASCII.
        self.ascii_bounder = [32, 126+1]


    # 初始化一个种群
    def init_population(self):

        population = np.random.randint(low=self.ascii_bounder[0], high=self.ascii_bounder[1],
                                       size=(self.n_population, self.password_size)).astype(np.int8)
        return population

    # 将个体的DNA转换成ASCII
    def translateDNA(self, DNA):                 
        return DNA.tostring().decode('ascii')

    # 计算种群中每个个体的适应度,适应度越高,说明该个体的基因越好
    def fitness(self, population):
        # self.password_ascii 是一个列表
        match_num = (population == self.password_ascii).sum(axis=1)
        # print(match_num)
        return match_num

    # 对种群按照其适应度进行采样,这样适应度高的个体就会以更高的概率被选择
    def select(self, population):
        # 1e-4 = 0.0001 为数组添加小数
        fitness = self.fitness(population) + 1e-4     # add a small amount to avoid all zero fitness
        idx = np.random.choice(np.arange(self.n_population), size=self.n_population, replace=True, p=fitness/fitness.sum())
        return population[idx]

    # 进行交配
    def create_child(self, parent, pop):
        if np.random.rand() < self.cross_rate:
            index = np.random.randint(0, self.n_population, size=1)                        
            cross_points = np.random.randint(0, 2, self.password_size).astype(np.bool)   
            parent[cross_points] = pop[index, cross_points]                            
        return parent

    # 基因突变
    def mutate_child(self, child):
        for point in range(self.password_size):

            if np.random.rand() < self.mutate_rate:
                child[point] = np.random.randint(*self.ascii_bounder)  # choose a random ASCII index
        return child

    # 进化
    def evolution(self):
        population = self.init_population()
        for i in range(self.n_iterations):
            fitness = self.fitness(population)
            best_person = population[np.argmax(fitness)]
            best_person_ascii = self.translateDNA(best_person)

            if i % 10 == 0:
                print(u'第%-4d次进化后, 基因最好的个体(与欲破解的密码最接近)是: \t %s'% (i, best_person_ascii))

            if best_person_ascii == self.password:
                print(u'第%-4d次进化后, 找到了密码: \t %s'% (i, best_person_ascii))
                break

            population = self.select(population)
            population_copy = population.copy()

            for parent in population:
                child = self.create_child(parent, population_copy)
                child = self.mutate_child(child)
                parent[:] = child

            population = population

def main():
    password = 'I have a dream!'     # 要破解的密码

    ga = GeneticAlgorithm(cross_rate=0.8, mutation_rate=0.01, n_population=300, n_iterations=500, password=password)

    ga.evolution()

if __name__ == '__main__':
    main()