Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
/* ****************************************** * Simple Genetic Algorithm * ****************************************** */
#include <stdlib.h>
#include <stdio.h>
#include 
#include <math.h>
#include <conio.h>

#define RaND_MAX 0x7FFFFFFF
#define random(num) (rand()%(num))
#define randomize() (srand((unsigned)time(NULL)))
#define POPULATION_SIZE 4
#define CHROM_LENGTH 4
#define PCROSS 0.6
#define PMUT 0.050
#define MAX_GEN 3
struct population
{
int value;
double gain;
unsigned char string[CHROM_LENGTH];
unsigned int fitness;
};
struct population pool[POPULATION_SIZE];
struct population new_pool[POPULATION_SIZE];
int selected[POPULATION_SIZE];
int generations;
/******************************************** encode ** Code a integer into binary string ********************************************/
void encode(int index, int value)
{
int i;
for (i=0; i < CHROM_LENGTH; i++)
pool[index].string[CHROM_LENGTH-1-i] = (value >> i) & 0x01;    /* bit wise and */

}

/******************************************** initialise_population ** Creates and initialize a population ********************************************/
void initialise_population()
{
int i;
void randomize();
for (i=0; i < POPULATION_SIZE; i++)
{
encode(i, random(2^CHROM_LENGTH));
printf("the value of i is %d \n",i);
}

}
/******************************************** select ** Selects strings for reproduction ********************************************/
int select(double sum_fitness)
{
int i;
int j;
double r, parsum;
parsum = 0;
j= rand ();
printf("the value of j is %d \n",j);
r = (double)(j % (int)sum_fitness); /* spin the roulette */
for (i=0; i < POPULATION_SIZE, parsum <= r; i++)
parsum += pool[i].fitness;
return (--i); /* returns a selected string */
}
/******************************************** flip ** Toss a biased coin ********************************************/
int flip(double prob)
{
double i;
i=((double)rand())/RaND_MAX;
if ((prob == 1.0) || (i < prob))
return (1);
else
return (0);
}

/******************************************** crossover ** Swaps 2 sub-strings ********************************************/
void crossover(int parent1, int parent2, int child1, int child2)
{
int i, site;
if (flip(PCROSS))
site = random(CHROM_LENGTH);
else
site = CHROM_LENGTH-1;
for ( int i=0;  i < CHROM_LENGTH; i++)
{
if ((i <= site) || (site==0))
{
new_pool[child1].string[i] = pool[parent1].string[i];
new_pool[child2].string[i] = pool[parent2].string[i];
}else
{
new_pool[child1].string[i] = pool[parent2].string[i];
new_pool[child2].string[i] = pool[parent1].string[i];
}
}
}
/******************************************** mutation ** Changes the values of string position ********************************************/
void mutation ()
{
int i, j;
for (i=0; i < POPULATION_SIZE; i++)
{
for (j=0; j < CHROM_LENGTH; j++)
 if (flip(PMUT))
pool[i].string[j] = ~new_pool[i].string[j] & 0x01;
else
pool[i].string[j] = new_pool[i].string[j] & 0x01;
}
}
/******************************************** decode ** Decode a binary string into an integer ********************************************/
int decode(int index)
{
int i, value;
value = 0;
for (i=0; i < CHROM_LENGTH; i++)
value += (int)pow(2.0,(double)i) * pool[index].string[CHROM_LENGTH-1-i];
return(value);
}
/******************************************** evaluate ** Objective function f(x)=x^2 ********************************************/
int evaluate(int value)
{
	double gain,gm2,gm6,i5;
	gain=(2*gm2*gm6)/(i5*0.0000004311);
return(gain);
}
/******************************************** statistics ** Print entermediary results ********************************************/
void statistics()
{
int i, j;
double gain;
printf("\n;Generation: %d\n;Selected Strings\n;", generations);
for (i=0; i< POPULATION_SIZE; i++)
printf(" %d", selected[i]);
 printf("\n");
printf("\n;X\tf(x)\t New_String\tX' \t gain");
for (i=0; i< POPULATION_SIZE; i++)
{
printf("\n %d\t%u\t;", pool[i].value, pool[i].fitness);
for (j=0; j<chrom_length;>printf(" %d",pool[i].string[j]);
printf("\t%d", decode(i));
printf("\t%g",gain);
}
}

main()
{
int i;
double sum_fitness, avg_fitness, old_avg_fitness;
generations = 1;
avg_fitness = 1;
initialise_population();
do
{
old_avg_fitness = avg_fitness;
sum_fitness = 0;        /* fitness evaluation */
double gm2,gm6,i5;
printf("enter the values of gm2 \n gm6 \n i5");
scanf("%g %g %g", &gm2,&gm6,&i5);
for (i=0; i<population_size;>{
pool[i].gain = decode(i);
pool[i].value=pool[i].gain;
pool[i].fitness = evaluate(pool[i].gain);
sum_fitness += pool[i].fitness;
}
avg_fitness = sum_fitness / POPULATION_SIZE;
printf("the sum of fitness is %g \n",sum_fitness);
for (i=0; i<population_size;>selected[i] = select(sum_fitness);
for (i=0; i<population_size;>crossover(selected[i],selected[i+1],i,i+1);
mutation();
statistics();
printf ("\nImprovment: %f\n", avg_fitness/old_avg_fitness);
}
while ((++generations < MAX_GEN) &&
((avg_fitness/old_avg_fitness) > 1.005) ||                 /* logical or */
((avg_fitness/old_avg_fitness) < 1.0));
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 9-Jun-15 8:01am
v2
Comments
Member 11724421 9-Jun-15 13:20pm    
Please help me with this genetic algo code !
Sergey Alexandrovich Kryukov 9-Jun-15 13:48pm    
This is not "code". Did you notice that is starts with
#include
#include
?

I understand that this is just the artifact of wrong HTML formatting, but who will fix it for you?
—SA
[no name] 9-Jun-15 13:49pm    
You have not provided any kind of a description of a problem and we are not your code debugging service.
KarstenK 9-Jun-15 13:50pm    
You should learn to use the debugger and fill your code with some trace output.
OriginalGriff 9-Jun-15 14:05pm    
I have added a code block to fix the HTML #include problem, and preserve your formatting... except you don't have any.

Go back to your code, and indent it correctly so it is easier to read. Then edit your question and post the relevant code fragments only, indented so we can read them (if you look at the question while you edit it, you will see what I did to it - do that to yours as well).

And then explain exactly what happens, what you have to do to make it happen, and what you expect to happen.
And then add what you have done to see if you can fix it.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

C++
for (i=0; i< POPULATION_SIZE; i++)
{
    printf("\n %d\t%u\t;", pool[i].value, pool[i].fitness);
    for (j=0; j<chrom_length;>printf(" %d",pool[i].string[j]);
    printf("\t%d", decode(i));
    printf("\t%g",gain);
}


there is an error on the fourth line. what are you trying to do?
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900