基准函数是测试演化计算算法性能的函数集,由于大部分基准函数集都是C/C++编写,Python编写的基准函数比较少,因此本文实现了13个常用基准函数的Python版。

基准函数定义

代码实现

benchmark.py

import numpy as np
import copy
"""
Author : Robin_Hua
update time : 2021.10.27
version : 2.0
"""class Sphere:def __init__(self, x):self.x = xdef getvalue(self):res = np.sum(self.x**2)return resclass Schwefel2_22:def __init__(self, x):self.x = xdef getvalue(self):res = np.sum(np.abs(self.x)) + np.prod(np.abs(self.x))return resclass Quadric:def __init__(self, x):self.x = xdef getvalue(self):d = self.x.shape[0]res = 0for i in range(d):res = res + np.sum(self.x[:i+1])**2return resclass Noise:def __init__(self,x):self.x = xdef getvalue(self):d = self.x.shape[0]res = np.sum(np.arange(1, d + 1) * self.x ** 4) + np.random.random()return resclass Schwefel2_21:def __init__(self,x):self.x = xdef getvalue(self):res = np.max(np.abs(self.x))return resclass Step:def __init__(self,x):self.x = xdef getvalue(self):res = np.sum(np.floor(self.x + 0.5) ** 2)return resclass Rosenbrock:def __init__(self,x):self.x = xdef getvalue(self):d = self.x.shape[0]res = np.sum(np.abs(100*(self.x[1:] - self.x[:-1]**2)**2 + (1 - self.x[:-1])**2))return resclass Schwefel:def __init__(self,x):self.x = xdef getvalue(self):d = self.x.shape[0]res = 418.9829*d - np.sum(self.x * np.sin(np.sqrt(np.abs(self.x))))return resclass Rastrigin:def __init__(self,x):self.x = xdef getvalue(self):d = self.x.shape[0]res = 10 * d + np.sum(self.x ** 2 - 10 * np.cos(2 * np.pi * self.x))return resclass Ackley:def __init__(self,x):self.x = xdef getvalue(self):d = self.x.shape[0]res = - 20 * np.exp(-0.2 * np.sqrt(np.mean(self.x ** 2)))res = res - np.exp(np.mean(np.cos(2 * np.pi * self.x))) + 20 + np.exp(1)return resclass Griewank:def __init__(self,x):self.x = xdef getvalue(self):d = self.x.shape[0]i = np.arange(1, d + 1)res = 1 + np.sum(self.x ** 2) / 4000 - np.prod(np.cos(self.x / np.sqrt(i)))return resclass Generalized_Penalized:def __init__(self,x):self.x = xdef u(self,a,k,m):temp = copy.deepcopy(self.x)temp[-a <= temp.any() <= a] = 0temp[temp > a] = k*(temp[temp > a]-a)**mtemp[temp < -a] = k * (-temp[temp < -a] - a) ** m"""temp = np.zeros_like(self.x)d = self.x.shape[0]for i in range(d):if self.x[i]>a:temp[i] = k*(self.x[i]-a)**melif self.x[i]<-a:temp[i] = k * (-self.x[i] - a) ** melse:pass"""return tempdef getvalue(self):d = self.x.shape[0]y = 1+1/4*(self.x+1)res = np.pi/d*(10*np.sin(np.pi*y[0])**2+np.sum((y[:-1]-1)**2*(1+10*np.sin(np.pi*y[1:])**2))+(y[-1]-1)**2)+np.sum(self.u(10,100,4))return resdef benchmark_func(x,func_num):func = func_list[func_num]res = func(x)return resfunc_list = [Sphere,Schwefel2_22,Quadric,Schwefel2_21,Step,Noise,Rosenbrock,Schwefel,Rastrigin,Ackley,Griewank,Generalized_Penalized]

调用方法

输入为向量x和函数编号func_num

import benchmark
import numpy as np
vector = np.random.random(30)
value = benchmark.benchmark_func(x=vector,func_num=0).getvalue()

参考文献:X. Yao, Y. Liu, and G. M. Lin, “Evolutionary programming made faster,”IEEE Trans. Evol. Comput., vol. 3, no. 2, pp. 82–102, Jul. 1999.

END

