0% found this document useful (0 votes)
53 views16 pages

Soft Computing Lab Projects in CSE

This document contains 3 programs related to machine learning and neural networks: 1. A perceptron program that trains a perceptron using a fixed increment learning algorithm and outputs the final weights. 2. A genetic algorithm implementation of the traveling salesman problem that finds the optimal tour between nodes. 3. A program that implements basic logic gates like AND, OR, NOT, and XOR and outputs the result.

Uploaded by

Ravi Shukla
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)
53 views16 pages

Soft Computing Lab Projects in CSE

This document contains 3 programs related to machine learning and neural networks: 1. A perceptron program that trains a perceptron using a fixed increment learning algorithm and outputs the final weights. 2. A genetic algorithm implementation of the traveling salesman problem that finds the optimal tour between nodes. 3. A program that implements basic logic gates like AND, OR, NOT, and XOR and outputs the result.

Uploaded by

Ravi Shukla
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

Department of Computer Science

and Engineering

Name of the Faculty : Ms. Sharda Tewari

Submitted By : Daksh Gupta


Roll No. : 208330199
Lab Name :
Lab No. : Soft Computing Lab (TCS-351)
Session : 2021-22
Branch / Semester : B.Tech CSE , 2nd year ,3rd semester
1. Create a perceptron with appropriate no. of inputs and outputs.
Train it using fixed increment learning algorithm until no change
in weights is required. Output the final weights. :-

Algorithm :
Start with a randomly chosen weight vector w0;
Let k=1;
While these exists input vector that are misclassified by: Wk-1 do
Let i be a misclassified input vector
Let Xk=class(ij)ij, impling that Wk-1.Xk<0
Update the weight vector to Wk= Wk-1 + nXk;
increment k;
End while;

Program :
#include<conio.h>
#include<iostream.h>
Void main( )
{
clrscr( );
int in[3],d,w[3],a=0;
for(inti=0;i<3,i++)
{
cout<<‖\n initialize the weight vector w‖<<i;
cin>>w[i]
}
for(i=0;i<3:i++)
{
cout<<‖\n enter the input vector i‖<<i;
cin>>in[i];
}
cout<<‖\n enter the desined output‖;
cin>>d;
intans=1;
while(ans= = 1)
{
for (a= 0, i==0;i<3;i++)
{
a = a + w[i] * in[i];
}
clrscr( );
cout<<‖\n desired output is‖<<d;
cout<<‖\n actual output is ―<<a;
int e;
e=d-a;
cout<<‖\n error is ―<<e;
cout<<‖\n press 1 to adjust weight else 0‖;
cin>>ans;
if (e<0)
{
for(i=0;i<3;i++)
{
w[i]=w[i]-1;
}
else if (e>0)
{
for(i=0;i<3:i++)
{
w[i]=w[i]+1;
}
}
}
getch( );
}

Output:
Desire output is 2
Actual output is 17
Error is -15
Press 1 to adjust weight else 0
2. Implement TSP using GA :-

Program:
importjava.util.*;
importjava.text.*;
class TSP
{
int weight[][],n,tour[],finalCost;
finalint INF=1000;
public TSP()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of nodes:=>");
n=s.nextInt();
weight=new int[n][n];
tour=new int[n-1];
for(inti=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
{
System.out.print("Enter weight of "+(i+1)+" to "+(j+1)+":=>");
weight[i][j]=s.nextInt();
}
}
}
System.out.println();
System.out.println("Starting node assumed to be node 1.");
eval();
}
publicint COST(intcurrentNode,intinputSet[],intsetSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
intsetToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(inti=0;i<setSize;i++)
{
int k=0;//initialise new set
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int
temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return min;
}
publicint MIN(intcurrentNode,intinputSet[],intsetSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
intsetToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(inti=0;i<setSize;i++)//considers each node of inputSet
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int
temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
returnminindex;
}
public void eval()
{
intdummySet[]=new int[n-1];
for(inti=1;i<n;i++)
dummySet[i-1]=i;
finalCost=COST(0,dummySet,n-1);
constructTour();
}
public void constructTour()
{
intpreviousSet[]=new int[n-1];
intnextSet[]=new int[n-2]; for(inti=1;i<n;i++)
previousSet[i-1]=i;
intsetSize=n-1;
tour[0]=MIN(0,previousSet,setSize);
for(inti=1;i<n-1;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(tour[i-1]!=previousSet[j])
nextSet[k++]=previousSet[j];
}
--setSize;
tour[i]=MIN(tour[i-1],nextSet,setSize);
for(int j=0;j<setSize;j++)
previousSet[j]=nextSet[j];
}
display();
}
public void display()
{
System.out.println();
System.out.print("The tour is 1-");
for(inti=0;i<n-1;i++)
System.out.print((tour[i]+1)+"-");
System.out.print("1");
System.out.println();
System.out.println("The final cost is "+finalCost);
}
}
classTSPExp
{
public static void main(String args[])
{
TSP obj=new TSP();
}
}
Output :
Enter no. of nodes:=> 5
Enter weight of 1 to 2:=>4
Enter weight of 1 to 3:=>6
Enter weight of 1 to 4:=>3
Enter weight of 1 to 5:=>7
Enter weight of 2 to 1:=>3
Enter weight of 2 to 3:=>1
Enter weight of 2 to 4:=>7
Enter weight of 2 to 5:=>4
Enter weight of 3 to 1:=>7
Enter weight of 3 to 2:=>4
Enter weight of 3 to 4:=>3
Enter weight of 3 to 5:=>6
Enter weight of 4 to 1:=>8
Enter weight of 4 to 2:=>5
Enter weight of 4 to 3:=>3
Enter weight of 4 to 5:=>2
Enter weight of 5 to 1:=>4
Enter weight of 5 to 2:=>3
Enter weight of 5 to 3:=>2
Enter weight of 5 to 4:=>1
Starting node assumed to be node 1.
The tour is 1-2-3-4-5-1
The final cost is 14
3.DESCRIPTION: Write a program to implement logic gates.

Program:
#include <iostream>
int main()
{
char menu; //Menu control variable
int result; //final output variable
int dataValue1;
int dataValue2;
cout<< "enter your Boolean operator code: (A,O,N,X): ";
cin>> menu;
switch (menu) //Menu control variable
{
case 'A':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
cout<< "Enter second Boolean value:";
cin>> dataValue2;
if(dataValue1 == 1 && dataValue2 == 1)
{
result = 1;
}
else
{
result = 0;
}
cout<< "show result:" << result;
break;
case 'O':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
cout<< "Enter second Boolean value:";
cin>> dataValue2;
if(dataValue1 == 1 || dataValue2 == 1)
{
result = 1;
}else
{
result = 0;
}
cout<< "show result:" << result;
break;
case 'N':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
result = !dataValue1;
cout<< "show result:" << result;
break;
case 'X':
cout<< "Enter first Boolean value:";
cin>> dataValue1;
cout<< "Enter second Boolean value:";
cin>> dataValue2;
if(dataValue1 = !dataValue1)
{
result = 1;
}else
{
result = 0;
}
cout<< "show result:" << result;
break;
default:
result = 0;
break;
}//end switch
cin.ignore(2);
return 0;
}//end main

Output:
Enter your Boolean operator code :(A,O,N,X) : A
Enter First Boolean value :1
Enter Second Boolean value :1
Show Result :1
Enter your Boolean operator code :(A,O,N,X) : O
Enter First Boolean value :1
Enter Second Boolean value :0
Show Result :1

4. DESCRIPTION: Write a program for Back Propagation Algorithm

Program:
# include <iostream.h>
#include <conio.h>
void main ()
{
inti ;
float delta, com, coeff = 0.1;
struct input
{
floatval,out,wo, wi;
int top;
} s[3] ;
cout<< ―\n Enter the i/p value to target o/p‖ << ―\t‖;
for (i=0; i<3 ; i++)
cin>> s [i], val>> s[i], top);
i = 0;
do
{
if (i = = 0)
{
W0 = -1.0;
W1 = -0.3;
}
else
{
W0 = del [i - 1], W0 ;
W1 = del [i - 1] , Wi ;
}
del [i]. aop = w0 + (wi * del [i]. val);
del [i].out = del [i]. aop);
delta = (top – del [i]. out) * del [i].out * (1 – del [i].out);
corr = coeff * delta * del [i].[out];
del [i].w0 = w1 + corr;
del [i]. w1 = w1 + corr;
i++;
}While ( i ! = 3)
cout<< ―VALUE‖<<‖Target‖<<‖Actual‖<<‖w0‖ <<‖w1‖<<’\n;
for (i=0; i=3; i++)
{
cout<< s [i].val<< s[i].top<<s[i].out << s[i]. w0<< s[i]. w1;
cout<< ―\n‖;
}
getch ();
}

Output:
Back Propagation network
1. Load data
2. Learn from the data
3. Compute output pattern
4. Make new data file
5. Save data
6. Print data
7. Change learning rate
8. Exit
Enter your Choice (1-8)

You might also like