⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lbg.java

📁 用于模拟矢量模式分类的LBG算法
💻 JAVA
字号:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

public class lbg extends Applet implements ActionListener
{

    private double D1;
    private double D2;
    private int WhatDraw;
    private int round;
    private int PointNumber;
    private int CenterNumber;
    private int Rxy[][];
    private int Dxy[][];
    private int Pxy[][];
    private int Nxy[][];
    private int CB[];
    private Button Button_1;
    private Button Button_2;
    private Button Button_3;
    private Label Label_1;
    private Label Label_2;
    private Label Label_3;
    private Label Label_4;
    private Label Caption_1;
    private Label Caption_2;
    private TextField TextInput_1;
    private TextField TextInput_2;
    private Image Symbol;

    public lbg()
    {
        D1 = 1000000D;
        D2 = 0.0D;
        WhatDraw = 0;
        round = 0;
        PointNumber = 200;
        CenterNumber = 20;
    }

    public void init()
    {
        setBackground(Color.white);
        Rxy = new int[400][2];
        Dxy = new int[400][2];
        Pxy = new int[50][2];
        Nxy = new int[50][2];
        CB = new int[50];
        LayoutGUI();
    }

    public void LayoutGUI()
    {
        setLayout(null);
        Button_1 = new Button("Random Point");
        Button_1.setBounds(530, 10, 100, 25);
        Button_1.addActionListener(this);
        add(Button_1);
        Button_2 = new Button("Initialize");
        Button_2.setBounds(530, 45, 100, 25);
        Button_2.addActionListener(this);
        Button_2.setEnabled(false);
        add(Button_2);
        Button_3 = new Button("Start Training");
        Button_3.setBounds(530, 80, 100, 25);
        Button_3.addActionListener(this);
        Button_3.setEnabled(false);
        add(Button_3);
        Label_1 = new Label("Point Number");
        Label_1.setBounds(530, 120, 100, 25);
        add(Label_1);
        TextInput_1 = new TextField("200");
        TextInput_1.setBounds(530, 147, 100, 25);
        add(TextInput_1);
        Label_2 = new Label("Center Number");
        Label_2.setBounds(530, 175, 100, 25);
        add(Label_2);
        TextInput_2 = new TextField("20");
        TextInput_2.setBounds(530, 202, 100, 25);
        add(TextInput_2);
        Label_3 = new Label("Epsilon");
        Label_3.setBounds(530, 230, 100, 25);
        add(Label_3);
        Caption_1 = new Label("0.0");
        Caption_1.setBounds(530, 257, 100, 25);
        add(Caption_1);
        Label_4 = new Label("Round");
        Label_4.setBounds(530, 285, 100, 25);
        add(Label_4);
        Caption_2 = new Label("0");
        Caption_2.setBounds(530, 312, 100, 25);
        add(Caption_2);
        Symbol = getImage(getDocumentBase(), "Symbol.gif");
    }

    public void paint(Graphics g)
    {
        g.drawImage(Symbol, 530, 350, this);
        switch(WhatDraw)
        {
        default:
            break;

        case 1: // '\001'
            DrawPoint1(g, Color.blue);
            break;

        case 2: // '\002'
            DrawPoint1(g, Color.blue);
            DrawPoint3(g, Color.green);
            break;

        case 3: // '\003'
            DrawPoint1(g, Color.blue);
            DrawPoint2(g, Color.green);
            DrawLines(g, Color.lightGray);
            try
            {
                Thread.currentThread();
                Thread.sleep(1000L);
            }
            catch(Exception exception) { }
            DrawPoint3(g, Color.red);
            break;
        }
    }

    public void RandomPoint()
    {
        Rxy[0][0] = (int)(Math.random() * 500D + 10D);
        Rxy[0][1] = (int)(Math.random() * 400D + 10D);
        for(int i = 1; i < PointNumber; i++)
        {
            int l = (int)(Math.random() * 500D + 10D);
            for(int j1 = 0; j1 < i;)
            {
                for(int j = 0; j <= i - 1; j++)
                {
                    if(l == Rxy[j][0])
                    {
                        l = (int)(Math.random() * 500D + 10D);
                        j1 = 0;
                        break;
                    }
                    j1++;
                }

            }

            Rxy[i][0] = l;
            int i1 = (int)(Math.random() * 400D + 10D);
            for(int k1 = 0; k1 < i;)
            {
                for(int k = 0; k <= i - 1; k++)
                {
                    if(i1 == Rxy[k][1])
                    {
                        i1 = (int)(Math.random() * 400D + 10D);
                        k1 = 0;
                        break;
                    }
                    k1++;
                }

            }

            Rxy[i][1] = i1;
        }

    }

    public void CenterPoint()
    {
        CB[0] = (int)(Math.random() * (double)PointNumber);
        Nxy[0][0] = Rxy[CB[0]][0];
        Nxy[0][1] = Rxy[CB[0]][1];
        for(int k = 1; k < CenterNumber; k++)
        {
            int i = (int)(Math.random() * (double)PointNumber);
            for(int j = 0; j < k;)
            {
                for(int l = 0; l <= k - 1; l++)
                {
                    if(i == CB[l])
                    {
                        i = (int)(Math.random() * (double)PointNumber);
                        j = 1;
                        break;
                    }
                    j++;
                }

            }

            CB[k] = i;
            Nxy[k][0] = Rxy[CB[k]][0];
            Nxy[k][1] = Rxy[CB[k]][1];
        }

    }

