0% found this document useful (0 votes)
122 views49 pages

NAME:-Sanjivini Ingle Subject: - Soft Computing Branch: - CSE Roll No: - 0191CS081069

This document contains code for experiments on soft computing concepts like the perceptron, Adaline, backpropagation neural network, and fuzzy logic operations. It includes code to initialize weights and inputs for the neural network experiments, as well as functions to perform operations like union, intersection, and complement on fuzzy sets. The code is tested and output is printed for each experiment and operation performed.

Uploaded by

Anadi Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views49 pages

NAME:-Sanjivini Ingle Subject: - Soft Computing Branch: - CSE Roll No: - 0191CS081069

This document contains code for experiments on soft computing concepts like the perceptron, Adaline, backpropagation neural network, and fuzzy logic operations. It includes code to initialize weights and inputs for the neural network experiments, as well as functions to perform operations like union, intersection, and complement on fuzzy sets. The code is tested and output is printed for each experiment and operation performed.

Uploaded by

Anadi Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

NAME:-

SANJIVINI INGLE

SUBJECT:-

SOFT COMPUTING

BRANCH:-

CSE

ROLL NO:-

0191CS081069

NAME:-

MUKESH KUMAR SANODIYA

SUBJECT:- SOFT COMPUTING


BRANCH:- CSE
ROLL NO:- 0191CS081044

EXPERIMENT NO.-1

/*PERCEPTRON*/
#include<stdio.h>
#include<conio.h>
main()
{
signed int x[4][2],tar[4];
float w[2],wc[2],out=0;
int i,j,k=0,h=0;
float s=0,b=0,bc=0,alpha=0;
float theta;
clrscr();
printf("Enter the value of theta & alpha");
scanf("%f%f",&theta,&alpha);
for(i=0;i<=3;i++)
{
printf("Enter the value of %d Inputrow & Target",i);
for(j=0;j<=1;j++)
{
scanf("%d",&x[i][j]);
}
scanf("%d",&tar[i]);
w[i]=0;
wc[i]=0;
}
printf("\Net \t Target\tWeight changes\tNew weights\t Bias changes\tBias \n");
printf("-----------------------------------------------------------------------------\n");
mew:
4

printf("ITERATION %d\n",h);
printf("----------------------------------------------------------------------------\n");
for(i=0;i<=3;i++)
{
for(j=0;j<=1;j++)
{
s+=(float)x[i][j]*w[j];
}
s+=b;
printf("%.2f\t",s);
if(s>theta)
out=1;
else if(s<-theta)
out=-1;
else
{
out=0;
}
printf("%d\t",tar[i]);
s=0;
if(out==tar[i])
{
for(j=0;j<=1;j++)
{
wc[j]=0;
bc=0;
printf("%.2f\t",wc[j]);
}
for(j=0;j<=1;j++)
printf("%.2f\t",w[j]);
5

k+=1;
b+=bc;
printf("%.2f\t\t",bc);
printf("%.2f\t",b);
}
else
{
for(j=0;j<=1;j++)
{
wc[j]=x[i][j]*tar[i]*alpha;
w[j]+=wc[j];
printf("%.2f\t",wc[j]);
wc[j]=0;
}
for(j=0;j<=1;j++)
printf("%.2f\t",w[j]);
bc=tar[i]*alpha;
b+=bc;
printf("%.2f\t\t",bc);
printf("%.2f\t",b);
}
printf("\n");
}
if(k==4)
{
printf("\nFinal weights\n");
for(j=0;j<=1;j++)
{
printf("w[%d]=%.2f\t",j,w[j]);
}
6

printf("Bias b=%.2f",b);
}
else
{
k=0;
h=h+1;
getch();
goto mew;
}
getch();
}

EXPERIMENT NO.-2

