Rabu, 26 Februari 2014

Program Logika Fuzzy Metode Mamdani dengan Visual Studio 2012

Dibawah ini adalah contoh program menggunakan logika fuzzy dengan metode mamdani. Program dibuat menggunakan Visual Studio 2012. Semoga bermanfaat.

Gambar tampilan:

Program C#:
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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float a1, b1, i1, r, t;
            a1 = float.Parse(textBox1.Text);
            b1 = float.Parse(textBox2.Text);
            i1 = float.Parse(textBox7.Text);

            if (i1 <= a1)
            {
                r = 1; t = 0;
                label12.Text = r.ToString();
                label13.Text = t.ToString();

            }
            if (i1 >= a1 && i1 <= b1)
            {
                r = ((b1 - i1) / (b1 - a1));
                t = ((i1 - a1) / (b1 - a1));
                label12.Text = r.ToString();
                label13.Text = t.ToString();

            }


            if (i1 >= b1)
            {
                r = 0; t = 1;
                label12.Text = r.ToString();
                label13.Text = t.ToString();

            }
            float a2, b2, i2, d, b;
            a2 = float.Parse(textBox3.Text);
            b2 = float.Parse(textBox4.Text);
            i2 = float.Parse(textBox8.Text);

            if (i2 <= a2)
            {
                d = 1; b = 0;
                label14.Text = d.ToString();
                label15.Text = b.ToString();

            }
            if (i2 >= a2 && i2 <= b2)
            {
                d = ((b2 - i2) / (b2 - a2));
                b = ((i2 - a2) / (b2 - a2));
                label14.Text = d.ToString();
                label15.Text = b.ToString();

            }
            if (i2 >= b2)
            {
                d = 0; b = 1;
                label14.Text = d.ToString();
                label15.Text = b.ToString();

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            double a1,a2,a3,a4,o1,o2,t1,t2,t3,r1,r2,r3;
            o1 = Convert.ToInt32(textBox5.Text);
            o2 = Convert.ToInt32(textBox6.Text);

            t1 = double.Parse(label12.Text);
            r1 = double.Parse(label13.Text);
            t2 = double.Parse(label14.Text);
            r2 = double.Parse(label15.Text);

            a1 = Math.Min( t1, r2);
           
            textBox10.Text = a1.ToString();

            a2 = Math.Min(t1, t2);
          
            textBox11.Text = a2.ToString();

            a3 = Math.Min(r1, r2);
           
            textBox12.Text = a3.ToString();

            a4 = Math.Min(r1, t2);
           
            textBox13.Text = a4.ToString();
           
            double Ma,Ma1,Ma2,Mi,Mi1,Mi2;
            Ma1 = Math.Max(a1, a2);
            Ma2 = Math.Max(a3, a4);
            Ma = Math.Max(Ma1, Ma2);
            textBox15.Text = Ma.ToString();
            Mi1 = Math.Min(a1, a2);
            Mi2 = Math.Min(a3, a4);
            Mi = Math.Min(Mi1, Mi2);
            textBox14.Text = Mi.ToString();
            r3 = (o2 - o1) * Mi + o1;
            textBox17.Text = r3.ToString();
            t3 = (o2 - o1) * Ma + o1;
            textBox16.Text = t3.ToString();

            double M1, M2, M3, A1, A2, A3,Z;
            M1 = ( Mi/2)*(Math.Pow(r3,2));
            M2 = (((1 / (o2 - o1))/2)*(Math.Pow(t3,3))) - (((o1 / (o2 - o1))/2)*Math.Pow(t3,2)) - (((1 / (o2 - o1))/3) * (Math.Pow(r3,3))) - (((o1 / (o2 - o1))/2)*(Math.Pow(r3,2)));
            M3 = ( Ma *Math.Pow(o2,2))/2 - (Ma * (o2 - o1)/2) * (o2 - o1);
            textBox20.Text = M1.ToString();
            textBox19.Text = M2.ToString();
            textBox18.Text = M3.ToString();
            A1 = r3 * Mi;
            A2 = (Mi+Ma)*(t3-r3)/2;
            A3 = (o2-(o2-o1))*Ma;
            textBox23.Text = A1.ToString();
            textBox22.Text = A2.ToString();
            textBox21.Text = A3.ToString();
            Z = (M1 + M2 + M3) / (A1 + A2 + A3);
            textBox9.Text = Z.ToString();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            label12.ResetText();
            label13.ResetText();
            label14.ResetText();
            label15.ResetText();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();
            textBox9.Clear();
            textBox10.Clear();
            textBox11.Clear();
            textBox12.Clear();
            textBox13.Clear();
            textBox14.Clear();
            textBox15.Clear();
            textBox16.Clear();
            textBox17.Clear();
            textBox18.Clear();
            textBox19.Clear();
            textBox20.Clear();
            textBox21.Clear();
            textBox22.Clear();
            textBox23.Clear();
                     
        }
     
    }

}

Tidak ada komentar:

Posting Komentar