/*------------------------------------------------------------- Copyright (C) 2000 Peter Clote All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. THE AUTHOR AND PUBLISHER MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. -------------------------------------------------------------*/ /* Program: genetic.c */ /* Sample program using the Population Genetics Algorithm */ /* Set includes {0,1,2,...,2^n-1} where n = NumBits */ /* Written by Rich Fiorito, modified by P. Clote, Feb 10, 1998 */ #include #include #include #include #define MAX_STEPS 10000 /* Arbitrary upper bound for convergence */ #define NumBits 16 #define MAX ((unsigned) (1< F(best)) best = bestOffspring; refresh(pop,npop); } while ((step < MAX_STEPS) && (abs(pfitness-nfitness)>change)); /* output */ printf("Last population fitness values\n\n"); printf(" INDIVIDUAL\t FITNESS\n\n"); printf("-----------------------------------------------\n"); for(i=0;i. first, the individuals are selected with probs according to their fitness. Therefore, a roulette wheele method is applied. the selected individuals are copied to population B second, choose individuals to crossover with crossover prob and copy them to the lower part of population C. Individuals, which do not crossover are copied to the upper part of C. third, individuals of the lower part of C are crossed over. Result is copied to D. */ void crossover(unsigned int A[P], unsigned int D[P], double pfitness) { static double p[P]; /* p[i] is relative fitness of i-th individual, i.e. the fitness of i divided by the population fitness */ static unsigned int B[P], C[P]; int i,j,x,y,m,M,r,co,index; unsigned int tempFirst, tempSec, temp; /* tempFirst, tempSec used to construct first, second child */ /* co is crossover point */ double q[P]; /* cumulative probabilities */ double total, z; relativefitness(A,p,pfitness); /* calculate relative fitness of the individuals */ /* determine cumulative probabilities q[i] = sum_{j<=i} p_j */ q[0]=p[0]; for (i=1;i2; index--) { x = rand()%index; temp = C[index-1]; C[index-1] = C[x]; C[x] = temp; } /* now perform crossover, using bit operations on the integer individuals */ for (y=0;y=co;j--){ tempFirst *= 2; tempSec *= 2; if ((r & C[y]) > 0) tempFirst++; if ((r & C[y+1]) > 0) tempSec++; r = r/2; } /* Put left side of Dad with right side of Mom */ for (j=co-1;j>=0;j--) { tempFirst *= 2; tempSec *= 2; if ((r & C[y+1]) > 0) tempFirst++; if ((r & C[y]) > 0) tempSec++; r = r/2; } D[y] = tempFirst; D[y+1] = tempSec; } for (y=m;y maxFitness ) { bestindex = i; maxFitness = f; } } *ptrToPopFit = sum; *ptrToIndFit = maxFitness; *ptrToBest = pop[bestindex]; } /* relativefitness(pop,p,pfitness) computes the relative fitness(fitness divided by pfitness) of each individual in pop and returns the results in array p. */ void relativefitness(unsigned int pop[P], double p[P], double pfitness) { int i; for(i=0;i0) temp++; r = r/2; /* toggle bit with probability mprob */ } pop[i]=temp; } } /* refresh copies population npop to pop */ void refresh(unsigned int pop[MAX], unsigned int npop[MAX]) { int i; for(i=0; i