Beispiel #1
0
        public static void Execute(LabirintGraph graph, TableLayoutPanel panel = null, LabelControl first = null,
                                   LabelControl best = null)
        {
            var iterations = 0;
            var queue      = new Queue <LabirintVertex>();

            queue.Enqueue(graph.Start);
            while (queue.Count > 0 && !found)
            {
                var curr = queue.Dequeue();
                curr.Visited = true;
                var elems = curr.Neighbours.FindAll(v => !v.Visited);
                foreach (var elem in elems)
                {
                    if (!queue.Contains(elem))
                    {
                        queue.Enqueue(elem);
                        if (graph.Size < maxSize)
                        {
                            index = graph.Vertices.IndexOf(elem);
                            if (elem != graph.Finish && panel != null)
                            {
                                panel.Controls[index].BackColor = GraphParser.Colors[5];
                            }
                        }

                        elem.Parent = curr;
                    }
                }

                if (queue.Contains(graph.Finish))
                {
                    found = true;
                }

                if (panel == null || curr.NodeType != CellTypes.Empty)
                {
                    continue;
                }

                iterations++;

                if (first != null) //&& !found)
                {
                    dataFirst.Data = iterations;
                    first.Invoke((MethodInvoker) delegate { first.Text = iterations.ToString(); });
                }

                best?.Invoke((MethodInvoker) delegate { best.Text = iterations.ToString(); });
                if (graph.Size < maxSize)
                {
                    panel.Controls[graph.Vertices.IndexOf(curr)].BackColor = GraphParser.Colors[4];
                }

                if (graph.Size < maxSize)
                {
                    Thread.Sleep(speed);
                }

                while (wait)
                {
                    ;
                }
            }

            dataBest.Data  = iterations;
            dataBest.Algo  = "BreadthFirst";
            dataFirst.Algo = "BreadthFirst";
            dataBest.Type  = "Best";
            dataFirst.Type = "First";

            if (graph.Size > maxSize)
            {
                return;
            }

            var built   = false;
            var steps   = 1;
            var current = graph.Finish;

            while (!built)
            {
                if (panel == null)
                {
                    continue;
                }
                if (current.Parent == graph.Start)
                {
                    built = true;
                    continue;
                }

                panel.Controls[graph.Vertices.IndexOf(current.Parent)].BackColor = Color.RoyalBlue;
                current = current.Parent;
                steps++;
            }

            if (graph.Size < 25)
            {
                built   = false;
                current = graph.Finish;
                while (!built)
                {
                    if (current == graph.Start)
                    {
                        built = true;
                        continue;
                    }

                    panel.Controls[graph.Vertices.IndexOf(current)].Controls[0].Invoke((MethodInvoker) delegate
                    {
                        panel.Controls[graph.Vertices.IndexOf(current)].Controls[0].Text = steps.ToString();
                    });
                    steps--;
                    if (current == graph.Start)
                    {
                        built = true;
                        continue;
                    }

                    current = current.Parent;
                }
            }
        }
Beispiel #2
0
        public static void Execute(LabirintGraph graph, TableLayoutPanel panel, LabelControl first,
                                   LabelControl best)
        {
            var found = false;

            graph.Start.Mark = 0;
            if (graph.Size < maxSize)
            {
                index = graph.Vertices.IndexOf(graph.Start);
                if (graph.Size < 25)
                {
                    panel.Controls[index].Controls[0].Invoke((MethodInvoker) delegate
                    {
                        panel.Controls[index].Controls[0].Text = "0";
                    });
                }
            }

            var iterations = 0;

            while (!graph.Vertices.TrueForAll(v => v.Visited) && condition)
            {
                var query = from vert in graph.Vertices
                            where !vert.Visited
                            select vert;
                var curr = query.MinBy(s => s.Mark + graph.ManhattanToFinish(s));
                int tmp  = curr.Mark + graph.ManhattanToFinish(curr);
                if (curr.Mark > int.MaxValue - 100000)
                {
                    break;
                }
                foreach (var v in curr.Neighbours)
                {
                    if (v.Visited)
                    {
                        continue;
                    }
                    if (v.Mark > curr.Mark + 1)
                    {
                        v.Mark = curr.Mark + 1;
                        if (graph.Size < maxSize)
                        {
                            index = graph.Vertices.IndexOf(v);
                            if (graph.Size < 25)
                            {
                                panel.Controls[index].Controls[0].Invoke((MethodInvoker) delegate
                                {
                                    panel.Controls[index].Controls[0].Text = v.Mark.ToString();
                                });
                            }

                            if (v != graph.Finish)
                            {
                                panel.Controls[index].BackColor = GraphParser.Colors[5];
                            }
                        }

                        v.Parent = curr;
                    }

                    if (v == graph.Finish)
                    {
                        found = true;
                        tmp   = iterations;
                        tmp++;
                        dataFirst.Data = tmp;
                        first.Invoke((MethodInvoker) delegate { first.Text = tmp.ToString(); });
                    }
                }
                curr.Visited = true;
                if (curr == graph.Finish)
                {
                    condition = false;
                }

                if (panel == null || curr.NodeType != CellTypes.Empty)
                {
                    continue;
                }

                iterations++;

                if (graph.Size < maxSize)
                {
                    panel.Controls[graph.Vertices.IndexOf(curr)].BackColor = GraphParser.Colors[4];
                }


                if (first != null && !found)
                {
                    first.Invoke((MethodInvoker) delegate { first.Text = iterations.ToString(); });
                }

                best?.Invoke((MethodInvoker) delegate { best.Text = iterations.ToString(); });
                if (graph.Size < maxSize)
                {
                    Thread.Sleep(speed);
                }

                while (wait)
                {
                    ;
                }
            }

            dataBest.Data  = iterations;
            dataBest.Algo  = "AStar";
            dataFirst.Algo = "AStar";
            dataBest.Type  = "Best";
            dataFirst.Type = "First";

            if (graph.Size > maxSize)
            {
                return;
            }

            var built   = false;
            var current = graph.Finish;

            while (!built)
            {
                if (panel == null)
                {
                    continue;
                }
                if (current.Parent == graph.Start)
                {
                    built = true;
                    continue;
                }

                panel.Controls[graph.Vertices.IndexOf(current.Parent)].BackColor = Color.RoyalBlue;
                current = current.Parent;
            }
        }
