人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2021-12-20 10:08 | 文章來源:站長之家

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

基準(zhǔn)函數(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ù)編號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)容!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部