Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.text.DecimalFormat;
  2. import java.util.LinkedList;
  3. import javax.swing.*;
  4. public class Perceptron{
  5.     private int x1=1,x2=1;
  6.     private double w1,w2;
  7.     private int []desired={0,0,0,1};
  8.     private double y=0;
  9.     private double c=0.2,alpha=0.1;
  10.     private double error;
  11.     private StringBuffer st;
  12.     public Perceptron(){
  13.  
  14.     }
  15.  
  16.     public void start(){    
  17.         st=new StringBuffer();
  18.         boolean exe=true;
  19.         int count=0;
  20.  
  21.         st.append(" x1  x2| desired |  w1\t  w2\t|actual | error\t|  w1\tw2 \n");
  22.         st.append(" ============================================================ \n");
  23.         while(exe&&count<50){
  24.             exe=false;
  25.             for(int i=0;i<4;i++){
  26.                 x2=Math.abs(x2-1); //to generate input,x2 0,1
  27.                 if(x2==0){
  28.                     x1=Math.abs(x1-1);//to generate input,x1 0,1
  29.                 }
  30.                 st.append("  "+x1+"  "+x2+" |     "+desired[i]
  31.                     +"\t | "+String.format("%.1f",w1)+"\t "+String.format("%.1f",w2)+"\t|    " );
  32.  
  33.                 activation();//activation process : y = summation of input(i) x weight(i), minus threshold
  34.  
  35.                 int actual;
  36.                 if(y<0){// y is negative
  37.                     actual = 0;
  38.                 }
  39.                 else{// y is positive
  40.                     actual = 1;
  41.                 }
  42.                 error=desired[i]-actual;
  43.                 if(error!=0){//error occur : error = -1,1
  44.                     exe=true;
  45.  
  46.                     weightTraining();// weight training : produce new weight
  47.                 }
  48.                 st.append(actual+"\t| "+error+"\t| "+w1+"  "+w2);
  49.                 st.append("\n");
  50.             }
  51.             count++;
  52.             st.append(" ============================================================ \n");
  53.         }
  54.         if(count>=50){
  55.             JOptionPane.showMessageDialog(null,"Program Terminate.\nbecause system can't learn.\n(Infinite Loop)'","Message",JOptionPane.ERROR_MESSAGE);
  56.         }
  57.     }
  58.  
  59.     public void initialize(double tres,double alpha,double w1, double w2){
  60.         this.w1=Double.parseDouble(String.format("%.1f",w1));
  61.         this.w2=Double.parseDouble(String.format("%.1f",w2));
  62.         this.c=Double.parseDouble(String.format("%.1f",tres));
  63.         this.alpha=Double.parseDouble(String.format("%.1f",alpha));
  64.  
  65.     }
  66.  
  67.     public void activation(){
  68.         y=(x1*w1+x2*w2)-c;
  69.     }
  70.  
  71.     public void weightTraining(){
  72.         DecimalFormat df = new DecimalFormat("#.#");
  73.         Double.parseDouble(df.format(w1));
  74.         w1=Double.parseDouble(df.format(w1+(alpha*error*x1)));
  75.         w2=Double.parseDouble(df.format(w2+(alpha*error*x2)));
  76.  
  77.     }
  78.  
  79.     public void reset(){
  80.     }
  81.  
  82.     public String toString(){
  83.         String str=st.toString();
  84.         return str;
  85.     }
  86.  
  87. }