Пример #1
0
        private void btTraining_Click(object sender, EventArgs e)
        {
            m_stop = false;
            // btTraining.Enabled = false;

            if (textParas.Text.Length > 0)
            {
                ANNWrapper.LoadBPParameters(Application.StartupPath + "\\" + textParas.Text);
            }
            else
            {
                ANNWrapper.InitBPParameters(64, 8, 4);
            }

            if (textTraingInputs.Lines.Length < 1)
            {
                return;
            }

            MessageBox.Show(textTraingInputs.Lines.Length.ToString() + " Lines!");

            int divideFactor = Int32.Parse(textParaFactor.Text);
            int count        = 0;

            ANNWrapper.InitTrainBPLearnSpeed(Double.Parse(textSpeed.Text));
            double accpt_diff = Double.Parse(textAvrgDiff.Text.ToString());

            double[] inputs = new double[64];
            double[] dests  = new double[4];
            while (true)
            {
                double this_dif = 0.0;
                foreach (string line in textTraingInputs.Lines)
                {
                    count++;

                    string[] strs = line.Split(',');
                    if (strs.Length != 65)
                    {
                        continue;
                    }

                    for (int i = 0; i < 64; i++)
                    {
                        inputs[i] = Double.Parse(strs[i]) / divideFactor;
                    }

                    string dest = Convert.ToString(Int32.Parse(strs[64]), 2);

                    if (dest.Length > 0 && dest[dest.Length - 1 - 0] == '1')
                    {
                        dests[0] = 1.0;
                    }
                    else
                    {
                        dests[0] = 0.0;
                    }

                    if (dest.Length > 1 && dest[dest.Length - 1 - 1] == '1')
                    {
                        dests[1] = 1.0;
                    }
                    else
                    {
                        dests[1] = 0.0;
                    }

                    if (dest.Length > 2 && dest[dest.Length - 1 - 2] == '1')
                    {
                        dests[2] = 1.0;
                    }
                    else
                    {
                        dests[2] = 0.0;
                    }

                    if (dest.Length > 3 && dest[dest.Length - 1 - 3] == '1')
                    {
                        dests[3] = 1.0;
                    }
                    else
                    {
                        dests[3] = 0.0;
                    }

                    double dif = ANNWrapper.Training(inputs, dests);
                    this_dif += dif;
                }

                this_dif /= textTraingInputs.Lines.Length;

                if (chkAutoSave.Checked)
                {
                    if (textParas.Text.Length < 1)
                    {
                        textParas.Text = "Training.dat";
                    }

                    ANNWrapper.SaveBPParameters(Application.StartupPath + "\\" + textParas.Text);
                }
                textUnMatch.AppendText(this_dif.ToString() + "\r\n");
                btTraining.Text = this_dif.ToString();
                btTraining.Update();
                if (this_dif <= accpt_diff || m_stop == true)
                {
                    ANNWrapper.SaveBPParameters(Application.StartupPath + "\\" + textParas.Text);
                    break;
                }
            }
            btTraining.Enabled = true;
            btTraining.Text    = "Train(" + count.ToString() + ")";
        }