Anda di halaman 1dari 13

LAPORAN TUGAS

JARINGAN SYARAF TIRUAN


HEBB DAN PERCEPTRON

Disusun oleh:
Ratna Maulidiyah

(1210100021)

Nurul Hilmi Rahmawati (1210100023)

Dosen:
Prof. DR. Mohammad Isa Irawan, MT

JURUSAN MATEMATIKA
INSTITUT TEKNOLOGI SEPULUH NOPEMBER
2014

BAB I
DESKRIPSI TUGAS
Tugas yang diberikan ada 2 yaitu program pencarian pola dengan
menggunakan metode hebb dan perceptron.
Program yang dibuat terdiri dari 4 tabel, yaitu:
1. Tabel AND
x1
1
1
0
0

x2
1
0
1
0

t
1
0
0
0

x2
1
0
1
0

t
1
1
1
0

x2
0
1
0
1

t
0
0
1
0

2. Tabel OR
x1
1
1
0
0
3. Tabel NOR
x1
1
1
0
0

4. Tabel NOT AND


x1
1
1
0
0

x2
0
1
0
1

t
1
0
1
1

BAB II
LISTING PROGRAM
PROGRAM HEBB
package hebb;
import java.util.*;
public class Hebb {
public static void main(String[] args) {
int x11 = 0, x12 = 0;
int x21 = 0, x22 = 0;
int x31 = 0, x32 = 0;
int x41 = 0, x42 = 0;
int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
double w01=0, w02=0;
double w11 = 0, w12;
double w21, w22;
double w31, w32;
double w41, w42;
double y1, y2, y3, y4;
double b01;
double xb11, xb12;
double xb21, xb22;
double xb31, xb32;
double xb41, xb42;
double bb1, bb2, bb3, bb4;
int pili;
int iter=0;
int buji;
double yuji1, yuji2, yuji3, yuji4;
double net1, net2, net3, net4;
Scanner input = new Scanner(System.in);
System.out.println("Contoh Pembelajran JST dengan Hebb");
System.out.println("--------------------------------------\n");
System.out.println("Masukan dari w1 dan w2 adalah 1 & 0");
System.out.println("\nbatas normal dari nilai w adalah -0.5 - o.5");
System.out.print("Masukkan nilai w1 : ");
w01 = input.nextDouble();
System.out.print("Masukkan nilai w2 : ");
w02 = input.nextDouble();
System.out.print("Masukkan nilai bias/b = ");

b01 = input.nextDouble();
//System.out.print("Masukkan jumlah iterasi : ");
//iterasi = input.nextInt();
System.out.println("\nPilih tabel yang dipake");
System.out.println("1. Tabel AND");
System.out.println("2. Tabel OR");
System.out.println("3. Tabel NOR");
System.out.println("4. Tabel NOT AND");
System.out.print("Tabel yang dipilih adalah : ");
pili = input.nextInt();
switch(pili)
{
case 1:
System.out.println("\nDengan Tabel AND dicari fd");
x11=1; x12=1;
x21=1; x22=0;
x31=0; x32=1;
x41=0; x42=0;
t1= x11 & x12;
t2= x21 & x22;
t3= x31 & x32;
t4= x41 & x42;
break;
case 2 :
System.out.println("\nDengan Tabel OR dicari fd");
x11=1; x12=1;
x21=1; x22=0;
x31=0; x32=1;
x41=0; x42=0;
t1= x11 | x12;
t2= x21 | x22;
t3= x31 | x32;
t4= x41 | x42;
break;
}
// cek tabel
//while(iter <=5)
w11 = x11 * t1; w12= x12 * t1; y1=t1; xb11=w01+w11; xb12=w02+w12;
bb1=b01+y1;
w21 = x21 * t2; w22= x22 * t2; y2=t2; xb21=xb11+w21; xb22=xb12+w22;
bb2=bb1+y2;
w31 = x31 * t3; w32= x32 * t3; y3=t3; xb31=xb21+w31; xb32=xb22+w32;
bb3=bb2+y3;
w41 = x41 * t4; w42= x42 * t4; y4=t4; xb41=xb31+w41; xb42=xb32+w42;
bb4=bb3+y4;
System.out.println("tabel iterasi pertama untuk pilihan tabel: " +pili);

System.out.println("x1 x2tdw1dw2dbx1bx2bbb");
System.out.println("--------------------------------------------");
System.out.println(+x11+""+x12+""+t1+""+w11+""+w12+""+y1+""+xb11+""+x
b12+""+bb1);
System.out.println(+x21+""+x22+""+t2+""+w21+""+w22+""+y2+""+xb21+""+x
b22+""+bb2);
System.out.println(+x31+""+x32+""+t3+""+w31+""+w32+""+y3+""+xb31+""+x
b32+""+bb3);
System.out.println(+x41+""+x42+""+t4+""+w41+""+w42+""+y4+""+xb41+""+x
b42+""+bb4);
// uji pengenalan pola
//bias = b01
// b01 >= new y=1
// b01 < net y=0
System.out.print("\nMasukkan bias untuk uji training = ");
buji = input.nextInt();
net1 =(x11*xb41)+(x12*xb42)+bb4;
if (net1>=buji)
yuji1 = 1;
else
yuji1 = 0;
net2 =(x21*xb41)+(x22*xb42)+bb4;
if (net2>=buji)
yuji2 = 1;
else
yuji2 = 0;
net3 =(x31*xb41)+(x32*xb42)+bb4;
if (net3>=buji)
yuji3 = 1;
else
yuji3 = 0;
net4 =(x41*xb41)+(x42*xb42)+bb4;
if (net4>=buji)
yuji4 = 1;
else
yuji4 = 0;
System.out.println("\ntabel yg dipilih uji training thd data");
System.out.println("x1 x2nety(uji)");
System.out.println("--------------------");
System.out.println(+x11+""+x12+""+net1+""+yuji1);
System.out.println(+x11+""+x12+""+net2+""+yuji2);
System.out.println(+x11+""+x12+""+net3+""+yuji3);
System.out.println(+x11+""+x12+""+net4+""+yuji4);
if ((t1==yuji1)&&(t2==yuji2)&&(t3==yuji3)&&(t4==yuji4))
System.out.println("\nJST Mengenali POLA");
else

System.out.println("\nJST TIDAK Mengenali POLA");


}}

