---
title: "A guide to Jaya Package"
output: rmarkdown::html_vignette
author:
- name: Mayur Kishor Shende
  email: mayur.k.shende@gmail.com
- name: Neeraj Dhanraj Bokde
  email: neerajdhanraj@gmail.com / neerajdhanraj@eng.au.dk
vignette: >
  %\VignetteIndexEntry{A_guide_to_JA}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
if (!require("evaluate")) install.packages("evaluate")
```
## 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.
```{r setup}
library(Jaya)
```
### Unconstrainted Problem

Minimize,
           $$f(x_i) = \sum_{i=1}^n x_i^2$$
subject to, $$-100 \le x_i \le 100$$
```{r unconstrainted problem}
# Test Function to minimize
square <- function(x){
  return((x[1]*x[1])+(x[2]*x[2]))
}
```

```{r fig.width=7, fig.height=5}
a <- jaya(fun = square, lower = c(-100,-100), upper = c(100,100), maxiter = 50, n_var = 2, seed = 100)

summary(a)

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$$
```{r g24}
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 ) }
```

```{r fig.width=7, fig.height=5}
b <- jaya(fun = g24, lower = c(0,0), upper = c(3,4), popSize = 30, maxiter = 30, n_var = 2, seed = 100)

summary(b)

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$$
```{r g11}
# 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) }
```

```{r fig.width=7, fig.height=5}
c <- jaya(fun = g11, lower = c(-1,-1), upper = c(1,1), maxiter = 100, n_var = 2, seed = 100)

summary(c)

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$$

```{r}
# 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,
```{r GA, message=FALSE, warning=FALSE, paged.print=FALSE}
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
# Value of f(x)
G@fitnessValue
```

#### For JA,
```{r fig.width=7, fig.height=5}
d <- jaya(fun = con, lower = c(0,0),  upper = c(1,13), maxiter = 100, n_var = 2, seed = 123, opt = "Maximize")

summary(d)

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.
