Rabu, 26 Februari 2014

Program Jaringan Saraf Tiruan Metode Hebb dengan Visual Studio 2012

Dibawah ini adalah program Jaringan Saraf Tiruan menggunakan metode Hebb. Program di buat menggunakan aplikasi Visual Studio 2012.

1. Gambar Program:

 


 


2. Source Code:
1.       Source code
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int orand(int x1, int x2, int h, int b, int dw1, int dw2, int db)
        {
            int t1, t2; int w1, w2, wb, j;
            if (comboBox1.SelectedIndex.Equals(0))
            {
                t1 = 1; t2 = -1;
                if ((x1 == 1) && (x2 == 1))
                {
                    dw1 = x1 * t1; dw2 = x2 * t1; db = b * t1;
                }
                else
                {
                    dw1 = x1 * t2; dw2 = x2 * t2; db = b * t2;
                }
            }
            if (comboBox1.SelectedIndex.Equals(1))
            {
                t1 = -1; t2 = 1;
                if ((x1 == -1) && (x2 == -1))
                {
                    dw1 = x1 * t1; dw2 = x2 * t1; db = b * t1;
                }
                else
                {
                    dw1 = x1 * t2; dw2 = x2 * t2; db = b * t2;
                }
            }
            if (comboBox1.SelectedIndex.Equals(2))
            {
                t1 = -1; t2 = 1;
                if ((x1 == 1) && (x2 == 1))
                {
                    dw1 = x1 * t1; dw2 = x2 * t1; db = b * t1;
                }
                else
                {
                    dw1 = x1 * t2; dw2 = x2 * t2; db = b * t2;
                }
            }
            if (comboBox1.SelectedIndex.Equals(3))
            {
                t1 = 1; t2 = -1;
                if ((x1 == -1) && (x2 == -1))
                {
                    dw1 = x1 * t1; dw2 = x2 * t1; db = b * t1;
                }
                else
                {
                    dw1 = x1 * t2; dw2 = x2 * t2; db = b * t2;
                }
            }
            w1 = 0; w2 = 0; wb = 0; w1 = w1 + dw1; w2 = w2 + dw2; wb = db + wb;
            j = ((x1 * w1) + (x2 * w2) + wb);
            if (j >= 0)
            {
                h = 1;
            }
            else h = 0;

            return h;
        }
        int xor(int x1, int x2, int h, int b, int dw1, int dw2, int dw3, int dw4, int db1, int db2)
        {
            int t1, t2, t3, t4;
            t1 = 1; t2 = -1;
            if ((x1 == 1) && (x2 == -1))
            {
                dw1 = x1 * t1; dw2 = x2 * t1; db1 = b * t1;
            }
            else
            {
                dw1 = x1 * t2; dw2 = x2 * t2; db1 = b * t2;
            }
            int wza1, wza2, wzab, j1, z2, z1;
            wza1 = 0; wza2 = 0; wzab = 0; wza1 = wza1 + dw1; wza2 = wza2 + dw2; wzab = wzab + db1;
            j1 = ((x1 * wza1) + (x2 * wza2) + wzab);
            if (j1 >= 0)
            {
                z1 = 1;
            }
            else z1 = -1;

            t3 = 1; t4 = -1;
            if ((x1 == -1) && (x2 == 1))
            {
                dw3 = x1 * t3; dw4 = x2 * t3; db2 = b * t3;
            }
            else
            {
                dw3 = x1 * t4; dw4 = x2 * t4; db2 = b * t4;
            }
            int wzb1, wzb2, wzbb, j2;
            wzb1 = 0; wzb2 = 0; wzbb = 0; wzb1 = wzb1 + dw3;       
            wzb2 = wzb2 + dw4; wzbb = wzbb + db2;
            j2 = ((x1 * wzb1) + (x2 * wzb2) + wzbb);
            if (j2 >= 0)
            {
                z2 = 1;
            }
            else z2 = -1;
            int ta, tb, dwa, dwb, dbb, wa, wb, wbb, j;
            ta = -1; tb = 1;
            if ((z1 == -1) && (z2 == -1))
            {
                dwa = z1 * ta; dwb = z2 * ta; dbb = b * ta;
            }
            else
            {
                dwa = z1 * tb; dwb = z2 * tb; dbb = b * tb;
            }
            wa = 0; wb = 0; wbb = 0;
            wa = wa + dwa;
            wb = wb + dwb;
            wbb = wbb + dbb;
            j = ((z1 * wa) + (z2 * wb) + wbb);
            if (j >= 0)
            {
                h = 1;
            }
            else h = 0;

            return h;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x1, x2;
            if (comboBox2.SelectedIndex.Equals(1))
            {
                x1 = -1;
            }
            else
                x1 = 1;
            if (comboBox3.SelectedIndex.Equals(1))
            {
                x2 = -1;
            }
            else
                x2 = 1;
            if (comboBox1.SelectedIndex.Equals(0))
            {
                int nilai;
                textBox2.Text = "AND";
                nilai = orand(x1, x2, 0, 1, 0, 0, 0);
                textBox1.Text = Convert.ToString(nilai);
            }
            else if (comboBox1.SelectedIndex.Equals(1))
            {
                int nilai;
                textBox2.Text = "OR";
                nilai = orand(x1, x2, 0, 1, 0, 0, 0);
                textBox1.Text = Convert.ToString(nilai);                     
            }
            else if (comboBox1.SelectedIndex.Equals(2))
            {
                int nilai;
                textBox2.Text = "NAND";
                nilai = orand(x1, x2, 0, 1, 0, 0, 0);
                textBox1.Text = Convert.ToString(nilai);
            }
            else if (comboBox1.SelectedIndex.Equals(3))
            {
                int nilai;
                textBox2.Text = "NOR";
                nilai = orand(x1, x2, 0, 1, 0, 0, 0);
                textBox1.Text = Convert.ToString(nilai);
            }
            else if (comboBox1.SelectedIndex.Equals(4))
            {
                int nilai;
                textBox2.Text = "XOR";
                nilai  = xor(x1, x2, 0, 1, 0, 0, 0, 0, 0, 0);
                textBox1.Text = Convert.ToString(nilai);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }

}

Tidak ada komentar:

Posting Komentar