static void Main(string[] args)
        {
            ///////////    Uniform Cost //////////////////////

            int[]     PuzzleArray   = new int[] { 0, 2, 3, 1, 8, 4, 7, 6, 5 };
            int[]     Easy          = new int[] { 1, 3, 4, 8, 6, 2, 7, 0, 5 };
            int[]     Medium        = new int[] { 2, 8, 1, 0, 4, 3, 7, 6, 5 };
            int[]     Hard          = new int[] { 5, 6, 7, 4, 0, 8, 3, 2, 1 };
            int[]     GoalState     = new int[] { 1, 2, 3, 8, 0, 4, 7, 6, 5 };
            StateNode GoalStateNode = new StateNode();
            int       Time          = 0;
            int       TotalCostOfAllMoves;
            //int CostFromStart;

            int SizeOfSortedList = 0;
            int MaxOfSortedList  = 0;

            int SizeOfPQ = 0;
            int MaxOfPQ  = 0;

            PuzzleArray = Hard;

            foreach (var item in PuzzleArray)
            {
                Console.Write(item.ToString() + " ");
            }

            Console.WriteLine();
            Console.WriteLine();

            foreach (var item in PuzzleArray)
            {
                Console.Write(item.ToString() + " ");
            }
            Console.WriteLine();
            Console.WriteLine("State array");
            Console.WriteLine();

            StateNode node = new StateNode();
            StateNode root = new StateNode(PuzzleArray);

            //foreach (var item in node.StateArray)
            //{

            //    Console.Write(item.ToString() + " ");

            //}

            Console.WriteLine();
            Console.WriteLine("State array Passing PuzzleArray");
            Console.WriteLine();

            foreach (var item in root.StateArray2)
            {
                Console.Write(item.ToString() + " ");
            }

            //// Create Queue of Nodes
            //Queue<StateNode> q = new Queue<StateNode>();

            // Create Queue that will accept queue returned from Successor function
            Queue <StateNode> qFromSuccessorFunction = new Queue <StateNode>();

            //// Put original puzzle state in queue
            //q.Enqueue(root);
            //SizeOfQueue = 1;
            //MaxOfQueue = MaxOfQueue + 1;


            // Depth First Search - Create a stack
            // Stack<StateNode> stack = new Stack<StateNode>();

            // Uniform Cost Search - Need to make a Sorted List
            // Sorted based on the smallest cost from the start node of tree
            // Each move made the cost is the value of tile moved
            SortedList <int, StateNode> sortedListBasedOnCostOfMovesFromStart = new SortedList <int, StateNode>();


            PriorityQueue <StateNode> pq = new PriorityQueue <StateNode>();

            //// Put original puzzle state in Sorted List
            //sortedListBasedOnCostOfMovesFromStart.Add(root.CostOfMovesFromStart, root);
            //SizeOfSortedList = SizeOfSortedList + 1;
            //MaxOfSortedList = MaxOfSortedList + 1;

            pq.Add(root.CostOfMovesFromStart, root);
            SizeOfPQ = SizeOfPQ + 1;
            MaxOfPQ  = MaxOfPQ + 1;

            //// Put original puzzle state on stack
            //stack.Push(root);
            //SizeOfStack = SizeOfStack + 1;
            //MaxOfStack = MaxOfStack + 1;

            // Create two Dictionaries to look up previous states for state checking. Need to check if on the stack or if it was
            // previously looked at.
            Dictionary <int, int> dictOfNodesInPQ = new Dictionary <int, int>();

            Dictionary <int, int> dictOfStateArraysSeenBefore = new Dictionary <int, int>();

            dictOfNodesInPQ.Add(root.ArrayStateInt, root.ArrayStateInt);



            // Beginning of General Search Loop
            bool keepRunning = true;

            while (keepRunning)
            {
                if (pq.Count == 0)
                {
                    Console.WriteLine("sortedList empty");
                    keepRunning = false;
                }
                if (keepRunning == false)
                {
                    break;
                }


                //Remove Nodes from from of the sortedList
                StateNode removeFromPQ = new StateNode();
                //Assigns Node to first element of list
                removeFromPQ = pq.RemoveMin();
                //Then need to remove the first element(<key, value> pair) of sorted list


                Time = Time + 1;

                //// Remove nodes from top of stack for Depth-First Search
                //StateNode removeFromTopOfStack = new StateNode();
                //removeFromTopOfStack = stack.Pop();
                //Time = Time + 1;

                // Remove node from Dictionary with list of nodes currently on stack
                dictOfNodesInPQ.Remove(removeFromPQ.ArrayStateInt);
                Console.WriteLine();
                Console.WriteLine("removing this node StateArrayInt: " + removeFromPQ.ArrayStateInt);
                Console.WriteLine();

                // add this node to dictionary of nodes we have seen before if we haven't already seen it
                if (dictOfStateArraysSeenBefore.ContainsKey(removeFromPQ.ArrayStateInt))
                {
                    //do nothing
                }
                else
                {
                    dictOfStateArraysSeenBefore.Add(removeFromPQ.ArrayStateInt, removeFromPQ.ArrayStateInt);
                }



                // check if node ArrayState is Equal to the GoalState to Solve PuzzleEight
                bool isEqual = Enumerable.SequenceEqual(removeFromPQ.StateArray2, GoalState);
                if (isEqual)
                {
                    Console.WriteLine();
                    Console.WriteLine("Puzzle is Solved!");
                    GoalStateNode = removeFromPQ;
                    Console.WriteLine();
                    break;
                }


                // create a queue to accept the queue returned from successor function


                qFromSuccessorFunction = StateNode.Successor(removeFromPQ);



                //// add nodes from Successor Function into the q for the loop

                while (qFromSuccessorFunction.Count != 0)
                {
                    StateNode removeFromFrontOfSuccessorQueue = new StateNode();
                    removeFromFrontOfSuccessorQueue = qFromSuccessorFunction.Dequeue();



                    //If we have already seen this state before or this state is on the stack currently, do nothing. Otherwise put state in both dictionaries
                    if (dictOfNodesInPQ.ContainsKey(removeFromFrontOfSuccessorQueue.ArrayStateInt) || (dictOfStateArraysSeenBefore.ContainsKey(removeFromFrontOfSuccessorQueue.ArrayStateInt)))
                    {
                        // do nothing
                    }
                    else
                    {
                        pq.Add(removeFromFrontOfSuccessorQueue.CostOfMovesFromStart, removeFromFrontOfSuccessorQueue);
                        // sortedListBasedOnCostOfMovesFromStart.Add(removeFromFrontOfSuccessorQueue.CostOfMovesFromStart, removeFromFrontOfSuccessorQueue);
                        dictOfNodesInPQ.Add(removeFromFrontOfSuccessorQueue.ArrayStateInt, removeFromFrontOfSuccessorQueue.ArrayStateInt);
                        dictOfStateArraysSeenBefore.Add(removeFromFrontOfSuccessorQueue.ArrayStateInt, removeFromFrontOfSuccessorQueue.ArrayStateInt);
                    }

                    SizeOfPQ = pq.Count;
                    //SizeOfSortedList = sortedListBasedOnCostOfMovesFromStart.Count();
                    //if (MaxOfSortedList < SizeOfSortedList)
                    //{
                    //    MaxOfSortedList = SizeOfSortedList;
                    //}
                    if (MaxOfPQ < SizeOfPQ)
                    {
                        MaxOfPQ = SizeOfPQ;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("out of loop");
            Console.WriteLine();


            Console.WriteLine();
            Console.WriteLine("Here were the successful moves to solve the puzzle:");
            Console.WriteLine();

            StateNode.PrettyPrintPathToSolvePuzzle(GoalStateNode);

            Console.WriteLine();
            Console.WriteLine("Time = " + Time);
            Console.WriteLine();
            Console.WriteLine("MaxOfPQ " + MaxOfPQ);
            Console.WriteLine();


            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            int[]     PuzzleArray   = new int[] { 1, 2, 3, 0, 8, 4, 7, 6, 5 };
            int[]     Easy          = new int[] { 1, 3, 4, 8, 6, 2, 7, 0, 5 };
            int[]     Medium        = new int[] { 2, 8, 1, 0, 4, 3, 7, 6, 5 };
            int[]     Hard          = new int[] { 5, 6, 7, 4, 0, 8, 3, 2, 1 };
            int[]     GoalState     = new int[] { 1, 2, 3, 8, 0, 4, 7, 6, 5 };
            StateNode GoalStateNode = new StateNode();
            int       Time          = 0;
            int       TotalCostOfAllMoves;
            int       MaxOfQueue  = 0;
            int       SizeOfQueue = 0;

            PuzzleArray = Hard;

            foreach (var item in PuzzleArray)
            {
                Console.Write(item.ToString() + " ");
            }

            Console.WriteLine();
            Console.WriteLine();

            foreach (var item in PuzzleArray)
            {
                Console.Write(item.ToString() + " ");
            }
            Console.WriteLine();
            Console.WriteLine("State array");
            Console.WriteLine();

            StateNode node = new StateNode();
            StateNode root = new StateNode(PuzzleArray);

            //foreach (var item in node.StateArray)
            //{

            //    Console.Write(item.ToString() + " ");

            //}

            Console.WriteLine();
            Console.WriteLine("State array Passing PuzzleArray");
            Console.WriteLine();

            foreach (var item in root.StateArray2)
            {
                Console.Write(item.ToString() + " ");
            }

            // Create Queue of Nodes
            Queue <StateNode> q = new Queue <StateNode>();

            // Create Queue that will accept queue returned from Successor function
            Queue <StateNode> qFromSuccessorFunction = new Queue <StateNode>();

            // Put original puzzle state in queue
            q.Enqueue(root);
            SizeOfQueue = 1;
            MaxOfQueue  = MaxOfQueue + 1;


            // Beginning of General Search Loop
            bool keepRunning = true;

            while (keepRunning)
            {
                if (q.Count == 0)
                {
                    Console.WriteLine("queue empty");
                    keepRunning = false;
                }
                if (keepRunning == false)
                {
                    break;
                }



                // Remove nodes from front of queue for Breadth-First Search
                StateNode removeFromFront = new StateNode();
                removeFromFront = q.Dequeue();
                Time            = Time + 1;

                // Check to see if ArrayState of node that was popped from queue matches Puzzle Solution(GoalState)


                bool isEqual = Enumerable.SequenceEqual(removeFromFront.StateArray2, GoalState);
                if (isEqual)
                {
                    Console.WriteLine();
                    Console.WriteLine("Puzzle is Solved!");
                    GoalStateNode = removeFromFront;
                    Console.WriteLine();
                    break;
                }



                // REturn the Node that matches solution


                // Call Successor Function to get the next nodes to add to list and Expand
                //Console.WriteLine();
                //Console.WriteLine("Entering Successor Function");
                //Console.WriteLine();

                // create a queue to accept the queue returned from successor function


                qFromSuccessorFunction = StateNode.Successor(removeFromFront);



                // add nodes from Successor Function into the q for the loop

                while (qFromSuccessorFunction.Count != 0)
                {
                    StateNode removeFromFrontOfSuccessorQueue = new StateNode();
                    removeFromFrontOfSuccessorQueue = qFromSuccessorFunction.Dequeue();
                    q.Enqueue(removeFromFrontOfSuccessorQueue);
                    SizeOfQueue = q.Count;
                    if (MaxOfQueue < SizeOfQueue)
                    {
                        MaxOfQueue = SizeOfQueue;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("out of loop");
            Console.WriteLine();


            Console.WriteLine();
            Console.WriteLine("Here were the successful moves to solve the puzzle:");
            Console.WriteLine();

            StateNode.PrettyPrintPathToSolvePuzzle(GoalStateNode);

            Console.WriteLine();
            Console.WriteLine("Time = " + Time);
            Console.WriteLine();
            Console.WriteLine("MaxOfQueue " + MaxOfQueue);
            Console.WriteLine();


            Console.ReadLine();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            int[]     PuzzleArray   = new int[] { 0, 2, 3, 1, 8, 4, 7, 6, 5 };
            int[]     Easy          = new int[] { 1, 3, 4, 8, 6, 2, 7, 0, 5 };
            int[]     Medium        = new int[] { 2, 8, 1, 0, 4, 3, 7, 6, 5 };
            int[]     Hard          = new int[] { 5, 6, 7, 4, 0, 8, 3, 2, 1 };
            int[]     GoalState     = new int[] { 1, 2, 3, 8, 0, 4, 7, 6, 5 };
            StateNode GoalStateNode = new StateNode();
            int       Time          = 0;
            int       TotalCostOfAllMoves;
            int       MaxOfQueue              = 0;
            int       SizeOfQueue             = 0;
            int       SizeOfStack             = 0;
            int       MaxOfStack              = 0;
            bool      IteritiveDeepeningSolve = false;

            //Max set of Levels to go Down in Iterative Deepening
            //int BigL = 0;

            for (int BigL = 0; BigL < 999999999; BigL++)
            {
                PuzzleArray = Hard;

                foreach (var item in PuzzleArray)
                {
                    Console.Write(item.ToString() + " ");
                }

                Console.WriteLine();
                Console.WriteLine();

                foreach (var item in PuzzleArray)
                {
                    Console.Write(item.ToString() + " ");
                }
                Console.WriteLine();
                Console.WriteLine("State array");
                Console.WriteLine();

                StateNode node = new StateNode();
                StateNode root = new StateNode(PuzzleArray);

                //foreach (var item in node.StateArray)
                //{

                //    Console.Write(item.ToString() + " ");

                //}

                Console.WriteLine();
                Console.WriteLine("State array Passing PuzzleArray");
                Console.WriteLine();

                foreach (var item in root.StateArray2)
                {
                    Console.Write(item.ToString() + " ");
                }

                //// Create Queue of Nodes
                //Queue<StateNode> q = new Queue<StateNode>();

                // Create Queue that will accept queue returned from Successor function
                Queue <StateNode> qFromSuccessorFunction = new Queue <StateNode>();

                //// Put original puzzle state in queue
                //q.Enqueue(root);
                //SizeOfQueue = 1;
                //MaxOfQueue = MaxOfQueue + 1;


                // Depth First Search - Create a stack
                Stack <StateNode> stack = new Stack <StateNode>();

                // Put original puzzle state on stack
                stack.Push(root);
                SizeOfStack = SizeOfStack + 1;
                MaxOfStack  = MaxOfStack + 1;

                // Create two Dictionaries to look up previous states for state checking. Need to check if on the stack or if it was
                // previously looked at.
                Dictionary <int, int> dictOfNodesOnStack = new Dictionary <int, int>();

                Dictionary <int, int> dictOfStateArraysSeenBefore = new Dictionary <int, int>();

                dictOfNodesOnStack.Add(root.ArrayStateInt, root.ArrayStateInt);



                // Beginning of General Search Loop
                bool keepRunning = true;
                while (keepRunning)
                {
                    if (stack.Count == 0)
                    {
                        Console.WriteLine("stack empty");
                        keepRunning = false;
                    }
                    if (keepRunning == false)
                    {
                        break;
                    }



                    //// Remove nodes from front of queue for Breadth-First Search
                    //StateNode removeFromFront = new StateNode();
                    //removeFromFront = q.Dequeue();
                    //Time = Time + 1;

                    // Remove nodes from top of stack for Depth-First Search
                    StateNode removeFromTopOfStack = new StateNode();
                    removeFromTopOfStack = stack.Pop();
                    Time = Time + 1;

                    // Remove node from Dictionary with list of nodes currently on stack
                    dictOfNodesOnStack.Remove(removeFromTopOfStack.ArrayStateInt);
                    Console.WriteLine();
                    Console.WriteLine("removing this node StateArrayInt: " + removeFromTopOfStack.ArrayStateInt);
                    Console.WriteLine();

                    // add this node to dictionary of nodes we have seen before if we haven't already seen it
                    if (dictOfStateArraysSeenBefore.ContainsKey(removeFromTopOfStack.ArrayStateInt))
                    {
                        //do nothing
                    }
                    else
                    {
                        dictOfStateArraysSeenBefore.Add(removeFromTopOfStack.ArrayStateInt, removeFromTopOfStack.ArrayStateInt);
                    }



                    bool isEqual = Enumerable.SequenceEqual(removeFromTopOfStack.StateArray2, GoalState);
                    if (isEqual)
                    {
                        Console.WriteLine();
                        //Console.WriteLine("Puzzle is Solved!");
                        IteritiveDeepeningSolve = true;
                        GoalStateNode           = removeFromTopOfStack;
                        Console.WriteLine();
                        break;
                    }



                    // REturn the Node that matches solution



                    // Check the level of Node in the tree. If Node level is less than BigL, call sussessor.
                    // If == to BigL, can't call successor. We reached limit.

                    if (removeFromTopOfStack.LevelDownInTree < BigL)
                    {
                        qFromSuccessorFunction = StateNode.Successor(removeFromTopOfStack);
                    }



                    //// add nodes from Successor Function into the q for the loop

                    //while (qFromSuccessorFunction.Count != 0)
                    //{
                    //    StateNode removeFromFrontOfSuccessorQueue = new StateNode();
                    //    removeFromFrontOfSuccessorQueue = qFromSuccessorFunction.Dequeue();
                    //    q.Enqueue(removeFromFrontOfSuccessorQueue);
                    //    SizeOfQueue = q.Count;
                    //    if (MaxOfQueue < SizeOfQueue)
                    //    {
                    //        MaxOfQueue = SizeOfQueue;
                    //    }
                    //}

                    //// add nodes from Successor Function into the q for the loop

                    while (qFromSuccessorFunction.Count != 0)
                    {
                        StateNode removeFromFrontOfSuccessorQueue = new StateNode();
                        removeFromFrontOfSuccessorQueue = qFromSuccessorFunction.Dequeue();



                        //If we have already seen this state before or this state is on the stack currently, do nothing. Otherwise put state in both dictionaries
                        if (dictOfNodesOnStack.ContainsKey(removeFromFrontOfSuccessorQueue.ArrayStateInt) || (dictOfStateArraysSeenBefore.ContainsKey(removeFromFrontOfSuccessorQueue.ArrayStateInt)))
                        {
                            // do nothing
                        }
                        else
                        {
                            stack.Push(removeFromFrontOfSuccessorQueue);
                            dictOfNodesOnStack.Add(removeFromFrontOfSuccessorQueue.ArrayStateInt, removeFromFrontOfSuccessorQueue.ArrayStateInt);
                            dictOfStateArraysSeenBefore.Add(removeFromFrontOfSuccessorQueue.ArrayStateInt, removeFromFrontOfSuccessorQueue.ArrayStateInt);
                        }

                        SizeOfStack = stack.Count();
                        if (MaxOfStack < SizeOfStack)
                        {
                            MaxOfStack = SizeOfStack;
                        }
                    }
                }

                if (IteritiveDeepeningSolve == true)
                {
                    Console.WriteLine();
                    Console.WriteLine("out of loop");
                    Console.WriteLine();


                    Console.WriteLine();
                    Console.WriteLine("Here were the successful moves to solve the puzzle:");
                    Console.WriteLine();

                    StateNode.PrettyPrintPathToSolvePuzzle(GoalStateNode);

                    Console.WriteLine();
                    Console.WriteLine("Time = " + Time);
                    Console.WriteLine();
                    Console.WriteLine("MaxOfStack " + MaxOfStack);
                    Console.WriteLine();


                    break;
                }


                //Console.ReadLine();
            }
            Console.WriteLine();
            Console.WriteLine("IterativeDeepening Solved at this level : " + GoalStateNode.LevelDownInTree);
            Console.WriteLine();
            Console.ReadLine();
        }