Python演化计算基准函数详解
作者:Robin-hlt 发布时间:2021-02-13 19:55:32
标签:Python,计算,函数,详解
基准函数是测试演化计算算法性能的函数集,由于大部分基准函数集都是C/C++编写,Python编写的基准函数比较少,因此本文实现了13个常用基准函数的Python版。
基准函数定义
代码实现
benchmark.py
import numpy as np
import copy
"""
Author : Robin_Hua
update time : 2021.10.14
version : 1.0
"""
class Sphere:
def __init__(self, x):
self.x = x
def getvalue(self):
res = np.sum(self.x**2)
return res
class Schwefel2_22:
def __init__(self, x):
self.x = x
def getvalue(self):
res = np.sum(np.abs(self.x)) + np.prod(np.abs(self.x))
return res
class Noise:
def __init__(self,x):
self.x = x
def getvalue(self):
d = self.x.shape[0]
res = np.sum(np.arange(1, d + 1) * self.x ** 4) + np.random.random()
return res
class Schwefel2_21:
def __init__(self,x):
self.x = x
def getvalue(self):
res = np.max(np.abs(self.x))
return res
class Step:
def __init__(self,x):
self.x = x
def getvalue(self):
res = np.sum(int(self.x + 0.5) ** 2)
return res
class Rosenbrock:
def __init__(self,x):
self.x = x
def 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 res
class Schwefel:
def __init__(self,x):
self.x = x
def getvalue(self):
d = self.x.shape[0]
res = 418.9829*d - np.sum(self.x * np.sin(np.sqrt(np.abs(self.x))))
return res
class Rastrigin:
def __init__(self,x):
self.x = x
def getvalue(self):
d = self.x.shape[0]
res = 10 * d + np.sum(self.x ** 2 - 10 * np.cos(2 * np.pi * self.x))
return res
class Ackley:
def __init__(self,x):
self.x = x
def 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 res
class Griewank:
def __init__(self,x):
self.x = x
def 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 res
class Generalized_Penalized:
def __init__(self,x):
self.x = x
def u(self,a,k,m):
temp = copy.deepcopy(self.x)
temp[-a <= temp.any() <= a] = 0
temp[temp > a] = k*(temp[temp > a]-a)**m
temp[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)**m
elif self.x[i]<-a:
temp[i] = k * (-self.x[i] - a) ** m
else:
pass
"""
return temp
def 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 res
def benchmark_func(x,func_num):
func = func_list[func_num]
res = func(x)
return res
func_list = [Sphere,Schwefel2_22,Noise,Schwefel2_21,Step,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()
来源:https://blog.csdn.net/Robin_hlt/article/details/120941732
0
投稿
猜你喜欢
- 今天的问题是请问以下 alert 弹出值分别是什么?var f = function f2()&nb
- 本篇阅读的代码片段来自于30-seconds-of-python。1、average_bydef average_by(lst, fn=la
- 例子:Response.Cookies("letwego")("visiter")="84
- 喜欢用Python写脚本的小伙伴可以跟着一起写一写呀。编写环境:Python2.x00x1:需要用到的模块需要用到的模块如下:import
- ChatGPT 是 OpenAI 开发的 GPT(Generative Pre-trained Transformer)语言模型的变体。它是
- 用CSV格式来保存文件是个不错的主意,因为大部分程序设计语言和应用程序都能处理这种格式,所以交流起来非常方便。然而这种格式的存储效率不是很高
- 场景:把一个时间字符串转成Date,存进Mysql。时间天数会比实际时间少1天,也可能是小时少了13-14小时Mysql的时区是CST(使用
- 基本原理使用Adodb.Stream读二进制文件然后进行解析,然后返回一数组第一个元素为类型(BMP JPG PNG GIF SWF)第二个
- 如果你现在正在使用Restful API,并且你需要通过web项目来构建json格式字符串的响应,那么这篇文章将帮助你使用javascrip
- python中for循环用于针对集合中的每个元素的一个代码块,而while循环能实现满足条件下的不断运行。使用while循环时,由于whil
- 1.什么是SQL语句sql语言:结构化的查询语言。(Structured Query Language),是关系数据库管理系统的标准语言。它
- 本文实例讲述了python 实现的发送邮件模板。分享给大家供大家参考,具体如下:##发送普通txt文件(与发送html邮件不同的是邮件内容设
- 参数数量及其作用tf.layers.dense用于添加一个全连接层。函数如下:tf.layers.dense( i
- 环境准备创建QQ互联应用创建一个QQ互联应用,并获取到App ID和App Key。QQ互联官网:https://connect.qq.co
- 时间真的存在吗?有观点认为,时间只是人类构想出来的一种概念,是用来衡量事物变化的标准。对于数据库来说时间伴随着数据并进。进入MySQL时间漩
- 首先说明一下SQL Server内存占用由哪几部分组成。SQL Server占用的内存主要由三部分组成:数据缓存(Data Buffer)、
- Array.prototype中定义了很多操作数组的方法,下面介绍ECMAScript3中的一些方法1.Array.join()方法该方法将
- Dapper的简介Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,
- 前提条件,两台服务器都安装了mysql相同的版本,数据库名也一样,最好数据都是尽量的差不多。mysql服务器端 192.168.0.1: 新
- 如下所示:device = torch.device("cuda:0" if torch.cuda.is_availab