day79

package machinelearning.gui;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;

import machinelearning.ann.FullAnn;
/**
 * ******************************************
 * The main entrance of Ann gui.
 *
 * @author Michelle Min MitchelleMin@163.com
 * @date 2021-08-11
 * ******************************************
 */
public class AnnMain implements ActionListener {
    /**
     * Select the arff file.
     */
    private FilenameField arffFilenameField;

    /**
     * The setting of alpha.
     */
    private DoubleField alphaField;

    /**
     * The setting of alpha.
     */
    private DoubleField betaField;

    /**
     * The setting of alpha.
     */
    private DoubleField gammaField;

    /**
     * Layer nodes, such as "4, 8, 8, 3".
     */
    private TextField layerNodesField;

    /**
     * Activators, such as "ssa".
     */
    private TextField activatorField;

    /**
     * The number of training rounds.
     */
    private IntegerField roundsField;

    /**
     * The learning rate.
     */
    private DoubleField learningRateField;

    /**
     * The mobp.
     */
    private DoubleField mobpField;

    /**
     * The message area.
     */
    private TextArea messageTextArea;

    /**
     ***************************
     * The only constructor.
     ***************************
     */
    public AnnMain() {
        // A simple frame to contain dialogs.
        Frame mainFrame = new Frame();
        mainFrame.setTitle("ANN. minfanphd@163.com");
        // The top part: select arff file.
        arffFilenameField = new FilenameField(30);
        arffFilenameField.setText("d:/data/iris.arff");
        Button browseButton = new Button(" Browse ");
        browseButton.addActionListener(arffFilenameField);

        Panel sourceFilePanel = new Panel();
        sourceFilePanel.add(new Label("The .arff file:"));
        sourceFilePanel.add(arffFilenameField);
        sourceFilePanel.add(browseButton);

        // Setting panel.
        Panel settingPanel = new Panel();
        settingPanel.setLayout(new GridLayout(3, 6));

        settingPanel.add(new Label("alpha"));
        alphaField = new DoubleField("0.01");
        settingPanel.add(alphaField);

        settingPanel.add(new Label("beta"));
        betaField = new DoubleField("0.02");
        settingPanel.add(betaField);

        settingPanel.add(new Label("gamma"));
        gammaField = new DoubleField("0.03");
        settingPanel.add(gammaField);

        settingPanel.add(new Label("layer nodes"));
        layerNodesField = new TextField("4, 8, 8, 3");
        settingPanel.add(layerNodesField);

        settingPanel.add(new Label("activators"));
        activatorField = new TextField("sss");
        settingPanel.add(activatorField);

        settingPanel.add(new Label("training rounds"));
        roundsField = new IntegerField("5000");
        settingPanel.add(roundsField);

        settingPanel.add(new Label("learning rate"));
        learningRateField = new DoubleField("0.01");
        settingPanel.add(learningRateField);

        settingPanel.add(new Label("mobp"));
        mobpField = new DoubleField("0.5");
        settingPanel.add(mobpField);

        Panel topPanel = new Panel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(BorderLayout.NORTH, sourceFilePanel);
        topPanel.add(BorderLayout.CENTER, settingPanel);

        messageTextArea = new TextArea(80, 40);

        // The bottom part: ok and exit.
        Button okButton = new Button(" OK ");
        okButton.addActionListener(this);
        // DialogCloser dialogCloser = new DialogCloser(this);
        Button exitButton = new Button(" Exit ");
        // cancelButton.addActionListener(dialogCloser);
        exitButton.addActionListener(ApplicationShutdown.applicationShutdown);
        Button helpButton = new Button(" Help ");
        helpButton.setSize(20, 10);
        helpButton.addActionListener(new HelpDialog("ANN", "src/machineLearning/gui/help.txt"));
        Panel okPanel = new Panel();
        okPanel.add(okButton);
        okPanel.add(exitButton);
        okPanel.add(helpButton);

        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(BorderLayout.NORTH, topPanel);
        mainFrame.add(BorderLayout.CENTER, messageTextArea);
        mainFrame.add(BorderLayout.SOUTH, okPanel);

        mainFrame.setSize(600, 500);
        mainFrame.setLocation(100, 100);
        mainFrame.addWindowListener(ApplicationShutdown.applicationShutdown);
        mainFrame.setBackground(GUICommon.MY_COLOR);
        mainFrame.setVisible(true);
    }//of the constructor

    /**
     ***************************
     * Read the arff file.
     ***************************
     */
    public void actionPerformed(ActionEvent ae) {
        String tempFilename = arffFilenameField.getText();

        // Read the layers nodes.
        String tempString = layerNodesField.getText().trim();

        int[] tempLayerNodes = null;
        try {
            tempLayerNodes = stringToIntArray(tempString);
        } catch (Exception ee) {
            ErrorDialog.errorDialog.setMessageAndShow(ee.toString());
            return;
        }//of try

        double tempLearningRate = learningRateField.getValue();
        double tempMobp = mobpField.getValue();
        String tempActivators = activatorField.getText().trim();
        FullAnn tempNetwork = new FullAnn(tempFilename, tempLayerNodes, tempLearningRate, tempMobp,
                tempActivators);
        int tempRounds = roundsField.getValue();

        long tempStartTime = new Date().getTime();
        for (int i = 0; i < tempRounds; i++) {
            tempNetwork.train();
        }//of for n
        long tempEndTime = new Date().getTime();
        messageTextArea.append("\r\nSummary:\r\n");
        messageTextArea.append("Training time: " + (tempEndTime - tempStartTime) + "ms.\r\n");

        double tempAccuracy = tempNetwork.test();
        messageTextArea.append("Accuracy: " + tempAccuracy + "\r\n");
        messageTextArea.append("End.");
    }//of actionPerformed

    /**
     **********************************
     * Convert a string with commas into an int array.
     *
     * @param paraString
     *            The source string
     * @return An int array.
     * @throws Exception
     *             Exception for illegal data.
     **********************************
     */
    public static int[] stringToIntArray(String paraString) throws Exception {
        int tempCounter = 1;
        for (int i = 0; i < paraString.length(); i++) {
            if (paraString.charAt(i) == ',') {
                tempCounter++;
            }//of if
        }//of for i

        int[] resultArray = new int[tempCounter];

        String tempRemainingString = new String(paraString) + ",";
        String tempString;
        for (int i = 0; i < tempCounter; i++) {
            tempString = tempRemainingString.substring(0, tempRemainingString.indexOf(",")).trim();
            if (tempString.equals("")) {
                throw new Exception("Blank is unsupported");
            }//of if

            resultArray[i] = Integer.parseInt(tempString);

            tempRemainingString = tempRemainingString
                    .substring(tempRemainingString.indexOf(",") + 1);
        }//of for i

        return resultArray;
    }//of stringToIntArray

    /**
     ***************************
     * The entrance method.
     *
     * @param args
     *            The parameters.
     ***************************
     */
    public static void main(String[] args) {
        new AnnMain();
    }//of main
}//of class AnnMain

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值