Beispiel #1
0
        public void idfs(PuzzleGrid grid, int depth)
        {
            if (depth < 0)
            {
                return; // end of recursion
            }
            //check if reached solution, to stop the algorithm
            if (grid.checkIfSolved())
            {
                Console.WriteLine("SOLVED!");
                grid.printGrid();
                this.goal = grid;
                return;
            }

            PuzzleGrid newPuzzleState;

            foreach (char i in possible_moves)
            {
                //Console.WriteLine(i);
                //Console.WriteLine(grid.move(i));
                newPuzzleState = grid.move(i);
                if (newPuzzleState != null && !doneMoves.Contains(newPuzzleState))
                {
                    CallIDFS(newPuzzleState, depth);
                    //  newPuzzleState.printGrid();
                }
            }
        }
Beispiel #2
0
        public void CallIDFS(PuzzleGrid newPuzzleState, int depth)
        {
            doneMoves.Add(newPuzzleState);
            idfs(newPuzzleState, depth - 1);
            if (goal != null)
            {
                return;
            }

            doneMoves.Remove(newPuzzleState);
        }
Beispiel #3
0
 public bool Equals(PuzzleGrid other)
 {
     if (other == null)
     {
         return(false);
     }
     else
     {
         return(this.Equals(other));
     }
 }
Beispiel #4
0
        public bool SMA(PuzzleGrid grid, int heuristic_id)
        {
            SimplePriorityQueue <PuzzleGrid> frontier  = new SimplePriorityQueue <PuzzleGrid>();
            HashSet <PuzzleGrid>             doneMoves = new HashSet <PuzzleGrid>();

            frontier.Enqueue(grid, 0);
            doneMoves.Add(grid);
            var previousCost = 0;

            while (frontier.Count != 0)
            {
                grid = frontier.Dequeue();

                foreach (char i in possible_moves)
                {
                    var newPuzzleState = grid.move(i);

                    if (newPuzzleState == null)
                    {
                        continue;
                    }

                    if (newPuzzleState.checkIfSolved())
                    {
                        Console.WriteLine("SOLVED!");
                        newPuzzleState.printGrid();
                        return(true);
                    }

                    if (!doneMoves.Contains(newPuzzleState))
                    {
                        grid._level_of_depth++;
                        int priorityLevel = 0;
                        if (heuristic_id == 1)
                        {
                            priorityLevel = newPuzzleState.manhatann_heuristic() + grid._level_of_depth;
                        }
                        else if (heuristic_id == 2)
                        {
                            priorityLevel = newPuzzleState.diagonal_heuristic() + grid._level_of_depth;
                        }
                        else if (heuristic_id == 3)
                        {
                            priorityLevel = (int)newPuzzleState.euclides_heuristic() + grid._level_of_depth;
                        }
                        frontier.Enqueue(newPuzzleState, Math.Max(priorityLevel, previousCost));
                        previousCost = priorityLevel;
                        doneMoves.Add(newPuzzleState);
                    }
                }
            }
            return(false);
        }
Beispiel #5
0
        public void iterativeDeepening(PuzzleGrid puzzleState, int depthLimit)
        {
            doneMoves.Add(puzzleState); //starting grid

            for (int i = 1; i <= depthLimit; i++)
            {
                idfs(puzzleState, i);  // depth-first search at this level

                // Check if the goal grid has been reached
                if (goal != null)
                {
                    return;
                }
            }
        }
Beispiel #6
0
 //copy contructor
 public PuzzleGrid(PuzzleGrid _previousGrid)
 {
     this._gridSize = _previousGrid._gridSize;
     this.history   = _previousGrid.history;
     for (var x = 0; x < _gridSize; x++)
     {
         for (var y = 0; y < _gridSize; y++)
         {
             this.grid[x, y] = _previousGrid.grid[x, y];
         }
     }
     this._level_of_depth = _previousGrid._level_of_depth;
     this._zeroColumn     = _previousGrid._zeroColumn;
     this._zeroRow        = _previousGrid._zeroRow;
 }
