遗传算法(Genetic Algorithm,GA)是一种基于自然选择和遗传学原理的优化搜索算法。它模拟生物进化过程中的遗传、变异、交叉等机制,在复杂的搜索空间中寻找最优解或近似最优解。遗传算法具有广泛的应用,包括函数优化、组合优化、机器学习、自动控制等领域。
以下是一个简单的使用遗传算法求解函数 f ( x ) = x 2 f(x)=x^2 f(x)=x2 在区间 [ 0 , 31 ] [0,31] [0,31] 上最大值的 Python 代码示例。这里采用二进制编码、轮盘赌选择、单点交叉和基本的位变异。
import random import math # 种群大小 POPULATION_SIZE = 50 # 染色体长度(这里因为求解范围是[0,31],用5位二进制编码即可表示) CHROMOSOME_LENGTH = 5 # 交叉概率 CROSSOVER_PROBABILITY = 0.7 # 变异概率 MUTATION_PROBABILITY = 0.01 # 最大迭代次数 MAX_GENERATIONS = 100 # 适应度函数,这里是目标函数f(x)=x^2 def fitness_function(chromosome): x = int(''.join(map(str, chromosome)), 2) return x ** 2 # 初始化种群,生成随机的二进制编码个体 def initialize_population(): population = [] for _ in range(POPULATION_SIZE): chromosome = [random.randint(0, 1) for _ in range(CHROMOSOME_LENGTH)] population.append(chromosome) return population # 计算种群中每个个体的适应度值 def calculate_fitness(population): fitness_values = [fitness_function(chromosome) for chromosome in population] return fitness_values # 轮盘赌选择 def roulette_wheel_selection(population, fitness_values): total_fitness = sum(fitness_values) selection_probs = [fitness / total_fitness for fitness in fitness_values] new_population = [] for _ in range(POPULATION_SIZE): random_number = random.random() cumulative_prob = 0 for i in range(len(population)): cumulative_prob += selection_probs[i] if cumulative_prob > random_number: new_population.append(population[i]) break return new_population # 单点交叉 def crossover(parent1, parent2): if random.random() < CROSSOVER_PROBABILITY: crossover_point = random.randint(1, CHROMOSOME_LENGTH - 1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] return child1, child2 return parent1, parent2 # 变异操作 def mutation(chromosome): for i in range(len(chromosome)): if random.random() < MUTATION_PROBABILITY: chromosome[i] = 1 - chromosome[i] return chromosome # 遗传算法主函数 def genetic_algorithm(): population = initialize_population() for generation in range(MAX_GENERATIONS): fitness_values = calculate_fitness(population) population = roulette_wheel_selection(population, fitness_values) new_population = [] for i in range(0, POPULATION_SIZE, 2): parent1 = population[i] parent2 = population[i + 1] child1, child2 = crossover(parent1, parent2) child1 = mutation(child1) child2 = mutation(child2) new_population.extend([child1, child2]) population = new_population best_fitness = max(calculate_fitness(population)) print(f"Generation {generation}: Best Fitness = {best_fitness}") best_chromosome = max(population, key=fitness_function) best_value = int(''.join(map(str, best_chromosome)), 2) print(f"Optimal solution: x = {best_value}, f(x) = {best_value ** 2}")
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283你可以运行 genetic_algorithm() 函数来执行遗传算法求解过程。
遗传算法是一种强大的优化搜索算法,它模拟生物进化过程,通过选择、交叉和变异等遗传操作在搜索空间中寻找最优解。通过合理选择算法参数、编码方式和适应度函数,遗传算法可以应用于多种类型的问题,并在许多领域取得了良好的效果。代码示例展示了其基本的实现过程,在实际应用中,可以根据具体问题对算法进行进一步的改进和优化,以提高搜索效率和求解质量。
相关知识
使用p5.js实现神经网络与遗传算法示例
遗传算法
神经网络与遗传算法融合的AI中国象棋程序源码分享
多目标遗传算法NSGA2.c++源码
基于改进遗传算法的工程施工进度优化
智能病虫害监控系统作用及原理详解
物流配送中烟花算法结合遗传算法的异质车队路径优化方法
遗传算法Matlab代码实现及其在推荐系统中的应用
详解花茶的制成与冲泡
基于遗传算法的花卉图案创新设计
网址: 遗传算法原理与详解 https://m.huajiangbk.com/newsview622828.html
上一篇: 稻盛和夫《经营十二条》|人生·工 |
下一篇: 在金鱼草中,花色是由一对等位基因 |