knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) if (!require("evaluate")) install.packages("evaluate")
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].
Jaya
package:Load Jaya
package.
library(Jaya)
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) summary(a) plot(a)
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) summary(b) plot(b)
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) summary(c) plot(c)
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) }
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
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)
[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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.