Using a Python function with caRamel In caRamel: Automatic Calibration by Evolutionary Multi Objective Algorithm

```knitr::opts_chunk\$set(echo = TRUE)
knitr::opts_chunk\$set(python.reticulate = FALSE)
```

Short Description

caRamel is a multiobjective evolutionary algorithm combining the MEAS algorithm and the NGSA-II algorithm.

```library(caRamel)
```

This example will use the reticulate package in order to call a Python function from R. Download the package from CRAN and then install and load it

```library(reticulate)
```

Kursawe test function has two objectives of three variables. This function will be written in a Python script named kursawe.py with the following content:

```{python kursawe, eval=F, echo=T} import numpy as np

def kursawe(x): k1 = -10 * np.exp(-0.2 * np.sqrt(x[0]2 + x[1]2)) - \ 10 * np.exp(-0.2 * np.sqrt(x[1]2 + x[2]2)) k2 = np.abs(x[0])0.8 + 5 * np.sin(x[0]3) + np.abs(x[1])0.8 +\ 5 * np.sin(x[1]3) + np.abs(x[2])0.8 + 5 * np.sin(x[2]3) return np.array([k1, k2])

```The Python function has to be loaded in R:

```r
use_python("/usr/local/bin/python3")
source_python("kursawe.py")
```

This function is not directly called from caRamel but with a new wrapper function and finally all can be gathered in it (recommended):

```wrapperFunction <- function(i) {
library(reticulate)
# python path
use_python("/usr/local/bin/python3")
# source the Python function
source_python("kursawe.py")
# call the Python function and return the results
return(kursawe(x[i,]))
}
```

The variables lie in the range [-5, 5]:

```nvar <- 3 # number of variables
bounds <- matrix(data = 1, nrow = nvar, ncol = 2) # upper and lower bounds
bounds[, 1] <- -5 * bounds[, 1]
bounds[, 2] <- 5 * bounds[, 2]
```

Both functions are to be minimized:

```nobj <- 2 # number of objectives
minmax <- c(FALSE, FALSE) # min and min
```

Set algorithmic parameters and launch caRamel:

```popsize <- 100 # size of the genetic population
archsize <- 100 # size of the archive for the Pareto front
maxrun <- 1000 # maximum number of calls
prec <- matrix(1.e-3, nrow = 1, ncol = nobj) # accuracy for the convergence phase

results <-
caRamel(nobj,
nvar,
minmax,
bounds,
wrapperFunction, # It's the wrapper function that will be called
popsize,
archsize,
maxrun,
prec)
```

Test if the convergence is successful and plot the optimal front:

```print(results\$success==TRUE)

plot(results\$objectives[,1], results\$objectives[,2], main="Kursawe Pareto front", xlab="Objective #1", ylab="Objective #2")
```

Finally plot the convergences of the objective functions:

```matplot(results\$save_crit[,1],cbind(results\$save_crit[,2],results\$save_crit[,3]),type="l",col=c("blue","red"), main="Convergence", xlab="Number of calls", ylab="Objectives values")
```

Try the caRamel package in your browser

Any scripts or data that you put into this service are public.

caRamel documentation built on March 18, 2022, 7:23 p.m.