Exemplo n.º 1
0
    static void Main(string[] args)
    {
        PuzzleState initState = null;
        PuzzleState goalState = null;

        try {
            initState = new PuzzleState(args[0]);
            goalState = new PuzzleState(args[1]);
        } catch {
            Console.WriteLine("Invalid input!\n" +
                              "Correct Example- Input in the form: 8,3,5,4,1,6,2,7,0 1,2,3,8,0,4,7,6,5 yields the following...\n" +
                              "Initial State\n" +
                              "8 3 5\n" +
                              "4 1 6\n" +
                              "2 7 0\n\n" +
                              "Goal State\n" +
                              "1 2 3\n" +
                              "8 0 4\n" +
                              "7 6 5\n");
            Console.WriteLine("Press enter to close...");
            Console.Read();
            Environment.Exit(1);
        }

        Heuristic[] heuristics = new Heuristic[] { Heuristic.ManhattanDistance };

        foreach (Heuristic h in heuristics)
        {
            PuzzleSolver solver = new PuzzleSolver(initState, goalState);
            Console.WriteLine(string.Format("\n\nSolving with {0} heuristic...", h.ToString()));
            var           watch   = System.Diagnostics.Stopwatch.StartNew();
            PuzzleResults results = solver.ApplyAStar(h);
            watch.Stop();

            results.Print();
            Console.WriteLine(string.Format("Time elapsed: {0} ms", watch.ElapsedMilliseconds));
        }

        Console.WriteLine("Done! Press Enter key to close...");
        Console.Read();
    }
Exemplo n.º 2
0
    public PuzzleResults ApplyAStar(Heuristic heuristic)
    {
        PriorityQueue <PuzzleState> fringe = new PriorityQueue <PuzzleState>();
        // Closed set
        HashSet <PuzzleState> visited = new HashSet <PuzzleState>();

        // We start with only the initial state generated.
        int numGenerated = 1;
        int numExpanded  = 0;

        PuzzleState pathEnd = null;

        fringe.Enqueue(InitState, 0);

        while (!fringe.IsEmpty())
        {
            PuzzleState current = fringe.Dequeue();
            visited.Add(current);
            numExpanded++;

            // Goal check.
            if (current.Equals(GoalState))
            {
                pathEnd = current;
                break;
            }

            List <PuzzleState> successors = current.GetSuccessors();

            foreach (PuzzleState s in successors)
            {
                if (!visited.Contains(s))
                {
                    // Compute heuristic value and add to total cost to get priority.
                    int hValue = Int32.MaxValue;
                    if (heuristic == Heuristic.MisplacedTiles)
                    {
                        hValue = s.ComputeMisplacedTileDistance(GoalState);
                    }
                    else if (heuristic == Heuristic.ManhattanDistance)
                    {
                        hValue = s.ComputeManhattanDistance(GoalState);
                    }
                    else if (heuristic == Heuristic.None)
                    {
                        hValue = 0;
                    }
                    else
                    {
                        throw new ArgumentException("Expected MisplacedTiles or ManhattanDistance heuristic.");
                    }

                    int  priority       = s.Depth + hValue;
                    bool alreadyExisted = fringe.Enqueue(s, priority);
                    if (!alreadyExisted)
                    {
                        numGenerated++;
                    }
                }
            }
        }


        // Build path into list.
        List <PuzzleState> path         = new List <PuzzleState>();
        PuzzleState        currPathNode = pathEnd;

        path.Add(currPathNode);

        while (currPathNode.Prev != null)
        {
            path.Add(currPathNode.Prev);
            currPathNode = currPathNode.Prev;
        }

        path.Reverse();

        PuzzleResults results = new PuzzleResults(numGenerated, numExpanded, path);

        return(results);
    }