# A guide to Jaya Package

mayur.k.shende@gmail.com neerajdhanraj@gmail.com / neerajdhanraj@eng.au.dk

## Jaya Algorithm (JA)

Jaya Algorithm is a gradient-free optimization algorithm [1]. It can be used for Maximization or Minimization of a function. It is a population based method which repeatedly modifies a population of individual solutions and capable of solving both constrained and unconstrained optimization problems. It does not contain any hyperparameters. Following examples demonstrate the performance of Jaya package. The examples are selected from the well-known report [2].

## Demonstration of Jaya package:

Load Jaya package.

library(Jaya)

### Unconstrainted Problem

Minimize, $f(x_i) = \sum_{i=1}^n x_i^2$ subject to, $-100 \le x_i \le 100$

# Test Function to minimize
square <- function(x){
return((x[1]*x[1])+(x[2]*x[2]))
}
a <- jaya(fun = square, lower = c(-100,-100), upper = c(100,100), maxiter = 50, n_var = 2, seed = 100)
#> function(x){
#>   return((x[1]*x[1])+(x[2]*x[2]))
#> }
#> <bytecode: 0x00000000141a3500>
#>                 x1           x2         f(x)
#> Best -0.0004926281 0.0002143597 2.886326e-07

summary(a)
#> Jaya Algorithm
#> Population Size      =  50
#> Number of iterations =  50
#> Number of variables  =  2
#>
#> MINIMIZE,
#> function(x){
#>   return((x[1]*x[1])+(x[2]*x[2]))
#> }
#> <bytecode: 0x00000000141a3500>
#>
#> Limits:
#> x1 = [-100,100]
#> x2 = [-100,100]
#>
#> Best Result:
#>                 x1           x2         f(x)
#> Best -0.0004926281 0.0002143597 2.886326e-07
#> Suggestions for the initial population:
#> data frame with 0 columns and 0 rows

plot(a)

### Constrainted Problem

#### g24 (Source: [2])

Minimize, $f(x) = -x_1 - x_2$ subject to, $g_1(x) = -2x_1^4 + 8x_1^3 - 8x_1^2 + x_2 - 2 \le 0 \\ g_2(x) = -4x_1^4 + 32x_1^3 - 88x_1^2 + 96x_2 +x_2 - 36 \le 0 \\ 0 \le x_1 \le 3 \\ 0 \le x_2 \le 4$

g24 <- function(x)
{
f <- f(x)
pen1 <- max(0, c1(x))
pen2 <- max(0, c2(x))
return(f + pen1 + pen2)
}

f <- function(x)
{ return(-x[1]-x[2]) }

#Constraints
c1 <- function(x)
{ return( -2*(x[1]**4) + 8*(x[1]**3) - 8*(x[1]**2) + x[2] - 2 ) }

c2 <- function(x)
{ return( -4*(x[1]**4) + 32*(x[1]**3) - 88*(x[1]**2) + 96*x[1] + x[2] -36 ) }
b <- jaya(fun = g24, lower = c(0,0), upper = c(3,4), popSize = 30, maxiter = 30, n_var = 2, seed = 100)
#> function(x)
#> {
#>   f <- f(x)
#>   pen1 <- max(0, c1(x))
#>   pen2 <- max(0, c2(x))
#>   return(f + pen1 + pen2)
#> }
#> <bytecode: 0x000000001229b950>
#>            x1       x2      f(x)
#> Best 2.329554 3.178352 -5.507887

summary(b)
#> Jaya Algorithm
#> Population Size      =  30
#> Number of iterations =  30
#> Number of variables  =  2
#>
#> MINIMIZE,
#> function(x)
#> {
#>   f <- f(x)
#>   pen1 <- max(0, c1(x))
#>   pen2 <- max(0, c2(x))
#>   return(f + pen1 + pen2)
#> }
#> <bytecode: 0x000000001229b950>
#>
#> Limits:
#> x1 = [0,3]
#> x2 = [0,4]
#>
#> Best Result:
#>            x1       x2      f(x)
#> Best 2.329554 3.178352 -5.507887
#> Suggestions for the initial population:
#> data frame with 0 columns and 0 rows

plot(b)

#### g11 (Source: [2])

Minimize, $f(x) = x_1^2 + (x_2 - 1)^2$ subject to, $h(x) = x_2 - x_1^2 = 0 \\ -1 \le x_1 \le 1 \\ -1 \le x_2 \le 1$

# Test Function to minimize
g11 <- function(x)
{
f <- f(x)
if(round(c1(x),2) != 0){
return(f + c1(x))
}
return(f)
}

f <- function(x)
{ return(x[1]**2 + (x[2] - 1)**2) }