Beispiel #7
0
        public Boolean DFS(PuzzleGrid grid)
        {
            Stack <PuzzleGrid>   frontier  = new Stack <PuzzleGrid>();
            HashSet <PuzzleGrid> doneMoves = new HashSet <PuzzleGrid>();

            frontier.Push(grid);
            doneMoves.Add(grid);


            while (frontier.Count != 0)
            {
                grid = frontier.Pop();

                foreach (char i in possible_moves)
                {
                    var newPuzzleState = grid.move(i);

                    if (newPuzzleState == null)
                    {
                        continue;
                    }

                    if (newPuzzleState.checkIfSolved())
                    {
                        Console.WriteLine("SOLVED!");
                        newPuzzleState.printGrid();
                        return(true);
                    }

                    if (!doneMoves.Contains(newPuzzleState))
                    {
                        frontier.Push(newPuzzleState);
                        doneMoves.Add(newPuzzleState);
                    }
                }
            }
            return(false);
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            // Console.Write("Please use following commands:\n",
            //          "-b || --bfs order \t\t Breadth First Search \n",
            //          "-d || --dfs order \t\t for Depth First Search \n",
            //          "-i || --idfs order \t\t for Iterative Depth First Search \n",
            //          "-h || --bf id_of_heurisic \t Best-first strategy \n",
            //          "-a || --astar id_of_heurisic \t A* strategy \n",
            //          "-s || --sma id_of_heurisic \t SMA* strategy for Breadth First Search \n");


            Console.Write("Enter the size of grid: ");

            int[,] rowGrid;
            string pattern = @"^([udlr]{4})|(x)$";
            Match  result;

            char[] order         = new char[4];
            var    possibleMoves = new List <char> {
                'u', 'd', 'l', 'r'
            };
            Stopwatch watch = new Stopwatch();
            long      elapsedMs;

            int[,] grid;
            PuzzleGrid puzzle;
            Algorithms al;
            int        gridSize;


            gridSize = Convert.ToInt32(Console.ReadLine());
            rowGrid  = new int[gridSize, gridSize];
            Console.WriteLine("Enter the grid, line by line with spaces(' '): ");
            try
            {
                for (var x = 0; x < gridSize; x++)
                {
                    string[] s = Console.ReadLine().Split(' ');
                    for (var y = 0; y < gridSize; y++)
                    {
                        rowGrid[x, y] = Int32.Parse(s[y]);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }

            watch  = new Stopwatch();
            grid   = rowGrid;
            puzzle = new PuzzleGrid(grid);

            string[] input; // to gather input
            int      heuristic_id = 0;


            Console.WriteLine("Enter the algorithm: ");
            input = Console.ReadLine().Split(' ');
            string algorithm = input[0];

            al = new Algorithms(order);

            if (algorithm != "--astar" && algorithm != "--sma" && algorithm != "-a" && algorithm != "-s" &&
                algorithm != "--bf" && algorithm != "-h")
            {
                Console.WriteLine("Give the order of moves: ");
                do
                {
                    input = Console.ReadLine().Split(' ');
                    input[0].ToLower();
                    result = Regex.Match(input[0], pattern);
                } while (!result.Success);

                // check if order should be random or not
                if (input[0][0] == 'x')
                {
                    for (int i = 0; i < 4; i++)
                    {
                        int index = rand.Next(possibleMoves.Count);
                        order[i] = possibleMoves[index];
                        possibleMoves.RemoveAt(index);
                    }
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                    {
                        order[i] = input[0][i];
                    }
                }
                Console.WriteLine(new string(order));
            }
            else
            {
                Console.WriteLine("Please enter the id of the heuristic: ");
                // tutaj wybór heurysyki
                input = Console.ReadLine().Split(' ');
                if (input[0][0] == '1')
                {
                    heuristic_id = 1;
                }
                else if (input[0][0] == '2')
                {
                    heuristic_id = 2;
                }
                else if (input[0][0] == '3')
                {
                    heuristic_id = 3;
                }
                for (int i = 0; i < 4; i++)
                {
                    int index = rand.Next(possibleMoves.Count);
                    order[i] = possibleMoves[index];
                    possibleMoves.RemoveAt(index);
                }
                Console.WriteLine(heuristic_id);
            }


            switch (algorithm)
            {
            case "-b":
            case "--bfs":
            {
                Console.WriteLine("BFS started:");
                watch = Stopwatch.StartNew();
                Console.WriteLine(al.BFS(puzzle));
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Elapsed time: {0} ms", elapsedMs);
                break;
            }

            case "-d":
            case "--dfs":
            {
                Console.WriteLine("DFS started:");
                watch = Stopwatch.StartNew();
                al.DFS(puzzle);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Elapsed time: {0} ms", elapsedMs);
                break;
            }

            case "-i":
            case "--idfs":
            {
                Console.WriteLine("IDFS started:");
                watch = Stopwatch.StartNew();
                al.IDFS(puzzle);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Elapsed time: {0} ms", elapsedMs);
                break;
            }

            case "-h":
            case "--bf":
            {
                Console.WriteLine("BF started:");
                watch = Stopwatch.StartNew();
                al.BFTS(puzzle, heuristic_id);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Elapsed time: {0} ms", elapsedMs);
                break;
            }

            case "-a":
            case "--astar":
            {
                Console.WriteLine("A* started:");
                watch = Stopwatch.StartNew();
                al.ASTAR(puzzle, heuristic_id);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Elapsed time: {0} ms", elapsedMs);
                break;
            }

            case "-s":
            case "--sma":
            {
                Console.WriteLine("SMA* started:");
                watch = Stopwatch.StartNew();
                al.SMA(puzzle, heuristic_id);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Elapsed time: {0} ms", elapsedMs);
                break;
            }

            default:
                Console.Write(
                    "Please use following commands:\n-b || --bfs order \t\t Breadth First Search \n-d || --dfs order \t\t for Depth First Search \n-i || --idfs order \t\t for Iterative Depth First Search \n-h || --bf id_of_heurisic \t Best-first strategy \n-a || --astar id_of_heurisic \t A* strategy \n-s || --sma id_of_heurisic \t SMA* strategy for Breadth First Search \n");
                break;
            }
        }
Beispiel #9
0
        public void IDFS(PuzzleGrid grid)
        {
            var idfsSolver = new IDFS(possible_moves);

            idfsSolver.iterativeDeepening(grid, MAX_DEPTH);
        }