コード例 #1
0
ファイル: frmHome.cs プロジェクト: januar/CrossOverPMXinRBFNN
 public frmHome()
 {
     InitializeComponent();
     listIrisItem = new List<IrisItem>();
     jumlahCluster = 3;
     cmbJumlahCluster.SelectedIndex = 2;
     cmbRBFEpoch.SelectedIndex = 3;
     cmbFitness.SelectedIndex = 0;
     cmbLearningRate.SelectedIndex = 0;
     ucresult = null;
 }
コード例 #2
0
ファイル: frmHome.cs プロジェクト: januar/CrossOverPMXinRBFNN
        private void btnProcessRBF_Click(object sender, EventArgs e)
        {
            if (listIrisItem.Count <= 0)
            {
                General.ShowError("Please load data from file");
                return;
            }

            if (listCluster == null)
            {
                General.ShowError("Please execute K-Means process!");
                return;
            }

            if (!Int32.TryParse(txtMaxEpoch.Text, out jumlahEpoch))
            {
                General.ShowError("Please enter valid number for max GA epoch!");
                return;
            }

            if (cmbRBFEpoch.SelectedItem.ToString() == "")
            {
                General.ShowError("Please select max epoch of RBF!");
                return;
            }

            if (cmbLearningRate.SelectedItem.ToString() == "")
            {
                General.ShowError("Please select learning rate!");
                return;
            }

            if (cmbFitness.SelectedItem.ToString() == "")
            {
                General.ShowError("Please select max fitness!");
                return;
            }

            jumlahRBFEpoch = Int32.Parse(cmbRBFEpoch.SelectedItem.ToString());
            learningRate = double.Parse(cmbLearningRate.SelectedItem.ToString());
            maxFitness = double.Parse(cmbFitness.SelectedItem.ToString());

            Matrix<double> matrixTarget = DenseMatrix.OfArray(new double[,] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } });
            RBFNN rbfnn = new RBFNN();
            outputRBFNN = new double[listIrisItem.Count][];
            outputActivationRBFNN = new int[listIrisItem.Count][];
            for (int i = 0; i < listIrisItem.Count; i++)
            {
                Matrix bobotMatrix = rbfnn.getBobot(listIrisItem[i], listCluster);
                double[] hiddenValue = rbfnn.countHiddenValue(listIrisItem[i], bobotMatrix, listCluster.Count);

                Matrix<double> weightToOutput = (bobotMatrix * bobotMatrix.Transpose()).Inverse() * bobotMatrix * matrixTarget;
                double[][] weight = weightToOutput.ToColumnArrays();

                outputRBFNN[i] = new double[2] { 0d, 0d };
                outputActivationRBFNN[i] = new int[2];
                outputActivationRBFNN[i][0] = rbfnn.TrainingRBF(ref outputRBFNN[i][0], ref weight[0], hiddenValue, learningRate, listIrisItem[i].YOut1, jumlahRBFEpoch);
                outputActivationRBFNN[i][1] = rbfnn.TrainingRBF(ref outputRBFNN[i][1], ref weight[1], hiddenValue, learningRate, listIrisItem[i].YOut2, jumlahRBFEpoch);
            }

            int loop = 0;
            outputGA = new double[listIrisItem.Count][];
            outputActivationGA = new int[listIrisItem.Count][];

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < listIrisItem.Count; i++)
            {
                Matrix bobotMatrix = rbfnn.getBobot(listIrisItem[i], listCluster);
                double[] hiddenValue = rbfnn.countHiddenValue(listIrisItem[i], bobotMatrix, listCluster.Count);
                Matrix<double> weightToOutput = (bobotMatrix * bobotMatrix.Transpose()).Inverse() * bobotMatrix * matrixTarget;
                double[][] weight = weightToOutput.ToColumnArrays();

                outputGA[i] = new double[2] { 0d, 0d };
                outputActivationGA[i] = new int[2];
                outputActivationGA[i][0] = rbfnn.TrainingRBF(ref outputGA[i][0], ref weight[0], hiddenValue, learningRate, listIrisItem[i].YOut1, jumlahRBFEpoch);
                outputActivationGA[i][1] = rbfnn.TrainingRBF(ref outputGA[i][1], ref weight[1], hiddenValue, learningRate, listIrisItem[i].YOut2, jumlahRBFEpoch);

                if (outputActivationGA[i][0] == listIrisItem[i].YOut1 && outputActivationGA[i][1] == listIrisItem[i].YOut2)
                    continue;

                List<Kromosom> kromosom = new List<Kromosom>();
                kromosom.Add(new Kromosom() { Gen = weight[0] });
                kromosom.Add(new Kromosom() { Gen = weight[1] });

                sb.Append("Iris item " + (i + 1) + "\r\n");
                loop = 0;
                double lastFitness = 0d;
                double tempFitness = 0d;
                do
                {
                    lastFitness = tempFitness;
                    tempFitness = 0d;
                    sb.Append("Generasi ke-" + (loop + 1) + "\r\n");
                    for (int j = 0; j < kromosom.Count; j++)
                    {
                        kromosom[j].Fitness = Genetika.CountFitness(kromosom[j]);
                        tempFitness += kromosom[j].Fitness;
                        sb.Append(String.Format("Chromosome {0} = {1}\r\n", j + 1, kromosom[j].print()));
                    }

                    kromosom = Genetika.CountFitnessRelative(kromosom, ref sb);
                    kromosom = Genetika.CrossOver(kromosom);
                    sb.Append("Cross Over \r\n");
                    sb.Append(Kromosom.printList(kromosom));

                    kromosom = Genetika.Mutasi(kromosom);
                    sb.Append("Mutasi \r\n");
                    sb.Append(Kromosom.printList(kromosom));
                    sb.Append(String.Format("Current fitness {0}, last fitness {1}", tempFitness, lastFitness));
                    sb.Append("\r\n\r\n");
                    loop++;
                } while (loop < jumlahEpoch && (loop == 1 || tempFitness < 0));
                sb.Append("\r\n");

                weight[0] = kromosom[0].Gen;
                weight[1] = kromosom[1].Gen;

                outputActivationGA[i][0] = rbfnn.CountActivation(ref outputGA[i][0], ref weight[0], hiddenValue);
                outputActivationGA[i][1] = rbfnn.CountActivation(ref outputGA[i][1], ref weight[1], hiddenValue);
            }

            ucresult = new ucResult();
            ucresult.setSB(sb);
            this.Controls.Add(ucresult);
            ucresult.Location = new Point(0, 22);
            panelControl1.Visible = false;
            ucresult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            ucresult.Dock = DockStyle.Fill;
        }