演化计算基准函数(Python版)相关推荐

  1. 【LeetCode】计算最大利润(Python版)

    一.思路 根据首次买入股票的值,与后一个数据做差值,及得到其利润. 二.输出结果:7 三.代码实现 def maxProfit(prices):#最开始最大利润为0,许哟啊进行初始化maxPro = ...

  2. python 仿真实验_生成仿真的演化网络实验【Python版】

    ''' 绘制动画 ''' import numpy as np import matplotlib.pyplot as plt from matplotlib import animation imp ...

  3. 计算身体质量指数BMI(python版)

    计算BMI(Python版): class BMI: #定义BMI类def __init__(self,xm,nl,tz,sg): #初始化函数self.xm = xmself.nl = int(nl ...

  4. python中用函数设计栈的括号匹配问题_数据结构和算法(Python版):利用栈(Stack)实现括号的匹配问题...

    算法 数据结构 数据结构和算法(Python版):利用栈(Stack)实现括号的匹配问题 在平时写程序当中,我们会经常遇到程序当中括号的匹配问题,也就是在程序当中左括号的数量和右括号的数量必须相等.如 ...

  5. python栈应用_栈应用之 后缀表达式计算 (python 版)

    栈应用之 后缀表达式计算 (python 版) 后缀表达式特别适合计算机处理 1.  中缀表达式.前缀表达式.后缀表达式区别 中缀表达式:(3 - 5) * (6 + 17 * 4) / 3 17 * ...

  6. python计算gpa,Python版GPA计算器

    最近在网申投简历时遇到一个需要计算GPA的问题,想起自己在上学时写的Excel公式版GPA计算器略显low,而且操作也比较复杂,于是一时兴起,写了个Python版的,在此分享给大家! 准备工作: 用户 ...

  7. 【极坐标下牛顿—拉夫逊潮流计算(matlab版+python版)】

    程序名称## 极坐标下牛顿-拉夫逊潮流计算(matlab版+python版) 程序功能(对象) 适用于任意大小的纯交流电网,支持节点和支路的增删: 适用于接入多个风电.光伏等分布式电源: 子函数包含: ...

  8. .stl 3D模型文件的读取计算,方法和程序实现(matlab版C++版python版)

    0. 背景描述 3D模型.3D打印中很常见的一种文件格式.STL文件,其描述的主要就是其表面点所组成的三角面片的点坐标信息(vertex),和法向量(normal). 如果单纯查看的话,很多软件都可以 ...

  9. 基于直接法的诺顿谐波潮流计算(matlab版+python版)

    程序名称## 基于直接解耦法的诺顿谐波潮流计算(matlab版+python版) 程序功能(对象) 适用于任意大小的纯交流电网,支持节点和支路的增删: 适用于接入多个风电.光伏等分布式电源: 将DG和 ...

最新文章

  1. layui父页面调用子页面的渲染_layui的iframe父子操作方法
  2. 销售行业ERP数据统计分析都有哪些维度?
  3. 轻量级c语言开发环境,几款轻量级的C/C++编写软件
  4. 【openGL基础系列】之画一个正方体玩玩吧
  5. 一般家用路由器买多大的合适_家用路由器多少兆合适
  6. 【WZOI】默写数字
  7. 捋一捋dubbo配置
  8. ffmpeg项目编译出错问题解决方案.
  9. 练习---打印出电影天堂中电影的下载链接
  10. UVa 10387 Billiard
  11. 【视频分享】尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇
  12. 【元宇宙欧米说】从GameFi的视角讨论Web2到Web3的利弊
  13. 各种数学平均数之间的关系
  14. $().each() 与 $.each()区别
  15. Visual Studio 2010已安装,sql server 2008 management studio安装教程
  16. PHP单例模式与常驻内存(总结)
  17. 1044 Shopping in Mars(柳神39行代码+详细注释)
  18. ffmpeg裁剪合并视频
  19. 《2022微隔离技术与安全用例研究报告》发布
  20. /proc/uptime文件中的内容

热门文章

  1. MongoDB文档查询操作(一)
  2. CRMEB :成功申报中国科协开源评选
  3. 计算机sci一区影响因子,SCI一区期刊及影响因子.pdf
  4. 2019年天津大学计算机专业本校保研经验帖
  5. Mac自动隐藏/显示程序坞有时延解决办法
  6. 2016年,都有哪些企业进入了美国的储能业务“角斗场”
  7. 正版软件 Windows系统、Office 软件、Microsoft 365 合集
  8. 采集碰到的一大堆麻烦事
  9. leetcode专题训练 77. Combinations
  10. 用auto.js写了一个抖音点赞、关注的脚本