PROGRAM PERCEPTRON
package perceptron;
import java.util.Scanner;
public class Perceptron {
public static void main(String[] args) {
int x11 = 1, x12 = 1;
int x21 = 1, x22 = 0;
int x31 = 0, x32 = 1;
int x41 = 0, x42 = 0;
double inpx1, inpx2, inp;
// TARGET
int t1 = 1, t2 =1, t3 = 1, t4 =-1;
// W1 LAMA dan W2 LAMA
double w01=0, w02=0;

double w11 = 0, w12;


double w21, w22;
double w31, w32;
double w41 = 0, w42;
//b
double y1, y2, y3, y4;
// Bias Awal
double b01;

double xb11, xb12;


double xb21, xb22;
double xb31, xb32;
double xb41 = 0, xb42 = 0;
double bb1, bb2, bb3, bb4 = 0;
double alfa;
int pili;
int iter=0;
int batasiter=0;
double buji;

double buji1 = 0;
double yuji1 = 0, yuji2 = 0, yuji3 = 0, yuji4 = 0;

// Y_in
double net1 = 0, net2, net3, net4;
Scanner input = new Scanner(System.in);
System.out.println("Contoh Pembelajran JST dengan Perceptron");
System.out.println("--------------------------------------\n");
System.out.println("w1 dan w2 adalah bobot awal.");
System.out.print("Masukkan nilai w1 : ");
w01 = input.nextDouble();
System.out.print("Masukkan nilai w2 : ");
w02 = input.nextDouble();
System.out.print("Masukkan nilai bias/b0 = ");
b01 = input.nextDouble();
System.out.print("Masukkan nilai alfa = ");
alfa= input.nextDouble();
System.out.print("\nMasukkan threshold (Teta) = ");
buji = input.nextDouble();
System.out.print("\nMasukkan jumlah iterasi = ");
batasiter = input.nextInt();
// proses pembelajaran
while (iter < batasiter)
{
net1 =(x11*w01)+(x12*w02)+b01;
buji1= 0-buji;
if (net1>=buji)
yuji1 = 1;
else if (net1<buji1)
yuji1 = -1;
else
yuji1 = 0;
if(t1==yuji1)
{
w11=0;
w12=0;
y1=0;
xb11=w01+w11;
xb12=w02+w12;

bb1=b01+y1;
}
else
{
w11 = x11 * t1 * alfa;
w12= x12 * t1 * alfa;
y1=alfa*t1;
xb11=w01+w11;
xb12=w02+w12;
bb1=b01+y1;
}
net2 =(x21*xb11)+(x22*xb12)+bb1;
if (net2>buji)
yuji2 = 1;
else if (net2<buji1)
yuji2 = -1;
else
yuji2 = 0;
if(t2==yuji2)
{
w21=0;
w22=0;
y2=0;
xb21=xb11+w21;
xb22=xb12+w22;
bb2=bb1+y2;
}
else
{
w21 = x21 * t2 * alfa;
w22= x22 * t2 * alfa;
y2=alfa*t2;
xb21=xb11+w21;
xb22=xb12+w22;
bb2=bb1+y2;
}
net3 =(x31*xb21)+(x32*xb22)+bb2;
if (net3>buji)
yuji3 = 1;
else if (net3<buji1)
yuji3 = -1;
else
yuji3 = 0;

if(t3==yuji3)
{
w31=0;
w32=0;
y3=0;
xb31=xb21+w31;
xb32=xb22+w32;
bb3=bb2+y3;
}
else
{
w31 = x31 * t3 * alfa;
w32= x32 * t3 * alfa;
y3=alfa*t3;
xb31=xb21+w31;
xb32=xb22+w32;
bb3=bb2+y3;
}
net4 =(x41*xb31)+(x42*xb32)+bb3;
if (net4>buji)
yuji4 = 1;
else if (net4<buji1)
yuji4 = -1;
else
yuji4 = 0;
if(t4==yuji4)
{
w41=0;
w42=0;
y4=0;
xb41=xb31+w41;
xb42=xb32+w42;
bb4=bb3+y4;
}
else
{
w41 = x41 * t4 * alfa;
w42= x42 * t4 * alfa;
y4=alfa*t4;
xb41=xb31+w41;
xb42=xb32+w42;
bb4=bb3+y4;
}

System.out.println("tabel iterasi "+iter);


System.out.println("x1\tx2\tt\tY_in\tY\tw1\tw2\tb\tW1\tW2\tbbaru");
System.out.println("------------------------------------------------------------------------------------");
System.out.println("\t\t\t\t\t\t\t\t"+String.format("%.1f",w01)+"\t"+String.format("
%.1f",w02)+"\t" +String.format("%.1f",b01));
System.out.println("\n");
System.out.println(+x11+"\t"+x12+"\t"+t1+"\t"+String.format("%.1f",net1)+"\t"+
yuji1+"\t"+w11+"\t"+w12+"\t"+y1+"\t"+String.format("%.1f",xb11)+"\t"+String.f
ormat("%.1f",xb12)+"\t"+String.format("%.1f",bb1));
System.out.println(+x21+"\t"+x22+"\t"+t2+"\t"+String.format("%.1f",net2)+"\t"+
yuji2+"\t"+w21+"\t"+w22+"\t"+y2+"\t"+String.format("%.1f",xb21)+"\t"+String.f
ormat("%.1f",xb22)+"\t"+String.format("%.1f",bb2));
System.out.println(+x31+"\t"+x32+"\t"+t3+"\t"+String.format("%.1f",net3)+"\t"+
yuji3+"\t"+w31+"\t"+w32+"\t"+y3+"\t"+String.format("%.1f",xb31)+"\t"+String.f
ormat("%.1f",xb32)+"\t"+String.format("%.1f",bb3));
System.out.println(+x41+"\t"+x42+"\t"+t4+"\t"+String.format("%.1f",net4)+"\t"+
yuji4+"\t"+w41+"\t"+w42+"\t"+y4+"\t"+String.format("%.1f",xb41)+"\t"+String.f
ormat("%.1f",xb42)+"\t"+String.format("%.1f",bb4));
System.out.println("\n");
if((t1!=yuji1)|(t2!=yuji2)|(t3!=yuji3)|(t4!=yuji4))
{
w01 = xb41;
w02 = xb42;
b01 = bb4;
}
else
break;
iter++;
}
if ((t1==yuji1)&&(t2==yuji2)&&(t3==yuji3)&&(t4==yuji4))
{
System.out.println("\nDengan "+(iter+1)+" iterasi JST Mengenali POLA");
}
else
System.out.println("\nDengan "+iter+" iterasi JST TIDAK Mengenali
POLA");

System.out.println("PROSES PENGUJIAN");
System.out.println("----------------");
System.out.print("X1 :");
inpx1 = input.nextInt();
System.out.print("X2 :");
inpx2 = input.nextInt();
inp = bb4+(inpx1*xb41)+(inpx2*xb42);
System.out.println("Output"+ String.format("%.1f",inp));
if (inp>buji){
yuji3 = 1;
} else if (inp<buji1)
yuji3 = -1;
else
yuji3 = 0;
System.out.println("Y = "+yuji3);
}

BAB III
RUNNING PROGRAM

PROGRAM HEBB

PROGRAM PERCEPTRON