Beispiel #3
0
        private void loadButton_ItemClick(object sender, ItemClickEventArgs e)
        {
            string str = string.Empty;

            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter      = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                ofd.FilterIndex = 1;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Stream fileStream = ofd.OpenFile();
                    using (var sr = new StreamReader(fileStream))
                    {
                        str = sr.ReadToEnd();
                    }

                    fileStream.Close();
                }
            }

            try
            {
                data = str.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                _dGraph                     = GraphParser.ParseGraph(data);
                _bGraph                     = GraphParser.ParseGraph(data);
                _aGraph                     = GraphParser.ParseGraph(data);
                AStar.condition             = true;
                BreadthFirst.found          = false;
                DijkstraAlgorithm.condition = true;

                if (data.Length > 100)
                {
                    speedUp.Enabled   = false;
                    speedDown.Enabled = false;
                    dijkstraPanel.Controls.Clear();
                    dijkstraPanel.RowCount    = 2;
                    dijkstraPanel.ColumnCount = 1;
                    dijkstraPanel.ColumnStyles.Clear();
                    dijkstraPanel.RowStyles.Clear();
                    dijkstraPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
                    dijkstraPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 90));
                    dijkstraPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 10));
                    dijkstraPanel.Controls.Add(LargeMazePictureBox1);
                    LargeMazeSize1.Text = "Размерность лабиринта:" + data.Length.ToString();
                    dijkstraPanel.Controls.Add(LargeMazeSize1);

                    bFirstPanel.Controls.Clear();
                    bFirstPanel.RowCount    = 2;
                    bFirstPanel.ColumnCount = 1;
                    bFirstPanel.ColumnStyles.Clear();
                    bFirstPanel.RowStyles.Clear();
                    bFirstPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
                    bFirstPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 90));
                    bFirstPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 10));
                    bFirstPanel.Controls.Add(LargeMazePictureBox2);
                    LargeMazeSize2.Text = "Размерность лабиринта:" + data.Length.ToString();
                    bFirstPanel.Controls.Add(LargeMazeSize2);

                    aStarPanel.Controls.Clear();
                    aStarPanel.RowCount    = 2;
                    aStarPanel.ColumnCount = 1;
                    aStarPanel.ColumnStyles.Clear();
                    aStarPanel.RowStyles.Clear();
                    aStarPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
                    aStarPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 90));
                    aStarPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 10));
                    aStarPanel.Controls.Add(LargeMazePictureBox3);
                    LargeMazeSize3.Text = "Размерность лабиринта:" + data.Length.ToString();
                    aStarPanel.Controls.Add(LargeMazeSize3);
                    return;
                }

                speedUp.Enabled         = true;
                speedDown.Enabled       = true;
                AStar.speed             = 501;
                DijkstraAlgorithm.speed = 1001;
                AStar.speed             = 501;
                int rows    = data.Length;
                int coloums = data[0].Length;

                //foreach (var control in tableLayoutPanel1.Controls)
                //{
                //    var table = control as TableLayoutPanel;
                //    if (table == null) continue;
                //    foreach (var panel in table.Controls)
                //    {
                //        var p = panel as PanelControl;
                //        if (p == null)
                //            continue;
                //        //foreach (var labelControl in p.Controls)
                //        //{
                //        //    var label = labelControl as System.Windows.Forms.Label;
                //        //    if (label == null) continue;
                //        //    label.Dispose();
                //        //}

                //        //p.Controls.Clear();
                //        p.Dispose();
                //    }
                //}

                dijkstraPanel.Controls.Clear();
                dijkstraPanel.RowCount    = rows;
                dijkstraPanel.ColumnCount = coloums;
                dijkstraPanel.ColumnStyles.Clear();
                dijkstraPanel.RowStyles.Clear();
                for (int i = 0; i < coloums; i++)
                {
                    dijkstraPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)100 / coloums));
                }

                for (int i = 0; i < rows; i++)
                {
                    dijkstraPanel.RowStyles.Add(new RowStyle(SizeType.Percent, (float)100 / coloums));
                }

                for (int i = 0; i < dijkstraPanel.ColumnCount; i++)
                {
                    for (int j = 0; j < dijkstraPanel.RowCount; j++)
                    {
                        PanelControl p = new PanelControl
                        {
                            BorderStyle = BorderStyles.UltraFlat,
                            LookAndFeel =
                            {
                                UseDefaultLookAndFeel = false,
                                Style                 = LookAndFeelStyle.Office2003
                            },
                            Dock   = DockStyle.Fill,
                            Margin = Padding.Empty
                        };
                        if (data.Length < 25)
                        {
                            p.Controls.Add(new System.Windows.Forms.Label
                            {
                                Text     = "",
                                AutoSize = true,
                                Dock     = DockStyle.Fill,
                                Size     = SizeFromClientSize(Parent.Size),
                                Font     = new Font("Times New Roman", 14)
                            });
                        }

                        dijkstraPanel.Controls.Add(p);
                    }
                }

                GraphParser.ParsePanel(data, dijkstraPanel);

                bFirstPanel.Controls.Clear();
                bFirstPanel.RowCount    = rows;
                bFirstPanel.ColumnCount = coloums;
                bFirstPanel.ColumnStyles.Clear();
                bFirstPanel.RowStyles.Clear();
                for (int i = 0; i < coloums; i++)
                {
                    bFirstPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)100 / coloums));
                }

                for (int i = 0; i < rows; i++)
                {
                    bFirstPanel.RowStyles.Add(new RowStyle(SizeType.Percent, (float)100 / coloums));
                }

                for (int i = 0; i < bFirstPanel.ColumnCount; i++)
                {
                    for (int j = 0; j < bFirstPanel.RowCount; j++)
                    {
                        PanelControl p = new PanelControl
                        {
                            BorderStyle = BorderStyles.UltraFlat,
                            LookAndFeel =
                            {
                                UseDefaultLookAndFeel = false,
                                Style                 = LookAndFeelStyle.Office2003
                            },
                            Dock   = DockStyle.Fill,
                            Margin = Padding.Empty
                        };
                        if (data.Length < 25)
                        {
                            p.Controls.Add(new System.Windows.Forms.Label
                            {
                                Text     = "",
                                AutoSize = true,
                                Dock     = DockStyle.Fill,
                                Size     = SizeFromClientSize(Parent.Size),
                                Font     = new Font("Times New Roman", 14)
                            });
                        }

                        bFirstPanel.Controls.Add(p);
                    }
                }

                GraphParser.ParsePanel(data, bFirstPanel);


                aStarPanel.Controls.Clear();
                aStarPanel.RowCount    = rows;
                aStarPanel.ColumnCount = coloums;
                aStarPanel.ColumnStyles.Clear();
                aStarPanel.RowStyles.Clear();
                for (int i = 0; i < coloums; i++)
                {
                    aStarPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)100 / coloums));
                }

                for (int i = 0; i < rows; i++)
                {
                    aStarPanel.RowStyles.Add(new RowStyle(SizeType.Percent, (float)100 / coloums));
                }

                for (int i = 0; i < aStarPanel.ColumnCount; i++)
                {
                    for (int j = 0; j < aStarPanel.RowCount; j++)
                    {
                        PanelControl p = new PanelControl
                        {
                            BorderStyle = BorderStyles.UltraFlat,
                            LookAndFeel =
                            {
                                UseDefaultLookAndFeel = false,
                                Style                 = LookAndFeelStyle.Office2003
                            },
                            Dock   = DockStyle.Fill,
                            Margin = Padding.Empty
                        };
                        if (data.Length < 25)
                        {
                            p.Controls.Add(new System.Windows.Forms.Label
                            {
                                Text     = "",
                                AutoSize = true,
                                Dock     = DockStyle.Fill,
                                Size     = SizeFromClientSize(Parent.Size),
                                Font     = new Font("Times New Roman", 14)
                            });
                        }

                        aStarPanel.Controls.Add(p);
                    }
                }

                GraphParser.ParsePanel(data, aStarPanel);

                if (allAlgsButton.Down)
                {
                    allAlgsButton.PerformClick();
                }

                if (bfsButton.Down)
                {
                    bfsButton.PerformClick();
                }

                if (astarButton.Down)
                {
                    astarButton.PerformClick();
                }

                if (dijkstraButton.Down)
                {
                    dijkstraButton.PerformClick();
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                MessageBox.Show(msg, "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }