Ejemplo n.º 1
0
    public void Search(int algrithm)
    {
        ClearPath();
        SearchAlgrithm ag = (SearchAlgrithm)algrithm;
        PathFind       pathFind;

        switch (ag)
        {
        case SearchAlgrithm.BFS:
            pathFind = new BreadthFirstSearch();
            break;

        case SearchAlgrithm.DFS:
            pathFind = new DeepFirstSearch();
            break;

        case SearchAlgrithm.Dijkstra:
            pathFind = new DijkstraSearch();
            break;

        case SearchAlgrithm.Greedy:
            pathFind = new GreedyBestFirstSearch();
            break;

        case SearchAlgrithm.AStar:
            pathFind = new AStarSearch();
            break;

        default:
            pathFind = new AStarSearch();
            break;
        }
        Node start = graph.GetNode((int)mapView.startPos.x, (int)mapView.startPos.y);
        Node end   = graph.GetNode((int)mapView.endPos.x, (int)mapView.endPos.y);

        if (pathFind.Find(graph, start, end))
        {
            path = pathFind.GetPath();
            Queue <Node> searchSteps = pathFind.GetSearchSteps();
            mainUI.labelSearchDesc.text = pathFind.Name() + " " + pathFind.SearchResultDesc();
            mainUI.SearchConsoleTxt     = "";
            StartCoroutine(DrawSearchSteps(searchSteps, path));
        }
        else
        {
            mainUI.labelConsole.text = "no path to the target point from the start point";
        }
    }
Ejemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            var runs = (int)numericUpDown1.Value;


            var search    = new AStarSearch(_grid);
            var timer     = new Stopwatch();
            var times     = new List <long>();
            var stepTimes = new List <double>();

            for (var runIndex = 0; runIndex < runs; runIndex++)
            {
                var start  = new Vector2Int((int)numericUpDown2.Value, (int)numericUpDown3.Value);
                var finish = new Vector2Int((int)numericUpDown4.Value, (int)numericUpDown5.Value);

                timer.Start();

                var greedyPath = search.GreedyFind(start, finish);
                var path       = search.Find(start, finish);

                timer.Stop();

                if (!greedyPath.Any())
                {
                    throw new Exception("Any");
                }
                if (!greedyPath.Last().Location.Equals(finish))
                {
                    finish = greedyPath.Last().Location;
                }
                if (!greedyPath.First().Location.Equals(start))
                {
                    throw new Exception("Start");
                }
                RenderPath(_width, _height, start, finish, greedyPath, path, search, _grid, runIndex, timer.Elapsed);

                times.Add(timer.ElapsedMilliseconds);
                stepTimes.Add((double)timer.ElapsedMilliseconds / greedyPath.Length);

                timer.Reset();
            }
        }
Ejemplo n.º 3
0
        private void button4_Click(object sender, EventArgs e)
        {
            panel1.MaximumSize = panel1.Size;

            var runs = 1;

            var image = (Bitmap)Image.FromFile("cavern.gif");

            var width  = image.Width;
            var height = image.Height;

            _grid = new Grid(width, height);

            if (!Directory.Exists("Paths"))
            {
                Directory.CreateDirectory("Paths");
            }

            var timer = new Stopwatch();

            var times     = new List <long>();
            var stepTimes = new List <double>();

            var search = new AStarSearch(_grid);

            for (var runIndex = 0; runIndex < runs; runIndex++)
            {
                if (numericUpDown2.Value > _grid.Width)
                {
                    numericUpDown2.Value = _grid.Width - 10;
                }

                if (numericUpDown3.Value > _grid.Width)
                {
                    numericUpDown3.Value = _grid.Height - 10;
                }

                if (numericUpDown4.Value > _grid.Width)
                {
                    numericUpDown4.Value = _grid.Width - 10;
                }

                if (numericUpDown5.Value > _grid.Width)
                {
                    numericUpDown5.Value = _grid.Height - 10;
                }

                var start  = new Vector2Int((int)numericUpDown2.Value, (int)numericUpDown3.Value);
                var finish = new Vector2Int((int)numericUpDown4.Value, (int)numericUpDown5.Value);

                for (var x = 0; x < width; x++)
                {
                    for (var y = 0; y < height; y++)
                    {
                        _grid[x, y].Blocked = (image.GetPixel(x, y).R + image.GetPixel(x, y).G + image.GetPixel(x, y).B) < 120;
                    }
                }

                timer.Start();

                var greedyPath = search.GreedyFind(start, finish);

                var path = search.Find(start, finish);

                timer.Stop();

                if (!greedyPath.Any() || !greedyPath.Last().Location.Equals(finish) || !greedyPath.First().Location.Equals(start))
                {
                    MessageBox.Show(
                        "Скорее всего вы указали стену, алгоритм не смог найти заданную точку",
                        "Введите новое значение",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error,
                        MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.DefaultDesktopOnly);
                }
                else
                {
                    RenderPath(width, height, start, finish, greedyPath, path, search, _grid, runIndex, timer.Elapsed); //greedy

                    times.Add(timer.ElapsedMilliseconds);
                    stepTimes.Add((double)timer.ElapsedMilliseconds / greedyPath.Length);

                    timer.Reset();
                }
            }
            if (times.Count > 0)
            {
                Console.WriteLine("Elapsed: Avg: {0:000}ms Min: {1:000}ms Max: {2:000}ms StepAvg:{3:0.000000}ms",
                                  times.Average(), times.Min(), times.Max(), stepTimes.Average());
            }
        }
Ejemplo n.º 4
0
        private static void Main(string[] args)
        {
            var runs = 100;

            var image = (Bitmap)Image.FromFile("cavern.gif");

            var width  = image.Width;
            var height = image.Height;

            var grid = new Grid(width, height);

            if (!Directory.Exists("Paths"))
            {
                Directory.CreateDirectory("Paths");
            }

            var timer = new Stopwatch();

            var times     = new List <long>();
            var stepTimes = new List <double>();

            var search = new AStarSearch(grid);

            for (var runIndex = 0; runIndex < runs; runIndex++)
            {
                var start = new Vector2Int(10, 10);
                var goal  = new Vector2Int(width - 10, height - 10);

                for (var x = 0; x < width; x++)
                {
                    for (var y = 0; y < height; y++)
                    {
                        grid[x, y].Blocked = (image.GetPixel(x, y).R + image.GetPixel(x, y).G + image.GetPixel(x, y).B) / 3 < 128;
                    }
                }

                timer.Start();

                var path = search.Find(start, goal);
                timer.Stop();

                if (!path.Any() || !path.Last().Location.Equals(goal) || !path.First().Location.Equals(start))
                {
                    throw new Exception("Failure");
                }

                RenderPath(width, height, start, goal, path, search, grid, runIndex, timer.Elapsed);

                times.Add(timer.ElapsedMilliseconds);
                stepTimes.Add((double)timer.ElapsedMilliseconds / path.Length);

                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Start: {0} Goal: {1}", start, goal);
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("Step: {0:0000} - {1:000.00000000}ms - {2:p}", path.Length, timer.ElapsedMilliseconds,
                                  (double)(runIndex + 1) / runs);

                timer.Reset();
            }

            Console.WriteLine("Elapsed: Avg: {0:000}ms Min: {1:000}ms Max: {2:000}ms StepAvg:{3:0.000000}ms",
                              times.Average(), times.Min(), times.Max(), stepTimes.Average());

            Console.ReadKey();
        }