You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-37Lines changed: 33 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -1,35 +1,14 @@
1
1
# GeneticAlgorithmPython
2
2
3
-
This project implements the genetic algorithm (GA) in Python mainly using NumPy.
3
+
This project implements the genetic algorithm based on NumPy. Out of this project, a library named **PyGAD** (Decimal Genetic Algorithm) is available at [PyPI](https://pypi.org/project/pygad) at this link: https://pypi.org/project/pygad
4
4
5
-
The project has 2 main files which are:
5
+
The project has a single module named `pygad.py`which contains a class named `GA`. Simply, to run the genetic algorithm all you need to do is to create an instance of this class and pass the appropriate parameters to its constructor. This class has all the required parameters and methods for implementing the genetic algorithm.
6
6
7
-
1.`ga.py`: Holds all necessary methods for implementing the genetic algorithm inside a class named `GA`.
7
+
The documentation starts by discussing the available parameters in addition to the steps of using the library/project.
8
8
9
-
2.`example.py`: Just gives an example of how to use the project by calling the methods in the `ga.py` file.
9
+
## Supported Parameters
10
10
11
-
To test the project, you can simply run the `example.py` file.
12
-
13
-
```
14
-
python example.py
15
-
```
16
-
17
-
## How to Use the Project?
18
-
19
-
To use the project, here is the summary of the minimum required steps:
20
-
21
-
1. Prepare the required parameters.
22
-
2. Import the `ga.py` module.
23
-
3. Create an instance of the `GA` class.
24
-
4. Run the genetic algorithm.
25
-
5. Plotting Results.
26
-
6. Saving & Loading the Results.
27
-
28
-
Let's discuss how to do each of these steps.
29
-
30
-
### The Supported Parameters
31
-
32
-
The project has many parameters to allow customizing the genetic algorithm for your purpose. Before running the GA, the parameters must be prepared. The list of all supported parameters is as follows:
11
+
The single module available in the `PyGAD` library is named `pygad.py` and contains a class named `GA`. For creating an instance of this class, there are a number of parameters that allows the user to customize the genetic algorithm. Before running the GA, the parameters must be prepared. The list of all supported parameters is as follows:
33
12
34
13
-`num_generations` : Number of generations.
35
14
-`sol_per_pop` : Number of solutions (i.e. chromosomes) within the population.
@@ -46,13 +25,30 @@ The project has many parameters to allow customizing the genetic algorithm for y
46
25
-`random_mutation_min_val=-1.0` : For `random` mutation, the `random_mutation_min_val` parameter specifies the start value of the range from which a random value is selected to be added to the gene. It defaults to `-1`.
47
26
-`random_mutation_max_val=1.0` : For `random` mutation, the `random_mutation_max_val` parameter specifies the end value of the range from which a random value is selected to be added to the gene. It defaults to `+1`.
48
27
49
-
The user doesn't have to specify all of such parameters while creating an instance of the GA class. A very important parameter you must care about is `fitness_func`.
28
+
The user doesn't have to specify all of such parameters while creating an instance of the GA class. A very important parameter you must care about is `fitness_func` which defines the fitness function.
29
+
30
+
Next, the steps of using the PyGAD library are discussed.
31
+
32
+
## How to Use the PyGAD?
33
+
34
+
To use PyGAD, here is a summary of the required steps:
35
+
36
+
1. Preparing the `fitness_func` parameter.
37
+
2. Preparing other parameters.
38
+
3. Example of preparing the parameters.
39
+
4. Import the `pygad.py` module.
40
+
5. Create an instance of the `GA` class.
41
+
6. Run the genetic algorithm.
42
+
7. Plotting Results.
43
+
8. Saving & Loading the Results.
44
+
45
+
Let's discuss how to do each of these steps.
50
46
51
47
### Preparing the `fitness_func` Parameter
52
48
53
49
Even there are a number of steps in the genetic algorithm pipeline that can work the same regardless of the problem being solved, one critical step is the calculation of the fitness value. There is no unique way of calculating the fitness value and it changes from one problem to another.
54
50
55
-
On **`15 April 2020`**, a new argument named `fitness_func` is added that allows the user to specify a custom function to be used as a fitness function. This function must be a **maximization function** so that a solution with a high fitness value returned is selected compared to a solution with a low value. Doing that allows the user to freely use the project to solve any problem by passing the appropriate fitness function.
51
+
On **`15 April 2020`**, a new argument named `fitness_func` is added that allows the user to specify a custom function to be used as a fitness function. This function must be a **maximization function** so that a solution with a high fitness value returned is selected compared to a solution with a low value. Doing that allows the user to freely use the library to solve any problem by passing the appropriate fitness function.
56
52
57
53
Let's discuss an example:
58
54
@@ -73,9 +69,9 @@ def fitness_func(solution):
73
69
return fitness
74
70
```
75
71
76
-
By creating this function, you are ready to use the project.
72
+
By creating this function, you are ready to use the library.
77
73
78
-
### Parameters Example
74
+
### Example of Preparing the Parameters
79
75
80
76
Here is an example for preparing the parameters:
81
77
@@ -97,14 +93,14 @@ keep_parents = 1
97
93
num_genes =len(function_inputs)
98
94
```
99
95
100
-
After the parameters are prepared, we can import the `ga` module and build an instance of the GA class.
96
+
After the parameters are prepared, we can import the `pygad` module and build an instance of the GA class.
101
97
102
-
### Import the `ga.py` Module
98
+
### Import the `pygad.py` Module
103
99
104
-
The next step is to import the `ga` module as follows:
100
+
The next step is to import the `pygad` module as follows:
105
101
106
102
```python
107
-
importga
103
+
importpygad
108
104
```
109
105
110
106
This module has a class named `GA` which holds the implementation of all methods for running the genetic algorithm.
The project supports different types for selecting the parents and applying the crossover & mutation operators.
177
+
The library supports different types for selecting the parents and applying the crossover & mutation operators. More features will be added in the future. To ask for a feature, please open an issue in the [GitHub project](https://github.com/ahmedfgad/GeneticAlgorithmPython) of the library: https://github.com/ahmedfgad/GeneticAlgorithmPython
182
178
183
179
The supported crossover operations at this time are:
184
180
@@ -202,7 +198,7 @@ The supported parent selection techniques at this time are:
202
198
- Random
203
199
- Tournament
204
200
205
-
More types will be added in the future. You can also ask for supporting more types by opening an issue in the project.
201
+
More types will be added in the future. You can also ask for supporting more types by opening an issue in the [GitHub project](https://github.com/ahmedfgad/GeneticAlgorithmPython) associated with the library: https://github.com/ahmedfgad/GeneticAlgorithmPython
206
202
207
203
## For More Information
208
204
@@ -224,7 +220,7 @@ You can also check my book cited as [**Ahmed Fawzy Gad 'Practical Computer Visio
224
220
225
221
**Important Note**
226
222
227
-
It is important to note that this project does not implement everything in GA and there are a wide number of variations to be applied. For example, this project just uses decimal representation for the chromosome and the binary representations might be preferred for other problems.
223
+
It is important to note that this library does not implement everything in GA and there are a wide number of variations to be applied. For example, this project just uses decimal representation for the chromosome and the binary representations might be preferred for other problems.
0 commit comments