您现在的位置是:群英 > 开发技术 > Python语言
Python中如何实现基准函数及调用?一文带你看懂
Admin发表于 2021-12-29 18:29:571124 次浏览

    这篇文章给大家分享的是关于基准函数的内容,基准函数是测试演化计算算法性能的函数集,大部分基准函数集都是C/C++编写,Python编写的基准函数比较少,对此本文就给大家分享一些常遇到基准函数的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有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: 基准函数
相关信息推荐
2022-11-01 17:32:47 
摘要:这篇文章介绍了WPF绘制矢量图形模糊问题的解决方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
2022-05-06 18:01:04 
摘要:php数组的键名不可以重复。在php中,数组的键名(key)是唯一的、是不会重复存在的,即使声明两个一样的键名,后一个声明的键名也会覆盖前一个键名;利用这一特性,可以使用array_flip()反转两次数组的键名和键值,实现数组去重效果。
2022-08-27 17:03:53 
摘要:将所有的资源(assets)归拢在一起后,还需要告诉 webpack 在哪里打包应用程序。webpack 的 output 属性描述了如何处理归拢在一起的代码(bundled code)。下面本篇文章就来带大家深入了解一下webpack核心概念中的输出(Output),希望对大家有所帮助!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部