Show the code
#############################
#### 二维函数 --- 可视化
##############################
## 1 、 Sphere 函数
f1_Sphere2 = function(x,y){
return(x^2 + y^2)
}
zsc
2019年2月21日
\[ 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)
}
Loading required package: ggplot2
Attaching package: '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
Loading required package: htmlwidgets
p = plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~z)
frameWidget(p)
\[ f(x) = \sum^n_i|x_i| + \prod^n_i|x_i| \]
全局最优点为\(x =(0,0,\cdots ,0 ),f(x) =0\)
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)
\[ 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)
\[ 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\)
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)
\[ 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\)
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)
\[ 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\)
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)
\[ 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)
\[ 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)
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.5.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] widgetframe_0.3.1 htmlwidgets_1.5.4 plotly_4.10.0 ggplot2_3.3.6
loaded via a namespace (and not attached):
[1] pillar_1.8.1 compiler_4.2.1 tools_4.2.1 digest_0.6.29
[5] viridisLite_0.4.1 jsonlite_1.8.0 evaluate_0.16 lifecycle_1.0.1
[9] tibble_3.1.8 gtable_0.3.0 pkgconfig_2.0.3 rlang_1.0.4
[13] cli_3.3.0 DBI_1.1.3 rstudioapi_0.14 crosstalk_1.2.0
[17] yaml_2.3.5 xfun_0.32 fastmap_1.1.0 httr_1.4.4
[21] withr_2.5.0 stringr_1.4.1 dplyr_1.0.9 knitr_1.40
[25] generics_0.1.3 vctrs_0.4.1 grid_4.2.1 tidyselect_1.1.2
[29] data.table_1.14.2 glue_1.6.2 R6_2.5.1 fansi_1.0.3
[33] rmarkdown_2.16.1 farver_2.1.1 tidyr_1.2.0 purrr_0.3.4
[37] magrittr_2.0.3 scales_1.2.1 htmltools_0.5.3 assertthat_0.2.1
[41] colorspace_2.0-3 utf8_1.2.2 stringi_1.7.8 lazyeval_0.2.2
[45] munsell_0.5.0
---
title: 智能算法测试函数可视化
date: '2019-02-21'
categories: matlab
---
## 智能算法测试函数可视化(二维)
### 1 Sphere 函数
$$
f(x) = \sum^n_{i=1}x_i^2
$$
全局最优点为$x =(0,0,\cdots ,0 ),f(x) =0$
```{r}
#############################
#### 二维函数 --- 可视化
##############################
## 1 、 Sphere 函数
f1_Sphere2 = function(x,y){
return(x^2 + y^2)
}
```
```{r}
y = x <- seq(-10, 10, 0.1)
z <- outer(x, y, f1_Sphere2)
library(plotly)
library(widgetframe)
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$
```{r}
## 2、Schwefel 函数
f2_Schwefel2 = function(x,y){
sum(abs(x),abs(y)) + abs(x)*abs(y)
}
```
```{r}
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)
```
### 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$
```{r}
## 3、Rosenbrock 函数
f2_Rosenbrock2 = function(x,y){
100*(y-x^2)^2 + (x-1)^2
}
```
```{r}
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)
```
### 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$
```{r}
## 4、Rastrigin
f2_Rastrigin2 = function(x,y){
x^2-10*cos(2*pi*x)+10 + y^2-10*cos(2*pi*y)+10
}
```
```{r}
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)
```
### 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$
```{r}
## 5/Griewank 函数
f2_Griewank = function(x,y){
1/4000 *(x^2+y^2)-cos(x/1)*cos(y/sqrt(2))+1
}
```
```{r}
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)
```
### 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$
```{r}
## 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)
}
```
```{r}
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)
```
### 7 Noise函数
$$
f(x) = \sum_{i=1}^nix_i^4 + rand[0,1)
$$
全局最优点为$x =(0,0,\cdots ,0 ),f(x) =0$
```{r}
## 7 、Noise函数
f2_Niose2 = function(x,y){
x^4+2*y^2 + rnorm(1,0,1)
}
```
```{r}
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$
```{r}
## 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
}
```
```{r}
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)
```
```{r}
sessionInfo()
```