/*ADALINE*/
#include<stdio.h>
#include<conio.h>
main()
{
signed int x[4][4],tar[4];
float wc[4],w[4],e=0,er=0,yin=0,alp=0.5,b=0,bc=0,t=0;
int i,j,k,q=1;
clrscr();
for(i=0;i<=3;i++)
{
printf("\nEnter the %d row and target\t",i);
for(j=0;j<=3;j++)
{
scanf("%d",&x[i][j]);
}
scanf("%d",&tar[i]);
printf("%d",tar[i]);
w[i]=0.0;
wc[i]=0.0;
}
mew:
er=0;e=0;
yin=0;
printf("\n ITERATION%d",q);
printf("\n------------------");
8

for(i=0;i<=3;i++)
{
t=tar[i];
for(j=0;j<=3;j++)
{
yin=yin+x[i][j]*w[j];
}
b=b+bc;
yin=yin+b;
bc=0.0;
printf("\nNet=%f\t",yin);
e=(float)tar[i]-yin;
yin=0.0;
printf("Error=%f\t",e);
printf("Target=%d\t\n",tar[i]);
er=er+e*e;
for(k=0;k<=3;k++)
{
wc[k]=x[i][k]*e*alp;
w[k]+=wc[k];
wc[k]=0.0;
}
printf("Weights \t");
for(k=0;k<=3;k++)
{
printf("%f\t",w[k]);
}
bc=e*alp;
printf("b=%.2f\t",b);
getch();
9

printf("\n Error Square=%f",er);


if(er<=1.000)
{
printf("\n");
for(k=0;k<=1;k++)
printf("%f\t",w[k]);
getch();
}
else
{
e=0;
er=0;
yin=0;
q=q+1;
goto mew;
}
getch(); }}

10

EXPERIMENT NO.-3

