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
Adding more description of the new parameter named fitness_func that accepts a function representing the fitness function that returns a fitness value for a single solution.
Copy file name to clipboardExpand all lines: README.md
+45-18Lines changed: 45 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,9 @@
1
1
# GeneticAlgorithmPython
2
-
3
2
This project implements the genetic algorithm (GA) in Python mainly using NumPy.
4
3
5
4
The project has 2 main files which are:
6
5
7
-
1.`ga.py`: Holds all necessary methods for implementing the GA.
6
+
1.`ga.py`: Holds all necessary methods for implementing the genetic algorithm inside a class named `GA`.
8
7
9
8
2.`example.py`: Just gives an example of how to use the project by calling the methods in the `ga.py` file.
10
9
@@ -27,15 +26,15 @@ To use the project, here is the summary of the minimum required steps:
27
26
28
27
Let's discuss how to do each of these steps.
29
28
30
-
### Preparing the Parameters
29
+
### The Supported Parameters
31
30
32
31
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:
33
32
34
33
-`num_generations` : Number of generations.
35
34
-`sol_per_pop` : Number of solutions (i.e. chromosomes) within the population.
36
35
-`num_parents_mating ` : Number of solutions to be selected as parents.
37
-
-`function_inputs ` : Inputs of the function to be optimized.
38
-
-`function_output` : The output of the function to be optimized.
36
+
-`num_genes`: Number of genes in the solution/chromosome.
37
+
-`fitness_func` : A function for calculating the fitness value for each solution.
39
38
-`parent_selection_type="sss"` : The parent selection type. Supported types are `sss` (for steady state selection), `rws` (for roulette wheel selection), `sus` (for stochastic universal selection), `rank` (for rank selection), `random` (for random selection), and `tournament` (for tournament selection).
40
39
-`keep_parents=-1` : Number of parents to keep in the current population. `-1` (default) means keep all parents in the next population. `0` means keep no parents in the next population. A value `greater than 0` means keep the specified number of parents in the next population. Note that the value assigned to `keep_parents` cannot be `< - 1` or greater than the number of solutions within the population `sol_per_pop`.
41
40
-`K_tournament=3` : In case that the parent selection type is `tournament`, the `K_tournament` specifies the number of parents participating in the tournament selection. It defaults to `3`.
@@ -46,12 +45,40 @@ The project has many parameters to allow customizing the genetic algorithm for y
46
45
-`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
46
-`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
47
49
-
The user doesn't have to specify all of such parameters while creating an instance of the GA class. Here is an example for preparing such parameters:
48
+
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`.
49
+
50
+
### Preparing the `fitness_func` Parameter
51
+
52
+
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.
53
+
54
+
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.
> where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=44
61
+
> What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function.
62
+
63
+
So, the task is about using the genetic algorithm to find the best values for the 6 weight `W1` to `W6`. Thinking of the problem, it is clear that the best solution is that returning an output that is close to the desired output `y=44`. So, the fitness function should return a value that gets higher when the solution's output is closer to `y=44`. Here is a function that does that. The function must accept a single parameter which is a 1D vector representing a single solution.
50
64
51
65
```python
52
-
function_inputs = [4,-2,3.5,5,-11,-4.7]
53
-
function_output =44
66
+
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
67
+
desired_output =44# Function output.
68
+
69
+
deffitness_func(solution):
70
+
output = numpy.sum(solution*function_inputs)
71
+
fitness =1.0/ numpy.abs(output - desired_output)
72
+
return fitness
73
+
```
54
74
75
+
By creating this function, you are ready to use the project.
The project supports different types for selecting the parents and applying the crossover & mutation operators.
154
181
155
182
The supported crossover operations at this time are:
@@ -196,9 +223,9 @@ You can also check my book cited as [**Ahmed Fawzy Gad 'Practical Computer Visio
196
223
197
224
**Important Note**
198
225
199
-
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 uses decimal representation for the chromosome and the binary representations might be preferred for other problems.
226
+
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.
0 commit comments