







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture handout is from Embedded Intelligent Robotics course. It includes: Genetic Algorithm Maze Solving Program, Initializing the Population, Code, Maze Layout, Chromosome, Calculate Fitness, Checkdeath Function, Sorting the Fitness, Chromosone, Reproducing and Crossover, Swapping
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








class chromozone { public: int chromo[64]; int fitness; };
typedef vector
class GA { public: int mazeSeq[8][8]; int mazeCopy[8][8]; int mazeLoc[8][8]; int populationSize; int maxLoop; int usedSpace; float mutationRate; float mutation; int checkDeath(int X, int Y, int &nxtX, int &nxtY); void init_population(vChrom &population, vChrom &tempStore); void calc_fitness(vChrom &chromozone); void mutate(chromozone &member); void mate(vChrom &population, vChrom &tempStore); };
for(i=0; i<8; i++) { mazeSeq[i][0]=9; } for(j=0; j<8; j++) { mazeSeq[0][j]=9; } for(j=0; j<8; j++) { mazeSeq[7][j]=9; } for(i=0; i<8; i++) { mazeSeq[i][7]=9; } mazeSeq[6][1] = 9; mazeSeq[6][2] = 9; mazeSeq[6][3] = 9; mazeSeq[3][7] = 7; mazeSeq[1][7] = 8; mazeSeq[2][2] = 9; mazeSeq[3][2] = 9; mazeSeq[4][2] = 9; mazeSeq[4][3] = 9; mazeSeq[2][4] = 9; mazeSeq[3][4] = 9; mazeSeq[4][4] = 9; mazeSeq[2][5] = 9; mazeSeq[3][5] = 9; mazeSeq[4][5] = 9; mazeSeq[5][5] = 9; mazeSeq[2][6] = 9;
for(i=0; i<8; i++) { for(j=0; j<8; j++) { if(mazeSeq[i][j] == 7) { startX = i; startY = j; } } }
for(i=0; i<8; i++) { for(j=0; j<8; j++) { if(mazeSeq[i][j] == 8) { endX = i; endY = j; } } }
for(i=0; i<8; i++) { for(j=0; j<8; j++) { if(mazeSeq[i][j] == 8) { endX = i; endY = j; } } }
for(l=0; l<chromozone.size(); l++) { distT = 0; curX = startX; curY = startY-1; for(i=0; i<8; i++) { for(j=0; j<8; j++) { mazeCopy[i][j]=0; } }
for(int m=0; m<usedSpace-1;m++) { for(int x=0; x<8; x++) { for(int y=0; y<8; y++) { if(mazeLoc[x][y] == m) mazeSeq[x][y] = chromozone[l].chromo[m]; } } }
do { if(checkDeath(curX, curY, nxtX, nxtY)==1) { break; } else { mazeCopy[curX][curY] = 1; curX = nxtX; curY = nxtY; if(mazeCopy[nxtX][nxtY] == 1) { break; } distT++; } } while((curY != endY)); chromozone[l].fitness = usedSpace - distT; } }
bool sort_fitness(chromozone x, chromozone y); bool sort_fitness(chromozone x, chromozone y) { return(x.fitness < y.fitness); }
inline void sort_by_fitness(vChrom &population);
inline void sort_by_fitness(vChrom &population) { sort(population.begin(), population.end(), sort_fitness); }
inline void print_best(vChrom &dir);
inline void print_best(vChrom &dir) { cout << "Direction: " << dir[0].chromo[0] << dir[0].chromo[1] << dir[0].chromo[2] << dir[0].chromo[3] << dir[0].chromo[4] << dir[0].chromo[5] << dir[0].chromo[6] << dir[0].chromo[7] << dir[0].chromo[8] << dir[0].chromo[9] << dir[0].chromo[10] << dir[0].chromo[11] << dir[0].chromo[12] << dir[0].chromo[13] << dir[0].chromo[14] << dir[0].chromo[15] << dir[0].chromo[16] << dir[0].chromo[17] << dir[0].chromo[18] << dir[0].chromo[19] << dir[0].chromo[20] << " (" << dir[0].fitness << ") " << endl; }
void GA::mate(vChrom &population, vChrom &tempStore) { int spos, j, k; for(int i=0; i<populationSize; i++) { j = rand() % (populationSize / 3); // 3 is optimal k = rand() % (populationSize / 3); spos = rand() % usedSpace;
for(int l=0; l<spos; l++) { tempStore[i].chromo[l] = population[j].chromo[l]; }
for(int l=spos; l<usedSpace; l++) { tempStore[i].chromo[l] = population[k].chromo[l]; }
if (rand() < mutation) mutate(tempStore[i]); } }
void GA::mutate(chromozone &member) { int lowest=1, highest=4; int range=(highest-lowest)+1; int mutPos = rand() % usedSpace; int delta = lowest+int(range*rand()/(RAND_MAX + 1.0));
member.chromo[mutPos] = delta; }
int main() { GA ga; int i=0; vChrom chromoPop, chromoStore, *population, *storeData; srand(unsigned(time(NULL))); ga.init_population(chromoPop, chromoStore); population = &chromoPop; storeData = &chromoStore;
for(int i=0; i<15000; i++) { ga.calc_fitness(population); sort_by_fitness(population); print_best(*population);
if ((*population)[0].fitness == 2) break;
ga.mate(*population, *storeData); swap(population, storeData); } _getch(); return 0; }
j = rand() % (populationSize / n); // n = to the values 2, 3, 4 k = rand() % (populationSize / n);
Fitness (Spaces from finish)
Generation
Series
Fitness Value
Generation
Population / 2
Population / 3
Population / 4