Program No. 1 AIM: Write The Program To Optimize The Following Function Maximize F (X) X Program

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 25

PROGRAM NO.

1
AIM: Write the program to optimize the following function
Maximize f(x) = x2

PROGRAM:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int pop[10][10],npop[10][10],tpop[10][10],x[10],fx[10],m_max=1,ico=0,ico1,it=0;
void iter(int [10][10],int,int);
int u_rand(int);
void cross_ov(int,int);
void roulette_wheel(int,int);
void mutat(int,int);
void main()
{
int k,m,j,i,p[10],n=0,a[10],nit;
clrscr();
time_t t0;
srand(time(&t0));
cout<<"Enter the number of Population in each iteration is : ";
cin>>n;
cout<<"Enter the number of iteration is : ";
cin>>nit;
m=5;//length of chromosomes...
for(i=0;i<n;i++)
{
for(j=m-1;j>=0;j--)
{
pop[i][j]=u_rand(2);
}
}
cout<<"INITIAL POPULATION is:";
iter(pop,n,m);
do
{
it++;
cout<<"\nIteration "<<it<<" is :\n";
roulette_wheel(n,m);
iter(pop,n,m);
getch();
}while(it<nit);

cout<<"\n\nAfter the "<<ico1<<" Iteration, the Maximum Value is : "<<(int)


sqrt(m_max);
getch();
}
void iter(int pp[10][10],int o, int p) //o=parents,m=5
{
int i,j,sum,avg,max=1;
for(i=0;i<o;i++)
{
x[i]=0;
for(j=0;j<p;j++)
{
x[i]=x[i]+(pp[i][j]*pow(2,p-1-j));
}
fx[i]=x[i]*x[i];
sum=sum+fx[i];
if (max<=fx[i])
max=fx[i];
}
avg=sum/o;
cout<<"\n\nS.No.\tPopulation\tX\tf(X)\n\n";
for(i=0;i<o;i++)
{
cout<<ico<<"\t";
ico++;
for(j=0;j<p;j++)
cout<<pp[i][j];
cout<<"\t\t"<<x[i]<<"\t"<<fx[i]<<"\n";
}
cout<<"\n\t Sum : "<<sum<<"\tAverage : "<<avg<<"\tMaximum :"<<max<<"\n";
if (m_max<max)
{
m_max=max;
ico1=it;
}
}

int u_rand(int x)
{
int y;
y=rand()%x;
return(y);
}

void roulette_wheel(int np,int mp)


{
int i,j,k;
int max1;
int random;
int running_total[4];
int current_total;
current_total = 0;
for (i=0;i<np;i++)
{
current_total=fx[i]+current_total;
running_total[i]=current_total;
}
max1=current_total;
for (i=0;i<np;i++)
{
random=u_rand(max1+1);
for (j=0;j<np;j++)
{
if (random>running_total[j])
for (k=0;k<mp;k++)
{
npop[i][k]=pop[j][k];
j=np+1;
}
}
}

getch();
cross_ov(np,mp);
getch();
}

void cross_ov(int np1,int mb1)


{
int i,j,k,l,co,temp;
i=0;
do
{
k=rand()%2;
do
{
co=0;
l=u_rand(mb1);
if (((k==0) && (l==0)) || ((k==1) && (l==mb1)))
co++;
}while(co!=0);
if ((k==0) && (l!=0))
{
for(j=0;j<l;j++)
{
temp=npop[i][j];
npop[i][j]=npop[i+1][j];
npop[i+1][j]=temp;
}
}
else if ((k==1) && (l!=mb1))
{
for(j=l;j<mb1;j++)
{
temp=npop[i][j];
npop[i][j]=npop[i+1][j];
npop[i+1][j]=temp;
}
}
i=i+2;
}while(i<np1);
for(i=0;i<np1;i++)
{
for(j=0;j<mb1;j++)
{
tpop[i][j]=npop[i][j];

}}
mutat(np1,mb1);
}

void mutat(int np2,int mb2)


{
int i,j,r,temp,k,z;
i=0;
do
{
for(k=0;k<np2;k++)
{
r=0;
if (i!=k)
{
for(j=0;j<mb2;j++)
{
if (tpop[i][j]==tpop[k][j])
r++;
}
if (r!=mb2-1)
{
z=u_rand(mb2);
if (tpop[i][z]==0)
tpop[i][z]=1;
else
tpop[i][z]=0;
if (npop[k][u_rand(mb2)]==0)
npop[k][u_rand(mb2)]=1;
else
npop[k][u_rand(mb2)]=0;
mutat(k,mb2);
}
}}
i++;
}while(i<np2);
for(i=0;i<np2;i++)
{
for(j=0;j<mb2;j++)
{
pop[i][j]=tpop[i][j];
}
}}
OUTPUT:

