Rabu, 26 Februari 2014

Program Logika Fuzzy Metode Back Propagazi dengan Visual Studio 2012

Dibawah ini adalah contoh program logika fuzzy dengan metode back propagazi. Dikembangkan dan dibuat menggunakan Program C# pada Visual Studio 2012.

1. Gambar Program:


2. 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;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        double[,] Va = new double[2, 4];
        double[] VBa = new double[4];
        double[] Wa = new double[5];
        double z1, z2, z3, z4, fz1, fz2, fz3, fz4, Ynet, fYnet;
        double fact, dfact1, dfact2, dfact3, dfact4;
        double dw1, dw2, dw3, dw4, dw5, fnet1, fnet2, fnet3, fnet4;
        double dv11, dv12, dv13, dv14, dv21, dv22, dv23, dv24;
        double db1, db2, db3, db4;
        double[,] X = new double[2, 4] { { 0, 0, 1, 1 }, { 0, 1, 0, 1 } };
        double[] Yo = new double[4];
        double[] fYo = new double[4];
        double[] To = new double[4];
        double[] Target = new double[4];
        double[,] ba = new double[4, 4];
        double[] Zin = new double[4];
        double[] fZin = new double[4];
        double[] Y = new double[2];
        double A, treshold, eror;
        double mse2;
        int i, epoh;

        public Form1()
        {
            InitializeComponent();
        }
        double random (double min, double max)
        {
            Random random = new Random();
            return random.NextDouble() * (max - min) + min;
        }
        double Z(double x1, double x2, double v1, double v2, double b)
        {
            double Z;
            Z = b + (x1 * v1) + (x2 * v2);
            return Z;
        }
        double fungsi(double Xin)
        {
            double fx;
            fx = 1 / (1 + (Math.Exp(-Xin)));
            return fx;
        }
        double TE(double Y, double tres)
        {
            double T;
            if (Y >= tres) { T = 1; }
            else { T = 0; }
            return T;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int w, x, y, z;
            for (w = 0; w < 2; w++)
            {
                for (x = 0; x < 4; x++)
                {
                    Thread.Sleep(18);
                    Va[w, x] = random(-1, 1);
                }
            }
            for (y = 0; y < 4; y++)
            {
                Thread.Sleep(18);
                VBa[y] = random(-1, 1);
            }
            for (z = 0; z < 5; z++)
            {
                Thread.Sleep(18);
                Wa[z] = random(-1, 1);
            }
            //menampilkan hasil random
            textBox37.Text = Va[0, 0].ToString(); textBox44.Text = Va[1, 0].ToString(); textBox48.Text = VBa[0].ToString();
            textBox38.Text = Va[0, 1].ToString(); textBox43.Text = Va[1, 1].ToString(); textBox47.Text = VBa[1].ToString();
            textBox39.Text = Va[0, 2].ToString(); textBox42.Text = Va[1, 2].ToString(); textBox46.Text = VBa[2].ToString();
            textBox40.Text = Va[0, 3].ToString(); textBox41.Text = Va[1, 3].ToString(); textBox45.Text = VBa[3].ToString();
            textBox61.Text = Wa[0].ToString(); textBox62.Text = Wa[1].ToString(); textBox63.Text = Wa[2].ToString();
            textBox64.Text = Wa[3].ToString(); textBox65.Text = Wa[4].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double ep=1;
            A = double.Parse(textBox15.Text); treshold = double.Parse(textBox16.Text);
            eror = double.Parse(textBox13.Text); epoh = int.Parse(textBox14.Text);
            Target[0] = double.Parse(textBox12.Text); Target[1] = double.Parse(textBox11.Text);
            Target[2] = double.Parse(textBox10.Text); Target[3] = double.Parse(textBox9.Text);
            for (i = 0; i < 4; i++)
            {
                for (; ep < epoh; ep++)
                {
       
                    z1 = Z(X[0, i], X[1, i], Va[0, 0], Va[1, 0], VBa[0]);
                    z2 = Z(X[0, i], X[1, i], Va[0, 1], Va[1, 1], VBa[1]);
                    z3 = Z(X[0, i], X[1, i], Va[0, 2], Va[1, 2], VBa[2]);
                    z4 = Z(X[0, i], X[1, i], Va[0, 3], Va[1, 3], VBa[3]);
                  

                    fz1 = fungsi(z1); fz2 = fungsi(z2);
                    fz3 = fungsi(z3); fz4 = fungsi(z4);
                   

                    Ynet = Wa[4] + ((fz1 * Wa[0]) + (fz2 * Wa[1]) + (fz3 * Wa[2]) + (fz4 * Wa[3]));
                    fYnet = fungsi(Ynet);
               

                    mse2 = Math.Pow(Target[i] - fYnet,2);
                

                    fact = (Target[i] - fYnet) * fYnet * (1 - fYnet);
                   

                    dw1 = A * fz1 * fact; dw2 = A * fz2 * fact;
                    dw3 = A * fz3 * fact;dw4 = A * fz4 * fact;
                    dw5= A * fact;
                   

                    dfact1 = fact * Wa[0]; dfact2 = fact * Wa[1];
                    dfact3 = fact * Wa[2]; dfact4 = fact * Wa[3];
               

                    fnet1 = dfact1 * fz1 * (1 - fz1);
                    fnet2 = dfact2 * fz2 * (1 - fz2);
                    fnet3 = dfact3 * fz3 * (1 - fz3);
                    fnet4 = dfact4 * fz4 * (1 - fz4);
                   

                    dv11 = A * fnet1 * X[0, 0]; dv21 = A * fnet1 * X[1, 0];
                    dv12 = A * fnet2 * X[0, 1]; dv22 = A * fnet2 * X[1, 1];
                    dv13 = A * fnet3 * X[0, 2]; dv23 = A * fnet3 * X[1, 2];
                    dv14 = A * fnet4 * X[0, 3]; dv24 = A * fnet4 * X[1, 3];
                   

                    db1 = A * fnet1;
                    db2 = A * fnet2;
                    db3 = A * fnet3;
                    db4 = A * fnet4;
                   

                    Va[0, 0] = Va[0, 0] + dv11; Va[1, 0] = Va[1, 0] + dv21;
                    Va[0, 1] = Va[0, 1] + dv12; Va[1, 1] = Va[1, 0] + dv22;
                    Va[0, 2] = Va[0, 2] + dv13; Va[1, 2] = Va[1, 0] + dv23;
                    Va[0, 3] = Va[0, 3] + dv14; Va[1, 3] = Va[1, 0] + dv24;
                    VBa[0] = VBa[0] + db1; VBa[2] = VBa[2] + db3;
                    VBa[1] = VBa[1] + db2; VBa[3] = VBa[3] + db4;
                    Wa[0] = Wa[0] + dw1; Wa[1] = Wa[1] + dw2;
                    Wa[2] = Wa[2] + dw3; Wa[3] = Wa[3] + dw4;
                    Wa[4] = Wa[4] + dw5;


                    if (mse2 < eror)
                    { break; }
                }
                Yo[i] = Wa[4] + (fz1 * Wa[0]) + (fz2 * Wa[1]) + (fz3 * Wa[2]) + (fz4 * Wa[3]);
                fYo[i] = Math.Round(fungsi(Yo[i]), 5);
                To[i] = Math.Round(TE(fYo[i], treshold), 5);
            }
            //tampilan
            textBox49.Text = Va[0, 0].ToString(); textBox56.Text = Va[1, 0].ToString();
            textBox50.Text = Va[0, 1].ToString(); textBox55.Text = Va[1, 1].ToString();
            textBox51.Text = Va[0, 2].ToString(); textBox54.Text = Va[1, 2].ToString();
            textBox52.Text = Va[0, 3].ToString(); textBox53.Text = Va[1, 3].ToString();
            textBox60.Text = VBa[0].ToString(); textBox58.Text = VBa[2].ToString();
            textBox59.Text = VBa[1].ToString(); textBox57.Text = VBa[3].ToString();
            textBox70.Text = Wa[0].ToString();  textBox69.Text = Wa[1].ToString();
            textBox68.Text = Wa[2].ToString();  textBox67.Text = Wa[3].ToString();
            textBox66.Text = Wa[4].ToString();
            textBox32.Text = Yo[0].ToString(); textBox31.Text = Yo[1].ToString();
            textBox30.Text = Yo[2].ToString(); textBox29.Text = Yo[3].ToString();
            textBox36.Text = fYo[0].ToString(); textBox35.Text = fYo[1].ToString();
            textBox34.Text = fYo[2].ToString(); textBox33.Text = fYo[3].ToString();
            textBox71.Text = mse2.ToString(); textBox72.Text = ep.ToString();
        }
       
        private void button3_Click(object sender, EventArgs e)
        {          
            textBox10.Clear(); textBox11.Clear(); textBox12.Clear();
            textBox13.Clear(); textBox14.Clear(); textBox15.Clear();
            textBox16.Clear(); textBox29.Clear(); textBox30.Clear();
            textBox31.Clear(); textBox31.Clear(); textBox33.Clear();
            textBox34.Clear(); textBox35.Clear(); textBox36.Clear();
            textBox37.Clear(); textBox38.Clear(); textBox39.Clear();
            textBox40.Clear(); textBox41.Clear(); textBox42.Clear();
            textBox43.Clear(); textBox44.Clear(); textBox45.Clear();
            textBox46.Clear(); textBox47.Clear(); textBox48.Clear();
            textBox49.Clear(); textBox50.Clear(); textBox51.Clear();
            textBox52.Clear(); textBox52.Clear(); textBox54.Clear();
            textBox55.Clear(); textBox56.Clear(); textBox57.Clear();
            textBox58.Clear(); textBox59.Clear(); textBox61.Clear();
            textBox62.Clear(); textBox63.Clear(); textBox64.Clear();
            textBox65.Clear(); textBox66.Clear(); textBox67.Clear();
            textBox68.Clear(); textBox69.Clear(); textBox70.Clear();
            textBox71.Clear(); textBox72.Clear(); textBox9.Clear();
            textBox32.Clear();
        }

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

    }
}