Esempio n. 1
0
        private static void CheckResultsOptimality()
        {
            int iterations              = 4;
            int optimalBfsLenghtCount   = 0;
            int optimalAStarLenghtCount = 0;

            var pathLengthCalculator = new PathLengthCalculator();

            Parallel.For(0, iterations, _ =>
            {
                Board board = new RandomBoardGenerator().Generate(Board.FinalBoard, 1000);

                var bfsResult     = new BfsSolver().Solve(board);
                var aStarResult   = new AStarSolver().Solve(board);
                var optimalResult = new AStarWithoutLoopsSolver().Solve(board);

                int bfsResultLenght     = pathLengthCalculator.CalculatePathLenght(bfsResult.FinalState);
                int aStarResultLenght   = pathLengthCalculator.CalculatePathLenght(aStarResult.FinalState);
                int optimalResultLenght = pathLengthCalculator.CalculatePathLenght(optimalResult.FinalState);

                if (bfsResultLenght == optimalResultLenght)
                {
                    Interlocked.Increment(ref optimalBfsLenghtCount);
                }

                if (aStarResultLenght == optimalResultLenght)
                {
                    Interlocked.Increment(ref optimalAStarLenghtCount);
                }
            });

            Console.WriteLine($"Iterations: {iterations}");
            Console.WriteLine($"Optimal results from BFS: {optimalBfsLenghtCount}");
            Console.WriteLine($"Optimal results from AStar: {optimalAStarLenghtCount}");
        }
Esempio n. 2
0
        private void GotoObject(NodeType nodeType)
        {
            var solver = new BfsSolver();


            var  solution     = solver.FindWay(ActionField, nodeType);
            Node reservedNode = solution.Route.Nodes.First().Copy();

            reservedNode.NodeType = NodeType.Gross;

            foreach (var node in solution.Route.Nodes)
            {
                if (reservedNode != null)
                {
                    ActionField.UpdateFieldNodeType(reservedNode.Point, reservedNode.NodeType);
                }

                reservedNode = node.Copy();

                ActionField.UpdateFieldNodeType(node.Point, NodeType.Agent);

                Thread.Sleep(1000);
            }
        }
Esempio n. 3
0
        private void StartSolvationProcess()
        {
            Task.Run(() =>
            {
                var solver   = new BfsSolver();
                var solution = solver.Solve(ActionField);

                Point previousPoint       = null;
                NodeType previousNodeType = NodeType.Agent;
                //foreach (var node in solution.Route.Nodes)
                //{
                //    if (previousPoint != null)
                //    {
                //        ActionField.UpdateFieldNodeType(previousPoint, previousNodeType);
                //    }
                //    previousNodeType = ActionField.Nodes.GetNode(point).NodeType;
                //    previousPoint = point;

                //    ActionField.UpdateFieldNodeType(point, NodeType.Agent);

                //    Thread.Sleep(1000);
                //}
            });
        }
Esempio n. 4
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(rooms, paths) = inputStream.ReadValue <int, int>();
            var dungeon = new BasicGraph(rooms);

            for (int i = 0; i < paths; i++)
            {
                var(a, b) = inputStream.ReadValue <int, int>();
                a--;
                b--;
                dungeon.AddEdge(new BasicEdge(a, b));
                dungeon.AddEdge(new BasicEdge(b, a));
            }

            var solver   = new BfsSolver(dungeon);
            var previous = solver.Search(new BasicNode(0));

            yield return("Yes");

            for (int i = 1; i < previous.Length; i++)
            {
                yield return(previous[i] + 1);
            }
        }
        /// <summary>
        /// Processes a TSP and reports errors.
        /// </summary>
        /// <param name="file">File name of TSP being processed.</param>
        private void ProcessTsp(string file)
        {
            System.Diagnostics.Debug.WriteLine($"Loading TSP at path {file} ...\n");

            // Try convert text to TSP
            var success = Interface.TryTextToTsp(file, out var tsp);

            // If conversion was a success
            if (success)
            {
                // Print TSP info to debug console
                tsp.Print();

                // Update name of problem on graph
                this.problemName.Text = tsp.Name;

                // Create solver and event handlers
                Solver solver = null;
                ProgressChangedEventHandler    updateHandler     = null;
                RunWorkerCompletedEventHandler completionHandler = null;
                int drawDelay = 0;

                // Set solvers and event handlers based on the chosen setting
                if (bruteForceRadioButton.IsChecked == true)
                {
                    solver = new BruteForceSolver();

                    MessageBox.Show("Solution method not yet implemented after refactoring.");
                    return;
                }
                else if (bfsRadioButton.IsChecked == true || dfsRadioButton.IsChecked == true)
                {
                    var goal = Convert.ToInt32(searchGoal.Text);

                    if (bfsRadioButton.IsChecked == true)
                    {
                        solver = new BfsSolver();
                    }
                    else
                    {
                        solver = new DfsSolver();
                    }

                    MessageBox.Show("Solution method not yet implemented after refactoring.");
                    return;
                }
                else if (greedyRadioButton.IsChecked == true)
                {
                    solver            = new GreedySolver();
                    updateHandler     = GreedyProgressChanged;
                    completionHandler = GreedyCompletion;
                    drawDelay         = GREEDY_DRAW_DELAY;
                }
                else if (geneticRadioButton.IsChecked == true)
                {
                    solver            = new GeneticSolver();
                    updateHandler     = GeneticProgressChanged;
                    completionHandler = GreedyCompletion;
                    drawDelay         = GREEDY_DRAW_DELAY;
                }
                else
                {
                    MessageBox.Show("No solution method was selected.");
                    return;
                }

                if (progressCheckBox.IsChecked == true)
                {
                    // Update continously
                    solver.ProgressChanged += updateHandler;
                }
                else
                {
                    // Update only at the end
                    solver.RunWorkerCompleted += completionHandler;
                }

                var workerArgs = new Tuple <Tsp, int>(tsp, progressCheckBox.IsChecked == true ? drawDelay : 0);

                // Run solver and draw output
                solver.RunWorkerAsync(workerArgs);
            }
            // If conversion failed
            else
            {
                MessageBox.Show($"Could not convert TSP from text file: \n\n{file}");
            }
        }
Esempio n. 6
0
 public void TestSolution()
 {
     var af            = CreateActionField();
     var solver        = new BfsSolver();
     var solutionRoute = solver.Solve(af);
 }