(S)crappy Genetic Algorithm! Visualise solving Styblinski–Tang function
This is the Scrappy GA algo. A quick and dirty GA written to be used for Injecting a Deep NN with Wgt and Topology updates.
It has quite a few functions that arnt used in the demo.
There are 2 functions that are viewable on the Glut interface.
I hope this GA is useful not least for its simplicity
1. random genes for each individual in population
2. select proportional to fitness 2 individuals using roullete or rank
3. Breed with crossover and mutate the offspring using probability
4. Replace parents if offspring is fitter
5. Repeat 2-4
Simple. Ive added some elitest features.
We are using 1 point crossover (look it up its the simplest)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
void crossover(int job1, int job2){ //Crossover 2
individuals
int point;
//int temp[size_];
for(int l=0;l<len;l++){
if(getlrand(0,1)<cross){
point = rand()%size_;
for(int i=0;i<point;i++){
ind[job2].gene[l][i]=ind[job1].gene[l][i];
}
for(int j=point;j<size_;j++){
ind[job1].gene[l][j]=ind[job2].gene[l][j];
}
}
}
}
|
The bin2dec function is a way to convert a binary string into a decimal
number. (Actually its not a string but an array of 0/1's)
There is a good bin2dec on [url=
https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-42.php]ww3[/url]
Ive just added some scaling for each problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
void bin2dec(data* dt,double lower,double upper){
double dec=0;
double val;
double sum=0;
for(int i=0;i<size;i++){
sum+=pow(2,i);
}
for(int j=0;j<len;j++){
val =1;
for(int i=size;i>0;i--){
if(gene[j][i] == 1){
dec += val;
}
val *=2;
}
var[j]=dt->wgts[j]=(dec/sum)*(upper-lower)+lower;
dec=0;
} }
|
Here is the Scrappy_GA code :
http://spacetripping.just4books.co.uk/HiRobot/viewtopic.php?f=7&t=85