/*BACK PROPAGATION NETWORK*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float v[2][4],w[4][1],vc[2][4],wc[4][1],de,del[4],bl,bia,bc[4],e=0;
float x[4][2],t[4],zin[4],delin[4],yin=0,y,dy,dz[4],b[4],z[4],es,alp=0.02;
int i,j,k=0,itr=0;
v[0][0]=0.1970;
v[0][1]=0.3191;
v[0][2]=-0.1448;
v[0][3]=0.3594;
v[1][0]=0.3099;
v[1][1]=0.1904;
v[1][2]=-0.0347;
v[1][3]=-0.4861;
w[0][0]=0.4919;
w[1][0]=-0.2913;
w[2][0]=-0.3979;
w[3][0]=0.3581;
b[0]=-0.3378;
b[1]=0.2771;
b[2]=0.2859;
11

b[3]=-0.3329;
bl=-0.141;
x[0][0]=-1;
x[0][1]=-1;
x[1][0]=-1;
x[1][1]=1;
x[2][0]=1;
x[2][1]=-1;
x[3][0]=1;
x[3][1]=1;
t[0]=0;
t[1]=1;
t[2]=1;
t[3]=0;
clrscr();
for(itr=0;itr<=387;itr++)
{
e=0;
es=0;
for(i=0;i<=3;i++)
{
do
{
for(j=0;j<=1;j++)
{
zin[k]+=x[i][j]*v[j][k];
}
zin[k]+=b[k];
k+=1;
}while(k<=4);
12

for(j=0;j<=3;j++)
{
z[j]=(1-exp(-zin[j]))/(1+exp(-zin[j]));
dz[j]=((1+z[j])*(1-z[j]))*0.5;
}
for(j=0;j<=3;j++)
{
yin+=z[j]*w[j][0];
}
yin+=bl;
y=(1-exp(-yin))/(1+exp(-yin));
dy=((1+y)*(1-y))*0.5;
de=(t[i]-y)*dy;
e=t[i]-y;
es+=0.5*(e*e);
for(j=0;j<=3;j++)
{
wc[j][0]=alp*de*z[j];
delin[j]=de*w[j][0];
del[j]=delin[j]*dz[j];
}
bia=alp*de;
for(k=0;k<=1;k++)
{
for(j=0;j<=3;j++)
{
vc[k][j]=alp*del[j]*x[i][k];
v[k][j]+=vc[k][j];
}
}
13

for(j=0;j<=3;j++)
{
bc[j]=alp*del[j];
w[j][0]+=wc[j][0];
b[j]+=bc[j];
}
bl+=bia;
for(j=0;j<=3;j++)
{
zin[j]=0;
z[j]=0;
dz[j]=0;
delin[j]=0;
del[j]=0;
bc[j]=0;
}
k=0;yin=0;y=0;
dy=0;bia=0;de=0;
}
printf("\nEpoch %d:\n",itr);
for(k=0;k<=1;k++)
{
for(j=0;j<=3;j++)
{
printf("%f\t",v[k][j]);
}
printf("\n");
}
printf("\n");
for(k=0;k<=3;k++)
14

{
printf("%f\t",w[k][0]);
}
printf("\n%f",bl);
printf("\t");
for(k=0;k<=3;k++)
{
printf("%f\t",b[k]);
}
getch();
}

15

EXPERIMENT NO.-4

/*BACK PROPAGATION NETWOR*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float v[2][4],w[4][1],vc[2][4],wc[4][1],de,del[4],bl,bia,bc[4],e=0;
float x[4][2],t[4],zin[4],delin[4],yin=0,y,dy,dz[4],b[4],z[4],es,alp=0.02;
int i,j,k=0,itr=0;
v[0][0]=0.1970;
v[0][1]=0.3191;
v[0][2]=-0.1448;
v[0][3]=0.3594;
v[1][0]=0.3099;
v[1][1]=0.1904;
v[1][2]=-0.0347;
v[1][3]=-0.4861;
w[0][0]=0.4919;
w[1][0]=-0.2913;
w[2][0]=-0.3979;
w[3][0]=0.3581;
b[0]=-0.3378;
b[1]=0.2771;
16

b[2]=0.2859;
b[3]=-0.3329;
bl=-0.141;
x[0][0]=-1;
x[0][1]=-1;
x[1][0]=-1;
x[1][1]=1;
x[2][0]=1;
x[2][1]=-1;
x[3][0]=1;
x[3][1]=1;
t[0]=0;
t[1]=1;
t[2]=1;
t[3]=0;
clrscr();
for(itr=0;itr<=387;itr++)
{
e=0;
es=0;
for(i=0;i<=3;i++)
{
do
{
for(j=0;j<=1;j++)
{
zin[k]+=x[i][j]*v[j][k];
}
zin[k]+=b[k];
k+=1;
17

}while(k<=4);
for(j=0;j<=3;j++)
{
z[j]=(1-exp(-zin[j]))/(1+exp(-zin[j]));
dz[j]=((1+z[j])*(1-z[j]))*0.5;
}
for(j=0;j<=3;j++)
{
yin+=z[j]*w[j][0];
}
yin+=bl;
y=(1-exp(-yin))/(1+exp(-yin));
dy=((1+y)*(1-y))*0.5;
de=(t[i]-y)*dy;
e=t[i]-y;
es+=0.5*(e*e);
for(j=0;j<=3;j++)
{
wc[j][0]=alp*de*z[j];
delin[j]=de*w[j][0];
del[j]=delin[j]*dz[j];
}
bia=alp*de;
for(k=0;k<=1;k++)
{
for(j=0;j<=3;j++)
{
vc[k][j]=alp*del[j]*x[i][k];
v[k][j]+=vc[k][j];
}
18

}
for(j=0;j<=3;j++)
{
bc[j]=alp*del[j];
w[j][0]+=wc[j][0];
b[j]+=bc[j];
}
bl+=bia;
for(j=0;j<=3;j++)
{
zin[j]=0;
z[j]=0;
dz[j]=0;
delin[j]=0;
del[j]=0;
bc[j]=0;
}
k=0;yin=0;y=0;
dy=0;bia=0;de=0;
}
printf("\nEpoch %d:\n",itr);
for(k=0;k<=1;k++)
{
for(j=0;j<=3;j++)
{
printf("%f\t",v[k][j]);
}
printf("\n");
}
printf("\n");
19

for(k=0;k<=3;k++)
{
printf("%f\t",w[k][0]);
}
printf("\n%f",bl);
printf("\t");
for(k=0;k<=3;k++)
{
printf("%f\t",b[k]);
}
getch();
}

20

EXPERIMENT NO.-5

#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<stdlib.h>

struct SET
{
float nr[5];
float dr[5];
int n;
};

typedef struct SET fuzzy;

void getval(fuzzy *m,char *x)


{
int i;
float f;
clrscr();
printf("\n Enter the %s:\n",x);
for(i=0;i<m->n;i++)
{
printf(" Numerator Element %d :",i+1);
21

scanf("%f",&f);
m->nr[i]=f;
fflush(stdin);
printf(" Denominator Element %d:",i+1);
scanf("%f",&f);
m->dr[i]=f;
}
}

void printval(fuzzy *m,char *x)


{
int i;
printf("\n %s={",x);
for(i=0;i<m->n;i++)
{
printf(" %6.2f / %6.2f",m->nr[i],m->dr[i]);
if(i!=m->n-1) putch('+');
}
printf(" }");
}

fuzzy unionset(fuzzy a,fuzzy b)


{
fuzzy temp;
char ch;
int i;
temp.n=a.n;
for(i=0;i<a.n;i++)
{
if(a.dr[i]!=b.dr[i])
22

{
printf("\n Denominators not equal");
getch();
exit(0);
}
if(a.nr[i]<b.nr[i])
temp.nr[i]=b.nr[i];
else
temp.nr[i]=a.nr[i];
temp.dr[i]=a.dr[i];
}
return temp;
}

fuzzy intersect(fuzzy a,fuzzy b)


{
fuzzy temp;
int i;
temp.n=a.n;
for(i=0;i<a.n;i++)
{
if(a.dr[i]!=b.dr[i])
{
printf("\n Denominators not equal");
getch();
exit(0);
}
if(a.nr[i]>b.nr[i])
temp.nr[i]=b.nr[i];
else
23

temp.nr[i]=a.nr[i];
temp.dr[i]=a.dr[i];
}
return temp;
}

fuzzy complement(fuzzy a)
{
fuzzy temp;
int i;
temp.n=a.n;
for(i=0;i<a.n;i++)
{
temp.nr[i]=1-a.nr[i];
temp.dr[i]=a.dr[i];
}
return temp;
}

void main()
{
fuzzy a,b,ans;
char ch;
clrscr();
printf("\n Enter the no of componets:");
scanf("%d",&a.n);
b.n=a.n;
getval(&a,"A");
getval(&b,"B");
clrscr();
24

printval(&a,"A");
printval(&b,"B");
getch();
while(1)
{
clrscr();
printf("\n Menu:\n 1.AUB\n 2.A^B\n 3.A~\n 4.B~ \n 5.Print S,A,B\n 6.Exit");
switch((ch=getch()))
{
case '1':
ans=unionset(a,b);
printval(&ans,"AUB");
getch();
break;
case '2':
ans=intersect(a,b);
printval(&ans,"A^B");
getch();
break;
case '3':
ans=complement(a);
printval(&ans,"A~");
getch();
break;
case '4':
ans=complement(b);
printval(&ans,"B~");
getch();
break;
case '5':
25

printval(&a,"A");
printval(&b,"B");
getch();
break;
case '6':
exit(0);
}
}
}

26

EXPERIMENT NO.-6

#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<stdlib.h>

struct SET
{
float nr[5];
float dr[5];
int n;
};

typedef struct SET fuzzy;

void printval(fuzzy *m,char *x)


{
int i;
printf("\n %s={",x);
for(i=0;i<m->n;i++)
{
printf(" %6.2f / %6.2f",m->nr[i],m->dr[i]);
if(i!=m->n-1) putch('+');
}
27

printf(" }");
}

fuzzy unionset(fuzzy a,fuzzy b)


{
fuzzy temp;
char ch;
int i;
temp.n=a.n;
for(i=0;i<a.n;i++)
{
if(a.dr[i]!=b.dr[i])
{
printf("\n Denominators not equal");
getch();
exit(0);
}
if(a.nr[i]<b.nr[i])
temp.nr[i]=b.nr[i];
else
temp.nr[i]=a.nr[i];
temp.dr[i]=a.dr[i];
}
return temp;
}

fuzzy intersect(fuzzy a,fuzzy b)


{
fuzzy temp;
int i;
28

temp.n=a.n;
for(i=0;i<a.n;i++)
{
if(a.dr[i]!=b.dr[i])
{
printf("\n Denominators not equal");
getch();
exit(0);
}
if(a.nr[i]>b.nr[i])
temp.nr[i]=b.nr[i];
else
temp.nr[i]=a.nr[i];
temp.dr[i]=a.dr[i];
}
return temp;
}

fuzzy complement(fuzzy a)
{
fuzzy temp;
int i;
temp.n=a.n;
for(i=0;i<a.n;i++)
{
temp.nr[i]=1-a.nr[i];
temp.dr[i]=a.dr[i];
}
return temp;
}
29

void main()
{
fuzzy a,b,ans;
char ch;
clrscr();
a.n=b.n=3;
a.nr[0]=0.1;

a.dr[0]=1;

a.nr[1]=0.2;

a.dr[1]=2;

a.nr[2]=0.3;

a.dr[2]=3;

b.nr[0]=0.4;

b.dr[0]=1;

b.nr[1]=0.3;

b.dr[1]=2;

b.nr[2]=0.2;

b.dr[2]=3;

printval(&a,"A");
printval(&b,"B");
printf("\n Menu:\n 1.AUB\n 2.A^B\n 3.A~\n 4.B~ \n 5.Print S,A,B\n 6.Exit");
while(1)
{
switch((ch=getch()))
{
case '1':
ans=unionset(a,b);
printval(&ans,"1.AUB");
break;
case '2':
ans=intersect(a,b);
printval(&ans,"2.A^B");
break;
case '3':
ans=complement(a);
30

printval(&ans,"3.A~");
break;
case '4':
ans=complement(b);
printval(&ans,"4.B~");
break;
case '5':
printval(&a,"A");
printval(&b,"B");
break;
case '6':
exit(0);
}
}
}

31

EXPERIMENT NO.-7
#include<stdio.h>
#include<conio.h>
int tsp[10][10]={{999,10,3,2,5,6,7,2,5,4},
{20,999,3,5,10,2,8,1,15,6},
{10,5,999,7,8,3,11,12,3,2},
{3,4,5,999,6,4,10,6,1,8},
{1,2,3,4,999,5,10,20,11,2},
{8,5,3,10,2,999,6,9,20,1},
{3,8,5,2,20,21,999,3,5,6},
{5,2,1,25,15,10,6,999,8,1},
{10,11,6,8,3,4,2,15,999,1},
{5,10,6,4,15,1,3,5,2,999}
};
int pa[1000][10]= {{0,1,2,3,4,5,6,7,8,9},
{9,8,6,3,2,1,0,4,5,7},
{2,3,5,0,1,4,9,8,6,7},
{4,8,9,0,1,3,2,5,6,7}
};

int i,j,k,l,m,y,loc,flag,row,col,it,x=3,y=3;
int count,row=0,res[1][10],row1,col1,z;
int numoff=4;
int offspring[1000][10];
int mincost=9999,mc;
32

main()
{
int gen;
clrscr();
printf("Number of Generation : ");
scanf("%d",&gen);
offcal1(pa);
offcal2(pa);
printf(" \n\t\t First Generation\n");
for(i=0;i<count;i++)
{
for(j=0;j<10;j++)
printf("%d ",offspring[i][j]);
printf("\n");
}

for(y=1;y<=gen-1;y++)
{
getch();
clrscr();
for(i=0;i<count;i++)
for(j=0;j<10;j++)
pa[i][j]=offspring[i][j];
numoff=count;
offcal1(pa);
offcal2(pa);
printf(" \n\t\t %d Generation\n",y+1);
for(i=0;i<count;i++)
{
for(j=0;j<10;j++)
33

printf("%d ",offspring[i][j]);
printf("\n");
}
getch();
clrscr();
}
printf("\n\nMinimum Cost Path\n");
for(z=0;z<10;z++)
printf("%d ",res[0][z]);
printf("\nMinimum Cost %d \n",mincost);
}

/* finding the offspring using cyclic crossover */


