|
| 1 | +[PyGAD](https://github.com/ahmedfgad/GeneticAlgorithmPython) is an open-source Python library for building the genetic algorithm and optimizing machine learning algorithms. It works with [Keras](https://keras.io) and [PyTorch](https://pytorch.org). |
| 2 | + |
| 3 | +> Try the [Optimization Gadget](https://optimgadget.com), a free cloud-based tool powered by PyGAD. It simplifies optimization by reducing or eliminating the need for coding while providing insightful visualizations. |
| 4 | +
|
| 5 | +[PyGAD](https://github.com/ahmedfgad/GeneticAlgorithmPython) supports different types of crossover, mutation, and parent selection operators. [PyGAD](https://github.com/ahmedfgad/GeneticAlgorithmPython) allows different types of problems to be optimized using the genetic algorithm by customizing the fitness function. It works with both single-objective and multi-objective optimization problems. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +*Logo designed by [Asmaa Kabil](https://www.linkedin.com/in/asmaa-kabil-9901b7b6)* |
| 10 | + |
| 11 | +Besides building the genetic algorithm, it builds and optimizes machine learning algorithms. Currently, [PyGAD](https://pypi.org/project/pygad) supports building and training (using genetic algorithm) artificial neural networks for classification problems. |
| 12 | + |
| 13 | +The library is under active development and more features added regularly. Please contact us if you want a feature to be supported. |
| 14 | + |
| 15 | +# Donation & Support |
| 16 | + |
| 17 | +You can donate to PyGAD via: |
| 18 | + |
| 19 | +- [Credit/Debit Card](https://donate.stripe.com/eVa5kO866elKgM0144): https://donate.stripe.com/eVa5kO866elKgM0144 |
| 20 | +- [Open Collective](https://opencollective.com/pygad): [opencollective.com/pygad](https://opencollective.com/pygad) |
| 21 | +- PayPal: Use either this link: [paypal.me/ahmedfgad](https://paypal.me/ahmedfgad) or the e-mail address ahmed.f.gad@gmail.com |
| 22 | +- Interac e-Transfer: Use e-mail address ahmed.f.gad@gmail.com |
| 23 | +- Buy a product at [Teespring](https://pygad.creator-spring.com/): [pygad.creator-spring.com](https://pygad.creator-spring.com) |
| 24 | + |
| 25 | +# Installation |
| 26 | + |
| 27 | +To install [PyGAD](https://pypi.org/project/pygad), simply use pip to download and install the library from [PyPI](https://pypi.org/project/pygad) (Python Package Index). The library lives a PyPI at this page https://pypi.org/project/pygad. |
| 28 | + |
| 29 | +Install PyGAD with the following command: |
| 30 | + |
| 31 | +```python |
| 32 | +pip3 install pygad |
| 33 | +``` |
| 34 | + |
| 35 | +# Quick Start |
| 36 | + |
| 37 | +To get started with [PyGAD](https://pypi.org/project/pygad), simply import it. |
| 38 | + |
| 39 | +```python |
| 40 | +import pygad |
| 41 | +``` |
| 42 | + |
| 43 | +Using [PyGAD](https://pypi.org/project/pygad), a wide range of problems can be optimized. A quick and simple problem to be optimized using the [PyGAD](https://pypi.org/project/pygad) is finding the best set of weights that satisfy the following function: |
| 44 | + |
| 45 | +``` |
| 46 | +y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + w6x6 |
| 47 | +where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=44 |
| 48 | +``` |
| 49 | + |
| 50 | +The first step is to prepare the inputs and the outputs of this equation. |
| 51 | + |
| 52 | +```python |
| 53 | +function_inputs = [4,-2,3.5,5,-11,-4.7] |
| 54 | +desired_output = 44 |
| 55 | +``` |
| 56 | + |
| 57 | +A very important step is to implement the fitness function that will be used for calculating the fitness value for each solution. Here is one. |
| 58 | + |
| 59 | +If the fitness function returns a number, then the problem is single-objective. If a `list`, `tuple`, or `numpy.ndarray` is returned, then it is a multi-objective problem (applicable even if a single element exists). |
| 60 | + |
| 61 | +```python |
| 62 | +def fitness_func(ga_instance, solution, solution_idx): |
| 63 | + output = numpy.sum(solution*function_inputs) |
| 64 | + fitness = 1.0 / numpy.abs(output - desired_output) |
| 65 | + return fitness |
| 66 | +``` |
| 67 | + |
| 68 | +Next is to prepare the parameters of [PyGAD](https://pypi.org/project/pygad). Here is an example for a set of parameters. |
| 69 | + |
| 70 | +```python |
| 71 | +fitness_function = fitness_func |
| 72 | + |
| 73 | +num_generations = 50 |
| 74 | +num_parents_mating = 4 |
| 75 | + |
| 76 | +sol_per_pop = 8 |
| 77 | +num_genes = len(function_inputs) |
| 78 | + |
| 79 | +init_range_low = -2 |
| 80 | +init_range_high = 5 |
| 81 | + |
| 82 | +parent_selection_type = "sss" |
| 83 | +keep_parents = 1 |
| 84 | + |
| 85 | +crossover_type = "single_point" |
| 86 | + |
| 87 | +mutation_type = "random" |
| 88 | +mutation_percent_genes = 10 |
| 89 | +``` |
| 90 | + |
| 91 | +After the parameters are prepared, an instance of the **pygad.GA** class is created. |
| 92 | + |
| 93 | +```python |
| 94 | +ga_instance = pygad.GA(num_generations=num_generations, |
| 95 | + num_parents_mating=num_parents_mating, |
| 96 | + fitness_func=fitness_function, |
| 97 | + sol_per_pop=sol_per_pop, |
| 98 | + num_genes=num_genes, |
| 99 | + init_range_low=init_range_low, |
| 100 | + init_range_high=init_range_high, |
| 101 | + parent_selection_type=parent_selection_type, |
| 102 | + keep_parents=keep_parents, |
| 103 | + crossover_type=crossover_type, |
| 104 | + mutation_type=mutation_type, |
| 105 | + mutation_percent_genes=mutation_percent_genes) |
| 106 | +``` |
| 107 | + |
| 108 | +After creating the instance, the `run()` method is called to start the optimization. |
| 109 | + |
| 110 | +```python |
| 111 | +ga_instance.run() |
| 112 | +``` |
| 113 | + |
| 114 | +After the `run()` method completes, information about the best solution found by PyGAD can be accessed. |
| 115 | + |
| 116 | +```python |
| 117 | +solution, solution_fitness, solution_idx = ga_instance.best_solution() |
| 118 | +print("Parameters of the best solution : {solution}".format(solution=solution)) |
| 119 | +print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness)) |
| 120 | + |
| 121 | +prediction = numpy.sum(numpy.array(function_inputs)*solution) |
| 122 | +print("Predicted output based on the best solution : {prediction}".format(prediction=prediction)) |
| 123 | +``` |
| 124 | + |
| 125 | +``` |
| 126 | +Parameters of the best solution : [3.92692328 -0.11554946 2.39873381 3.29579039 -0.74091476 1.05468517] |
| 127 | +Fitness value of the best solution = 157.37320042925006 |
| 128 | +Predicted output based on the best solution : 44.00635432206546 |
| 129 | +``` |
| 130 | + |
| 131 | +There is more to do using PyGAD. Read its documentation to explore the features of PyGAD. |
| 132 | + |
| 133 | +# PyGAD's Modules |
| 134 | + |
| 135 | +[PyGAD](https://pypi.org/project/pygad) has the following modules: |
| 136 | + |
| 137 | +1. The main module has the same name as the library `pygad` which is the main interface to build the genetic algorithm. |
| 138 | +2. The `nn` module builds artificial neural networks. |
| 139 | +3. The `gann` module optimizes neural networks (for classification and regression) using the genetic algorithm. |
| 140 | +4. The `cnn` module builds convolutional neural networks. |
| 141 | +5. The `gacnn` module optimizes convolutional neural networks using the genetic algorithm. |
| 142 | +6. The `kerasga` module to train [Keras](https://keras.io) models using the genetic algorithm. |
| 143 | +7. The `torchga` module to train [PyTorch](https://pytorch.org) models using the genetic algorithm. |
| 144 | +8. The `visualize` module to visualize the results. |
| 145 | +9. The `utils` module contains the operators (crossover, mutation, and parent selection) and the NSGA-II code. |
| 146 | +10. The `helper` module has some helper functions. |
| 147 | + |
| 148 | +The documentation discusses these modules. |
| 149 | + |
| 150 | +# PyGAD Citation - Bibtex Formatted |
| 151 | + |
| 152 | +If you used PyGAD, please consider citing its paper with the following details: |
| 153 | + |
| 154 | +``` |
| 155 | +@article{gad2023pygad, |
| 156 | + title={Pygad: An intuitive genetic algorithm python library}, |
| 157 | + author={Gad, Ahmed Fawzy}, |
| 158 | + journal={Multimedia Tools and Applications}, |
| 159 | + pages={1--14}, |
| 160 | + year={2023}, |
| 161 | + publisher={Springer} |
| 162 | +} |
| 163 | +``` |
| 164 | + |
0 commit comments