Enter the number of Population in each iteration is : 4


Enter the number of iteration is : 2
INITIAL POPULATION is:

S.No. Population X f(X)

0 01111 15 225
1 00110 6 36
2 00101 5 25
3 01001 9 81

Sum : 367 Average : 91 Maximum :225

Iteration 1 is :

S.No. Population X f(X)

4 01110 14 196
5 11111 31 961
6 00110 6 36
7 01000 8 64

Sum : 1624 Average : 406 Maximum :961

Iteration 2 is :

S.No. Population X f(X)

8 10101 21 441
9 10111 23 529
10 11100 28 784
11 01011 11 121

Sum : 3132 Average : 783 Maximum :784

After the 1 Iteration, the Maximum Value is: 31


PROGRAM NO. 2

AIM: Write the program to perform different types of crossover operations


a) Single point crossover b) Two point crossover c) Uniform crossover

PROGRAM:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int pop[10][10],npop[10][10],spop[10][10],tpop[10][10],ico=0;
int u_rand(int);
void singlecross_ov(int,int,int [10][10]);
void twocross_ov(int,int,int [10][10]);
void unicross_ov(int,int,int [10][10]);
void main()
{
int k,m,j,i,n=2,choice;
clrscr();
char ch;
time_t t0;
srand(time(&t0));
m=5;//length of chromosomes...
cout<<"**************************\n";
cout<<"PARENT POPULATION is:";
for(i=0;i<n;i++)
{
for(j=m-1;j>=0;j--)
{
pop[i][j]=u_rand(2);
}
}
cout<<"\nS.No.\tPopulation\n";
for(i=0;i<n;i++)
{
cout<<ico+1<<"\t";
ico++;
for(j=0;j<m;j++)
{
npop[i][j]=pop[i][j];
spop[i][j]=pop[i][j];
cout<<pop[i][j];
}
cout<<"\n";
}

cout<<"**************************\n";
do
{
cout<<"\n\n\n1.Single Point Crossover \n";
cout<<"2.Two Point Crossover\n";
cout<<"3.Uniform Crossover\n";
cout<<"Enter choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n\nSingle Point Crossover:\n";
for(i=0;i<n;i++)
{
cout<<"Parent"<<i+1<<":";
for(j=0;j<m;j++)
{
cout<<pop[i][j];
}
cout<<"\n";
}
singlecross_ov(n,m,pop);
break;
case 2: cout<<"\n\nTwo Point Crossover:\n";
for(i=0;i<n;i++)
{
cout<<"Parent"<<i+1<<":";
for(j=0;j<m;j++)
{
cout<<npop[i][j];
}
cout<<"\n";
}
twocross_ov(n,m,npop);
break;
case 3:cout<<"\n\nUniform Crossover:\n";
for(i=0;i<n;i++)
{
cout<<"Parent"<<i+1<<":";
for(j=0;j<m;j++)
{
cout<<spop[i][j];
}
cout<<"\n";
}
unicross_ov(n,m,spop);
break;

}
cout<<"\nDo you want to continue?(Y/N)";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}

int u_rand(int x)
{
int y;
y=rand()%x;
return(y);
}
void singlecross_ov(int np1,int mb1,int pp[10][10])
{
int i,j,k,l,co,temp;
i=0;
do
{
k=rand()%2;
do
{
co=0;
l=u_rand(mb1);
if (((k==0) && (l==0)) || ((k==1) && (l==mb1)))
co++;
}while(co!=0);
cout<<"Crossover site:"<<l<<"\n";
if ((k==0) && (l!=0))
{
for(j=0;j<l;j++)
{
temp=pp[i][j];
pp[i][j]=pp[i+1][j];
pp[i+1][j]=temp;
}
}
else if ((k==1) && (l!=mb1))
{
for(j=l;j<mb1;j++)
{
temp=pp[i][j];
pp[i][j]=pp[i+1][j];
pp[i+1][j]=temp;
}
}
i=i+2;
}while(i<np1);
cout<<"\n\nAfter Crossover\n";
for(i=0;i<np1;i++)
{
cout<<"Children"<<i+1<<":";
for(j=0;j<mb1;j++)
{
tpop[i][j]=pp[i][j];
cout<<tpop[i][j];
}
cout<<"\n";
}

void twocross_ov(int np1,int mb1,int pp1[10][10])


{
int i,j,k,l,p,co,temp;
i=0;
do
{
do
{
co=0;
l=u_rand(mb1);
p=u_rand(mb1);
if(l==0||p==0||p==l)
co++;
}while(co!=0);
cout<<"Crossover Sites:"<<l<<","<<p<<"\n";

if (l<p)
{
for(j=l;j<p;j++)
{
temp=pp1[i][j];
pp1[i][j]=pp1[i+1][j];
pp1[i+1][j]=temp;
}
}
else if(l>p)
{
for(j=p;j<l;j++)
{
temp=pp1[i][j];
pp1[i][j]=pp1[i+1][j];
pp1[i+1][j]=temp;
}
}
i=i+2;
}while(i<np1);
cout<<"\nAfter Crossover\n\n";
for(i=0;i<np1;i++)
{
cout<<"Children"<<i+1<<":";
for(j=0;j<mb1;j++)
{
tpop[i][j]=pp1[i][j];
cout<<tpop[i][j];
}
cout<<"\n";
}

void unicross_ov(int np1,int mb1,int pp2[10][10])


{
int i,j,k,l,p,co,temp,mask[10],child[10];
i=0;
cout<<"\nMask:";
do
{
for(j=0;j<mb1;j++)
{
mask[j]=u_rand(2);
cout<<mask[j];
if(mask[j]==1)
child[j]=pp2[0][j];
else
child[j]=pp2[1][j];
}
i=i+2;
}while(i<np1);
cout<<"\nAfter Crossover\n\n";
cout<<"Children"<<":";
for(j=0;j<mb1;j++)
{
cout<<child[j];
}
cout<<"\n";
}
OUTPUT:

**************************
PARENT POPULATION is:
S.No. Population
1 11110
2 01011
**************************
1.Single Point Crossover
2.Two Point Crossover
3.Uniform Crossover
Enter choice:1

Single Point Crossover:


Parent1:11110
Parent2:01011

Crossover site:3

After Crossover
Children1:01010
Children2:11111

Do you want to continue?(Y/N)Y

1.Single Point Crossover


2.Two Point Crossover
3.Uniform Crossover
Enter choice:2

Two Point Crossover:


Parent1:11110
Parent2:01011

Crossover Sites:3,4

After Crossover
Children1:11110
Children2:01011

Do you want to continue?(Y/N)Y


1.Single Point Crossover
2.Two Point Crossover
3.Uniform Crossover
Enter choice:3

Uniform Crossover:
Parent1:11110
Parent2:01011
Mask:11111
After Crossover
Children:11110

Do you want to continue?(Y/N)


PROGRAM NO. 3

AIM: Write the program to perform different types of mutation operations


a) Bit Flipping b) Interchanging c) Reversing

PROGRAM:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int pop[10][10],tpop[10][10],npop[10][10],spop[10][10],ico=0;
int u_rand(int);
void bitmutat(int,int,int [10][10]);
void intermutat(int,int,int [10][10]);
void revmutat(int,int,int [10][10]);
void main()
{
int k,m,j,i,n,choice;
clrscr();
char ch;
time_t t0;
srand(time(&t0));
cout<<"******************************\n";
cout<<"Enter the number of population:";
cin>>n;
cout<<"\n";
m=5;//length of chromosomes...
cout<<"PARENT POPULATION is:";
for(i=0;i<n;i++)
{
for(j=m-1;j>0;j--)
{
pop[i][j]=u_rand(2);
}
}
cout<<"\nS.No.\tPopulation\n";
for(i=0;i<n;i++)
{
cout<<ico<<"\t";
ico++;
for(j=0;j<m;j++)
{ npop[i][j]=pop[i][j];
spop[i][j]=pop[i][j];
cout<<npop[i][j];
}
cout<<"\n";
}
cout<<"****************************\n";
do
{
cout<<"\n***Mutation operator Menu***\n";
cout<<"1.Bit Flipping Mutation \n";
cout<<"2.Interchanging\n";
cout<<"3.Reversing\n";
cout<<"Enter choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n\nBit Flipping Mutation:\n";
for(i=0;i<n;i++)
{
cout<<"Parent"<<i+1<<":";
for(j=0;j<m;j++)
{
cout<<npop[i][j];
}
cout<<"\n";
}
bitmutat(n,m,npop);
break;
case 2: cout<<"\n\nInterchanging Mutation:\n";
for(i=0;i<n;i++)
{
cout<<"Parent"<<i+1<<":";
for(j=0;j<m;j++)
{
cout<<pop[i][j];
}
cout<<"\n";
}
intermutat(n,m,pop);
break;
case 3: cout<<"\n\nReverse Mutation:\n";
for(i=0;i<n;i++)
{
cout<<"Parent"<<i+1<<":";
for(j=0;j<m;j++)
{
cout<<spop[i][j];
}
cout<<"\n";
}
revmutat(n,m,spop);
}
cout<<"\nDo you want to continue?(Y/N)";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}

int u_rand(int x)
{
int y;
y=rand()%x;
return(y);
}

void bitmutat(int np2,int mb2,int pp[10][10])


