AI 文章摘要

遗传算法受生物进化启发,通过初始化种群、评估适应度、选择、交叉和突变等操作迭代优化解。文章以01序列问题为例,演示算法如何找到包含最多1的序列,并提供Python代码模拟整个过程,展示了遗传算法在优化搜索中的有效性。阅读此文章大概需要7分钟。
摘要更新时间:2026-06-25 14:11

引言

进化是自然界中最强大的力量之一,它塑造了我们周围的生物多样性和复杂性。受生物进化启发,人工智能领域涌现出一种强大的优化算法——遗传算法。遗传算法模拟了生物进化的基本原理,通过遗传、突变和选择等操作,能够高效地搜索解空间,找到最优解或接近最优解。

在计算机科学领域,遗传算法被广泛应用于解决各种优化问题。其中,01序列问题作为一个简单而具有挑战性的例子,展示了遗传算法在优化问题中的强大能力。在01序列问题中,我们的目标是在给定长度的01序列中找到包含最多1的序列,这看似简单的问题背后隐藏着复杂的优化过程。

本文将介绍遗传算法的基本原理,并以01序列问题为例,演示遗传算法如何在优化搜索中发挥作用。我们将探讨遗传算法如何逐步优化01序列,找到包含最多1的序列。

相关生物学术语

  1. 个体(Individual):在遗传算法中代表问题的一个可能解,类比于生物学中的个体。
  2. 种群(Population):由一组个体组成的集合,类比于生物学中的种群。
  3. 基因型(Genotype):个体的基因表示形式,描述了个体的基因组成,类比于生物学中的基因型。
  4. 表现型(Phenotype):基因型经过表达后的形态特征,描述了个体的外在表现,类比于生物学中的表现型。
  5. 适应度(Fitness):衡量个体在解决问题中的优劣程度的指标,类比于生物学中个体在特定环境中生存和繁殖的能力。
  6. 选择(Selection):根据个体的适应度选择父代个体用于繁殖下一代的操作,类比于生物学中自然选择的过程。
  7. 交叉(Crossover):父代个体基因交换产生新个体的操作,类比于生物学中基因重组的过程。
  8. 突变(Mutation):在新个体基因中引入随机变化的操作,类比于生物学中基因突变的现象。
  9. 进化(Evolution):种群经过选择、交叉和突变等操作逐步优化解的过程,类比于生物学中物种的演化过程。

基本原理

  1. 初始化种群
    遗传算法开始于一个随机生成的种群,种群中包含多个个体,每个个体代表了问题的一个可能解。这些个体通常以某种方式编码,比如01序列问题中可以用01串来表示。
  2. 评估适应度
    对种群中的每个个体计算适应度,适应度函数评估了个体在解空间中的优劣程度。在01序列问题中,适应度函数可以是计算序列中1的数量。
  3. 选择
    根据个体的适应度,选择适应度高的个体作为父代参与繁殖。常用的选择方法包括轮盘赌选择、锦标赛选择等。
  4. 交叉
    选定的父代个体通过交叉操作生成新的个体,将父代的某些特征组合起来形成新的个体。交叉操作有多种方式,如单点交叉、多点交叉等。
  5. 突变
    在新生成的个体中引入随机变化,即突变操作。突变有助于维持种群的多样性,防止陷入局部最优解。

问题解决方案

  1. 问题描述
    首先,明确01序列问题的描述:在给定长度的01序列中,找到包含最多1的序列。
  2. 遗传算法解决方案
  • 初始化:开始时,随机生成一个种群,种群中包含多个01序列个体。
  • 适应度评估:对每个个体计算适应度,即计算序列中1的数量,适应度越高表示该个体包含更多的1。
  • 选择:通过选择操作,根据适应度高低选择个体作为父代参与繁殖。
  • 交叉:选定的父代个体进行交叉操作,生成新的个体。可以采用单点交叉、多点交叉等方式。
  • 突变:在新生成的个体中引入随机变化,即突变操作,有助于保持种群的多样性。
  • 迭代优化:重复进行选择、交叉和突变操作,逐步优化种群中的个体,直到满足停止条件(如达到最大迭代次数或找到满意解)。

算法模拟(Python)

下面来演示遗传算法的一个迭代执行过程

代码运行截图:

遗传算法

