Here is the source code for my senior research project on Genetic Algorithms from back in 2000. Amazing !!
/*
Copyright 2000 Stanford Mings
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
struct generation
{
int string[5][10];
int fitness[5];
};
void showgeneration(struct generation gp);
void updatefitness(struct generation *gp);
void makepopulation(struct generation *gp);
void reproduce(struct generation *gp);
void savebest(struct generation *gp, int best[10]);
void replacebest(struct generation *gp, int best[10]);
void mutategeneration(struct generation *gp)
{
int or,rm,m,v=2,p=0,mod=0;
/*
for(m=0;m<5;m++)
{
for(v=0;v<10;v++)
{
rm=rand()%55;
// rm=1;
if ((rm==p)&&(mod<10))
{
(*gp).string[m][v]=(rm)%3;
printf(“Mutation at string #%d”,m);
printf(” and element #%d”,v);
printf(” with it being changed to %d”,rm%3);
printf(” !!\n”);
mod++;
}
p++;
}
}
*/
// srand(time(NULL));
for (p=0;p<v;p++)
{
rm=rand()%50;
mod=rand()%3;
or=(*gp).string[rm/10][rm%10];
(*gp).string[rm/10][rm%10]=mod;
// printf(“\n Printing sting %d at point %d from %d to %d”,rm/11,rm%11,or,mod);
}
// printf(“\n”);
}
void reproduce(struct generation *gp)
{
int m,v,f,g,k;
int val[5];
struct generation newgp;
val[0]=(*gp).fitness[0];
k=(*gp).fitness[0];
for(m=1;m<5;m++)
{
k+=(*gp).fitness[m];
val[m]=(*gp).fitness[m]+val[m-1];
}
for (m=0;m<5;m++)
{
g=rand()%k;
f=0;
while(val[f]<g)
f++;
for(v=0;v<10;v++)
newgp.string[m][v]=(*gp).string[f][v];
}
(*gp)=newgp;
}
void savebest(struct generation *gp,int best[10])
{
int bst,v,m,k;
bst=(*gp).fitness[0];
v=0;
for(m=1;m<5;m++)
if (bst<(*gp).fitness[m])
{
v=m;
bst=(*gp).fitness[m];
}
for (k=0;k<10;k++)
best[k]=(*gp).string[v][k];
}
void replacebest(struct generation *gp,int best[10])
{
int v,m,k,d;
v=rand()%5;
// printf(“\n Replacing %d \n”,v);
for (k=0;k<10;k++)
(*gp).string[v][k]=best[k];
}
void makepopulation(struct generation *gp)
{
int d,v,j,sum=0,m,s,p=0,rpick[5],st[5],fn[5];
struct generation gtemp=(*gp);
int newpop[5];
for(m=0;m<5;m++)
sum=sum+(*gp).fitness[m];
d=0;
j=0;
while (d<5)
{
st[d]=j;
fn[d]=j+(*gp).fitness[d];
j=(*gp).fitness[d]+j;
d++;
}
for(m=0;m<5;m++)
rpick[m]=rand()%sum;
for (m=0;m<5;m++)
for(p=0;p<5;p++)
if((rpick[m]>=st[p])&&(rpick[m]<fn[p]))
newpop[m]=p;
for (p=0;p<5;p++)
for(v=0;v<10;v++)
(*gp).string[p][v]=gtemp.string[newpop[p]][v];
/*
for(p=0;p<5;p++)
{
printf(“\n String #%d”,p);
printf(” New String #%d”,newpop[p]);
}
printf(“\n”);
scanf(“%d”,&p);
*/
updatefitness(gp);
}
void xovergeneration(struct generation *gp)
{
int stg1,stg2,xpoint,xlen,m,xver[10];
stg1=rand()%5;
stg2=rand()%5;
while (stg2==stg1)
stg2=rand()%5;
xpoint=rand()%8;
// printf(“\n Xpoint =%d and Xlen= %d”,xpoint, xlen);
xlen=1+(rand()%8);
// printf(“\n Xpoint =%d and Xlen= %d”,xpoint, xlen);
while((xlen+xpoint)>=10)
xlen=1+rand()%8;
// printf(“\n Xpoint =%d and Xlen= %d”,xpoint, xlen);
// xpoint=2;
m=xpoint;
while(m<(xpoint+xlen))
{
xver[m]=(*gp).string[stg1][m];
m++;
}
m=xpoint;
// showgeneration(*gp);
while(m<(xlen+xpoint))
{
(*gp).string[stg1][m]=(*gp).string[stg2][m];
(*gp).string[stg2][m]=xver[m];
m++;
}
/* printf(“\n The xchange points are from element #%d”,xpoint);
printf(” to #%d”,xlen+xpoint);
printf(” for strings %d”,stg1);
printf(” and %d”,stg2);
printf(“\n\n”); */
// scanf(“%d”,stg2);
// updatefitness(gp);
// showgeneration(*gp);
}
void reproducegeneration(struct generation *gp)
{
int xpoint,m,v,hfit1,hfit2,lfit,lstring,stg1,stg2,xver[10];
hfit1=0;
hfit2=0;
lfit=22;
lstring=0;
for (m=0;m<5;m++)
{
if (hfit1<(*gp).fitness[m])
{
hfit1=(*gp).fitness[m];
stg1=m;
}
// srand(stg1);
}
stg1=rand()%5;
while(stg2==stg1)
stg2=rand()%5;
for(m=0;m<5;m++)
if (lfit>(*gp).fitness[m])
{
lfit=(*gp).fitness[m];
lstring=m;
}
/* This code here needs to be updated so that it will set the fitness to the
strings of the highest values(2) including those strings that have the
same value
*/
/* for(m=0;m<5;m++)
{
if ((hfit2<(*gp).fitness[m])&&((*gp).fitness[m]!=hfit1))
{
hfit2=(*gp).fitness[m];
stg2=m;
}
}
*/
// xpoint=rand()%10;
xpoint=5;
// printf(“\nS1 =%d”,lstring);
// printf(“\nL =%d”,lfit);
// printf(“\nXpoint=%d”,xpoint);
// printf(“Bad string = %d”,lstring);
// printf(“\n first string = %d”,stg1);
// printf(“\n second string = %d”,stg2);
//
// showgeneration((*gp));
m=0;
while(m<10)
{
if (m>xpoint)
(*gp).string[lstring][m]=(*gp).string[stg2][m];
else
(*gp).string[lstring][m]=(*gp).string[stg1][m];
m++;
}
updatefitness(gp);
// printf(“Bad string = %d”,lstring);
// printf(“\n first string = %d”,stg1);
// printf(“\n second string = %d”,stg2);
// showgeneration((*gp));
}
void showgeneration(struct generation gen)
{
int m,v;
for(m=0;m<5;m++)
// printf(“\n—————-\n”);
{
for(v=0;v<10;v++)
{
printf(“%d”,gen.string[m][v]);
printf(” “);
}
printf(“|=%d”,gen.fitness[m]);
printf(“\n”);
}
printf(“\n”);
// scanf(“%d”,&v);
}
void findgoal(struct generation gen,int s)
{
int p,m;
for(m=0;m<5;m++)
if (gen.fitness[m]>=20)
{
printf(“\n\nFINISH !!!!\n Generation =%d”,s);
printf(“\n”);
showgeneration(gen);
scanf(“%d”,&p);
}
}
void updatefitness(struct generation *gp)
{
int f,m,v;
for (m=0;m<5;m++)
{
f=0;
for (v=0;v<10;v++)
f=f+(*gp).string[m][v];
(*gp).fitness[m]=f;
}
}
struct generation populate()
{
int m,v;
struct generation popg;
for (m=0;m<5;m++)
for (v=0;v<10;v++)
// popg.string[m][v]=rand();
popg.string[m][v]=rand()%3;
return popg;
}
void main()
{
struct generation gen[5000000],geno;
int s,m,rm,in,g;
int best[10];
struct tm tim;
time_t timet;
unsigned seed;
timet=time(NULL);
// printf(“Please enter seed :%d”,timet);
// scanf(“%u”,&seed);
// seed=904;
seed=timet;
srand(seed);
gen[0]=populate();
updatefitness(&gen[0]);
// printf(“Please enter mutation factor : %d”,tim.tm_min);
// scanf(“%d”,&rm);
// rm=10;
printf(“\nEnter the # of generations (1-5000000) :”);
scanf(“%d”,&g);
// g=5000000;
printf(“\nShow generations ?(1 for yes, 0 for no) :”);
scanf(“%d”,&s);
// s=0;
printf(“\n\n Searching…….\n”);
for(m=0;m<g;m++)
{
// printf(“Generation %d”,m);
// printf(“\n”);
// showgeneration(gen[m]);
// seed=time(NULL);
// srand(seed);
// showgeneration(gen[m]);
savebest(&gen[m],best);
reproduce(&gen[m]);
updatefitness(&gen[m]);
// showgeneration(gen[m]);
// makepopulation(&gen[m]);
// updatefitness(&gen[m]);
mutategeneration(&gen[m]);
updatefitness(&gen[m]);
xovergeneration(&gen[m]);
updatefitness(&gen[m]);
replacebest(&gen[m],best);
updatefitness(&gen[m]);
// reproducegeneration(&gen[m]);
// updatefitness(&gen[m]);
if (s==1)
showgeneration(gen[m]);
// printf(“\n”);
// reproducegeneration(&gen[m]);
// makepopulation(&gen[m]);
findgoal(gen[m],m);
// printf(“\n”);
// scanf(“%d”,&in);
// printf(“\n”);
if ((m+1)!=g)
gen[m+1]=gen[m];
}
}