遗传算法:Python实现的完整指南

2024年08月16日 由 alex 发表 145 0

遗传算法简介

什么是遗传算法?

想象一下,如果我们能模仿自然进化的过程来解决机器学习领域中的复杂问题,那将会是怎样的一番景象。这正是遗传算法(GA)所能做到的。遗传算法是一种基于查尔斯-达尔文的自然进化论的解决问题的方法。自然选择的过程启发了它们。这种算法的工作原理就像大自然选择最强壮的生物生孩子一样,这样下一代就会更加强壮。


为什么要使用遗传算法?

当传统方法失效时,遗传算法对优化问题非常有益。遗传算法可以高效地浏览庞大而复杂的搜索空间,因此非常适合需要在限制条件下找到最优解的任务。从进化神经网络架构到优化超参数,遗传算法都是机器学习工具箱中的强大工具。


基因表达编程(GEP)

什么是基因表达编程?

基因表达编程(GEP)是遗传算法的一种变体,在这种算法中,个体被编码为固定长度的线性字符串,然后再表达为不同大小和形状的非线性实体。基因表达编程结合了遗传算法和遗传编程的优点,因此在解决复杂问题方面表现出了很好的效果。


基因表达编程的应用

  • 符号回归 发现最适合一组数据点的数学模型。
  • 分类: 开发将数据归入预定义类别的模型。
  • 时间序列预测: 根据历史数据预测未来值。


了解遗传优化

遗传优化是指使用遗传算法解决优化问题。这一过程包括生成一批可能的解决方案,并根据它们在既定目标下的表现对其进行迭代改进。让我们来看看遗传优化的实际应用。


案例研究 1:优化神经网络架构

研究人员已在多项研究中成功应用遗传算法优化神经网络架构。发表在《神经计算》(Neurocomputing)杂志上的一项研究就利用遗传算法优化了用于图像分类的神经网络架构。这项研究在 MNIST 数据集上取得了 97.5% 的准确率,超过了传统的优化方法。


案例研究 2:利用遗传编程进行期权定价

在这项研究中,遗传编程被用来发展期权定价模型。研究比较了遗传编程与传统 Black-Scholes 模型的性能,发现遗传编程在准确性和强度方面都优于传统模型。


遗传算法的算法

初始化

遗传算法的第一步是生成潜在解决方案的初始种群。你可以随机或使用某些策略生成这个群体。种群的大小是影响算法性能的一个重要参数。


适应度函数

健壮度函数是评估种群中每个个体表现的重要组成部分。在我们的推荐系统中,适配函数基于用户参与度指标,如点击率和用户满意度得分。


选择

选择包括选择表现最好的个体作为下一代的父母。最常见的选择方法有

  • 轮盘选择法: 根据个体的适合度比例进行选择。
  • 锦标赛选择: 随机选择一组个体,选出其中最优秀的个体。
  • 等级选择: 根据个体的适应度进行排序,然后根据这些排序进行选择。


交叉

交叉又称重组,是将两个父方案合并形成子方案。常见的交叉策略包括以下几种:

  1. 在单点交叉中,我们选择一个交叉点,然后在亲本之间交换交叉点前后的基因。
  2. 两点交叉: 选择两个交叉点,交换这两个点之间的基因。
  3. 在均匀交叉中,父母随机交换基因。


突变

突变是对单个解决方案进行随机改变,以保持基因的变异。必须仔细平衡突变率,以便在保留好的解决方案的同时进行适当的探索。


终止

遗传算法会重复选择、交叉和突变的过程,直到达到停止标准。这个标准可以是预先确定的世代数、一定的适合度水平,也可以是与后代相比没有明显改善。


代码示例: 函数优化遗传算法

适应度函数


import numpy as np
# Define the fitness function
def fitness(x):
  # Maximize the function f(x) = x^2
  return x**2


遗传算法参数


# Define the GA parameters
POP_SIZE = 100
GENS = 100
CROSSOVER_PROB = 0.8
MUTATION_PROB = 0.2


初始人口


# Initialize the population
pop = np.random.rand(POP_SIZE)
# Evaluate the fitness of the initial population
fitness_values = np.array([fitness(x) for x in pop])


选择


