Jst - Metode Perceptron

Langsung saja ya.. cek code berikut untuk algoritma perceptron dengan bahasa pemrograman java



/*
 * Program ini ditulis dikomputer
 * Iskandar M. Alawi.
 */

package jstgo;
/*
 * Program ini ditulis dikomputer
 * Iskandar M. Alawi.
 */

import java.util.*;

/*
 *Administrator @ Iskandar M. Alawi
 */
public class Perceptron {
    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 = 0, 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;
    double alfa;

    int pili;
    int iter=0;
    int batasiter=0;
    int buji;
    double yuji1 = 0, yuji2 = 0, yuji3 = 0, yuji4 = 0;
    double net1 = 0, net2, net3, net4;

        Scanner input = new Scanner(System.in);

        System.out.println("Contoh Pembelajran JST untuk pengenalan tabel OR / AND");
        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 untuk uji training = ");
        buji = input.nextInt();
        System.out.print("\nMasukkan jumlah iterasi = ");
        batasiter = input.nextInt();

        System.out.println("\nPilih tabel yang dipake");
        System.out.println("1. Tabel OR");
        System.out.println("2. Tabel AND");
        System.out.print("Tabel yang dipilih adalah : ");
        pili = input.nextInt();

        switch(pili)
        {
        case 1:
            System.out.println("\nDengan Tabel OR");
            x11=1;  x12=1;
            x21=1;  x22=-1;
            x31=-1; x32=1;
            x41=-1; x42=-1;

            t1= x11 & x12;
            t2= x21 & x22;
            t3= x31 & x32;
            t4= x41 & x42;
        break;
        case 2 :
            System.out.println("\nDengan Tabel AND");
            x11=1;  x12=1;
            x21=1;  x22=-1;
            x31=-1; x32=1;
            x41=-1; x42=-1;

            t1= x11 | x12;
            t2= x21 | x22;
            t3= x31 | x32;
            t4= x41 | x42;
        break;
        }

    // cek tabel
    while (iter < batasiter)
    {
    net1 =(x11*w01)+(x12*w02)+b01;
    if (net1>buji)
        yuji1 = 1;
    else if (net1==buji)
        yuji1 = 0;
    else
        yuji1 = -1;

    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==buji)
        yuji2 = 0;
    else
        yuji2 = -1;

    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==buji)
        yuji3 = 0;
    else
        yuji3 = -1;

    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==buji)
        yuji4 = 0;
    else
        yuji4 = -1;

    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+" untuk pilihan tabel: " +pili);
    System.out.println("x1    x2     t    net    fnet   dw1     dw2     db     x1b    x2b    bb");
    System.out.println("-----------------------------------------------------------------------");
    System.out.println(+x11+"     "+x12+"     "+t1+"    "+net1+"    "+yuji1+"    "+w11+"     "+w12+"     "+y1+"     "+xb11+"    "+xb12+"    "+bb1);
    System.out.println(+x21+"    "+x22+"    "+t2+"    "+net2+"    "+yuji2+"   "+w21+"     "+w22+"    "+y2+"     "+xb21+"    "+xb22+"    "+bb2);
    System.out.println(+x31+"    "+x32+"    "+t3+"    "+net3+"    "+yuji3+"    "+w31+"    "+w32+"    "+y3+"     "+xb31+"    "+xb32+"   "+bb3);
    System.out.println(+x41+"   "+x42+"    "+t4+"   "+net4+"   "+yuji4+"    "+w41+"     "+w42+"    "+y4+"     "+xb41+"    "+xb42+"   "+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");
}}

Comments

Popular posts from this blog

Shenzhen Hi-tec network Technology co. Ltd

Network Technology course

Network Technology book Pdf