本文主要是介绍[Python科学计算] 基于geatpy实现差分算法DE 多种变异策略,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码拆解
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 Problemdef __init__(self):name = 'Weierstrass Function' # Initialize nameM = 1 # Dimension of Objectivemaxormins = [1] # 1: Maximum, 2: MinimumDim = 10 # Dimension of Decision VariablesvarTypes = [0] * Dim # 1: Discrete, 0: Continuouslb = [-0.5]*Dim # Low Boundub = [0.5]*Dim # Up Boundlbin = [1]*Dimubin = lbinea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)
这篇关于[Python科学计算] 基于geatpy实现差分算法DE 多种变异策略的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!