[Python科学计算] 基于geatpy实现差分算法DE 多种变异策略

本文介绍了如何使用geatpy这个高性能Python遗传算法工具箱实现DE/rand/1, DE/best/1和DE/currentToBest/1三种差分进化算法,并与手工实现的PSO算法进行对比。通过Weierstrass Function作为测试函数,展示了算法的运行过程和结果。完整代码可在作者的GitHub上找到。" 46906215,2700825,线段树详解:构建与分解策略,"['数据结构', '算法', '线性数据结构', '树形结构']
摘要由CSDN通过智能技术生成

代码拆解

geatpy简介:一个高性能Python遗传算法工具箱,提供了面向对象的进化算法框架。pip install geatpy即可安装。官网: geatpy.com

本文实现DE/rand/1, DE/best/1和DE/currentToBest/1三种变异策略的算法对比,以及上文中手工实现的PSO算法进行对比。python手工实现PSO算法传送门:

https://blog.csdn.net/hush19/article/details/114407925

以Weierstrass Function作为测试函数。有关DE原理及其变异策略的内容在我之前的文章有写,传送门:

https://blog.csdn.net/hush19/article/details/113649013

下图为Weierstrass Function的三维图像

Part 1: 引包

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@FileName: Weierstrass Function.PY
@Abstract: Evaluate performance of PSO, DE/rand/1, DE/best/1, DE/CTB/1 based on Weierstrass Function
@Time: 2021/03/06 00:54:08
@Requirements: numpy, geatpy, pandas, matplotlib
@Author: WangZy ntu.wangzy@gmail.com
@Version: -
'''

import numpy as np
import geatpy as ea
import matplotlib.pyplot as plt
import pandas as pd
from PSOKW import PSOKW

Part 2: 定义存储结果的变量

rand1_recorder = [] #每次运行结果的最优解
best1_recorder = []
ctbest_recorder = []
pso_recorder = []
rand1_avg_recorder = [] #每次运行中每一代的均值结果
rand1_best_recorder = [] #每次运行中每一代的最佳结果
best1_avg_recorder = []
best1_best_recorder = []
ctbest_avg_recorder = []
ctbest_best_recorder = []
pso_best_recorder = []

Part 3: 问题定义

class MyProblem(ea.Problem):  # Define the Problem
    def __init__(self):
        name = 'Weierstrass Function'  # Initialize name
        M = 1  # Dimension of Objective
        maxormins = [1]  # 1: Maximum, 2: Minimum
        Dim = 10  # Dimension of Decision Variables
        varTypes = [0] * Dim  # 1: Discrete, 0: Continuous
        lb = [-0.5]*Dim  # Low Bound
        ub = [0.5]*Dim  # Up Bound
        lbin = [1]*Dim
        ubin = lbin
        ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)

    def aimFunc(self, pop):  # Objective Function
        x = pop.Phen  
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是基于精英保留策略差分进化算法Python程序示例: ```python import random # 定义目标函数,这里以简单的二元函数为例 def target_function(x): return x[0] ** 2 + x[1] ** 2 # 定义差分进化算法函数 def differential_evolution(population_size, dimensions, bounds, max_iter): # 初始化种群 population = [] for i in range(population_size): individual = [] for j in range(dimensions): individual.append(random.uniform(bounds[j][0], bounds[j][1])) population.append(individual) # 迭代更新 for i in range(max_iter): new_population = [] # 遍历种群中的每个个体 for j in range(population_size): # 选择三个不同的随机个体 indices = [index for index in range(population_size) if index != j] a, b, c = population[random.choice(indices)], population[random.choice(indices)], population[random.choice(indices)] # 变异操作 mutant = [a[j] + 0.5 * (b[j] - c[j]) for j in range(dimensions)] # 交叉操作 trial = [] for j in range(dimensions): if random.random() < 0.5: trial.append(mutant[j]) else: trial.append(population[j][j]) # 选择操作 if target_function(trial) < target_function(population[j]): new_population.append(trial) else: new_population.append(population[j]) # 更新种群 new_population.sort(key=target_function) population = new_population[:int(population_size / 2)] + population[int(population_size / 2):] # 返回最优解 population.sort(key=target_function) return population[0] # 测试程序 bounds = [(-5, 5), (-5, 5)] # 定义搜索空间 result = differential_evolution(population_size=100, dimensions=2, bounds=bounds, max_iter=1000) print("最优解:", result) print("最优目标函数值:", target_function(result)) ``` 在上述程序中,我们定义了一个目标函数 `target_function`,它接受一个二元元组并返回一个标量值。然后,我们定义了一个 `differential_evolution` 函数,它接受四个参数:种群大小、搜索空间维度、搜索空间边界和最大迭代次数。该函数实现差分进化算法的主要逻辑,包括初始化种群、迭代更新、变异操作、交叉操作和选择操作。最后,我们在程序末尾测试了该算法实现效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值