pygasolver 0.5.10
Genetic Algorithm solver with full customization.
License: MIT

# PyGASolver

[![PyPI](https://badge.fury.io/py/pygasolver.svg)](https://pypi.org/project/pygasolver) [![Downloads](https://pepy.tech/badge/pygasolver)](https://pypi.python.org/pypi/pygasolver) [![Downloads Month](https://pepy.tech/badge/pygasolver/month)](https://pypi.python.org/pypi/pygasolver)

<p align="center">
  <img width="460" height="300" src="https://raw.githubusercontent.com/fernaper/pygasolver/master/logo.svg">
</p>

## Introduction

**PyGASolver** is a really **powerful** library, with high **customization** possibilities, **optimized** to use the maximum number of cores,
**easy** to use (most parameters are configured with default values that allow you to get a very good performance without too much effort),
includes **testing** systems for practically all the code (code coverage of approximately 99%) and **typing** on almost all the code
(so it is easy to read and self explanatory).
The development of this library is very fast and we are always ready to implement new features that you may need.

The main goal is that you can focus on **solving your problem** and forget about the most basic parts of the implementation of genetic algorithms.

## Why use this library?

Before working on this library I wanted to **solve a complex problem** of genetic algorithms to train **neural networks**.
I found that all the existing libraries were too difficult to use, or, on the other hand, they were too simple and did not allow customization.

However, this library was built in a **very well structured** way, **any programmer can easily understand** how to proceed,
and which parts needs to be implemented.

At the same time **all methods can be overwritten**, or **classes can be inherited** to create new functionalities.

Last but not least, most genetic algorithm libraries forgot a very important part of the creation of these systems,
the evaluation of the generated population in general terms and progress by epochs.
In this library we have implemented a system that allows you to **obtain the performance per generation of the best, worst and average individuals**.
It also allows you to visualize all the information using both `matplotlib` and `plotly` graphs.

All of these features are included but it does not mean that the system requires all of these libraries to work.
The only necessary library is  `numpy`, the other libraries are only necessary if you activate the modules that require them.

These are:

- **tqdm**: If you want to display the time per generation, and the estimated time remaining.
- **pandas**: If you want to store your evolution in pandas DataFrame's. Also needed if you want to plot `matplotlib` or `plotly` charts.
- **matplotlib**: If you want to plot with this library.
- **plotly**: If you want to plot with this library.

## Installation

We encourage you to install the library using pip:

```bash
pip install pygasolver
```

The lastet version can be installed with:

```bash
pip install git+https://github.com/fernaper/pygasolver
```

If you wish to build from sources, download or clone the repository and type:

```bash
python setup.py install
```

## Requirements

The most basic features of PyGASolver requires `Python 3.7` or greater and `numpy`.
In order to visualize the results we recommend `matplotlib` or `plotly`.

## Examples

The example files can be found in the [samples](https://github.com/fernaper/pygasolver/tree/master/samples) directory.

You can see how to impement the Onemax problem optimization with Genetic Algorithms using PyGASolver:

```python
from random import randint
from pygasolver import GeneticAlgorithm, GAConfig

def crossover(parent1, parent2):
    index = randint(0, len(parent1) - 1)
    return parent1[:index] + parent2[index:], parent2[:index] + parent1[index:]

def mutate(individual):
    index = randint(0, len(individual)-1)
    individual[index] = 0 if individual[index] == 1 else 1
    return individual

ga_config = GAConfig(
    create_individual=lambda : [randint(0,1) for _ in range(10)],
    fitness=lambda individual: sum(individual),
    early_stop=lambda chromosome : chromosome.fitness == 10,
    crossover=crossover, mutate=mutate,
)
ga = GeneticAlgorithm(ga_config)
solution = ga.run()
print(solution.genes)
```

## Commercial use

If you are interested in using this product commercially or want any kind of related support, please review [this information](https://github.com/fernaper/pygasolver/blob/master/COMMERCIAL.md).

## Note

* Currently under active development.