offcal1(pa)
int pa[1000][10];
{
count=0;
for(i=0;i<1000;i++)
for(j=0;j<10;j++)
offspring[i][j]=-1;

for(k=0;k<numoff;k++)
{
for(l=k+1;l<numoff;l++)
{
offspring[row][0]=pa[k][0];
loc=pa[l][0];
flag=1;
while(flag != 0)
34

{
for(j=0;j<10;j++)
{
if(pa[k][j] == loc )
{
if (offspring[row][j]==-1)
{
offspring[row][j]=loc;
loc=pa[l][j];
}
else
flag=0;
}
}
}/* end while*/
for(m=0;m<10;m++)
{
if(offspring[row][m] == -1)
offspring[row][m]=pa[l][m];
}
for(z=0;z<10;z++)
{
if(z<9)
{
row1=offspring[row][z];
col1=offspring[row][z+1];
mc=mc+tsp[row1][col1];
}
else
{
35

row1=offspring[row][z];
col1=offspring[row][0];
mc=mc+tsp[row1][col1];
}
}
if(mc < mincost)
{
for(z=0;z<10;z++)
res[0][z]=offspring[row][z];
mincost=mc;
}
count++;
row++;
}/* end l*/
}
}

offcal2(pa)
int pa[1000][10];
{
for(k=0;k<numoff;k++)
{
for(l=k+1;l<numoff;l++)
{
offspring[row][0]=pa[l][0];
loc=pa[k][0];
flag=1;
while(flag != 0)
{
36

for(j=0;j<10;j++)
{
if(pa[l][j] == loc )
{
if (offspring[row][j]==-1)
{
offspring[row][j]=loc;
loc=pa[k][j];
}
else
flag=0;
}
}
}/* end while*/
for(m=0;m<10;m++)
{
if(offspring[row][m] == -1)
offspring[row][m]=pa[k][m];
}
for(z=0;z<10;z++)
{
if(z<9)
{
row1=offspring[row][z];
col1=offspring[row][z+1];
mc=mc+tsp[row1][col1];
}
else
{
row1=offspring[row][z];
37

col1=offspring[row][0];
mc=mc+tsp[row1][col1];
}
}
row++;
if(mc < mincost)
{
for(z=0;z<10;z++)
res[0][z]=offspring[row][z];
mincost=mc;
}
count++;
}/* end l*/
}
}

