- #1

- 146

- 2

Hi all, I understand Genetic algorithms aside form why crossover helps things, it has no guarantee of getting the best characteristics of each chromosome.

Ie say you had a genetic algorithm to calculate square roots, fitness = 1(Abs(NumToFindRoot of-(Guess*Guess))

and your guess was a binary string 00100111001 where the bottom half represents the decimals and the top half the integer part. say our initial population was

110011= 51

001010= 20

111000 =7

001011= 52

now select using tournament or roulette method:

111000 =7

001010 =20

and cross them:

111010= 23

then mutate

110010 =19

Now say we keep repeating this:

111000=7

cross->

110011=51

equals->

111111=63

mutate->

111110 31

---------

001010 20

cross

110011 51

equals

001011 52

mutate

000011 = 48

Continue repeating with population of 3, assuming we trying to find square root of 16

000011 = 48

111110 = 31

110010 = 19

----------

010010 = 18

111000 = 7

001010 = 20

---------

010000 = 2

101010 = 21

110010 =19

-------

010110 = 26

100010 = 17

010000 =2

Seems to be getting circular. Am I going about this wrong? can someone explain why random crossover can actually work?

My code just infinite loops, so i found a GA online and modified it to do this job and it still infinite loops, unless the fitness function needs to be a lot more in depth perhaps? I'm thinking my issue is that there isn't a "close to square root" because of the way im representing my number?

Ie say you had a genetic algorithm to calculate square roots, fitness = 1(Abs(NumToFindRoot of-(Guess*Guess))

and your guess was a binary string 00100111001 where the bottom half represents the decimals and the top half the integer part. say our initial population was

110011= 51

001010= 20

111000 =7

001011= 52

now select using tournament or roulette method:

111000 =7

001010 =20

and cross them:

111010= 23

then mutate

110010 =19

Now say we keep repeating this:

111000=7

cross->

110011=51

equals->

111111=63

mutate->

111110 31

---------

001010 20

cross

110011 51

equals

001011 52

mutate

000011 = 48

Continue repeating with population of 3, assuming we trying to find square root of 16

000011 = 48

111110 = 31

110010 = 19

----------

010010 = 18

111000 = 7

001010 = 20

---------

010000 = 2

101010 = 21

110010 =19

-------

010110 = 26

100010 = 17

010000 =2

Seems to be getting circular. Am I going about this wrong? can someone explain why random crossover can actually work?

My code just infinite loops, so i found a GA online and modified it to do this job and it still infinite loops, unless the fitness function needs to be a lot more in depth perhaps? I'm thinking my issue is that there isn't a "close to square root" because of the way im representing my number?

Code:

```
for (int i = 0; i <30; i++)
{
Individual indiv1 = tournamentSelection(pop);
Individual indiv2 = tournamentSelection(pop);
Individual newIndiv = crossover(indiv1, indiv2);
newPopulation.saveIndividual(i, newIndiv);
}
for (int i = elitismOffset; i < newPopulation.size(); i++)
{
mutate(newPopulation.getIndividual(i));
}
```

Last edited: