0% found this document useful (0 votes)
57 views18 pages

Soft Computing Lab Updates

The document contains 7 experiments on soft computing algorithms: 1. Implementing the Perceptron training algorithm in code. 2. Writing a program for the ADALINE network. 3. Defining the EX-OR function with the MEDALINE network. 4. Simulating the Back Propagation network using C. 5. Writing a program for the Full Counter neural network. 6. Writing programs for AI algorithms like hill climbing, A*, and AO* in C++. 7. Case studies on genetic algorithms and artificial intelligence.

Uploaded by

RuDresh Divedi
Copyright
© © All Rights Reserved
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)
57 views18 pages

Soft Computing Lab Updates

The document contains 7 experiments on soft computing algorithms: 1. Implementing the Perceptron training algorithm in code. 2. Writing a program for the ADALINE network. 3. Defining the EX-OR function with the MEDALINE network. 4. Simulating the Back Propagation network using C. 5. Writing a program for the Full Counter neural network. 6. Writing programs for AI algorithms like hill climbing, A*, and AO* in C++. 7. Case studies on genetic algorithms and artificial intelligence.

Uploaded by

RuDresh Divedi
Copyright
© © All Rights Reserved
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/ 18

INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

INDEX
Sr. No. List of Practical

1. Implement a code for Perceptron training algorithm.

2. Write a program for ADALINE network.

3. Define EX-OR function in your program with MEDALINE.

4. Simulate Back Propagation network in your program using C.

5. Write a program for Full Counter in Neural network.

6. Write a program for different Artificial Intelligence algorithms-


Hill Climbing, A* and AO* algorithm in C++.

7. Case Studies- Genetic Algorithm, Artificial Intelligence.

Experiment-1
SOFT COMPUTING IT-802 Page 1
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Implement a code for Perceptron training algorithm

/*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:
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;

SOFT COMPUTING IT-802 Page 2


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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]);
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);

SOFT COMPUTING IT-802 Page 3


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

}
printf("\n");
}
if(k==4)
{
printf("\nFinal weights\n");
for(j=0;j<=1;j++)
{
printf("w[%d]=%.2f\t",j,w[j]);
}
printf("Bias b=%.2f",b);
}
else
{
k=0;
h=h+1;
getch();
goto mew;
}
getch();
}

Experiment-2

Write a program for ADALINE network

SOFT COMPUTING IT-802 Page 4


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

/*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------------------");
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");

SOFT COMPUTING IT-802 Page 5


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

for(k=0;k<=3;k++)
{
printf("%f\t",w[k]);
}
bc=e*alp;
printf("b=%.2f\t",b);
getch();
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();
}

Experiment-3
Define EX-OR function in your program with MEDALINE
SOFT COMPUTING IT-802 Page 6
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

//XOR function using madaline//

#include<stdio.h>
#include<conio.h>
void main()
{
signed int x[4][2],tar[4];
float w[2][2],a,o[2];
float wc[2][2],zin[2],z1=0,z2=0,yin=0,b[2],er=0,b3=0,v1=0,v2=0.5;
int i,j,c=0,in,d;
float bc[2];
float alp=0.5;
clrscr();
for(i=0;i<=3;i++)
{
printf("Enter the %d row & target:");
for(j=0;j<=1;j++)
scanf("%d",&x[i][j]);
scanf("%d",&tar[i]);
}
getch();
printf("Enter Weights:");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%f",&a);
w[i][j]=a;
wc[i][j]=0;
}
printf("bias");
scanf("%f",&b[i]);
zin[i]=0;
}
mew:
printf("Iteration\n");
printf("--------------\n");
for(i=0;i<=3;i++)
{
for(in=0;in<=1;in++)
{

SOFT COMPUTING IT-802 Page 7


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

for(j=0;j<=1;j++)
{
zin[in]+=x[i][j]*w[j][c];
}
zin[in]+=b[in];
printf("zin%d= %.3f\t",in,zin[in]);
c+=1;
}
c=0;
d=1;
if(zin[c]>=0 & zin[d]>=0)
z1=z2=1;
else if(zin[c]>=0 & zin[d]<=0)
{
z1=1;
z2=-1;

}
else if(zin[c]<=0 & zin[d]>=0)
{
z1=-1;
z2=1;
}
else
{
z1=z2=-1;
}
yin=z1*v1+z2*v2+b3;
printf("NET %.3f\t",yin);
for(in=0;in<=1;in++)
{
o[in]=tar[i]-zin[in];
er+=o[in]*o[in];
zin[in]=0;
}
if(yin==tar[i])
{
for(in=0;in<=1;in++)
{
for(j=0;j<=1;j++)
{

SOFT COMPUTING IT-802 Page 8


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

wc[in][j]=0;
w[in][j]+=wc[in][j];
}
bc[in]=0;
}
yin=0;
}
else
{
for(in=0;in<=1;in++)
{
for(j=0;j<=1;j++)
{
wc[in][j]=alp*o[j]*x[i][in];
printf("wc%d%d=%.3f\t",in,j,wc[in][j]);
w[in][j]+=wc[in][j];
printf("w=%.3f\t",w[in][j]);
wc[in][j]=0;
}
}
for(in=0;in<=1;in++)
{
bc[in]=alp*o[in];
b[in]+=bc[in];
printf("\nb%d=%.3f",in,b[in]);
}
for(in=0;i<=1;in++)
{
bc[in]=0;
}
yin=0;
}
printf("\n");
}
if(er<=1)
{
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
printf("%.3f",w[i][j]);
}

SOFT COMPUTING IT-802 Page 9


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

}
else
{
yin=0;
for(in=0;in<=1;in++)
{
bc[in]=0;
zin[in]=0;
}
er=0;
getch();
goto mew;
}
getch();
}

Experiment-4
Simulate Back Propagation network in your program using C

/*BACK PROPAGATION NETWOR*/

SOFT COMPUTING IT-802 Page 10


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

#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;
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];
SOFT COMPUTING IT-802 Page 11
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

}
zin[k]+=b[k];
k+=1;
}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];
}
}
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;
}
SOFT COMPUTING IT-802 Page 12
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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++)
{
printf("%f\t",w[k][0]);
}
printf("\n%f",bl);
printf("\t");
for(k=0;k<=3;k++)
{
printf("%f\t",b[k]);
}
getch();
}
getch();
}

Experiment-5
Write a program for Full Counter in Neural network
/* FULL COUNTER*/

#include<stdio.h>
#include<conio.h>
void main ()
{
SOFT COMPUTING IT-802 Page 13
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

float alp=0.6,x=0.1,n[10],v[1][10],d[10],p,w[1][10],y,bet=0.6;
float u[10][1],t[10][1],a=0.6,b=0.6;
int i,j,J,k=0,m;
clrscr();
v[0][0]=0.1;v[0][1]=0.15;v[0][2]=0.2;
v[0][3]=0.3;v[0][4]=0.5;v[0][5]=1.5;
v[0][6]=3.0;v[0][7]=5.0;v[0][8]=7.0;
v[0][9]=9.0;
w[0][0]=9.0;w[0][1]=7.0;w[0][2]=5.0;
w[0][3]=3.0;w[0][4]=1.5;w[0][5]=0.5;
w[0][6]=0.2;w[0][7]=0.2;w[0][8]=0.15;
w[0][9]=0.1;
u[0][0]=0.1;u[1][0]=0.15;u[2][0]=0.2;
u[3][0]=0.3;u[4][0]=0.5;u[5][0]=1.5;
u[6][0]=3.0;u[7][0]=5.0;u[8][0]=7.0;
u[9][0]=9.0;
t[0][0]=9.0;t[1][0]=7.0;t[2][0]=5.0;
t[3][0]=3.0;t[4][0]=1.5;t[5][0]=0.5;
t[6][0]=0.3;t[7][0]=0.2;t[8][0]=0.15;
t[9][0]=0.1;
do
{
y=1/x;
printf("\n");
for(j=0;j<=9;j++)
{
n[j]=(x-v[0][j])*(x-v[0][j])+(y-w[0][j])*(y-w[0][j]);
d[j]=n[j];
}
for(m=0;m<=9;m++)
{
for(j=m;j<=9;++j)
{
if(d[k]>d[j])
{
p=d[j];
d[j]=d[k];
d[k]=p;
}
}
k+=1;
}
for(j=0;j<=9;j++)
{
if(d[0]==n[j])
{
J=j;
}
}
v[0][J]+=alp*(x-v[0][J]);
SOFT COMPUTING IT-802 Page 14
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

w[0][J]+=bet*(y-w[0][J]);
printf("\nInput X=%f",x);
printf("\nUpdated weights: v");
for(j=0;j<=9;j++)
{
printf("%f\t",v[0][j]);
n[j]=0;
d[j]=0;
}
printf("\n Updated weights: w");
for(j=0;j<=9;j++)
{
printf("%f\t",w[0][j]);
}
x=x+0.5;
alp=alp/1.014;
bet=bet/1.014;
J=0;
k=0;
getch();
}
while(x<=10.50);
x=0.1;
do
{
for(j=0;j<=9;j++)
{
n[j]=(x-v[0][j])*(x-v[0][j])+(y-w[0][j])*(y-w[0][j]);
d[j]=n[j];
}
for(m=0;m<=9;m++)
{
for(j=m;j<=9;j++)
{
if(d[k]>d[j])
{
p=d[j];
d[j]=d[k];
d[k]=p;
}
}
k+=1;
}
for(j=0;j<=9;j++)
{
if(d[0]==n[j])
{
J=j;
}
}
SOFT COMPUTING IT-802 Page 15
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

u[J][0]+=a*(y-u[J][0]);
t[J][0]+=b*(x-t[J][0]);
printf("\n Input=%f",x);
printf("\n Updated wights u:");
for(j=0;j<=9;j++)
{
printf("%f\t",u[j][0]);
n[j]=0;
d[j]=0;
}
printf("\nUpdated weights t:");
for(j=0;j<=9;j++)
{
printf("%f\t",t[j][0]);
}
k=0;
J=0;
a=a/1.014;
b=b/1.014;
x=x+0.5;
y=1/x;
getch();
}while(x<=10.5);
getch();
}

Experiment-6
Write a program for different Artificial Intelligence algorithms- Hill
Climbing, A and AO* algorithm in C++

Hill Climbing algorithms


#include<iostream>
#include<cstdio>
using namespace std;

SOFT COMPUTING IT-802 Page 16


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

int calcCost(int arr[],int N){


int c=0;
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++) if(arr[j]<arr[i]) c++;
}
return c;
}

void swap(int arr[],int i,int j){


int tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}

int main(){
int N;
scanf("%d",&N);
int arr[N];
for(int i=0;i<N;i++) scanf("%d",&arr[i]);
int bestCost=calcCost(arr,N),newCost,swaps=0;;
while(bestCost>0){
for(int i=0;i<N-1;i++){
swap(arr,i,i+1);
newCost=calcCost(arr,N);
if(bestCost>newCost){
printf("After swap %d: \n",++swaps);
for(int i=0;i<N;i++) printf("%d ",arr[i]);
printf("\n");
bestCost=newCost;
}
else swap(arr,i,i+1);
}
}
printf("Final Ans\n");
for(int i=0;i<N;i++) printf("%d ",arr[i]);
printf("\n");

SOFT COMPUTING IT-802 Page 17


INDORE INSTITUTE OF SCIENCE & TECHNOLOGY, INDORE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

return 0;
}

Online Case Studies


http://www.wileyindia.com/wileyprecise-3/precise-textbooks/principles-of-
soft-computing-2nd-ed.htm\

http://www.mathworks.com/matlabcentral/fileexchange/32905-neural-
network-simple-programs-for-beginners

SOFT COMPUTING IT-802 Page 18

You might also like