38

EXPERIMENT NO.-8
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>

char input[15],parent[50][15],child[50][15],mating_pool[105][15],mutant[05][15];
int pfit[50],cfit[50],fit[105],mfit[05],gen=0;

void get_input()
{
int i;
clrscr();
printf("\n\n\n\t\tWORD MATCHING PROBLEM - GENETIC ALGORITHMS ASSIGNMENT");
printf("\n\t **********************************************************");
printf("\n\n\n\n\t\tENTER THE WORD TO BE MATCHED : ");
scanf("%s",input);
printf("\n\n\n\t THE ASCII EQUIVALENT OF THE LETTERS IN THE ENTERED WORD");
printf("\n\t--------------------------------------------------------------");
printf("\n\n LETTERS :");
for(i=0;i<strlen(input);i++)
{
printf(" %c ",input[i]);
}
printf("\n ASCII :");
39

for(i=0;i<strlen(input);i++)
{
printf(" %3d",input[i]);
}
getch();

void initial_pop()
{
int i,j;
randomize();
for(i=0;i<50;i++)
{
for(j=0;j<strlen(input);j++)
{
parent[i][j]=random(26)+97;
if(parent[i][j]==input[j])
{
pfit[i]++;
}
}
}
}

void display()
{
int i,j,nexti;
clrscr();
printf("\n\n\t\t THE CHROMOSOMES OF PARENTS AND CHILDREN");
40

printf("\n\t

--------------------------------------------\n");

printf("\n\t\t PREVIOUS GENERATION CHILDREN CHROMOSOMES\n\n");


for(i=0;i<50;i++)
{
if(((i)%4)==0) printf("\n");
for(j=0;j<strlen(input);j++)
{
printf("%c",child[i][j]);
}
printf("% 2d ",cfit[i]);
}
printf("\n\t\t\tMUTANTS OF THIS GENERATION\n");
for(i=0;i<05;i++)
{
if (i==3) printf("\n");
for(j=0;j<strlen(input);j++)
{
printf("%c",mutant[i][j]);
}
printf("% 2d ",mfit[i]);
}
getch();
clrscr();
printf("\n\n\t\t THE CHROMOSOMES OF PARENTS AND CHILDREN");
printf("\n\t

--------------------------------------------\n");

printf("\n\t\t NEXT GENERATION PARENTS CHROMOSOMES\n\n");


for(i=0;i<50;i++)
{
if(((i)%4)==0) printf("\n");
for(j=0;j<strlen(input);j++)
41

{
printf("%c",parent[i][j]);
}
printf("% 2d ",pfit[i]);
}
getch();
}

void reproduction() //sorting_based_on_fitness()


{
char tempc;
int temp;
int i,j,k;

for(i=0;i<50;i++)
{
for(j=0;j<strlen(input);j++)
{
mating_pool[i][j]=parent[i][j];
fit[i]=pfit[i];
}
}
for(i=50;i<100;i++)
{
for(j=0;j<strlen(input);j++)
{
mating_pool[i][j]=child[i-50][j];
fit[i]=cfit[i-50];
}
}
42

for(i=100;i<105;i++)
{
for(j=0;j<strlen(input);j++)
{
mating_pool[i][j]=mutant[i-100][j];
fit[i]=mfit[i-100];
}
}
//sorting
for(i=0;i<105;i++)
{
for(j=i+1;j<105;j++)
{
if(fit[i]<fit[j])
{
for(k=0;k<strlen(input);k++)
{
tempc=mating_pool[i][k];
mating_pool[i][k]=mating_pool[j][k];
mating_pool[j][k]=tempc;

temp=fit[i];
fit[i]=fit[j];
fit[j]=temp;
}
}
}
}
for(i=0;i<50;i++)
{
43

for(j=0;j<strlen(input);j++)
{
parent[i][j]=mating_pool[i][j];
pfit[i]=fit[i];
}
}
for(i=50;i<100;i++)
{
for(j=0;j<strlen(input);j++)
{
child[i-50][j]=mating_pool[i][j];
cfit[i-50]=fit[i];
}
}

void crossover()
{
int xover_pt;
int i,j,k;
for(i=0;i<50;i++)
{
xover_pt=random(strlen(input));
cfit[i]=0;
cfit[i+1]=0;
for(j=0;j<xover_pt;j++)
{
child[i][j]=parent[i][j];
if (input[j]==child[i][j])
44

cfit[i]++;
child[i+1][j]=parent[i+1][j];
if(input[j]==child[i+1][j])
cfit[i+1]++;
}
for(j=xover_pt;j<strlen(input);j++)
{
child[i][j]=parent[i+1][j];
if(input[j]==child[i][j])
cfit[i]++;
child[i+1][j]=parent[i][j];
if(input[j]==child[i+1][j])
cfit[i+1]++;
}
i++;
}
}
void mutation()
{
int i,mut_pt,j;
char mut_val;
randomize();
for(i=0;i<05;i++)
{
mut_pt=random(strlen(input));
mut_val=random(26)+97;
mfit[i]=0;
for(j=0;j<mut_pt;j++)
{
mutant[i][j]=parent[1][j];
45

if (mutant[i][j]==input[j])
{
mfit[i]++;
}
}
mut_val=input[j];
mutant[i][mut_pt]=mut_val;
if (mutant[i][j]==input[j])
{
mfit[i]++;
}
for(j=mut_pt+1;j<strlen(input);j++)
{
mutant[i][j]=parent[1][j];
if (mutant[i][j]==input[j])
{
mfit[i]++;
}
}
}
}

void results()
{
int i;
clrscr();
printf("\n\n\n\t\tWORD MATCHING PROBLEM - GENETIC ALGORITHM ASSIGNMENT");
printf("\n\t **********************************************************");
printf("\n\n\n\t\t THE MATCHING WORD FOR THE GIVEN INPUT WORD");
printf("\n\n\t\t

OBTAINED USING GENETIC ALGORITHM");


46

printf("\n\n\t\t\t

");

for(i=0;i<strlen(input);i++)
{
printf("%c",parent[0][i]);
}
printf("\n\t\t\t --");
for(i=0;i<strlen(input);i++)
{
printf("-");
}
printf("--\n\n\n\t\t

USER INPUT : %s",input);

printf("\n\n\n\t THE FITNESS OF THE GA GENERATED WORD AND THE USER'S INPUT");
printf("\n\n\t\t\t\t %2d/%d",pfit[0],strlen(input));
printf("\n\n\n\t\t\t GENERATIONS COUNT : %d",gen);
}

int input_choice()
{
int choice,i;
clrscr();
printf("\n\n\n\n\t\t\t GENEREATION NUMBER : %d",gen);
printf("\n\t\t

------------------------------");

printf("\n\n\n\t\tTHE
FITTEST
GENERATION\n\n\n\t\t\t\t");

INDIVIDUAL

TILL

THE

PREVIOUS

for(i=0;i<strlen(input);i++)
{
printf("%c",parent[0][i]);
}
printf(" / ");
for(i=0;i<strlen(input);i++)
47

{
printf("%c",input[i]);
}

printf("\n\n\n\t\t\t

WITH A FITNESS OF %d/%d",pfit[0],strlen(input));

printf("\n\n\n\n\t\tENTER YOUR CHOICE (TO CONTINUE 1 TO EXIT 0) : ");


scanf("%d",&choice);
return choice;
}

void main()
{
int i,choice;
clrscr();
get_input();
initial_pop();
//display();
reproduction(); //sorting_based_on_fitness();
display();
printf("\nENTER YOUR CHOICE (TO CONTINUE 1 TO EXIT 0) : ");
scanf("%d",&choice);
while((choice==1)&&(pfit[0]!=strlen(input)))
{
crossover();
gen++;
mutation();
reproduction(); //sorting_based_on_fitness();
display();
choice=input_choice();
}
48

sound(1000);
delay(200);
nosound();
delay(200);
results();
getch();
sound(1000);
delay(200);
nosound();
}

49

You might also like