如上,我生成了一个01序列的列表,每一条01序列都比作一个基因,此列表也就相当于一个种群,首先先输出了所要求解的最好的基因序列(即1最多的01序列)的序列内容以及它的适应度值(在此例子中,适应度的评估标准就是1数量最多,适应度就高),再然后开始挑选基因(即01序列),在此例中,我采用的是轮盘赌选择方法对基因(01序列)进行选择,就是先计算种群(01列表)中所有个体的适应度值(一个01序列1的个数)之和,然后,计算每个个体(01序列)被选择的概率,即根据个体的适应度值(一个01序列1的个数)与总适应度值(列表中1的总个数)的比例来确定选择概率,再然后,利用random.choices函数根据计算得到的选择概率来选择个体的索引,通过选择的索引,从原种群中选出相应的个体,形成新的被选择的种群selected_population,选出新总群之后,之后把新种群中的01序列两两进行交叉和变异操作之后变为新的基因再替换掉原种群中的基因序列,之后又得到一个新的种群,这就是一个迭代过程,后面的也是一样的依次迭代下去(注:在交叉的时候交叉点是随机的,交叉点在索引1到倒数第二位任意一个位置,在变异的时候,它是单点变异,首先设定了一个变异率,在程序中是这样设定的,循环遍历01序列的每个数字,同时用random函数随机一个0到1的数,如果小于此设定的变异率,那么当前数字就变,周就会判断当前序列位的数字,如果为0变1,否则1变0)

代码

import random

def fitness_function(chromosome):
    return sum(chromosome)

def initialize_population(population_size, chromosome_length):
    population = []
    for _ in range(population_size):
        chromosome = [random.randint(0, 1) for _ in range(chromosome_length)]
        population.append(chromosome)
    return population

def selection(population, fitness_values):
    total_fitness = sum(fitness_values)
    selection_probs = [fitness / total_fitness for fitness in fitness_values]  #选择概率
    selected_population = []
    selected_indices = set()
    while len(selected_population) < len(population):
        index = random.choices(range(len(population)),weights = selection_probs)[0]
        if index not in selected_indices:
            selected_indices.add(index)
            selected_population.append(population[index])

    return selected_population     

def crossover(parent1, parent2):
    crossover_point = random.randint(1, len(parent1)-1)
    child1 = parent1[:crossover_point] + parent2[crossover_point:]
    child2 = parent2[:crossover_point] + parent1[crossover_point:]
    return child1, child2

def mutation(chromosome, mutation_rate):
    mutated_chromosome = []
    for bit in chromosome:
        if random.random() < mutation_rate:
            mutated_chromosome.append(1 if bit == 0 else 0)
        else:
            mutated_chromosome.append(bit)
    return mutated_chromosome

def genetic_algorithm(population_size, chromosome_length, mutation_rate, generations):
    population = initialize_population(population_size, chromosome_length)
    print("种群:")
    for chromosome in population:
        print(chromosome)
    for generation in range(generations):
        print(f"迭代 {generation+1}")
        fitness_values = [fitness_function(chromosome) for chromosome in population]
        best_chromosome = population[fitness_values.index(max(fitness_values))]
        print(f"Best Chromosome = {best_chromosome},Fitness = {max(fitness_values)}")
        
        selected_population = selection(population, fitness_values)
        print("所选择的种群:")
        for chromosome in selected_population:
            print(chromosome)
        next_generation = []
        # Keep track of selected chromosomes
        selected_indices = set()
        for i in range(0, len(selected_population), 2):
            # Choose distinct parents
            while True:
                index1, index2 = random.sample(range(len(selected_population)), 2)
                if index1 not in selected_indices and index2 not in selected_indices:
                    selected_indices.add(index1)
                    selected_indices.add(index2)
                    break
            parent1 = selected_population[index1]
            parent2 = selected_population[index2]
            child1, child2 = crossover(parent1, parent2)
            print(f"交叉: {parent1} + {parent2} -> {child1} + {child2}")
            child1 = mutation(child1, mutation_rate)
            child2 = mutation(child2, mutation_rate)
            print(f"变异:{child1} + {child2}")
            next_generation.extend([child1, child2])
        population = next_generation
        print("新种群:")
        for chromosome in population:
            print(chromosome)
        print("------------------------")

population_size = 6
chromosome_length = 8
mutation_rate = 0.1
generations = 5

genetic_algorithm(population_size, chromosome_length, mutation_rate, generations)