差分进化算法 (Differential Evolution) 算法详解及案例分析

差分进化算法 (Differential Evolution) 算法详解及案例分析

目录

  • 差分进化算法 (Differential Evolution) 算法详解及案例分析
    • 1. 引言
    • 2. 差分进化算法 (DE) 算法原理
      • 2.1 基本概念
      • 2.2 算法步骤
    • 3. 差分进化算法的优势与局限性
      • 3.1 优势
      • 3.2 局限性
    • 4. 案例分析
      • 4.1 案例1: 单目标优化问题
        • 4.1.1 问题描述
        • 4.1.2 代码实现
        • 4.1.3 流程图
        • 4.1.4 优化曲线
      • 4.2 案例2: 多目标优化问题
        • 4.2.1 问题描述
        • 4.2.2 代码实现
        • 4.2.3 流程图
        • 4.2.4 优化曲线
      • 4.3 案例3: 约束优化问题
        • 4.3.1 问题描述
        • 4.3.2 代码实现
        • 4.3.3 流程图
        • 4.3.4 优化曲线
    • 5. 总结
    • 6. 参考文献


1. 引言

差分进化算法 (Differential Evolution, DE) 是一种基于群体智能的全局优化算法,由 Storn 和 Price 在 1997 年提出。该算法通过模拟生物进化中的变异、交叉和选择操作,逐步优化目标函数。差分进化算法因其简单、高效和鲁棒性强,被广泛应用于单目标优化、多目标优化和约束优化问题。

本文将详细介绍差分进化算法的原理,并通过三个具体案例展示其在实际问题中的应用。每个案例将提供完整的 Python 实现代码、流程图以及优化曲线。


2. 差分进化算法 (DE) 算法原理

2.1 基本概念

差分进化算法通过维护一个种群,利用种群中个体的差异信息生成新个体,并通过选择操作保留较优个体。其主要操作包括:

  1. 初始化:随机生成初始种群。
  2. 变异:利用种群中个体的差异生成变异个体。
  3. 交叉:将变异个体与目标个体进行交叉,生成试验个体。
  4. 选择:根据适应度值选择较优个体进入下一代。

