Python演化计算基准函数详解

 更新时间:2021年10月25日 14:29:44   作者:Robin-hlt  
这篇文章主要介绍了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()

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • python实现信号时域统计特征提取代码

    python实现信号时域统计特征提取代码

    今天小编就为大家分享一篇python实现信号时域统计特征提取代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • python判断一个数是否能被另一个整数整除的实例

    python判断一个数是否能被另一个整数整除的实例

    今天小编就为大家分享一篇python判断一个数是否能被另一个整数整除的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • python查看列的唯一值方法

    python查看列的唯一值方法

    今天小编就为大家分享一篇python查看列的唯一值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python命令行工具Click快速掌握

    python命令行工具Click快速掌握

    这篇文章主要介绍了python命令行工具Click快速掌握,写 Python 的经常要写一些命令行工具,虽然标准库提供有命令行解析工具 Argparse,但是写起来非常麻烦,我很少会使用它。命令行工具中用起来最爽的就是 Click,,需要的朋友可以参考下
    2019-07-07
  • python删除列表内容

    python删除列表内容

    这里给大家通过2个例子,分析了下如何使用python删除列表内容,也给出了思路,分别通过pop和remove方法来实现,有相关需求的小伙伴可以参考下。
    2015-08-08
  • 使用Python实现从零开始打造一个三维绘图系统

    使用Python实现从零开始打造一个三维绘图系统

    这篇文章主要为大家详细介绍了如何使用Python实现一个绘图系统,通过指定x,y,z的表达式,以实现三维绘图的目的,感兴趣的可以了解下
    2024-02-02
  • pyqt5 QListWidget的用法解析

    pyqt5 QListWidget的用法解析

    这篇文章主要介绍了pyqt5 QListWidget的用法解析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • python比较2个xml内容的方法

    python比较2个xml内容的方法

    这篇文章主要介绍了python比较2个xml内容的方法,涉及Python操作XML文件的相关技巧,需要的朋友可以参考下
    2015-05-05
  • Python实现发送与接收邮件的方法详解

    Python实现发送与接收邮件的方法详解

    这篇文章主要介绍了Python实现发送与接收邮件的方法,结合实例形式分析了Python基于smtplib库使用SMTP协议进行邮件发送及基于poplib库使用POP3服务器接收邮件的相关操作技巧,需要的朋友可以参考下
    2018-03-03
  • 基于Python实现用户管理系统

    基于Python实现用户管理系统

    这篇文章主要为大家详细介绍了基于Python实现用户管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02

最新评论