0% found this document useful (0 votes)
48 views

Forums Topic 88211 Genetic Algorithm Program

This document describes a genetic algorithm program for optimization problems. It initializes a population of chromosomes (possible solutions), calculates their fitness, selects the best ones for reproduction, randomly mutates some genes, and repeats for multiple generations to evolve better solutions. The specific problem is to maximize or minimize an objective function based on the presence or absence of ingredients in a recipe. The algorithm uses binary chromosomes to represent ingredient combinations, calculates their fitness score based on an evaluation function, and applies genetic operators like reproduction, crossover, and mutation over many generations to evolve improved recipes.

Uploaded by

milanmotta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Forums Topic 88211 Genetic Algorithm Program

This document describes a genetic algorithm program for optimization problems. It initializes a population of chromosomes (possible solutions), calculates their fitness, selects the best ones for reproduction, randomly mutates some genes, and repeats for multiple generations to evolve better solutions. The specific problem is to maximize or minimize an objective function based on the presence or absence of ingredients in a recipe. The algorithm uses binary chromosomes to represent ingredient combinations, calculates their fitness score based on an evaluation function, and applies genetic operators like reproduction, crossover, and mutation over many generations to evolve improved recipes.

Uploaded by

milanmotta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

http://www.dreamincode.

net/forums/topic/88211-genetic-algorithm-program/

view source print?


01 #include<stdio.h> 02 #include<stdlib.h> 03 04 05 06 int fitness(bool* chromosome); 07 08 int main() 09 { 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 //Reproduction } for(i=1;i<population;i++) { if(fitness(chromosome[best])<fitness(chromosome[i])) best=i; //Evaluation int best=0; //will store the index for the best in the population for(g=0;g<100;g++) { printf("generation %d\n",g); } } //initializing population for(i=0;i<population;i++) { for(j=0;j<7;j++) { //randomize the chromosome chromosome[i][j]=rand()%2; int i,j,g; //counters

int population=10; bool chromosome[10][7]; //population

1 of 2

12/11/2011 12:26 PM

http://www.dreamincode.net/forums/topic/88211-genetic-algorithm-program/

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 } 73 }

for(i=0;i<population;i++) { //to not reproduct with itself //it would be a waste of time //(although this if could introduce a break in the pipeline, well.. forget about this) if(i!=best) { for(j=0;j<7;j++) { //either the gene will be kept the same or it will be changed //to be what the best chromosome has if(rand()%2) chromosome[i][j]=chromosome[best][j]; else chromosome[i][j]=chromosome[i][j];

//mutation if(rand()%100<4) chromosome[i][j]=rand()%2;

} }

printf("best fitness %d\n",fitness(chromosome[best]));

return 0;

74 int fitness(bool* chromosome) 75 { 76 77 78 79 80 81 } // the ingredients are: // 0 1 2 3 4 5 6 salt sugar lemon egg water onion apple

return ( -chromosome[0] + chromosome[1] + chromosome[2] -chromosome[3] + chromosome[4] - chromosome[5] -chromosome[6] );

2 of 2

12/11/2011 12:26 PM

You might also like