---
title: "Optimizing with Jaya: A Vignette"
output: rmarkdown::html_vignette
vignette: >
 %\VignetteIndexEntry{Jaya_Usage}
 %\VignetteEngine{knitr::rmarkdown}
 %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# 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:

```{r setup}
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

```{r single-optimization, fig.width=7, fig.height=6}
# 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)

# 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

```{r multi-optimization, fig.width=7, fig.height=6}
# 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)


# 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:

```{r early-stopping}
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)
```

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