c1 <- function(x)
{ return(x[2] - x[1]**2) }
c <- jaya(fun = g11, lower = c(-1,-1), upper = c(1,1), maxiter = 100, n_var = 2, seed = 100)
#> function(x)
#> {
#>   f <- f(x)
#>   if(round(c1(x),2) != 0){
#>     return(f + c1(x))
#>   }
#>   return(f)
#> }
#> <bytecode: 0x00000000182e7ec8>
#>            x1        x2     f(x)
#> Best 0.708031 0.5061709 0.745175

summary(c)
#> Jaya Algorithm
#> Population Size      =  50
#> Number of iterations =  100
#> Number of variables  =  2
#>
#> MINIMIZE,
#> function(x)
#> {
#>   f <- f(x)
#>   if(round(c1(x),2) != 0){
#>     return(f + c1(x))
#>   }
#>   return(f)
#> }
#> <bytecode: 0x00000000182e7ec8>
#>
#> Limits:
#> x1 = [-1,1]
#> x2 = [-1,1]
#>
#> Best Result:
#>            x1        x2     f(x)
#> Best 0.708031 0.5061709 0.745175
#> Suggestions for the initial population:
#> data frame with 0 columns and 0 rows

plot(c)

### Comparison with Genetic Algorithm (GA):

This section compares the performance of JA with GA for a contrained function discussed in [1]. For comparison purpose R package GA [3] is used.

Minimize, $f(x) = 100(x_1^2 - x_2)^2 + (1 - x_2)^2$ subject to, $x_1x_2 + x_1 - x_2 + 1.5 \le 0 \\ 10 - x_1x_2 \le 0 \\ 0 \le x_1 \le 1 \\ 0 \le x_2 \le 13$

# Function to test for
f <- function(x)
{ return( 100*((x[1]**2 - x[2])**2) + (1 - x[1])**2 ) }

# Constraints
c1 <- function(x)
{ return( (x[1]*x[2]) + x[1] - x[2] + 1.5) }

c2 <- function(x)
{ return(10 - (x[1]*x[2])) }

# Function with penalty
con <- function(x){
func <- -f(x)
pen <- sqrt(.Machine$double.xmax) pen1 <- max(0, c1(x))*pen pen2 <- max(0, c2(x))*pen return(func - pen1 - pen2) } #### For GA, library(GA) G <- ga("real-valued", fitness = con, lower = c(0,0), upper = c(1,13), maxiter = 1000, run = 200, seed = 123) # Values of x1 and x2 G@solution #> x1 x2 #> [1,] 0.8120632 12.31468 # Value of f(x) G@fitnessValue #> [1] -13584.49 #### For JA, d <- jaya(fun = con, lower = c(0,0), upper = c(1,13), maxiter = 100, n_var = 2, seed = 123, opt = "Maximize") #> function(x){ #> func <- -f(x) #> pen <- sqrt(.Machine$double.xmax)
#>   pen1 <- max(0, c1(x))*pen
#>   pen2 <- max(0, c2(x))*pen
#>   return(func - pen1 - pen2)
#> }
#> <bytecode: 0x00000000123a6170>
#>             x1      x2      f(x)
#> Best 0.8122023 12.3122 -13578.18

summary(d)
#> Jaya Algorithm
#> Population Size      =  50
#> Number of iterations =  100
#> Number of variables  =  2
#>
#> MAXIMIZE,
#> function(x){
#>   func <- -f(x)
#>   pen <- sqrt(.Machine\$double.xmax)
#>   pen1 <- max(0, c1(x))*pen
#>   pen2 <- max(0, c2(x))*pen
#>   return(func - pen1 - pen2)
#> }
#> <bytecode: 0x00000000123a6170>
#>
#> Limits:
#> x1 = [0,1]
#> x2 = [0,13]
#>
#> Best Result:
#>             x1      x2      f(x)
#> Best 0.8122023 12.3122 -13578.18
#> Suggestions for the initial population:
#> data frame with 0 columns and 0 rows

plot(d)

## References

[1] Rao, R. (2016). Jaya: A simple and new optimization algorithm for solving constrained and unconstrained optimization problems. International Journal of Industrial Engineering Computations, 7(1), 19-34.

[2] Liang, J. J., Runarsson, T. P., Mezura-Montes, E., Clerc, M., Suganthan, P. N., Coello, C. C., & Deb, K. (2006). Problem definitions and evaluation criteria for the CEC 2006 special session on constrained real-parameter optimization. Journal of Applied Mechanics, 41(8), 8-31.

[3] Scrucca, L. (2013). GA: a package for genetic algorithms in R. Journal of Statistical Software, 53(4), 1-37.