10000 Update README.md · imjoy/GeneticAlgorithmPython@ce4e8e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce4e8e2

Browse files
authored
Update README.md
1 parent 4a4bf55 commit ce4e8e2

File tree

1 file changed

+66
-22
lines changed

1 file changed

+66
-22
lines changed

README.md

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ All the parameters and functions passed to the GA class constructor are used as
6666
- `mutation`: Refers to the method that applies the mutation operator based on the selected type of mutation in the `mutation_type` property.
6767
- `select_parents`: Refers to a method that selects the parents based on the parent selection type specified in the `parent_selection_type` attribute.
6868
- `best_solutions_fitness`: A list holding the fitness values of the best solutions for all generation.
69-
- `best_solution_generation`: The generation number at which the best solution is reached. It is only assigned the generation number after the `run()` method completes. Otherwise, its value is -1.
69+
- `best_solution_generation`: The generation number at which the best fitness value is reached. It is only assigned the generation number after the `run()` method completes. Otherwise, its value is -1.
7070
- `cal_pop_fitness`: A method that calculates the fitness values for all solutions within the population by calling the function passed to the `fitness_func` parameter for each solution.
7171

7272
Next, the steps of using the PyGAD library are discussed.
@@ -76,12 +76,12 @@ Next, the steps of using the PyGAD library are discussed.
7676
To use PyGAD, here is a summary of the required steps:
7777

7878
1. Preparing the `fitness_func` parameter.
79-
2. Preparing other parameters.
80-
3. Example of preparing the parameters.
81-
4. Import the `pygad.py` module.
82-
5. Create an instance of the `GA` class.
83-
6. Run the genetic algorithm.
84-
7. Plotting Results.
79+
2. Preparing Other Parameters.
80+
3. Import `pygad`.
81+
4. Create an Instance of the `pygad.GA` Class.
82+
5. Run the Genetic Algorithm.
83+
6. Plotting Results.
84+
7. Information about the Best Solution.
8585
8. Saving & Loading the Results.
8686

8787
Let's discuss how to do each of these steps.
@@ -118,9 +118,9 @@ The function must accept 2 parameters:
118118

119119
By creating this function, you are ready to use the library.
120120

121-
### Example of Preparing the Parameters
121+
### Preparing Other Parameters
122122

123-
Here is an example for preparing the parameters:
123+
Here is an example for preparing the other parameters:
124124

125125
```python
126126
num_generations = 50
@@ -143,7 +143,7 @@ mutation_type = "random"
143143
mutation_percent_genes = 10
144144
```
145145

146-
#### Optional `callback_generation` Parameter
146+
#### The `callback_generation` Parameter
147147

148148
In PyGAD 2.0.0 and higher, an optional parameter named `callback_generation` is supported which allow the user to call a function (with a single parameter) after each generation. Here is a simple function that just prints the current generation number and the fitness value of the best solution in the current generation. The `generations_completed` attribute of the GA class returns the number of the last completed generation.
149149

@@ -161,21 +161,21 @@ ga_instance = pygad.GA(...,
161161
...)
162162
```
163163

164-
After the parameters are prepared, we can import the `pygad` module and build an instance of the GA class.
164+
After the parameters are prepared, we can import the `pygad` and build an instance of the GA class.
165165

166-
### Import the `pygad.py` Module
166+
### Import the `pygad`
167167

168-
The next step is to import the `pygad` module as follows:
168+
The next step is to import `pygad` as follows:
169169

170170
```python
171171
import pygad
172172
```
173173

174-
This module has a class named `GA` which holds the implementation of all methods for running the genetic algorithm.
174+
PyGAD has a class named `GA` which holds the implementation of all methods for running the genetic algorithm.
175175

176-
### Create an Instance of the `GA` Class.
176+
### Create an Instance of the `pygad.GA` Class.
177177

178-
The `GA` class is instantiated where the previously prepared parameters are fed to its constructor. The constructor is responsible for creating the initial population.
178+
The `pygad.GA` class is instantiated where the previously prepared parameters are fed to its constructor. The constructor is responsible for creating the initial population.
179179

180180
```python
181181
ga_instance = pygad.GA(num_generations=num_generations,
@@ -217,20 +217,40 @@ ga_instance.plot_result()
217217

218218
![Fig02](https://user-images.githubusercontent.com/16560492/78830005-93111d00-79e7-11ea-9d8e-a8d8325a6101.png)
219219

220+
### Information about the Best Solution
221+
222+
The following information about the best solution in the last population is be returned using the `best_solution()` method.
223+
224+
- Solution
225+
- Fitness value of the solution
226+
- Index of the solution within the population
227+
228+
```python
229+
solution, solution_fitness, solution_idx = ga_instance.best_solution()
230+
print("Parameters of the best solution : {solution}".format(solution=solution))
231+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
232+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
233+
```
234+
235+
Using the `best_solution_generation` attribute of the instance from the `pygad.GA` class, the generation number at which the **best fitness** is reached could be fetched.
236+
237+
```python
238+
if ga_instance.best_solution_generation != -1:
239+
print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))
240+
```
241+
220242
### Saving & Loading the Results
221243

222-
After the `run()` method completes, it is possible to save the current instance of the genetic algorithm to avoid losing the progress made. The `save()` method is available for that purpose. According to the next code, a file named `genetic.pkl` will be created and saved in the current directory.
244+
After the `run()` method completes, it is possible to save the current instance of the genetic algorithm to avoid losing the progress made. The `save()` method is available for that purpose. Just pass the file name to it without an extension. According to the next code, a file named `genetic.pkl` will be created and saved in the current directory.
223245

224246
```python
225-
# Saving the GA instance.
226-
filename = 'genetic' # The filename to which the instance is saved. The name is without extension.
247+
filename = 'genetic'
227248
ga_instance.save(filename=filename)
228249
```
229250

230251
You can also load the saved model using the `load()` function and continue using it. For example, you might run the genetic algorithm for a number of generations, save its current state using the `save()` method, load the model using the `load()` function, and then call the `run()` method again.
231252

232253
```python
233-
# Loading the saved GA instance.
234254
loaded_ga_instance = pygad.load(filename=filename)
235255
```
236256

@@ -313,14 +333,38 @@ print("Best solution reached after {best_solution_generation} generations.".form
313333
To start with coding the genetic algorithm, you can check the tutorial titled [**Genetic Algorithm Implementation in Python**](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) available at these links:
314334

315335
- https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad
316-
317336
- https://towardsdatascience.com/genetic-algorithm-implementation-in-python-5ab67bb124a6
337+
- https://www.kdnuggets.com/2018/07/genetic-algorithm-implementation-python.html
318338

319339
[This tutorial](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) is prepared based on a previous version of the project but it still a good resource to start with coding the genetic algorithm.
320340

321341
![Fig03](https://user-images.githubusercontent.com/16560492/78830052-a3c19300-79e7-11ea-8b9b-4b343ea4049c.png)
322342

323-
You can also check my book cited as [**Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7**](https://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665).
343+
Get started with the genetic algorithm by reading the tutorial titled [**Introduction to Optimization with Genetic Algorithm**](https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad) which is available at these links:
344+
345+
* https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad
346+
* https://www.kdnuggets.com/2018/03/introduction-optimization-with-genetic-algorithm.html
347+
* https://towardsdatascience.com/introduction-to-optimization-with-genetic-algorithm-2f5001d9964b
348+
349+
![Introduction to Genetic Algorithm](https://user-images.githubusercontent.com/16560492/82078259-26252d00-96e1-11ea-9a02-52a99e1054b9.jpg)
350+
351+
Read about building neural networks in Python through the tutorial titled [**Artificial Neural Network Implementation using NumPy and Classification of the Fruits360 Image Dataset**](https://www.linkedin.com/pulse/artificial-neural-network-implementation-using-numpy-fruits360-gad) available at these links:
352+
353+
* https://www.linkedin.com/pulse/artificial-neural-network-implementation-using-numpy-fruits360-gad
354+
* https://towardsdatascience.com/artificial-neural-network-implementation-using-numpy-and-classification-of-the-fruits360-image-3c56affa4491
355+
* https://www.kdnuggets.com/2019/02/artificial-neural-network-implementation-using-numpy-and-image-classification.html
356+
357+
![Building Neural Networks Python](https://user-images.githubusercontent.com/16560492/82078281-30472b80-96e1-11ea-8017-6a1f4383d602.jpg)
358+
359+
Read about training neural networks using the genetic algorithm through the tutorial titled [**Artificial Neural Networks Optimization using Genetic Algorithm with Python**](https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad) available at these links:
360+
361+
- https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad
362+
- https://towardsdatascience.com/artificial-neural-networks-optimization-using-genetic-algorithm-with-python-1fe8ed17733e
363+
- https://www.kdnuggets.com/2019/03/artificial-neural-networks-optimization-genetic-algorithm-python.html
364+
365+
![Training Neural Networks using Genetic Algorithm Python](https://user-images.githubusercontent.com/16560492/82078300-376e3980-96e1-11ea-821c-aa6b8ceb44d4.jpg)
366+
367+
You can also check my book cited as [**Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7**](https://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665) which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.
324368

325369
![Fig04](https://user-images.githubusercontent.com/16560492/78830077-ae7c2800-79e7-11ea-980b-53b6bd879eeb.jpg)
326370

0 commit comments

Comments
 (0)
0