[](https://colab.research.google.com/github/cihan-ates/data-driven-engineering/blob/master/DDE_II_Advanced_Topics/Lecture%2010/GA_I.ipynb)
%% Cell type:markdown id: tags:
“It is not the strongest of the species that survives,
not the most intelligent that survives.
It is the one that is the most adaptable to change.”
― Charles Darwin
%% Cell type:markdown id: tags:
# Important Note
Lecture notes and notebooks must not be copied and/or distributed without the express permission of ITS.
%% Cell type:markdown id: tags:
# Example I: Guessing a password
In this example, we will code a GA based function with mutations. Our objective is to guess the password, about which we know the number of characters to generate a chromosome.
%% Cell type:code id: tags:
```
# Libraries:
import datetime
import random
from bisect import bisect_left
from math import exp
```
%% Cell type:code id: tags:
```
#Step 0: decide on a fitness criteria:
#------------------------------------------
'''
Feedback score for natural selection:
Since we are trying to guess a password, we need to compare our guess with the feedback signal;
we need to create a score defining how close our guess is. Since all characters should match the true value,
we can assign 1 for each match.
*
Here we will loop over the characters. Note that our example is simple; our chromosome includes only one 'gene'.
'''
def get_fitness(genes, target):
return sum(1 for expected, actual in zip(target, genes)
if expected == actual)
```
%% Cell type:code id: tags:
```
#Step 1: create a population pool:
'''
* Our guess will be updated so population := 1
* We can consider that we have mitosis and we will immediately kill the parent.
'''
```
%%%% Output: execute_result
'\n* Our guess will be updated so population := 1\n* We can consider that we have mitosis and we will immediately kill the parent.\n'
%% Cell type:code id: tags:
```
#Step 2: create a parent pool:
'''
Creating first parents:
We will generate a sequence of random characters from our database, geneSet
Now we will move to a more difficult problem, in which it is difficult to find the global minimum or maximum point, if we do not regularize the evolutionary path.