欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python演化計(jì)算基準(zhǔn)函數(shù)詳解

 更新時(shí)間:2021年10月25日 14:29:44   作者:Robin-hlt  
這篇文章主要介紹了Python演化計(jì)算基準(zhǔn)函數(shù),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧,希望能夠給你帶來幫助

基準(zhǔn)函數(shù)是測(cè)試演化計(jì)算算法性能的函數(shù)集,由于大部分基準(zhǔn)函數(shù)集都是C/C++編寫,Python編寫的基準(zhǔn)函數(shù)比較少,因此本文實(shí)現(xiàn)了13個(gè)常用基準(zhǔn)函數(shù)的Python版。

基準(zhǔn)函數(shù)定義

在這里插入圖片描述

代碼實(shí)現(xiàn)

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]

調(diào)用方法

輸入為向量x和函數(shù)編號(hào)func_num

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

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • python實(shí)現(xiàn)信號(hào)時(shí)域統(tǒng)計(jì)特征提取代碼

    python實(shí)現(xiàn)信號(hào)時(shí)域統(tǒng)計(jì)特征提取代碼

    今天小編就為大家分享一篇python實(shí)現(xiàn)信號(hào)時(shí)域統(tǒng)計(jì)特征提取代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python判斷一個(gè)數(shù)是否能被另一個(gè)整數(shù)整除的實(shí)例

    python判斷一個(gè)數(shù)是否能被另一個(gè)整數(shù)整除的實(shí)例

    今天小編就為大家分享一篇python判斷一個(gè)數(shù)是否能被另一個(gè)整數(shù)整除的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python查看列的唯一值方法

    python查看列的唯一值方法

    今天小編就為大家分享一篇python查看列的唯一值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python命令行工具Click快速掌握

    python命令行工具Click快速掌握

    這篇文章主要介紹了python命令行工具Click快速掌握,寫 Python 的經(jīng)常要寫一些命令行工具,雖然標(biāo)準(zhǔn)庫提供有命令行解析工具 Argparse,但是寫起來非常麻煩,我很少會(huì)使用它。命令行工具中用起來最爽的就是 Click,,需要的朋友可以參考下
    2019-07-07
  • python刪除列表內(nèi)容

    python刪除列表內(nèi)容

    這里給大家通過2個(gè)例子,分析了下如何使用python刪除列表內(nèi)容,也給出了思路,分別通過pop和remove方法來實(shí)現(xiàn),有相關(guān)需求的小伙伴可以參考下。
    2015-08-08
  • 使用Python實(shí)現(xiàn)從零開始打造一個(gè)三維繪圖系統(tǒng)

    使用Python實(shí)現(xiàn)從零開始打造一個(gè)三維繪圖系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)一個(gè)繪圖系統(tǒng),通過指定x,y,z的表達(dá)式,以實(shí)現(xiàn)三維繪圖的目的,感興趣的可以了解下
    2024-02-02
  • pyqt5 QListWidget的用法解析

    pyqt5 QListWidget的用法解析

    這篇文章主要介紹了pyqt5 QListWidget的用法解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • python比較2個(gè)xml內(nèi)容的方法

    python比較2個(gè)xml內(nèi)容的方法

    這篇文章主要介紹了python比較2個(gè)xml內(nèi)容的方法,涉及Python操作XML文件的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python實(shí)現(xiàn)發(fā)送與接收郵件的方法詳解

    Python實(shí)現(xiàn)發(fā)送與接收郵件的方法詳解

    這篇文章主要介紹了Python實(shí)現(xiàn)發(fā)送與接收郵件的方法,結(jié)合實(shí)例形式分析了Python基于smtplib庫使用SMTP協(xié)議進(jìn)行郵件發(fā)送及基于poplib庫使用POP3服務(wù)器接收郵件的相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • 基于Python實(shí)現(xiàn)用戶管理系統(tǒng)

    基于Python實(shí)現(xiàn)用戶管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了基于Python實(shí)現(xiàn)用戶管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02

最新評(píng)論