 # Single Layer Perceptron Learning Algorithm and Flowchart of the Program and the Code of the Program in C++  The Single Layer Perceptron Learning Algorithm:

Step 1:        Initialize all weights and threshold and values.
Step 2:        Get the weighted sum of one input.
Step 3:        Compare the weighted sum with the threshold and set value to output.
Step 4:        If the input is of class A

If the desired output mismatches with the output
Decrease those weights which have corresponding 1 in the input
Else
Take the next input and go to Step 2
Else
If the desired output mismatches with the output
Increase those weights which have corresponding 1 in the input
Else
Take the next input and go to Step 2
Step 5:        While all weights has been set, take input from the user.
Step 6:        Get the weighted sum for this input
Step 7:        Compare the weighted sum with the threshold
if weighted sum is less then the threshold
input is from Class A
Else
input is from class B

The Flowchart of the Program:

Part 01 of the Flowchart of the Program on Single Layer Perceptron Learning Algorithm
[Click on the Image to Enlarge for Better View]

Part 02 of the Flowchart of the Program on Single Layer Perceptron Learning Algorithm

The Code of the Program:

1)      /*
2)      Perceptron Leanrning Algorithm
3)      By Tanmay Chakrabarty
4)      ID 08520011
5)      CSE, UITS, Rajshahi Campus
6)
7)      Compiled and Ran succesfully with Dev C++
8)      */

9)      #include<iostream.h>
10)     #include<conio.h>

11)     int main()
12)          {
13)          double  weights, Threshold = 0.5, sum = 0.0, output=0.0;
14)          int values, given_value, i, j, k;
15)          char* new_start = "Not Required";
16)          char option;
17)          /*
18)          weights for storing the weights | Threshold to store the threshold
19)          sum to store the weighted sum | output to store the 0 or 1 after comparing the sum with the threshold
20)          values to store the values | given_value is the value that will entered by the user to test
21)          i, j, k are loop controller | new_start is an indicator whether to start from the beginning or not while weights have been found ok for one of the 16 values
22)          option has been used to take input from the user for program flow control
23)          */

24)          //---(calculating and storing the values for the values)
25)          i=0;
26)          j=0;
27)          while(i<16)
28)               {
29)               j=3;
30)               k=i;
31)               while(j>=0)
32)                    {
33)                    values[i][j]=k%2;
34)                    k=k/2;
35)                    j--;
36)                    }
37)               i++;
38)               }
39)   //------------------------------------------------------

40)     //starting the main loop
41)     option='s';
42)     while(option=='s' || option=='S')
43)               {
44)               cout<<"\n---No kind of input error has been handled, enter accuret values)-----";
46)               j=0;
47)               while(j<4)
48)                    {
49)                    cout<<"\tWeights 01: ";
50)                    cin>>weights[j];                 //taking the weights from the user
51)                    j++;
52)                    }
53)               cout<<"Please input the Threshold : ";
54)               cin>>Threshold;                  //taking threshold from the user

55)               i=0;

56)               while(i<16)                      //to run the loop 16 times (0-15)
57)                    {
58)                    sum=0;
59)                    j=0;
60)                    while(j<4)
61)                         {
62)                         sum += (values[i][j]*weights[j]); //calculating the weighted sum
63)                         j++;
64)                         }
65)                    if(sum < Threshold)                   //comparing weighted sum with threshold
66)                         output = 0;                 //and setting the value for the output
67)                    else
68)                          output = 1;

69)               if(i < 8)                             //to find which class the value is from, it indicates that this is from class A (0-7)
70)               {
71)               if(output != 0)                       //The desired output of class A is 0, but if not then we will decrease
72)                    {                                //that corresponding weight of the input
73)                    j=0;
74)                    while(j<4)
75)                         {                           //multiplying with corresponding values beacuse we need to
76)                         weights[j] -= ((0.1) * (values[i][j]));     //that weight which correspondingly having 1 in its input
77)                         j++;                        //if value is 0, "((0.1) * (values[i][j]))" will result 0
78)                         }                            //thus the weight will not decrease, otherwise, it will
79)                    new_start = "Required";               //as wrong weights has been found, thus after we have decreased the weigths
80)                    i=i;                             //we will test these weights for these inputs again and after ok, this value of
81)                    }                               //new_start will indicate to start from first value with these new corrected weights
82)               else
83)                    {
84)                    if(new_start=="Required")   //when weights has been found ok and new_start = "Required", it means we will start from first
85)                         {
86)                         new_start="Not Required";   //Untill we found these weight faulty for any value we will just go on
87)                         i=0;
88)                         }
89)                    else
90)                         i++;                        //weights are ok, so go on
91)                    }
92)               }
93)               else                             //this is class B part and same as previous, only that we will increase weights here
94)                    {
95)                    if(output == 1)
96)                         {
97)                         if(new_start=="Required")
98)                              {
99)                              new_start="Not Required";
100)                            i=0;
101)                            }
102)                       else
103)                            i++;
104)                       }
105)                  else
106)                       {
107)                       j=0;
108)                       while(j<4)
109)                            {
110)                            weights[j] += ((0.1) * (values[i][j]));
111)                            j++;
112)                            }
113)                       new_start = "Required";
114)                       i=i;
115)                       }
116)                  }
117)             }

118)   cout<<"\n\nWeights are "<<weights<<" , "<<weights<<" , "<<weights<<" , "<<weights<<" while threshold is "<<Threshold;

119)   cout<<"\n\n---------------------(Weights are set, ready to percept)---------------------";
120)   option='e';
121)   while(option=='e' or option=='E')
122)        {
123)        cout<<"\n\nPlease enter an element to find which class is it of : \t";

124)        j=0;
125)        while(j<4)
126)             {
127)             cin>>given_value[j];                  //taking the value to test from user
128)             cout<<"\t\t\t\t\t\t\t";
129)             j++;
130)             }
131)        sum=0;
132)        j=0;
133)        while(j<4)
134)             {
135)             sum += (given_value[j]*weights[j]);        //getting the weight
136)             j++;
137)             }
138)        if(sum < Threshold)                   //comparing the weight with the threshold and getting the result
139)             cout<<"\nThe percepted element "<<given_value<<given_value<<given_value<<given_value<<" is from Class A\n";
140)        else
141)             cout<<"\nThe percepted element "<<given_value<<given_value<<given_value<<given_value<<" is from Class B\n";

142)        cout<<"\nPress """"e"""" to enter an element again or """"s"""" to start from the beginning, any (else key+enter) to exit: ";
143)        option=getch();
144)        if(option!='e' || option!='E' || option!='s' || option!='S' )
145)             system("EXIT");
146)        }
147)   }

148)   return 0;
149)   }

The Output of the Program:

[It was an assignment when I was having the course Theory of Computation. I don't think this blog post has simplified the matter at all. However if it helps you anyway, I will be glad to know that.]