2.2 算法步骤

  1. 初始化

    • 随机生成初始种群 X = { x 1 , x 2 , … , x N } X = \{x_1, x_2, \dots, x_N\} X={x1,x2,,xN},其中 x i x_i xi 表示第 i i i 个个体。
    • 每个个体 x i x_i xi 是一个 D D D 维向量,表示解空间中的一个点。
  2. 变异

    • 对于每个目标个体 x i x_i xi,生成变异个体 v i v_i vi
      v i = x r 1 + F ⋅ ( x r 2 − x r 3 ) v_i = x_{r1} + F \cdot (x_{r2} - x_{r3}) vi=xr1+F(xr2xr3)
      其中, r 1 , r 2 , r 3 r1, r2, r3 r1,r2,r3 是随机选择的个体索引, F F F 是缩放因子。
  3. 交叉

    • 将变异个体 v i v_i vi 与目标个体 x i x_i xi 进行交叉,生成试验个体 u i u_i ui
      u i , j = { v i , j , if rand ( 0 , 1 ) ≤ C R  or  j = j rand x i , j , otherwise u_{i,j} = \begin{cases} v_{i,j}, & \text{if } \text{rand}(0,1) \leq CR \text{ or } j = j_{\text{rand}} \\ x_{i,j}, & \text{otherwise} \end{cases} ui,j={vi,j,xi,j,if rand(0,1)CR or j=jrandotherwise
      其中, C R CR CR 是交叉概率, j rand j_{\text{rand}} jrand 是随机选择的维度索引。
  4. 选择

    • 比较试验个体 u i u_i ui 和目标个体 x i x_i xi 的适应度值,选择较优个体进入下一代:
      x i t + 1 = { u i , if  f ( u i ) ≤ f ( x i ) x i , otherwise x_i^{t+1} = \begin{cases} u_i, & \text{if } f(u_i) \leq f(x_i) \\ x_i, & \text{otherwise} \end{cases} xit+1={ui,xi,if f(ui)f(xi)otherwise
  5. 迭代

    • 重复步骤 2-4,直到满足终止条件。

3. 差分进化算法的优势与局限性

3.1 优势

  • 全局搜索能力强:通过变异操作引入多样性,避免陷入局部最优。
  • 参数少:主要参数为种群大小 N N N、缩放因子 F F F 和交叉概率 C R CR CR
  • 鲁棒性强:适用于连续、离散、单目标和多目标优化问题。

3.2 局限性

  • 参数敏感:算法性能依赖于参数设置。
  • 收敛速度慢:对于高维问题,收敛速度较慢。
  • 局部搜索能力弱:在接近最优解时,搜索效率较低。

4. 案例分析

4.1 案例1: 单目标优化问题

4.1.1 问题描述

求解单目标优化问题:
f ( x ) = ∑ i = 1 D x i 2 , x i ∈ [ − 5.12 , 5.12 ] f(x) = \sum_{i=1}^D x_i^2, \quad x_i \in [-5.12, 5.12] f(x)=i=1Dxi2,xi[5.12,5.12]

4.1.2 代码实现
import numpy as np
import matplotlib.pyplot as plt

class DifferentialEvolution:
    def __init__(self, objective_func, bounds, population_size=50, max_generations=100, F=0.5, CR=0.7):
        self.objective_func = objective_func
        self.bounds = bounds
        self.population_size = population_size
        self.max_generations = max_generations
        self.F = F
        self.CR = CR
        self.dimensions = len(bounds)
        self.population = None
        self.fitness = None

    def initialize_population(self):
        self.population = np.random.uniform(
            low=[b[0] for b in self.bounds],
            high=[b[1] for b in self.bounds],
            size=(self.population_size, self.dimensions)
        )
        self.fitness = np.array([self.objective_func(ind) for ind in self.population])

    def mutate(self, target_idx):
        a, b, c = np.random.choice(self.population_size, 3, replace=False)
        mutant = self.population[a] + self.F * (self.population[b] - self.population[c])
        return mutant

    def crossover(self, target_idx, mutant):
        trial = np.copy(self.population[target_idx])
        cross_points = np.random.rand(self.dimensions) <= self.CR
        cross_points[np.random.randint(self.dimensions)] = True
        trial[cross_points] = mutant[cross_points]
        return trial

    def select(self, target_idx, trial):
        trial_fitness = self.objective_func(trial)
        if trial_fitness < self.fitness[target_idx]:
            self.population[target_idx] = trial
            self.fitness[target_idx] = trial_fitness

    def run(self):
        self.initialize_population()
        best_fitness_history = []

        for generation in range(self.max_generations):
            for i in range(self.population_size):
                mutant = self.mutate(i)
                trial = self.crossover(i, mutant)
                self.select(i, trial)

            best_fitness = np.min(self.fitness)
            best_fitness_history.append(best_fitness)
            print(f"Generation {generation + 1}: Best Fitness = {best_fitness}")

        return best_fitness_history

# 定义目标函数
def objective_function(x):
    return np.sum(x**2)

# 定义边界
bounds = [(-5.12, 5.12)] * 10

# 运行算法
de = DifferentialEvolution(objective_function, bounds, population_size=50, max_generations=100)
best_fitness_history = de.run()

# 绘制优化曲线
plt.plot(best_fitness_history)
plt.title('DE Optimization Curve for Single-Objective Problem')
plt.xlabel('Generation')
plt.ylabel('Best Fitness')
plt.show()
C:\Users\Administrator\Documents\code\yhsf>C:/software/python39/python.exe c:/Users/Administrator/Documents/code/yhsf/demo1.py
Generation 1: Best Fitness = 37.75832295895839
Generation 2: Best Fitness = 37.75832295895839
Generation 3: Best Fitness = 37.665206407771855
Generation 4: Best Fitness = 27.868288924606585
Generation 5: Best Fitness = 26.32768920670396
Generation 6: Best Fitness = 26.32768920670396
Generation 7: Best Fitness = 20.333746275357953
Generation 8: Best Fitness = 14.640315469596102
Generation 9: Best Fitness = 14.640315469596102
Generation 10: Best Fitness = 14.640315469596102
Generation 11: Best Fitness = 12.07330648333177
Generation 12: Best Fitness = 11.33071787589119
Generation 13: Best Fitness = 9.961137625541191
Generation 14: Best Fitness = 7.3371710631556954
Generation 15: Best Fitness = 7.3371710631556954
Generation 16: Best Fitness = 7.3371710631556954
Generation 17: Best Fitness = 7.3371710631556954
Generation 18: Best Fitness = 7.3371710631556954
Generation 19: Best Fitness = 6.040988956976091
Generation 20: Best Fitness = 3.502271255117573
Generation 21: Best Fitness = 3.502271255117573
Generation 22: Best Fitness = 2.0100850049774004
Generation 23: Best Fitness = 2.0100850049774004
Generation 24: Best Fitness = 2.0100850049774004
Generation 25: Best Fitness = 2.0100850049774004
Generation 26: Best Fitness = 2.0100850049774004
Generation 27: Best Fitness = 1.8327010452494987
Generation 28: Best Fitness = 1.8327010452494987
Generation 29: Best Fitness = 1.8327010452494987
Generation 30: Best Fitness = 1.8327010452494987
Generation 31: Best Fitness = 1.0823501355050198
Generation 32: Best Fitness = 1.0823501355050198
Generation 33: Best Fitness = 0.7566949878867334
Generation 34: Best Fitness = 0.7050031372515128
Generation 35: Best Fitness = 0.7050031372515128
Generation 36: Best Fitness = 0.7050031372515128
Generation 37: Best Fitness = 0.7050031372515128
Generation 38: Best Fitness = 0.4489938937381844
Generation 39: Best Fitness = 0.4489938937381844
Generation 40: Best Fitness = 0.4489938937381844
Generation 41: Best Fitness = 0.4104254249116195
Generation 42: Best Fitness = 0.4104254249116195
Generation 43: Best Fitness = 0.39368510716020266
Generation 44: Best Fitness = 0.20057292653320077
Generation 45: Best Fitness = 0.20057292653320077
Generation 46: Best Fitness = 0.1939199488334692
Generation 47: Best Fitness = 0.1400472313221252
Generation 48: Best Fitness = 0.1400472313221252
Generation 49: Best Fitness = 0.1400472313221252
Generation 50: Best Fitness = 0.1320916710296219
Generation 51: Best Fitness = 0.07043025264280887
Generation 52: Best Fitness = 0.06222145354290613
Generation 53: Best Fitness = 0.06222145354290613
Generation 54: Best Fitness = 0.06222145354290613
Generation 55: Best Fitness = 0.06222145354290613
Generation 56: Best Fitness = 0.05449857652107404
Generation 57: Best Fitness = 0.04995916356983875
Generation 58: Best Fitness = 0.04028161421729717
Generation 59: Best Fitness = 0.03731392548135427
Generation 60: Best Fitness = 0.03731392548135427
Generation 61: Best Fitness = 0.03731392548135427
Generation 62: Best Fitness = 0.029695078475206256
Generation 63: Best Fitness = 0.029695078475206256
Generation 64: Best Fitness = 0.02874881687043088
Generation 65: Best Fitness = 0.026175715813639245
Generation 66: Best Fitness = 0.017562954399615596
Generation 67: Best Fitness = 0.017562954399615596
Generation 68: Best Fitness = 0.016972776360096666
Generation 69: Best Fitness = 0.016972776360096666
Generation 70: Best Fitness = 0.016972776360096666
Generation 71: Best Fitness = 0.009258187202784015
Generation 72: Best Fitness = 0.00910336154656241
Generation 73: Best Fitness = 0.007704343149247564
Generation 74: Best Fitness = 0.007704343149247564
Generation 75: Best Fitness = 0.007704343149247564
Generation 76: Best Fitness = 0.007704343149247564
Generation 77: Best Fitness = 0.0023885895039290873
Generation 78: Best Fitness = 0.0023885895039290873
Generation 79: Best Fitness = 0.0023885895039290873
Generation 80: Best Fitness = 0.0023885895039290873
Generation 81: Best Fitness = 0.0023885895039290873
Generation 82: Best Fitness = 0.0023885895039290873
Generation 83: Best Fitness = 0.0023885895039290873
Generation 84: Best Fitness = 0.0015221196180357124
Generation 85: Best Fitness = 0.0015221196180357124
Generation 86: Best Fitness = 0.0012952681972181876
Generation 87: Best Fitness = 0.0008420065176953722
Generation 88: Best Fitness = 0.0007367066254967593
Generation 89: Best Fitness = 0.0007367066254967593
Generation 90: Best Fitness = 0.000465154526028501
Generation 91: Best Fitness = 0.000465154526028501
Generation 92: Best Fitness = 0.000465154526028501
Generation 93: Best Fitness = 0.0004563256318120555
Generation 94: Best Fitness = 0.00044701222773503466
Generation 95: Best Fitness = 0.00044701222773503466
Generation 96: Best Fitness = 0.00044701222773503466
Generation 97: Best Fitness = 0.00033614584190847397
Generation 98: Best Fitness = 0.00021517569214993674
Generation 99: Best Fitness = 0.00021517569214993674
Generation 100: Best Fitness = 0.00021517569214993674
4.1.3 流程图
初始化种群
变异
交叉
选择
是否达到最大迭代次数?
返回最优解
4.1.4 优化曲线

运行代码后,优化曲线将显示每次迭代的最优适应度值变化。
差分进化算法 (Differential Evolution) 算法详解及案例分析_第1张图片


4.2 案例2: 多目标优化问题

4.2.1 问题描述

求解多目标优化问题:
f 1 ( x ) = ∑ i = 1 D x i 2 , f 2 ( x ) = ∑ i = 1 D ( x i − 2 ) 2 f_1(x) = \sum_{i=1}^D x_i^2, \quad f_2(x) = \sum_{i=1}^D (x_i - 2)^2 f1(x)=i=1Dxi2,f2(x)=i=1D(xi2)2

4.2.2 代码实现
class DifferentialEvolutionMO:
    def __init__(self, objective_funcs, bounds, population_size=50, max_generations=100, F=0.5, CR=0.7):
        self.objective_funcs = objective_funcs
        self.bounds = bounds
        self.population_size = population_size
        self.max_generations = max_generations
        self.F = F
        self.CR = CR
        self.dimensions = len(bounds)
        self.population = None
        self.fitness = None

    def initialize_population(self):
        self.population = np.random.uniform(
            low=[b[0] for b in self.bounds],
            high=[b[1] for b in self.bounds],
            size=(self.population_size, self.dimensions)
        )
        self.fitness = np.array([self.evaluate_fitness(ind) for ind in self.population])

    def evaluate_fitness(self, individual):
        return np.array([func(individual) for func in self.objective_funcs])

    def dominates(self, a, b):
        return np.all(a <= b) and np.any(a < b)

    def mutate(self, target_idx):
        a, b, c = np.random.choice(self.population_size, 3, replace=False)
        mutant = self.population[a] + self.F * (self.population[b] - self.population[c])
        return mutant

    def crossover(self, target_idx, mutant):
        trial = np.copy(self.population[target_idx])
        cross_points = np.random.rand(self.dimensions) <= self.CR
        cross_points[np.random.randint(self.dimensions)] = True
        trial[cross_points] = mutant[cross_points]
        return trial

    def select(self, target_idx, trial):
        trial_fitness = self.evaluate_fitness(trial)
        if self.dominates(trial_fitness, self.fitness[target_idx]):
            self.population[target_idx] = trial
            self.fitness[target_idx] = trial_fitness

    def run(self):
        self.initialize_population()
        best_fitness_history = []

        for generation in range(self.max_generations):
            for i in range(self.population_size):
                mutant = self.mutate(i)
                trial = self.crossover(i, mutant)
                self.select(i, trial)

            best_fitness = np.min(self.fitness, axis=0)
            best_fitness_history.append(best_fitness)
            print(f"Generation {generation + 1}: Best Fitness = {best_fitness}")

        return best_fitness_history

# 定义目标函数
def objective_function_1(x):
    return np.sum(x**2)

def objective_function_2(x):
    return np.sum((x - 2)**2)

# 定义边界
bounds = [(-5.12, 5.12)] * 10

# 运行算法
de_mo = DifferentialEvolutionMO([objective_function_1, objective_function_2], bounds, population_size=50, max_generations=100)
best_fitness_history = de_mo.run()

# 绘制优化曲线
plt.plot(best_fitness_history)
plt.title('DE Optimization Curve for Multi-Objective Problem')
plt.xlabel('Generation')
plt.ylabel('Best Fitness')
plt.legend(['Objective 1', 'Objective 2'])
plt.show()
C:\Users\Administrator\Documents\code\yhsf>C:/software/python39/python.exe c:/Users/Administrator/Documents/code/yhsf/demo1.py
Generation 1: Best Fitness = [25.45725123 48.65198317]
Generation 2: Best Fitness = [25.45725123 48.65198317]
Generation 3: Best Fitness = [21.13878637 19.96978815]
Generation 4: Best Fitness = [21.13878637 19.96978815]
Generation 5: Best Fitness = [21.13878637 19.96978815]
Generation 6: Best Fitness = [21.13878637 19.96978815]
Generation 7: Best Fitness = [20.19557877 19.96978815]
Generation 8: Best Fitness = [20.19557877 19.96978815]
Generation 9: Best Fitness = [20.19557877 19.96978815]
Generation 10: Best Fitness = [20.19557877 19.96978815]
Generation 11: Best Fitness = [20.19557877 19.96978815]
Generation 12: Best Fitness = [12.59289352 13.44349443]
Generation 13: Best Fitness = [12.59289352 13.44349443]
Generation 14: Best Fitness = [12.59289352 13.44349443]
Generation 15: Best Fitness = [12.59289352 13.44349443]
Generation 16: Best Fitness = [12.59289352 13.44349443]
Generation 17: Best Fitness = [12.59289352 13.44349443]
Generation 18: Best Fitness = [12.59289352 13.44349443]
Generation 19: Best Fitness = [12.59289352 13.44349443]
Generation 20: Best Fitness = [12.59289352 13.44349443]
Generation 21: Best Fitness = [ 9.17982724 13.44349443]
Generation 22: Best Fitness = [ 9.17982724 11.66864116]
Generation 23: Best Fitness = [ 9.17982724 11.66864116]
Generation 24: Best Fitness = [ 9.17982724 11.66864116]
Generation 25: Best Fitness = [ 9.17982724 11.12152932]
Generation 26: Best Fitness = [ 9.17982724 11.12152932]
Generation 27: Best Fitness = [ 9.17982724 11.12152932]
Generation 28: Best Fitness = [ 6.90670347 11.12152932]
Generation 29: Best Fitness = [ 6.90670347 11.12152932]
Generation 30: Best Fitness = [ 6.90670347 11.12152932]
Generation 31: Best Fitness = [ 6.90670347 11.12152932]
Generation 32: Best Fitness = [6.90670347 6.3422328 ]
Generation 33: Best Fitness = [6.90670347 6.3422328 ]
Generation 34: Best Fitness = [6.90670347 6.3422328 ]
Generation 35: Best Fitness = [6.90670347 6.3422328 ]
Generation 36: Best Fitness = [6.90670347 6.3422328 ]
Generation 37: Best Fitness = [6.90670347 6.3422328 ]
Generation 38: Best Fitness = [6.90670347 6.33369816]
Generation 39: Best Fitness = [6.90670347 6.33369816]
Generation 40: Best Fitness = [5.58744792 6.33369816]
Generation 41: Best Fitness = [5.58744792 6.33369816]
Generation 42: Best Fitness = [5.58744792 6.33369816]
Generation 43: Best Fitness = [5.58744792 6.33369816]
Generation 44: Best Fitness = [2.30680838 6.33369816]
Generation 45: Best Fitness = [2.30680838 6.33369816]
Generation 46: Best Fitness = [2.30680838 6.33369816]
Generation 47: Best Fitness = [2.30680838 6.33369816]
Generation 48: Best Fitness = [2.30680838 6.33369816]
Generation 49: Best Fitness = [2.30680838 6.33369816]
Generation 50: Best Fitness = [2.30680838 5.39630504]
Generation 51: Best Fitness = [2.30680838 5.39630504]
Generation 52: Best Fitness = [2.30680838 5.39630504]
Generation 53: Best Fitness = [2.30680838 5.39630504]
Generation 54: Best Fitness = [2.30680838 5.39630504]
Generation 55: Best Fitness = [2.30680838 5.39630504]
Generation 56: Best Fitness = [2.30680838 5.39630504]
Generation 57: Best Fitness = [2.30680838 5.39630504]
Generation 58: Best Fitness = [2.30680838 5.39630504]
Generation 59: Best Fitness = [2.30680838 5.39630504]
Generation 60: Best Fitness = [2.30680838 5.39630504]
Generation 61: Best Fitness = [2.30680838 5.39630504]
Generation 62: Best Fitness = [2.30680838 5.39630504]
Generation 63: Best Fitness = [2.30680838 5.39630504]
Generation 64: Best Fitness = [2.30680838 5.39630504]
Generation 65: Best Fitness = [2.30680838 5.39630504]
Generation 66: Best Fitness = [2.30680838 5.39630504]
Generation 67: Best Fitness = [2.30680838 5.03874297]
Generation 68: Best Fitness = [2.30680838 5.03874297]
Generation 69: Best Fitness = [2.30680838 5.03874297]
Generation 70: Best Fitness = [2.30680838 5.03874297]
Generation 71: Best Fitness = [2.30680838 5.03874297]
Generation 72: Best Fitness = [2.30680838 5.03874297]
Generation 73: Best Fitness = [2.30680838 5.03874297]
Generation 74: Best Fitness = [2.30680838 5.03874297]
Generation 75: Best Fitness = [2.30680838 5.03874297]
Generation 76: Best Fitness = [2.30680838 5.03874297]
Generation 77: Best Fitness = [2.30680838 5.03874297]
Generation 78: Best Fitness = [2.30680838 5.03874297]
Generation 79: Best Fitness = [2.30680838 5.03874297]
Generation 80: Best Fitness = [2.30680838 5.03874297]
Generation 81: Best Fitness = [2.30680838 5.03874297]
Generation 82: Best Fitness = [2.30680838 5.03874297]
Generation 83: Best Fitness = [2.30680838 5.03874297]
Generation 84: Best Fitness = [2.30680838 5.03874297]
Generation 85: Best Fitness = [2.30680838 5.03874297]
Generation 86: Best Fitness = [2.30680838 5.03874297]
Generation 87: Best Fitness = [2.30680838 5.03874297]
Generation 88: Best Fitness = [2.30680838 5.03874297]
Generation 89: Best Fitness = [2.30680838 5.03874297]
Generation 90: Best Fitness = [2.30680838 5.03874297]
Generation 91: Best Fitness = [2.30680838 5.03874297]
Generation 92: Best Fitness = [2.30680838 5.03874297]
Generation 93: Best Fitness = [2.30680838 5.03874297]
Generation 94: Best Fitness = [2.30680838 5.03874297]
Generation 95: Best Fitness = [2.30680838 5.03874297]
Generation 96: Best Fitness = [2.30680838 5.03874297]
Generation 97: Best Fitness = [2.30680838 5.03874297]
Generation 98: Best Fitness = [2.30680838 5.03874297]
Generation 99: Best Fitness = [2.30680838 5.03874297]
Generation 100: Best Fitness = [2.30680838 5.03874297]
4.2.3 流程图
初始化种群
变异
交叉
选择
是否达到最大迭代次数?
返回最优解
4.2.4 优化曲线

运行代码后,优化曲线将显示每次迭代的最优适应度值变化。
差分进化算法 (Differential Evolution) 算法详解及案例分析_第2张图片


4.3 案例3: 约束优化问题

4.3.1 问题描述

求解约束优化问题:
f ( x ) = ∑ i = 1 D x i 2 , subject to  ∑ i = 1 D x i ≥ 1 f(x) = \sum_{i=1}^D x_i^2, \quad \text{subject to } \sum_{i=1}^D x_i \geq 1 f(x)=i=1Dxi2,subject to i=1Dxi1

4.3.2 代码实现
class DifferentialEvolutionCO:
    def __init__(self, objective_func, constraints, bounds, population_size=50, max_generations=100, F=0.5, CR=0.7):
        self.objective_func = objective_func
        self.constraints = constraints
        self.bounds = bounds
        self.population_size = population_size
        self.max_generations = max_generations
        self.F = F
        self.CR = CR
        self.dimensions = len(bounds)
        self.population = None
        self.fitness = None

    def initialize_population(self):
        self.population = np.random.uniform(
            low=[b[0] for b in self.bounds],
            high=[b[1] for b in self.bounds],
            size=(self.population_size, self.dimensions)
        )
        self.fitness = np.array([self.evaluate_fitness(ind) for ind in self.population])

    def evaluate_fitness(self, individual):
        penalty = 0
        for constraint in self.constraints:
            if not constraint(individual):
                penalty += 1e6
        return self.objective_func(individual) + penalty

    def mutate(self, target_idx):
        a, b, c = np.random.choice(self.population_size, 3, replace=False)
        mutant = self.population[a] + self.F * (self.population[b] - self.population[c])
        return mutant

    def crossover(self, target_idx, mutant):
        trial = np.copy(self.population[target_idx])
        cross_points = np.random.rand(self.dimensions) <= self.CR
        cross_points[np.random.randint(self.dimensions)] = True
        trial[cross_points] = mutant[cross_points]
        return trial

    def select(self, target_idx, trial):
        trial_fitness = self.evaluate_fitness(trial)
        if trial_fitness < self.fitness[target_idx]:
            self.population[target_idx] = trial
            self.fitness[target_idx] = trial_fitness

    def run(self):
        self.initialize_population()
        best_fitness_history = []

        for generation in range(self.max_generations):
            for i in range(self.population_size):
                mutant = self.mutate(i)
                trial = self.crossover(i, mutant)
                self.select(i, trial)

            best_fitness = np.min(self.fitness)
            best_fitness_history.append(best_fitness)
            print(f"Generation {generation + 1}: Best Fitness = {best_fitness}")

        return best_fitness_history

# 定义目标函数
def objective_function(x):
    return np.sum(x**2)

# 定义约束条件
def constraint(x):
    return np.sum(x) >= 1

# 定义边界
bounds = [(-5.12, 5.12)] * 10

# 运行算法
de_co = DifferentialEvolutionCO(objective_function, [constraint], bounds, population_size=50, max_generations=100)
best_fitness_history = de_co.run()

# 绘制优化曲线
plt.plot(best_fitness_history)
plt.title('DE Optimization Curve for Constrained Problem')
plt.xlabel('Generation')
plt.ylabel('Best Fitness')
plt.show()
C:\Users\Administrator\Documents\code\yhsf>C:/software/python39/python.exe c:/Users/Administrator/Documents/code/yhsf/demo1.py
Generation 1: Best Fitness = 32.99933243129078
Generation 2: Best Fitness = 32.99933243129078
Generation 3: Best Fitness = 32.99933243129078
Generation 4: Best Fitness = 32.99933243129078
Generation 5: Best Fitness = 32.99933243129078
Generation 6: Best Fitness = 28.869891258772203
Generation 7: Best Fitness = 28.869891258772203
Generation 8: Best Fitness = 21.579197677845894
Generation 9: Best Fitness = 21.579197677845894
Generation 10: Best Fitness = 18.781799864391274
Generation 11: Best Fitness = 18.781799864391274
Generation 12: Best Fitness = 18.781799864391274
Generation 13: Best Fitness = 18.781799864391274
Generation 14: Best Fitness = 18.781799864391274
Generation 15: Best Fitness = 12.49175493602986
Generation 16: Best Fitness = 12.49175493602986
Generation 17: Best Fitness = 12.49175493602986
Generation 18: Best Fitness = 8.697898462844833
Generation 19: Best Fitness = 8.697898462844833
Generation 20: Best Fitness = 8.697898462844833
Generation 21: Best Fitness = 7.156641746679249
Generation 22: Best Fitness = 4.264690941132197
Generation 23: Best Fitness = 4.264690941132197
Generation 24: Best Fitness = 4.264690941132197
Generation 25: Best Fitness = 4.264690941132197
Generation 26: Best Fitness = 4.264690941132197
Generation 27: Best Fitness = 4.264690941132197
Generation 28: Best Fitness = 4.264690941132197
Generation 29: Best Fitness = 4.264690941132197
Generation 30: Best Fitness = 4.264690941132197
Generation 31: Best Fitness = 2.1346101781232574
Generation 32: Best Fitness = 2.1346101781232574
Generation 33: Best Fitness = 2.1346101781232574
Generation 34: Best Fitness = 2.1346101781232574
Generation 35: Best Fitness = 2.1346101781232574
Generation 36: Best Fitness = 1.3793268597939867
Generation 37: Best Fitness = 1.3793268597939867
Generation 38: Best Fitness = 1.3793268597939867
Generation 39: Best Fitness = 1.3793268597939867
Generation 40: Best Fitness = 1.3793268597939867
Generation 41: Best Fitness = 1.3793268597939867
Generation 42: Best Fitness = 1.3793268597939867
Generation 43: Best Fitness = 1.2739335060133878
Generation 44: Best Fitness = 1.2739335060133878
Generation 45: Best Fitness = 0.4275280759003961
Generation 46: Best Fitness = 0.4275280759003961
Generation 47: Best Fitness = 0.4275280759003961
Generation 48: Best Fitness = 0.4275280759003961
Generation 49: Best Fitness = 0.4275280759003961
Generation 50: Best Fitness = 0.4275280759003961
Generation 51: Best Fitness = 0.4275280759003961
Generation 52: Best Fitness = 0.4275280759003961
Generation 53: Best Fitness = 0.4275280759003961
Generation 54: Best Fitness = 0.4275280759003961
Generation 55: Best Fitness = 0.4275280759003961
Generation 56: Best Fitness = 0.4275280759003961
Generation 57: Best Fitness = 0.4203439018696698
Generation 58: Best Fitness = 0.4203439018696698
Generation 59: Best Fitness = 0.4203439018696698
Generation 60: Best Fitness = 0.4203439018696698
Generation 61: Best Fitness = 0.4203439018696698
Generation 62: Best Fitness = 0.4203439018696698
Generation 63: Best Fitness = 0.4203439018696698
Generation 64: Best Fitness = 0.349341424777753
Generation 65: Best Fitness = 0.349341424777753
Generation 66: Best Fitness = 0.349341424777753
Generation 67: Best Fitness = 0.349341424777753
Generation 68: Best Fitness = 0.316030826335501
Generation 69: Best Fitness = 0.316030826335501
Generation 70: Best Fitness = 0.3127618615762042
Generation 71: Best Fitness = 0.29876729382262374
Generation 72: Best Fitness = 0.20016731901550422
Generation 73: Best Fitness = 0.20016731901550422
Generation 74: Best Fitness = 0.20016731901550422
Generation 75: Best Fitness = 0.20016731901550422
Generation 76: Best Fitness = 0.20016731901550422
Generation 77: Best Fitness = 0.20016731901550422
Generation 78: Best Fitness = 0.20016731901550422
Generation 79: Best Fitness = 0.1787609165963444
Generation 80: Best Fitness = 0.1787609165963444
Generation 81: Best Fitness = 0.1787609165963444
Generation 82: Best Fitness = 0.15578438434015338
Generation 83: Best Fitness = 0.15578438434015338
Generation 84: Best Fitness = 0.15578438434015338
Generation 85: Best Fitness = 0.15578438434015338
Generation 86: Best Fitness = 0.15578438434015338
Generation 87: Best Fitness = 0.15578438434015338
Generation 88: Best Fitness = 0.15578438434015338
Generation 89: Best Fitness = 0.14094610632797916
Generation 90: Best Fitness = 0.13714790188709136
Generation 91: Best Fitness = 0.13714790188709136
Generation 92: Best Fitness = 0.13714790188709136
Generation 93: Best Fitness = 0.13714790188709136
Generation 94: Best Fitness = 0.13714790188709136
Generation 95: Best Fitness = 0.13666995008436933
Generation 96: Best Fitness = 0.13666995008436933
Generation 97: Best Fitness = 0.13666995008436933
Generation 98: Best Fitness = 0.12081782531774324
Generation 99: Best Fitness = 0.12081782531774324
Generation 100: Best Fitness = 0.12081782531774324
4.3.3 流程图
初始化种群
变异
交叉
选择
是否达到最大迭代次数?
返回最优解
4.3.4 优化曲线

运行代码后,优化曲线将显示每次迭代的最优适应度值变化。
差分进化算法 (Differential Evolution) 算法详解及案例分析_第3张图片


5. 总结

差分进化算法是一种高效的全局优化算法,适用于单目标、多目标和约束优化问题。本文通过三个案例展示了差分进化算法在不同问题中的应用,并提供了完整的代码实现和优化曲线。在实际应用中,差分进化算法的性能依赖于参数设置,需要通过实验进行调整。


6. 参考文献

  1. Storn, R., & Price, K. (1997). Differential Evolution – A Simple and Efficient Heuristic for Global Optimization over Continuous Spaces. Journal of Global Optimization, 11(4), 341-359.
  2. Das, S., & Suganthan, P. N. (2011). Differential Evolution: A Survey of the State-of-the-Art. IEEE Transactions on Evolutionary Computation, 15(1), 4-31.
  3. Price, K., Storn, R. M., & Lampinen, J. A. (2005). Differential Evolution: A Practical Approach to Global Optimization. Springer.
  4. Zhang, J., & Sanderson, A. C. (2009). JADE: Adaptive Differential Evolution with Optional External Archive. IEEE Transactions on Evolutionary Computation, 13(5), 945-958.

你可能感兴趣的:(python,算法,python,开发语言,选择,DE,差分进化算法,变异)