Esempio n. 1
0
        public void FindSolutionTest()
        {
            byte[] puzzle = new byte[] { 2, 5, 3, 4, 1, 7, 11, 8, 10, 6, 14, 0, 9, 13, 15, 12 };
            // byte[] puzzle = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10, 13, 11, 12, 9, 14, 0, 15 };
            GraphExplorer explorer = GraphExplorer.CreateGraphExplorer
                                         (new byte[] { 4, 4 },
                                         puzzle,
                                         new char[] { 'u', 'd', 'l', 'r' }, new DFS());

            explorer.TargetState = new NodeState(new byte[] { 4, 4 },
                                                 new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 });
            string sol = explorer.TraverseForSolution();

            Assert.IsFalse(string.IsNullOrEmpty(sol));
        }
Esempio n. 2
0
        public void AlgorithmManhattanHeuristicTest()
        {
            byte[]            puzzle            = new byte[] { 1, 2, 3, 4, 5, 11, 0, 7, 9, 6, 10, 8, 13, 14, 15, 12 };
            HeuristicProvider heuristicProvider = new Manhattan(solution);
            GraphExplorer     explorer          = GraphExplorer.CreateGraphExplorer(
                new byte[] { 4, 4 },
                puzzle,
                new char[] { 'u', 'd', 'l', 'r' }, new AStar(heuristicProvider));

            explorer.TargetState = new NodeState(new byte[] { 4, 4 },
                                                 new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 });
            string sol = explorer.TraverseForSolution();

            Assert.IsFalse(string.IsNullOrEmpty(sol));
        }
Esempio n. 3
0
        public void FindSolutionTest()
        {
            byte[]        puzzle   = new byte[] { 1, 2, 3, 4, 5, 11, 0, 7, 9, 6, 10, 8, 13, 14, 15, 12 };
            GraphExplorer explorer = GraphExplorer.CreateGraphExplorer
                                         (new byte[] { 4, 4 },
                                         puzzle,
                                         new char[] { 'u', 'd', 'l', 'r' }, new BFS());

            explorer.TargetState = new NodeState(new byte[] { 4, 4 },
                                                 new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 });
            var sol = explorer.TraverseForSolution();

            System.Console.WriteLine(sol);
            Assert.IsNotNull(sol);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if (args.Length == 5)
            {
                GraphExplorer explorer     = null;
                IFinder       finder       = null;
                string        inputFile    = args[2];
                string        outputFile   = args[3];
                string        dataFile     = args[4];
                string        operations   = null;
                string        solutionPath = "solution.txt";
                IState        solutionState;
                byte[]        dimensions, root;
                (dimensions, root) = LoadInputFile(inputFile);
                if (File.Exists(solutionPath))
                {
                    byte[] solutionDimensions, state;
                    (solutionDimensions, state) = LoadInputFile(solutionPath);
                    solutionState = new NodeState(solutionDimensions, state);
                }
                else
                {
                    byte   limit         = (byte)(dimensions[0] * dimensions[1]);
                    byte[] solutionArray = new byte[limit];
                    for (byte i = 0; i < limit - 1; ++i)
                    {
                        solutionArray[i] = (byte)(i + 1);
                    }
                    solutionArray[limit - 1] = 0;
                    solutionState            = new NodeState(dimensions, solutionArray);
                }
                try
                {
                    switch (args[0])
                    {
                    case "bfs":
                        finder     = new BFS();
                        operations = args[1].ToLower();
                        break;

                    case "dfs":
                        finder     = new DFS();
                        operations = Reverse(args[1].ToLower());
                        break;

                    case "astr":
                        HeuristicProvider heuristicProvider = null;
                        switch (args[1])
                        {
                        case "manh":
                            heuristicProvider = new Manhattan(solutionState);
                            break;

                        case "hamm":
                            heuristicProvider = new Hamming(solutionState);
                            break;
                        }
                        operations = "lrud";
                        finder     = new AStar(heuristicProvider);
                        break;
                    }
                    if (Enumerable.SequenceEqual(dimensions, solutionState.Dimensions))
                    {
                        explorer = GraphExplorer.CreateGraphExplorer(dimensions,
                                                                     root, operations.ToCharArray(), finder);
                        explorer.TargetState = solutionState;
                    }
                    else
                    {
                        throw new NullReferenceException("root state and solution state have different dimensions");
                    }
                }
                catch (NullReferenceException e)
                {
                    Console.WriteLine($"Wrong parameters format {Environment.NewLine}{e.Message}");
                }
                catch (FormatException e)
                {
                    Console.WriteLine($"Wrong input file format {Environment.NewLine}{e.Message}");
                }
                string solution = explorer.TraverseForSolution();
                WriteOutputFiles(outputFile, dataFile, solution, explorer);
            }
        }