    public void Go()
    {
        int ai[] = new int[50];
        int ai1[][] = new int[50][2];
        round++;
        Caption_2.setText(Integer.toString(round));
        D2 = 0.0D;
        for(int i = 0; i < PointNumber; i++)
        {
            int j = closest1(i);
            int k = (Rxy[i][0] - Nxy[j][0]) * (Rxy[i][0] - Nxy[j][0]) + (Rxy[i][1] - Nxy[j][1]) * (Rxy[i][1] - Nxy[j][1]);
            D2 = D2 + (double)k;
            Dxy[i][0] = Nxy[j][0];
            Dxy[i][1] = Nxy[j][1];
            ai[j]++;
            ai1[j][0] = ai1[j][0] + Rxy[i][0];
            ai1[j][1] = ai1[j][1] + Rxy[i][1];
        }

        D2 = D2 / (double)PointNumber;
        double d = Math.floor(((D1 - D2) / D1) * 1000D) / 1000D;
        Caption_1.setText(Double.toString(d));
        if(d <= 0.0050000000000000001D)
        {
            showStatus("Training Completely!!");
            Button_3.setEnabled(false);
            Button_1.setEnabled(true);
            TextInput_1.setEnabled(true);
            TextInput_2.setEnabled(true);
        }
        D1 = D2;
        for(int l = 0; l < CenterNumber; l++)
        {
            Pxy[l][0] = Nxy[l][0];
            Pxy[l][1] = Nxy[l][1];
            if(ai[l] != 0)
            {
                Nxy[l][0] = ai1[l][0] / ai[l];
                Nxy[l][1] = ai1[l][1] / ai[l];
            }
            int i1 = closest2(l);
            Nxy[l][0] = Rxy[i1][0];
            Nxy[l][1] = Rxy[i1][1];
        }

    }

    public void DrawPoint1(Graphics g, Color color)
    {
        g.setColor(color);
        for(int i = 0; i < PointNumber; i++)
            g.fillOval(Rxy[i][0], Rxy[i][1], 7, 7);

    }

    public void DrawPoint2(Graphics g, Color color)
    {
        g.setColor(color);
        for(int i = 0; i < CenterNumber; i++)
            g.fillOval(Pxy[i][0], Pxy[i][1], 7, 7);

    }

    public void DrawPoint3(Graphics g, Color color)
    {
        g.setColor(color);
        for(int i = 0; i < CenterNumber; i++)
            g.fillOval(Nxy[i][0], Nxy[i][1], 7, 7);

    }

    public void DrawLines(Graphics g, Color color)
    {
        g.setColor(color);
        for(int i = 0; i < PointNumber; i++)
            g.drawLine(Rxy[i][0] + 3, Rxy[i][1] + 3, Dxy[i][0] + 3, Dxy[i][1] + 3);

    }

    public int closest1(int i)
    {
        int j = (Rxy[i][0] - Nxy[0][0]) * (Rxy[i][0] - Nxy[0][0]) + (Rxy[i][1] - Nxy[0][1]) * (Rxy[i][1] - Nxy[0][1]);
        int l = 0;
        for(int i1 = 1; i1 < CenterNumber; i1++)
        {
            int k = (Rxy[i][0] - Nxy[i1][0]) * (Rxy[i][0] - Nxy[i1][0]) + (Rxy[i][1] - Nxy[i1][1]) * (Rxy[i][1] - Nxy[i1][1]);
            if(k < j)
            {
                j = k;
                l = i1;
            }
        }

        return l;
    }

    public int closest2(int i)
    {
        int j = (Rxy[0][0] - Nxy[i][0]) * (Rxy[0][0] - Nxy[i][0]) + (Rxy[0][1] - Nxy[i][1]) * (Rxy[0][1] - Nxy[i][1]);
        int l = 0;
        for(int i1 = 1; i1 < PointNumber; i1++)
        {
            int k = (Rxy[i1][0] - Nxy[i][0]) * (Rxy[i1][0] - Nxy[i][0]) + (Rxy[i1][1] - Nxy[i][1]) * (Rxy[i1][1] - Nxy[i][1]);
            if(k < j)
            {
                j = k;
                l = i1;
            }
        }

        return l;
    }

    public void Reset()
    {
        round = 0;
        D1 = 1000000D;
        Caption_1.setText("0.0");
        Caption_2.setText("0");
        showStatus("Start Training!!");
    }

    public void AccessText1()
    {
        String s = TextInput_1.getText();
        PointNumber = Integer.parseInt(s, 10);
        if(PointNumber > 400)
        {
            PointNumber = 400;
            TextInput_1.setText("400");
        } else
        if(PointNumber < 100)
        {
            PointNumber = 100;
            TextInput_1.setText("100");
        }
        TextInput_1.setEnabled(false);
    }

    public void AccessText2()
    {
        String s = TextInput_2.getText();
        CenterNumber = Integer.parseInt(s, 10);
        if(CenterNumber > 50)
        {
            CenterNumber = 50;
            TextInput_2.setText("50");
        } else
        if(CenterNumber < 10)
        {
            CenterNumber = 10;
            TextInput_2.setText("10");
        }
        Button_1.setEnabled(false);
        TextInput_2.setEnabled(false);
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        if(actionevent.getSource() == Button_1)
        {
            WhatDraw = 1;
            Reset();
            AccessText1();
            RandomPoint();
            Button_2.setEnabled(true);
            repaint();
        }
        if(actionevent.getSource() == Button_2)
        {
            WhatDraw = 2;
            AccessText2();
            CenterPoint();
            Button_3.setEnabled(true);
            repaint();
        }
        if(actionevent.getSource() == Button_3)
        {
            WhatDraw = 3;
            Button_2.setEnabled(false);
            Go();
            repaint();
        }
    }
}

//<applet code=lbg width=800 height=600></applet>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -