Sample program of Real Coded Genetic Algorithm
// Sample source code of Real Coded Genetic Algorithm
//
// Coded by T.Takahama
#include <stdio.h>
#include <stdlib.h>
#define GAUSSRAND
/* Random number generator in [0, 1] */
#define Rand() ((double)rand()/RAND_MAX)
/* Random integer generator in [0, n-1] */
#define RandInt(n) ((int)(rand()/(RAND_MAX+1.0)*(n)))
/* Structure of an individual */
typedef struct {
double *x; /* genes */
double f; /* fitness value */
} IndividualRec, *Individual;
Individual Best;
/*
Parameters for GA
*/
/* Number of individuals */
#define Nindividuals 50
/* Maximum number of iterations */
#define T_MAX 100
/* parameters for selection */
#define TournamentSize 5
/* parameters for crossover */
#define Pc 0.6
#define Alpha 0.366
/* parameters for mutation */
#define Pm 0.1
#define Delta_0 0.01
#define Delta_T 0.0
/*
Definitions for a problem
*/
#define BETTER(y1, y2) (y1<y2)
#define Lower -1.0
#define Upper 1.0
#define Variables 2
int NEval=0; /* number of function evaluations */
void CopyIndividual(Individual dest, Individual src);
void EvaluateIndividual(Individual P)
{
/* x1^2 + x2^2 */
P->f=P->x[0]*P->x[0]+P->x[1]*P->x[1];
if(NEval==0 || BETTER(P->f, Best->f))
CopyIndividual(Best, P);
NEval++;
}
/* Initialization of individuals: problem dependent */
/* The function returns the index of the best individual */
void Initialize(Individual P, int n)
{
int i, j;
for(i=0; i<n; i++) {
for(j=0; j<Variables; j++) /* problem dependent */
P[i].x[j]=Lower+(Upper-Lower)*Rand();
EvaluateIndividual(&P[i]);
}
}
/*
Definitions for GA
*/
/* allocate new data structures */
#define New(type, n, msg) (type *)NewCell(sizeof(type), n, msg)
void *NewCell(int size, int n, char *msg)
{
void *new;
if((new=malloc(size*n))==NULL) {
fprintf(stderr, "Cannot allocate memory for %d %s\n", n, msg);
exit(1);
}
return new;
}
/* allocate "n" new individuals */
Individual NewIndividuals(int n)
{
int i;
Individual P;
P=New(IndividualRec, n, "individuals");
for(i=0; i<n; i++) {
P[i].x=New(double, Variables, "x");
}
return P;
}
/* Copy an individual */
void CopyIndividual(Individual dest, Individual src)
{
int j;
for(j=0; j<Variables; j++)
dest->x[j]=src->x[j];
dest->f=src->f;
}
/* Print a individual */
void Print(Individual P)
{
int j;
for(j=0; j<Variables; j++)
printf("%f ", P->x[j]);
printf(" = %g\n", P->f);
}
/* selection */
int Select(Individual P, int n)
{
int i, k;
int best;
best=RandInt(n);
for(i=0; i<TournamentSize-1; i++) {
k=RandInt(n);
if(BETTER(P[k].f, P[best].f)) best=k;
}
return best;
}
/* crossover */
void Crossover(Individual new, Individual mom, Individual dad)
{
int j;
double r;
for(j=0; j<Variables; j++) {
r=Rand()*(1+2.0*Alpha)-Alpha;
new->x[j]=r*mom->x[j]+(1-r)*dad->x[j];
}
}
#ifdef GAUSSRAND
#include <math.h>
double gaussrand(void)
{
static double V1, V2, S;
static int phase = 0;
double X;
if(phase == 0) {
do {
double U1 = Rand();
double U2 = Rand();
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return(X);
}
#endif
/* mutation */
void Mutation(double x[], double delta)
{
int j;
for(j=0; j<Variables; j++)
if(Rand()<Pm)
#ifdef GAUSSRAND
x[j]+=delta*gaussrand(); /* gauss random generator */
#else
x[j]+=delta*(Rand()-0.5)*2.0; /* uniform random generator */
#endif
}
/* Genetic Algorithm */
main()
{
int t, i, j;
Individual Pop, New, Temp;
int mom, dad;
double delta;
Pop=NewIndividuals(Nindividuals);
New=NewIndividuals(Nindividuals);
Best=NewIndividuals(1);
Initialize(Pop, Nindividuals);
delta=Delta_0;
for(t=1; t<=T_MAX; t++) {
for(i=0; i<Nindividuals; i+=2) {
mom=Select(Pop, Nindividuals);
dad=Select(Pop, Nindividuals);
if(Rand()<Pc) {
Crossover(&New[i], &Pop[mom], &Pop[dad]);
Crossover(&New[i+1], &Pop[dad], &Pop[mom]);
}
else {
CopyIndividual(&New[i], &Pop[mom]);
CopyIndividual(&New[i+1], &Pop[dad]);
}
Mutation(New[i].x, delta);
Mutation(New[i+1].x, delta);
EvaluateIndividual(&New[i]);
EvaluateIndividual(&New[i+1]);
}
Temp=Pop; Pop=New; New=Temp;
delta-=(Delta_0-Delta_T)/T_MAX;
printf("%4d: ", t); Print(Best);
}
printf("Total %d evaluations\n", NEval);
}