# bat-algorithm

A simple JavaScript library implementing the Bat Algorithm for optimization problems.

📑 Table of contents

🌟 Introduction

The Bat Algorithm is a nature-inspired optimization algorithm designed to solve complex optimization problems. Mimicking the echolocation behavior of bats, it balances exploration and exploitation to efficiently search for optimal solutions. In this project, we provide a simple JavaScript implementation of the Bat Algorithm, offering a user-friendly batAlgorithm() function with customizable parameters. By integrating this algorithm into your JavaScript projects, you can leverage its power to tackle a wide range of optimization tasks.

⚠️ Considerations

When implementing the Bat Algorithm in JavaScript, it's essential to consider the language's suitability. Here's why JavaScript may not be the optimal choice for utilizing the Bat Algorithm:

- Numerical Computation Limitations:

- JavaScript's primary focus is web development, and it may lack specialized numerical computation capabilities compared to languages like Python with libraries such as NumPy.
- Performance limitations and a potential lack of advanced mathematical functions in JavaScript can impact complex numerical optimization tasks.

- JavaScript's ecosystem excels in web-related tasks, but it may have a smaller footprint for scientific computing and optimization algorithms.
- While there are numerical computation libraries in JavaScript, they might not offer the same functionality and performance optimizations as dedicated numerical computation libraries in other languages.

- JavaScript's dynamically typed nature can introduce challenges in handling complex numerical operations, affecting type safety and performance.
- JavaScript's single-threaded execution may not fully utilize parallel processing capabilities, potentially impacting the performance of computationally intensive tasks.

Despite these considerations, utilizing this JavaScript library offers the following advantages:

- Easy Integration: Seamlessly integrate the Bat Algorithm into existing JavaScript projects without additional language dependencies.
- Web-Based Applications: JavaScript is convenient for web-based environments or real-time interactions.
- JavaScript Proficiency: Leverage the familiarity and expertise of JavaScript, reducing development time and easing maintenance efforts.

Please assess your project requirements and consider these aspects before choosing JavaScript as the implementation language for the Bat Algorithm.

⚙️ Installation

You can install the bat-algorithm library using npm, the Node.js package manager. Before proceeding, ensure that you have Node.js and npm installed on your system.

`npm install bat-algorithm`

Once the installation is complete, you can import the library into your JavaScript project using the require or import statement:

`const { batAlgorithm } = require('bat-algorithm');`

Note: This library requires Node.js version 12 or above.

📘 Usage

To use the batAlgorithm() function from the bat-algorithm library, follow the example below:

### Example

```
const batAlgorithm = require('bat-algorithm');
// Define the cost function
function sphere(x) {
let sum = 0
for (let i = 0; i < x.length; i++) {
sum += x[i] * x[i]
}
return sum
}
// Call the batAlgorithm() function with optional parameters
const result = await batAlgorithm(sphere);
const result = await batAlgorithm(sphere, "./myFolder", 150, 10000, 100, 30, 2, 1, -10, 10, 0, 10);
```

Please ensure that you provide a valid cost function as the first argument when calling the batAlgorithm() function. Customize the optional parameters as needed to suit your optimization problem.

### Arguments

Here is a table listing the arguments that can be passed to the batAlgorithm() function, along with their default values and descriptions:

Argument | Default Value | Description |
---|---|---|

costFunc | - |
The cost function to be minimized. |

folderPath | undefined | The folder path to save the data in.(see output) |

saveRate | 100 | The rate at wich data is saved.(see output) |

maxGen | 1000 | The maximum number of generations. |

popSize | 50 | The population size. |

dimension | 30 | The dimension of the problem. |

maxLoudness | 2 | The maximum loudness value. |

maxPulseRate | 1 | The maximum pulse rate value. |

fMin | 0 | The minimum frequency value. |

fMax | 10 | The maximum frequency value. |

lowerBound | -5 | The lower bound of the search space. |

upperBound | 5 | The upper bound of the search space. |

📊 Output

This library provides an easy and accessible way to save all or part of the computed data during the execution of the Bat Algorithm. The function is asynchronous and returns a Promise, which is resolved at the end of the Bat Algorithm execution, providing an array representing the positions of the best individual at the end of the call. (see example)

Additionally, the library offers data recording functionalities. If the function receives a second parameter, it will be treated (if not undefined) as the path of the directory where the data should be saved. It is also possible to modify the "save pulse" parameter by passing a number as the third argument (optional, with a default value of 100). When a second argument is provided to the function, data will be recorded every x generations (where x is the save pulse value, which can be modified).

Each recording is saved as a JSON file, created at each recording occurrence. Therefore, multiple files are generated with a single function call. The JSON file includes essential data such as generation number, loudness array, pulse rate array, frequency array, position array, velocity array, and the best individual.

Feel free to explore and utilize these data recording capabilities to monitor and analyze the progress and results of the Bat Algorithm in your projects.

👥 Authors

Guillaume Giannantonio

🔖 References

Yang, X.-S. "A new metaheuristic bat-inspired algorithm." Nature inspired cooperative strategies for optimization (NICSO 2010). Springer Berlin Heidelberg, 2010. 65-74.