parents = np.array([pop[np.argmax(fitness_values)] for _ in range(POP_SIZE//2)])


交叉


offspring = []
  for _ in range(POP_SIZE//2):
    parent1, parent2 = parents[np.random.randint(0, len(parents), 2)]
    child = (parent1 + parent2) / 2
    offspring.append(child)


突变


for i in range(len(offspring)):  # Iterate over the correct range of offspring
    if np.random.rand() < MUTATION_PROB:
      offspring[i] += np.random.normal(0, 0.1)


下面是完整的实施过程:


import numpy as np
# Define the fitness function
def fitness(x):
  # Maximize the function f(x) = x^2
  return x**2
# Define the GA parameters
POP_SIZE = 100
GENS = 100
CROSSOVER_PROB = 0.8
MUTATION_PROB = 0.2
# Initialize the population
pop = np.random.rand(POP_SIZE)
# Evaluate the fitness of the initial population
fitness_values = np.array([fitness(x) for x in pop])
# Main GA loop
for gen in range(GENS):
  # Selection
  parents = np.array([pop[np.argmax(fitness_values)] for _ in range(POP_SIZE//2)])
  
  # Crossover
  offspring = []
  for _ in range(POP_SIZE//2):
    parent1, parent2 = parents[np.random.randint(0, len(parents), 2)]
    child = (parent1 + parent2) / 2
    offspring.append(child)
  
  # Mutation
  for i in range(len(offspring)):  # Iterate over the correct range of offspring
    if np.random.rand() < MUTATION_PROB:
      offspring[i] += np.random.normal(0, 0.1)
  
  # Replace the population with the new offspring
  pop = offspring
  
  # Evaluate the fitness of the new population
  fitness_values = np.array([fitness(x) for x in pop])
  
  # Print the best fitness value
  print(f"Generation {gen+1}, Best Fitness: {np.max(fitness_values)}")
# Print the final best solution
print(f"Final Best Solution: {pop[np.argmax(fitness_values)]}")


输出


Generation 1, Best Fitness: 1.4650152220573687
Generation 2, Best Fitness: 1.8054426063247935
Generation 3, Best Fitness: 2.1124584418178354
Generation 4, Best Fitness: 2.34514080269685
.          
.
.
.
.
Generation 99, Best Fitness: 254.58556629300833
Generation 100, Best Fitness: 260.9705918019082
Final Best Solution: 16.154584234882314


机器学习中的遗传算法

为什么在机器学习中使用遗传算法?

遗传算法可用于机器学习中的特征选择、超参数调整和模型优化等任务。它们有助于探索复杂的搜索区域,从而找到传统方法可能会遗漏的最佳解决方案。


超参数优化

超参数调整对机器学习模型至关重要。遗传算法可以有效地搜索超参数空间,找到最优配置。例如,在训练神经网络时,遗传算法可以优化学习率、批量大小和架构参数。


特征选择

特征选择对于提高模型性能同时降低复杂度非常重要。遗传算法可以从大型数据集中识别出最相关的特征,从而建立更准确、更高效的模型。


下面是一个在机器学习中使用遗传算法(GA)进行特征选择的示例:


import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from deap import base, creator, tools, algorithms
# Load the iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Define the number of features to select
num_features = 3
# Define the fitness function
def fitness(individual):
    # Select the features based on the individual
    selected_indices = [i for i, x in enumerate(individual) if x == 1]
    
    # Handle the case where no features are selected
    if not selected_indices:
        return 0,  # Return a low fitness value if no features are selected
    selected_features = np.array([X[:, i] for i in selected_indices]).T
    
    # Create a random forest classifier with the selected features
    clf = RandomForestClassifier(n_estimators=100)
    
    # Evaluate the model using cross-validation
    scores = cross_val_score(clf, selected_features, y, cv=5)
    
    # Return the mean score as the fitness value
    return np.mean(scores),
# Create a DEAP creator for the fitness function
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# Create a DEAP toolbox for the GA
toolbox = base.Toolbox()
toolbox.register("attr_bool", np.random.choice, [0, 1])
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(X[0]))
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", fitness)
# Create a population of 50 individuals
pop = toolbox.population(n=50)
# Evaluate the initial population
fitnesses = toolbox.map(toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
    ind.fitness.values = fit
# Run the GA for 20 generations
for g in range(20):
    offspring = algorithms.varAnd(pop, toolbox, cxpb=0.5, mutpb=0.1)
    fits = toolbox.map(toolbox.evaluate, offspring)
    for fit, ind in zip(fits, offspring):
        ind.fitness.values = fit
    pop = toolbox.select(offspring, k=len(pop))
# Print the best individual and the corresponding fitness value
best_individual = tools.selBest(pop, k=1)[0]
print("Best Individual:", best_individual)
print("Best Fitness:", best_individual.fitness.values[0])
# Select the features based on the best individual
selected_features = np.array([X[:, i] for i, x in enumerate(best_individual) if x == 1]).T
# Print the selected features
print("Selected Features:", selected_features)


输出


Best Individual: [0, 0, 1, 1]
Best Fitness: 0.9666666666666668
Selected Features: [[1.4 0.2]
                    [1.4 0.2]
                      .
                      .
                      .
                    [5.1 1.8]]


结论

遗传算法是机器学习中一种强大的优化工具。它们从自然进化中汲取灵感,高效地探索大型复杂搜索空间。从超参数调整到特征选择,遗传算法已在各种应用中证明了自己的价值。


文章来源:https://levelup.gitconnected.com/genetic-algorithm-complete-guide-with-python-implementation-747d62dbe9bd
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消