# Optimizing with Jaya: A Vignette

# Introduction

The `Jaya` package provides a gradient-free optimization algorithm that can solve single-objective and multi-objective optimization problems. This vignette demonstrates how to use the `Jaya` package for optimization tasks.

## Key Features

- Single-objective optimization
- Multi-objective Pareto optimization
- Adaptive population adjustment
- Early stopping based on improvement thresholds

# Installation

Ensure the `Jaya` package is installed and loaded:

    library(Jaya)

# Single-Objective Optimization

To solve a single-objective optimization problem using `jaya`, define an objective function and specify the bounds of the variables.

## Example: Minimize Sphere Function

    # Define the sphere function
    sphere_function <- function(x) sum(x^2)

    # Set optimization parameters
    lower_bounds <- c(-5, -5, -5)
    upper_bounds <- c(5, 5, 5)

    # Run optimization
    single_result <- jaya(
      fun = sphere_function,
      lower = lower_bounds,
      upper = upper_bounds,
      popSize = 20,
      maxiter = 50,
      n_var = length(lower_bounds),
      opt = "minimize"
    )

    # Summary of results
    summary(single_result)
    #> Jaya Algorithm
    #> Population Size      = 20
    #> Number of iterations = 50
    #> Number of variables  = 3
    #>
    #> Objective: minimize
    #> Objective Function:
    #> [[1]]
    #> function (x)
    #> sum(x^2)
    #> <bytecode: 0x0000010c48162888>
    #>
    #>
    #> Limits:
    #> x1 = [-5, 5]
    #> x2 = [-5, 5]
    #> x3 = [-5, 5]
    #>
    #> Best Result:
    #>            Best.x1      Best.x2       Best.x3    Best.f.x.
    #> Best -0.0001646166 0.0001403169 -7.613403e-05 5.258384e-08

    # Plot the best value over iterations
    plot(single_result)

# Multi-Objective Optimization

To solve a multi-objective optimization problem, provide a list of objectives and specify the bounds of the variables.

## Example: Optimize Two Sphere Functions

    # Define multiple objectives
    objective1 <- function(x) sum(x^2)
    objective2 <- function(x) sum((x - 2)^2)

    # Combine objectives
    objectives <- list(objective1, objective2)

    # Run optimization
    multi_result <- jaya_multi(
      objectives = objectives,
      lower = lower_bounds,
      upper = upper_bounds,
      popSize = 30,
      maxiter = 100,
      n_var = length(lower_bounds)
    )

    # Summary of results
    summary(multi_result)
    #>              Length Class      Mode
    #> Pareto_Front 5      data.frame list
    #> Solutions    5      data.frame list

    # Pairwise plots for multi-objective Pareto front
    plot_jaya_multi_pairwise(multi_result)

# Advanced Features

## Early Stopping

To stop optimization early if the improvement falls below a threshold, use the `early_stopping` parameter:

    early_stopping_result <- jaya(
      fun = sphere_function,
      lower = lower_bounds,
      upper = upper_bounds,
      popSize = 20,
      maxiter = 50,
      n_var = length(lower_bounds),
      early_stopping = TRUE,
      tolerance = 1e-6,
      patience = 5
    )

    summary(early_stopping_result)
    #> Jaya Algorithm
    #> Population Size      = 20
    #> Number of iterations = 50
    #> Number of variables  = 3
    #>
    #> Objective: minimize
    #> Objective Function:
    #> [[1]]
    #> function (x)
    #> sum(x^2)
    #> <bytecode: 0x0000010c48162888>
    #>
    #>
    #> Limits:
    #> x1 = [-5, 5]
    #> x2 = [-5, 5]
    #> x3 = [-5, 5]
    #>
    #> Best Result:
    #>            Best.x1      Best.x2      Best.x3    Best.f.x.
    #> Best -0.0002190876 0.0005351352 0.0004002594 4.945766e-07

# Conclusion

The `Jaya` package offers a versatile and easy-to-use optimization framework for a variety of problems. Customize the algorithm parameters to fit your specific needs and leverage its advanced features for improved performance.

# References

- R. V. Rao (2016). Jaya: A Simple and New Optimization Algorithm for Solving Constrained and Unconstrained Optimization Problems. International Journal of Industrial Engineering Computations. doi:10.5267/j.ijiec.2015.8.004