{
int i,j,r,temp,k,z;
i=0;
do
{
z=u_rand(mb2);

if(pp[i][z]==0)
pp[i][z]=1;
else
pp[i][z]=0;
i++;
}while(i<np2);
cout<<"Mutation site:"<<z<<"\n";
cout<<"\n\nAfter Mutation\n";
for(i=0;i<np2;i++)
{
cout<<"Children"<<i+1<<":";
for(j=0;j<mb2;j++)
{
tpop[i][j]=pp[i][j];
cout<<tpop[i][j];
}
cout<<"\n";
}
}

void intermutat(int np2,int mb2,int pp1[10][10])


{
int i,j,z,z1,temp,co;
i=0;
do
{
z=u_rand(mb2);
do
{
co=0;
z1=u_rand(mb2);
if((z1==z1)&&(pp1[i][z1]==pp1[i][z]))
co++;
}while(co!=0);

cout<<"Mutation sites:"<<z<<","<<z1<<"\n";
temp=pp1[i][z];
pp1[i][z]=pp1[i][z1];
pp1[i][z1]=temp;
i++;
}while(i<np2);
cout<<"\nAfter Mutation\n";

for(i=0;i<np2;i++)
{
cout<<"Children"<<i+1<<":";
for(j=0;j<mb2;j++)
{
tpop[i][j]=pp1[i][j];
cout<<tpop[i][j];
}
cout<<"\n";
}
}

void revmutat(int np2,int mb2,int pp2[10][10])


{
int i,j,z,temp,co;
i=0;
do
{
z=u_rand(mb2);
if(z==(mb2-1))
continue;
else
{

if(pp2[i][z+1]==0)
pp2[i][z+1]=1;
else
pp2[i][z+1]=0;
}
i++;
}while(i<np2);
cout<<"Mutation site:"<<z<<"\n";
cout<<"\nAfter Mutation\n";

for(i=0;i<np2;i++)
{
cout<<"Children"<<i+1<<":";
for(j=0;j<mb2;j++)
{
tpop[i][j]=pp2[i][j];
cout<<tpop[i][j];
}
cout<<"\n";
}
}
OUTPUT:
******************************
Enter the number of population:3
PARENT POPULATION is:
S.No. Population
0 01011
1 01101
2 01000
****************************
***Mutation operator Menu***
1.Bit Flipping Mutation
2.Interchanging
3.Reversing
Enter choice:1

Bit Flipping Mutation:


Parent1:01011
Parent2:01101
Parent3:01000

After Mutation
Children1:01010
Children2:01100
Children3:01001

Do you want to continue?(Y/N)Y


***Mutation operator Menu***
1.Bit Flipping Mutation
2.Interchanging
3.Reversing
Enter choice:2

Interchanging Mutation:
Parent1:01011
Parent2:01101
Parent3:01000

After Mutation
Children1:01110
Children2:01011
Children3:00001

Do you want to continue?(Y/N)

***Mutation operator Menu***


1.Bit Flipping Mutation
2.Interchanging
3.Reversing
Enter choice:3
Reverse Mutation:
Parent1:01011
Parent2:01101
Parent3:01000

After Mutation
Children1:01111
Children2:01100
Children3:01001

Do you want to continue?(Y/N)


PROGRAM NO. 4

AIM: Write the program to simulate Traveling Salesman Problem using genetic
algorithm approach

PROGRAM:
#include<iostream.h>
#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}
};
void offcal1(int pa[1000][10]);
void offcal2(int pa[1000][10]);

int i,j,k,l,m,loc,flag,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;
int main()
{
int gen;
clrscr();
cout<<"Number of Generation : ";
cin>>gen;
offcal1(pa);
offcal2(pa);
cout<<" \n\t\t First Generation\n";
for(i=0;i<count;i++)
{
for(j=0;j<10;j++)
cout<<offspring[i][j];
cout<<"\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);
cout<<" \n\t\tGeneration\n";
cout<<y+1;
for(i=0;i<count;i++)
{
for(j=0;j<10;j++)
cout<<offspring[i][j];
cout<<"\n";
}
getch();
clrscr();
}
cout<<"\n\nMinimum Cost Path\n";
for(z=0;z<10;z++)
cout<<res[0][z];
cout<<"\nMinimum Cost\n"<<mincost;
getch();
return 0;
}
/* finding the offspring using cyclic crossover */
void offcal1(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)
{
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
{
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*/
}
}
void offcal2(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)
{
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];
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*/
}
}
OUTPUT:
Number of Generation : 1

First Generation
0823416759
0123459867
0123456789
9853210467
9860132457
2350149867
9163250487
2350146789
4890132567
2360149857
4893210567
4890132567

Minimum Cost Path


0823416759
Minimum Cost
53

You might also like