You are on page 1of 7

Nama : Zulfa Iflakhul Fadilla

NIM : 19518241012
Prodi / Kelas : Pendidikan Teknik Mekatronika / E

JST Perceptron
Program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ex_Perceptron
{
public partial class Form1 : Form
{
int x1, x2, b, c;
int w11, w12, w21, w22, b1, b2, v1, v2;
int gerbang, u, net;
int jml_perceptron, y_perceptron;
int[] input_x1 = { 1, 1, -1, -1 };
int[] input_x2 = { 1, -1, 1, -1 };
int[] bias = { 1, 1, 1, 1 };
int[] target_AND = { 1, -1, -1, -1 };
int[] target_OR = { 1, 1, 1, -1 };
int[] target_XOR = { -1, 1, 1, -1 };
int[] target_Z1 = { -1, -1, 1, -1 };
int[] target_Z2 = { -1, 1, -1, -1 };
int[] out_Z1 = new int[4];
int[] out_Z2 = new int[4];
int logika_AND = 0, logika_OR = 0, logika_XOR = 0;
int delta_w1, delta_w2, delta_wb, w1_baru, w2_baru, wb_baru;
int w1_AND, w2_AND, wb_AND, w1_OR, w2_OR, wb_OR;
int w1_lama = 0, w2_lama = 0, wb_lama = 0;
int cek = 0;
int teta = 0, alfa = 1;
public Form1()
{
InitializeComponent();
}

private void x1_0_CheckedChanged(object sender, EventArgs e)


{
x1 = -1;
}

private void x1_1_CheckedChanged(object sender, EventArgs e)


{
x1 = 1;
}

private void x2_0_CheckedChanged(object sender, EventArgs e)


{
x2 = -1;
}

private void x2_1_CheckedChanged(object sender, EventArgs e)


{
x2 = 1;
}

private void radioButtonAND_CheckedChanged(object sender, EventArgs e)


{
gerbang = 1;
if (logika_AND == 0)
{
ulangi:
cek = 0;
for (u = 0; u < 4; u++)
{
jml_perceptron = (input_x1[u] * w1_lama) + (input_x2[u] *
w2_lama) + (bias[u] * wb_lama);
if (jml_perceptron > teta) y_perceptron = 1;
else if (jml_perceptron < teta) y_perceptron = -1;
else y_perceptron = 0;
if (target_AND[u] == y_perceptron)
{ delta_w1 = 0; delta_w2 = 0; delta_wb = 0; }
else
{
delta_w1 = alfa * input_x1[u] * target_AND[u];
delta_w2 = alfa * input_x2[u] * target_AND[u];
delta_wb = alfa * bias[u] * target_AND[u];
}
w1_AND = w1_lama + delta_w1; w1_lama = w1_AND;
w2_AND = w2_lama + delta_w2; w2_lama = w2_AND;
wb_AND = wb_lama + delta_wb; wb_lama = wb_AND;
if (delta_w1 > 0) cek++;
}
if (cek > 0) goto ulangi;
else { w1_lama = 0; w2_lama = 0; wb_lama = 0; }
logika_AND = logika_AND + 1;
}
}

private void radioButtonOR_CheckedChanged(object sender, EventArgs e)


{
gerbang = 2;
if (logika_OR == 0)
{
ulangi:
cek = 0;
for (u = 0; u < 4; u++)
{
jml_perceptron = (input_x1[u] * w1_lama) + (input_x2[u] *
w2_lama) + (bias[u] * wb_lama);
if (jml_perceptron > teta) y_perceptron = 1;
else
if (jml_perceptron < teta) y_perceptron = -1;
else y_perceptron = 0;
if (target_OR[u] == y_perceptron)
{ delta_w1 = 0; delta_w2 = 0; delta_wb = 0; }
else
{
delta_w1 = alfa * input_x1[u] * target_OR[u];
delta_w2 = alfa * input_x2[u] * target_OR[u];
delta_wb = alfa * bias[u] * target_OR[u];
}
w1_OR = w1_lama + delta_w1; w1_lama = w1_OR;
w2_OR = w2_lama + delta_w2; w2_lama = w2_OR;
wb_OR = wb_lama + delta_wb; wb_lama = wb_OR;
if (delta_w1 > 0) cek++;
}
if (cek > 0) goto ulangi;
else { w1_lama = 0; w2_lama = 0; wb_lama = 0; }
logika_OR = logika_OR + 1;
}
}

private void radioButtonXOR_CheckedChanged(object sender, EventArgs e)


{
gerbang = 3;
if (logika_XOR == 0)
{
ulangi:
cek = 0;
for (u = 0; u < 4; u++)
{
jml_perceptron = (input_x1[u] * w1_lama) + (input_x2[u] *
w2_lama) + (bias[u] * wb_lama);
if (jml_perceptron > teta) y_perceptron = 1;
else
if (jml_perceptron < teta) y_perceptron = -1;
else y_perceptron = 0;
if (target_Z1[u] == y_perceptron)
{ delta_w1 = 0; delta_w2 = 0; delta_wb = 0; }
else
{
delta_w1 = alfa * input_x1[u] * target_Z1[u];
delta_w2 = alfa * input_x2[u] * target_Z1[u];
delta_wb = alfa * bias[u] * target_Z1[u];
}
w1_baru = w1_lama + delta_w1; w1_lama = w1_baru;
w2_baru = w2_lama + delta_w2; w2_lama = w2_baru;
wb_baru = wb_lama + delta_wb; wb_lama = wb_baru;
if (delta_w1 > 0) cek++;
}
if (cek > 0) goto ulangi;
else { w1_lama = 0; w2_lama = 0; wb_lama = 0; }
w11 = w1_baru; w12 = w2_baru; b1 = wb_baru;
for (u = 0; u < 4; u++)
{
net = (input_x1[u] * w1_baru) + (input_x2[u] * w2_baru) +
wb_baru;
if (net >= 0) c = 1; else c = -1;
out_Z1[u] = c;
}
//X-OR mencai Z2
ulang:
cek = 0;
for (u = 0; u < 4; u++)
{
jml_perceptron = (input_x1[u] * w1_lama) + (input_x2[u] *
w2_lama) + (bias[u] * wb_lama);
if (jml_perceptron > teta) y_perceptron = 1;
else
if (jml_perceptron < teta) y_perceptron = -1;
else y_perceptron = 0;
if (target_Z2[u] == y_perceptron)
{ delta_w1 = 0; delta_w2 = 0; delta_wb = 0; }
else
{
delta_w1 = alfa * input_x1[u] * target_Z2[u];
delta_w2 = alfa * input_x2[u] * target_Z2[u];
delta_wb = alfa * bias[u] * target_Z2[u];
}
w1_baru = w1_lama + delta_w1; w1_lama = w1_baru;
w2_baru = w2_lama + delta_w2; w2_lama = w2_baru;
wb_baru = wb_lama + delta_wb; wb_lama = wb_baru;
if (delta_w1 > 0) cek++;
}
if (cek > 0) goto ulang;
else { w1_lama = 0; w2_lama = 0; wb_lama = 0; }
w21 = w1_baru; w22 = w2_baru; b2 = wb_baru;
for (u = 0; u < 4; u++)
{
net = (input_x1[u] * w1_baru) + (input_x2[u] * w2_baru) +
wb_baru;
if (net >= 0) c = 1; else c = -1;
out_Z2[u] = c;
}
//X-OR mencari Y
loop:
cek = 0;
for (u = 0; u < 4; u++)
{
jml_perceptron = (out_Z1[u] * w1_lama) + (out_Z2[u] * w2_lama) +
(bias[u] * wb_lama);
if (jml_perceptron > teta) y_perceptron = 1;
else
if (jml_perceptron < teta) y_perceptron = -1;
else y_perceptron = 0;
if (target_XOR[u] == y_perceptron)
{ delta_w1 = 0; delta_w2 = 0; delta_wb = 0; }
else
{
delta_w1 = alfa * out_Z1[u] * target_XOR[u];
delta_w2 = alfa * out_Z2[u] * target_XOR[u];
delta_wb = alfa * bias[u] * target_XOR[u];
}
w1_baru = w1_lama + delta_w1; w1_lama = w1_baru;
w2_baru = w2_lama + delta_w2; w2_lama = w2_baru;
wb_baru = wb_lama + delta_wb; wb_lama = wb_baru;
if (delta_w1 > 0) cek++;
}
if (cek > 0) goto loop;
else { w1_lama = 0; w2_lama = 0; wb_lama = 0; }
v1 = w1_baru; v2 = w2_baru; b = wb_baru;
logika_XOR = logika_XOR + 1;
}
}

private void button1_Click(object sender, EventArgs e)


{
if (gerbang == 1)
{
net = (x1 * w1_AND) + (x2 * w2_AND) + wb_AND;
if (net >= 0) b = 1; else b = -1;
output.Text = Convert.ToString(b);
logika.Text = Convert.ToString("AND");

bobot_w1.Text = Convert.ToString(w1_AND);
bobot_w2.Text = Convert.ToString(w2_AND);
bobot_b.Text = Convert.ToString(wb_AND);
}
if (gerbang == 2)
{
net = (x1 * w1_OR) + (x2 * w2_OR) + wb_OR;
if (net >= 0) b = 1; else b = -1;
output.Text = Convert.ToString(b);
logika.Text = Convert.ToString("OR");

bobot_w1.Text = Convert.ToString(w1_OR);
bobot_w2.Text = Convert.ToString(w2_OR);
bobot_b.Text = Convert.ToString(wb_OR);
}
if (gerbang == 3)
{
if (x1 == 1 && x2 == 1)
net = (out_Z1[0] * v1) + (out_Z2[0] * v2) + b;
else if (x1 == 1 && x2 == -1)
net = (out_Z1[1] * v1) + (out_Z2[1] * v2) + b;
else if (x1 == -1 && x2 == 1)
net = (out_Z1[2] * v1) + (out_Z2[2] * v2) + b;
else if (x1 == -1 && x2 == -1)
net = (out_Z1[3] * v1) + (out_Z2[3] * v2) + b;
if (net >= 0) b = 1; else b = -1;
output.Text = Convert.ToString(b);
logika.Text = Convert.ToString("X-OR");

bobot_w11.Text = Convert.ToString(w11);
bobot_w12.Text = Convert.ToString(w12);
bobot_w21.Text = Convert.ToString(w21);
bobot_w22.Text = Convert.ToString(w22);
bobot_b1.Text = Convert.ToString(b1);
bobot_b2.Text = Convert.ToString(b2);
bobot_v1.Text = Convert.ToString(v1);
bobot_v2.Text = Convert.ToString(v2);
}
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}

Hasil program

You might also like