Exemplo n.º 1
0
        /// <summary>
        /// Инициализация 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            dgvLearningSource.AutoGenerateColumns = true;
            dgvPerformance.AutoGenerateColumns = false;
            Tree_property = new Property_Tree();

            asd = new Tree_View();
            openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath, "Resources");

            toolTip1.SetToolTip(groupBox1, "+/- раскрыть/скрыть узел \n*-раскрыть полностью ветвь");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Создание дерева 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvLearningSource.DataSource == null)
                {
                    MessageBox.Show("Загрузите данные");
                    return;
                }
                if (Tree_property.Property_View == true)
                {
                    Tree_property.ShowDialog();
                }
                // Завершаем операцию с DataGridView
                dgvLearningSource.EndEdit();

                #region Алгоритм С4.5
                ///
                ///Алгоритм С4.5
                ///
                if (Tree_property.Alg == "C4.5")
                {
                    // // создаем матрицу из  data table
                    double[,] sourceMatrix = (dgvLearningSource.DataSource as DataTable).ToMatrix(out sourceColumns);

                    C45Learning c45;

                    // получаем входные значения
                    double[][] inputs = sourceMatrix.Submatrix(null, 0, Tree_property.Coun_In - 1).ToArray();

                    // получаем выходные значения
                    int[] outputs = sourceMatrix.GetColumn(Tree_property.Coun_Out - 1).ToInt32();

                    DecisionVariable[] attributes = new DecisionVariable[Tree_property.Coun_In];

                    for (int j = 0; j < Tree_property.Coun_In; j++)
                    {
                        attributes[j] = new DecisionVariable(dgvLearningSource.Columns[j].Name, DecisionAttributeKind.Continuous);
                    }

                    // создаем дерево решений
                    tree = new DecisionTree(attributes, 60);

                    c45 = new C45Learning(tree);
                    double error = c45.Run(inputs, outputs);
                }
                #endregion

                #region Алгоритм ID3
                ///
                ///Алгоритм ID3
                ///
                if (Tree_property.Alg == "ID3")
                {
                    // создаем матрицу из дататыйбл
                    int[][] arr = (dgvLearningSource.DataSource as DataTable).ToIntArray(sourceColumns);
                    int[,] sourceMatrix = arr.ToMatrix();

                    //// получаем входные значения

                    int[][] inputs = sourceMatrix.Submatrix(null, 0, Tree_property.Coun_In - 1).ToArray();

                    //// получаем выходные значения
                    int[] outputs = sourceMatrix.GetColumn(Tree_property.Coun_In - 1);

                    DecisionVariable[] attributes = new DecisionVariable[Tree_property.Coun_In];

                    for (int j = 0; j < Tree_property.Coun_In; j++)
                    {
                        attributes[j] = new DecisionVariable(j.ToString(), DecisionAttributeKind.Continuous);
                    }

                    // создаем дерево решений
                    tree = new DecisionTree(attributes, 60);

                    ID3Learning id3learning = new ID3Learning(tree);

                    double error = id3learning.Run(inputs, outputs);

                }
                #endregion

                asd.Dispose();
                asd.Close();

                Drawing dr = new Drawing();

                dr.recursion(tree.Root, tree.Root.Branches, 0);
                dr.Save_();

                asd = new Tree_View();
                asd.userControl11.Load_f(Application.StartupPath);

                System.Linq.Expressions.Expression df = tree.ToExpression();

               // выбираем tabe page для просмотра дерева
                tabControl.SelectTab(tabOverview);

                // отображаем построенной дереыыо решений
                decisionTreeView1.TreeSource = tree;

                try
                {
                    File.Copy(@".\Resources\recursion.png", @".\Resources\recursion2.png", true);
                }
                catch
                {
                }
                using (Stream s = File.OpenRead(@".\Resources\recursion2.png"))
                {
                    pictureBox1.Image = Image.FromStream(s);
                }

            }
            catch (Exception t)
            {
                MessageBox.Show(t.Message);
            }
        }