Vous êtes sur la page 1sur 2

Main.

java

package kecerdasanbuatan;

public class Main {

public static void main(String[] args) {


// TODO code application logic here
perceptron m = new perceptron();
m.hitung();
}
}

Perceptron.java

package kecerdasanbuatan;

public class perceptron {


private int x1[], x2[], bias[], target[];
private double w[], b, alfa, theta, yin;
private boolean kondisi = false;

public perceptron(){
x1 = new int[4];
x2 = new int[4];
bias = new int[4];
target = new int [4];
alfa = 0.8;
theta = 0.5;
w = new double [2];
w[0]=0;
w[1]=0;
b=0;
x1[0]=1;
x1[1]=1;
x1[2]=0;
x1[3]=0;
x2[1]=0;
x2[2]=1;
x2[3]=0;
x2[0]=1;
for(int i=0;i<4;i++){
bias[i]=1;
}
target[0]=1;
for(int i=1;i<4;i++){
target[i]=-1;
}
}

public void hitung(){


int y;
double temp;
//int i =0;
int stop=1;
while(kondisi==false){
stop=0;
for(int i =0;i<4;i++){
yin = b+(x1[i]*w[0])+(x2[i]*w[1]);
if(yin>theta)
y=1;
else if((yin >= -theta)&&(yin <= theta))
y=0;
else
y=-1;
temp = b;
if(y!=target[i]){
w[0] = w[0]+(alfa*target[i]*x1[i]);
w[1] = w[1]+(alfa*target[i]*x2[i]);
b = b + (alfa*target[i]);
}
else{
stop++;
}
}

if(stop==4){
kondisi = true;
}
else{
kondisi = false;
}
//}
}
System.out.println("w[0] = "+w[0]+" w[1] = "+w[1]+" b= " + b);
}
}

Vous aimerez peut-être aussi