智能算法测试函数可视化

1 智能算法测试函数可视化(二维)

1.1 1 Sphere 函数

\[f(x) = \sum^n_{i=1}x_i^2\] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)

#############################
#### 二维函数 --- 可视化
##############################

## 1 、 Sphere 函数
f1_Sphere2 = function(x,y){
  return(x^2 + y^2)
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f1_Sphere2)
library(plotly)
## 载入需要的程辑包:ggplot2
## 
## 载入程辑包:'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(widgetframe)
## 载入需要的程辑包:htmlwidgets
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

## 2 Schwefel 函数 \[f(x) = \sum^n_i|x_i| + \prod^n_i|x_i|\] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)

## 2、Schwefel 函数
f2_Schwefel2 = function(x,y){
  sum(abs(x),abs(y)) + abs(x)*abs(y)
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_Schwefel2)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

1.2 3 Rosenbrock 函数

\[f(x) = \sum^n_{i=1} \left( 100 ( x_{i+1}- x_i^2 )^2+ (x_i -1)^2 \right)\] 全局最优点为\(x =(1,1,\cdots ,1 ),f(x) =0\)

## 3、Rosenbrock 函数
f2_Rosenbrock2 = function(x,y){
  100*(y-x^2)^2 + (x-1)^2
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_Rosenbrock2)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

1.3 4、Rastrigin

\[f(x) = \sum_{i=1}^n \left( x_i^2 - 10cos(2\pi x_i) +10 \right)\] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)

## 4、Rastrigin
f2_Rastrigin2 = function(x,y){
   x^2-10*cos(2*pi*x)+10 + y^2-10*cos(2*pi*y)+10
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_Rastrigin2)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

1.4 5 Griewank 函数

\[f(x) = \frac{1}{4000}\sum_{i=1}^n x_i^2 -\prod^n_{i=1}cos\left( \frac{x_i}{\sqrt{i}}\right) +1\] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)

## 5/Griewank 函数
f2_Griewank = function(x,y){
  1/4000 *(x^2+y^2)-cos(x/1)*cos(y/sqrt(2))+1
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_Griewank)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

1.5 6 Ackley 函数

\[f(x) = -20 Exp \left(-0.2 \times \sqrt{\frac{1}{30} \sum_{i=1}^n x_i^2 } \right) - Exp \left(\sqrt{\frac{1}{30} \cos(2 \pi x_i) } \right) +20 +e\] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)

## 6 Ackley 函数
f2_Ackley2 = function(x,y){
  -20*exp(-0.2*sqrt(1/30 * (x^2+y^2))) -exp(1/30*(cos(2*pi*x)+cos(2*pi*y)))+20+exp(1)
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_Ackley2)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

1.6 7 Noise函数

\[f(x) = \sum_{i=1}^nix_i^4 + rand[0,1)\] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)

## 7 、Noise函数
f2_Niose2 = function(x,y){
  x^4+2*y^2 + rnorm(1,0,1)
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_Niose2)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)

## 8 shaffer函数 \[f(x) = \frac{\sin^2(\sqrt{x_i^2 +x_2^2}) - 0.5 }{[1+0.001(x_1+x_2)]^2} - 0.5 \] 全局最优点为\(x =(0,0,\cdots ,0 ),f(x) = -1\)

## 8/shaffer函数
f2_shaffer2 = function(x,y){
  (sin(x^2+y^2)^2 -0.5)/(1+0.001*(x^2+y^2))^2 - 0.5
}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f2_shaffer2)
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)
sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.5
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] widgetframe_0.3.1 htmlwidgets_1.5.1 plotly_4.9.2.1    ggplot2_3.3.2    
## 
## loaded via a namespace (and not attached):
##  [1] pillar_1.4.6      compiler_4.0.2    tools_4.0.2       digest_0.6.25    
##  [5] viridisLite_0.3.0 jsonlite_1.7.0    evaluate_0.14     lifecycle_0.2.0  
##  [9] tibble_3.0.3      gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.7      
## [13] crosstalk_1.1.0.1 yaml_2.2.1        blogdown_0.20     xfun_0.17        
## [17] withr_2.2.0       stringr_1.4.0     dplyr_1.0.1       httr_1.4.2       
## [21] knitr_1.29        generics_0.0.2    vctrs_0.3.2       grid_4.0.2       
## [25] tidyselect_1.1.0  glue_1.4.1        data.table_1.13.0 R6_2.4.1         
## [29] rmarkdown_2.3     bookdown_0.20     farver_2.0.3      tidyr_1.1.1      
## [33] purrr_0.3.4       magrittr_1.5      scales_1.1.1      ellipsis_0.3.1   
## [37] htmltools_0.5.0   colorspace_1.4-1  stringi_1.4.6     lazyeval_0.2.2   
## [41] munsell_0.5.0     crayon_1.3.4

次;