コード例 #1
0
        public static StateNode CreateNode(StateNode node)
        {
            NodeManager nManager = NodeManager.getInstance();

            // call base function
            return(nManager.baseCreateNode(node));
        }
コード例 #2
0
 public StateNode(int[] array)
 {
     this.StateArray2 = array;
     this.pUp         = null;
     this.pDown       = null;
     this.pRight      = null;
     this.pLeft       = null;
     this.pParent     = null;
 }
コード例 #3
0
 public StateNode()
 {
     this.StateArray2 = new int[] { 1, 2, 3, 8, 9, 4, 7, 6, 5 };
     this.pUp         = null;
     this.pDown       = null;
     this.pRight      = null;
     this.pLeft       = null;
     this.pParent     = null;
 }
コード例 #4
0
 public StateNode()
 {
     this.StateArray2   = new int[] { 1, 2, 3, 8, 9, 4, 7, 6, 5 };
     this.pUp           = null;
     this.pDown         = null;
     this.pRight        = null;
     this.pLeft         = null;
     this.pParent       = null;
     this.ArrayStateInt = HashConvertToInt(this.StateArray2);
 }
コード例 #5
0
 public StateNode(int[] array)
 {
     this.StateArray2   = array;
     this.pUp           = null;
     this.pDown         = null;
     this.pRight        = null;
     this.pLeft         = null;
     this.pParent       = null;
     this.ArrayStateInt = HashConvertToInt(this.StateArray2);
 }
コード例 #6
0
        public static void Add(StateNode node)
        {
            // make sure not null
            Debug.Assert(node != null);

            // get Singleton instance
            NodeManager nManager = NodeManager.getInstance();

            // call base class function
        }
コード例 #7
0
 // Copy Constructor for Deep Copy
 public StateNode(StateNode otherNode)
 {
     this.StateArray2 = new int[] { 9, 9, 9, 9, 9, 9, 9, 9, 9 };
     this.StateArray2 = otherNode.StateArray2;
     this.pUp         = null;
     this.pDown       = null;
     this.pRight      = null;
     this.pLeft       = null;
     this.pParent     = null;
 }
コード例 #8
0
 // Copy Constructor for Deep Copy
 public StateNode(StateNode otherNode)
 {
     this.StateArray2   = new int[] { 9, 9, 9, 9, 9, 9, 9, 9, 9 };
     this.StateArray2   = otherNode.StateArray2;
     this.pUp           = null;
     this.pDown         = null;
     this.pRight        = null;
     this.pLeft         = null;
     this.pParent       = null;
     this.ArrayStateInt = HashConvertToInt(this.StateArray2);
 }
コード例 #9
0
        public static int[] SwapNums4(StateNode node, int position1, int position2)
        {
            StateNode newNode = new StateNode(node);

            int[] TempArray = newNode.StateArray2;
            int   temp      = TempArray[position1];

            TempArray[position1] = TempArray[position2];
            TempArray[position2] = temp;
            return(TempArray);
        }
コード例 #10
0
        public static void PrettyPrintPathToSolvePuzzle(StateNode node)
        {
            int TotalCostOfAllMoves = 0;
            // create a stack to put the nodes on. Later we will pop them off to get the order of moves
            // to solve the puzzle
            Stack <StateNode> stackOfNodes = new Stack <StateNode>();

            //StateNode nodePointer = new StateNode();
            StateNode nodePointer  = null;
            StateNode nodePointer2 = null;

            nodePointer  = node;
            nodePointer2 = node.pParent;
            while (nodePointer != null)
            {
                stackOfNodes.Push(nodePointer);

                nodePointer = nodePointer.pParent;
            }
            int LengthOfSolution = stackOfNodes.Count();

            foreach (StateNode sn in stackOfNodes)
            {
                foreach (var item in sn.StateArray2)
                {
                    Console.Write(item.ToString() + " ");
                }
                Console.WriteLine();
                Console.WriteLine("CostOfMove = " + sn.CostOfMove);
                TotalCostOfAllMoves = TotalCostOfAllMoves + sn.CostOfMove;
                Console.WriteLine("TotalCostOfAllMoves = " + TotalCostOfAllMoves);
                Console.WriteLine();
                Console.WriteLine("Direction of Move = " + sn.direction);
                Console.WriteLine();
                Console.WriteLine("Length =  " + (LengthOfSolution - 1));
                Console.WriteLine();
                Console.WriteLine("ArrayStateInt " + sn.ArrayStateInt);
                Console.WriteLine();
            }


            Console.WriteLine("\nGoalStateNode\n");
            foreach (var item in node.StateArray2)
            {
                Console.Write(item.ToString() + " ");
            }
        }
コード例 #11
0
        public static int NumTilesOutOfPlace(StateNode node)
        {
            int NumOutOfPlace = 0;


            if (node.StateArray2[0] != 1)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[1] != 2)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[2] != 3)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[5] != 4)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[8] != 5)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[7] != 6)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[6] != 7)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }
            if (node.StateArray2[3] != 8)
            {
                NumOutOfPlace = NumOutOfPlace + 1;
            }

            return(NumOutOfPlace);
        }
コード例 #12
0
        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();
        }
コード例 #13
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();
        }
コード例 #14
0
        public StateNode baseCreateNode(StateNode InNode)
        {
            StateNode sNode = new StateNode(InNode.StateArray2);

            return(sNode);
        }
コード例 #15
0
        public static Queue <StateNode> Successor(StateNode sNode)
        //public static void Successor(StateNode sNode)
        {
            // determine where the zero is in the array. This will allow us to determine all the next possible moves.
            // Then we can determine all the next possible states the puzzle can be in.


            for (int i = 0; i < 9; i++)
            {
                if (sNode.StateArray2[i] == 0)
                {
                    sNode.zeroPosition = i;
                }
            }
            Console.WriteLine("zeroPosition = " + sNode.zeroPosition);
            Console.WriteLine();

            // Make 4 Nodes
            //StateNode sNodeLeft;
            //sNodeLeft = NodeManager.CreateNode(sNode);
            StateNode sNodeLeft  = new StateNode(sNode);
            StateNode sNodeRight = new StateNode(sNode);
            StateNode sNodeUp    = new StateNode(sNode);
            StateNode sNodeDown  = new StateNode(sNode);

            // point the parent pointer of each node to the sNode
            sNodeLeft.pParent  = sNode;
            sNodeUp.pParent    = sNode;
            sNodeDown.pParent  = sNode;
            sNodeRight.pParent = sNode;
            // point the pointers of the sNode to each of the nodes just created
            sNode.pLeft  = sNodeLeft;
            sNode.pRight = sNodeRight;
            sNode.pUp    = sNodeUp;
            sNode.pDown  = sNodeDown;
            //

            //Now determine all the next possible states the puzzle can be in
            // CREATE A LIST WITH ALL THE NEXT STATES
            Queue <StateNode> qNodes = new Queue <StateNode>();


            switch (sNode.zeroPosition)
            {
            case 0:
                // move left, need to create a node
                // Console.WriteLine("Case0\n\n");
                sNodeLeft.StateArray2          = SwapNums3(sNodeLeft.StateArray2, 1, 0);
                sNodeLeft.CostOfMove           = sNodeLeft.StateArray2[0];
                sNodeLeft.direction            = StateNode.DirectionOfMove.Left;
                sNodeLeft.ArrayStateInt        = HashConvertToInt(sNodeLeft.StateArray2);
                sNodeLeft.CostOfMovesFromStart = sNodeLeft.CostOfMove + sNode.CostOfMovesFromStart;
                // move up

                sNodeUp.StateArray2          = SwapNums3(sNodeUp.StateArray2, 3, 0);
                sNodeUp.CostOfMove           = sNodeUp.StateArray2[0];
                sNodeUp.direction            = StateNode.DirectionOfMove.Up;
                sNodeUp.ArrayStateInt        = HashConvertToInt(sNodeUp.StateArray2);
                sNodeUp.CostOfMovesFromStart = sNodeUp.CostOfMove + sNode.CostOfMovesFromStart;
                // sNodeUp.StateArray2 = SwapNums4(sNodeUp, 3, 0);
                sNodeRight = null;
                sNodeDown  = null;
                break;

            case 1:
                // Console.WriteLine("Case1\n\n");
                // move right
                sNodeRight.StateArray2          = SwapNums3(sNodeRight.StateArray2, 1, 0);
                sNodeRight.CostOfMove           = sNodeRight.StateArray2[1];
                sNodeRight.direction            = StateNode.DirectionOfMove.Right;
                sNodeRight.ArrayStateInt        = HashConvertToInt(sNodeRight.StateArray2);
                sNodeRight.CostOfMovesFromStart = sNodeRight.CostOfMove + sNode.CostOfMovesFromStart;
                // move left
                sNodeLeft.StateArray2          = SwapNums3(sNodeLeft.StateArray2, 2, 1);
                sNodeLeft.CostOfMove           = sNodeLeft.StateArray2[1];
                sNodeLeft.direction            = StateNode.DirectionOfMove.Left;
                sNodeLeft.ArrayStateInt        = HashConvertToInt(sNodeLeft.StateArray2);
                sNodeLeft.CostOfMovesFromStart = sNodeLeft.CostOfMove + sNode.CostOfMovesFromStart;
                // move up
                sNodeUp.StateArray2          = SwapNums3(sNodeUp.StateArray2, 1, 4);
                sNodeUp.CostOfMove           = sNodeUp.StateArray2[1];
                sNodeUp.direction            = StateNode.DirectionOfMove.Up;
                sNodeUp.ArrayStateInt        = HashConvertToInt(sNodeUp.StateArray2);
                sNodeUp.CostOfMovesFromStart = sNodeUp.CostOfMove + sNode.CostOfMovesFromStart;
                // delete down
                sNodeDown = null;
                break;

            case 2:
                // Console.WriteLine("Case2\n\n");
                // move right
                sNodeRight.StateArray2          = SwapNums3(sNodeRight.StateArray2, 1, 2);
                sNodeRight.CostOfMove           = sNodeRight.StateArray2[2];
                sNodeRight.direction            = StateNode.DirectionOfMove.Right;
                sNodeRight.ArrayStateInt        = HashConvertToInt(sNodeRight.StateArray2);
                sNodeRight.CostOfMovesFromStart = sNodeRight.CostOfMove + sNode.CostOfMovesFromStart;
                // move up
                sNodeUp.StateArray2          = SwapNums3(sNodeUp.StateArray2, 2, 5);
                sNodeUp.CostOfMove           = sNodeUp.StateArray2[2];
                sNodeUp.direction            = StateNode.DirectionOfMove.Up;
                sNodeUp.ArrayStateInt        = HashConvertToInt(sNodeUp.StateArray2);
                sNodeUp.CostOfMovesFromStart = sNodeUp.CostOfMove + sNode.CostOfMovesFromStart;
                // delete down and left
                sNodeDown = null;
                sNodeLeft = null;
                break;

            case 3:
                //  Console.WriteLine("Case3\n\n");
                // move down
                sNodeDown.StateArray2          = SwapNums3(sNodeDown.StateArray2, 3, 0);
                sNodeDown.CostOfMove           = sNodeDown.StateArray2[3];
                sNodeDown.direction            = StateNode.DirectionOfMove.Down;
                sNodeDown.ArrayStateInt        = HashConvertToInt(sNodeDown.StateArray2);
                sNodeDown.CostOfMovesFromStart = sNodeDown.CostOfMove + sNode.CostOfMovesFromStart;
                // move left
                sNodeLeft.StateArray2          = SwapNums3(sNodeLeft.StateArray2, 3, 4);
                sNodeLeft.CostOfMove           = sNodeLeft.StateArray2[3];
                sNodeLeft.direction            = StateNode.DirectionOfMove.Left;
                sNodeLeft.ArrayStateInt        = HashConvertToInt(sNodeLeft.StateArray2);
                sNodeLeft.CostOfMovesFromStart = sNodeLeft.CostOfMove + sNode.CostOfMovesFromStart;
                // move up
                sNodeUp.StateArray2          = SwapNums3(sNodeUp.StateArray2, 3, 6);
                sNodeUp.CostOfMove           = sNodeUp.StateArray2[3];
                sNodeUp.direction            = StateNode.DirectionOfMove.Up;
                sNodeUp.ArrayStateInt        = HashConvertToInt(sNodeUp.StateArray2);
                sNodeUp.CostOfMovesFromStart = sNodeUp.CostOfMove + sNode.CostOfMovesFromStart;

                sNodeRight = null;
                break;

            case 4:
                //  Console.WriteLine("Case4\n\n");
                // move down
                sNodeDown.StateArray2          = SwapNums3(sNodeDown.StateArray2, 4, 1);
                sNodeDown.CostOfMove           = sNodeDown.StateArray2[4];
                sNodeDown.direction            = StateNode.DirectionOfMove.Down;
                sNodeDown.ArrayStateInt        = HashConvertToInt(sNodeDown.StateArray2);
                sNodeDown.CostOfMovesFromStart = sNodeDown.CostOfMove + sNode.CostOfMovesFromStart;
                // move right
                sNodeRight.StateArray2          = SwapNums3(sNodeRight.StateArray2, 4, 3);
                sNodeRight.CostOfMove           = sNodeRight.StateArray2[4];
                sNodeRight.direction            = StateNode.DirectionOfMove.Right;
                sNodeRight.ArrayStateInt        = HashConvertToInt(sNodeRight.StateArray2);
                sNodeRight.CostOfMovesFromStart = sNodeRight.CostOfMove + sNode.CostOfMovesFromStart;
                // move left
                sNodeLeft.StateArray2          = SwapNums3(sNodeLeft.StateArray2, 4, 5);
                sNodeLeft.CostOfMove           = sNodeLeft.StateArray2[4];
                sNodeLeft.direction            = StateNode.DirectionOfMove.Left;
                sNodeLeft.ArrayStateInt        = HashConvertToInt(sNodeLeft.StateArray2);
                sNodeLeft.CostOfMovesFromStart = sNodeLeft.CostOfMove + sNode.CostOfMovesFromStart;
                // move up
                sNodeUp.StateArray2          = SwapNums3(sNodeUp.StateArray2, 4, 7);
                sNodeUp.CostOfMove           = sNodeUp.StateArray2[4];
                sNodeUp.direction            = StateNode.DirectionOfMove.Up;
                sNodeUp.ArrayStateInt        = HashConvertToInt(sNodeUp.StateArray2);
                sNodeUp.CostOfMovesFromStart = sNodeUp.CostOfMove + sNode.CostOfMovesFromStart;
                break;

            case 5:
                //  Console.WriteLine("Case5\n\n");
                // move down
                sNodeDown.StateArray2          = SwapNums3(sNodeDown.StateArray2, 5, 2);
                sNodeDown.CostOfMove           = sNodeDown.StateArray2[5];
                sNodeDown.direction            = StateNode.DirectionOfMove.Down;
                sNodeDown.ArrayStateInt        = HashConvertToInt(sNodeDown.StateArray2);
                sNodeDown.CostOfMovesFromStart = sNodeDown.CostOfMove + sNode.CostOfMovesFromStart;
                // move up
                sNodeUp.StateArray2          = SwapNums3(sNodeUp.StateArray2, 5, 8);
                sNodeUp.CostOfMove           = sNodeUp.StateArray2[5];
                sNodeUp.direction            = StateNode.DirectionOfMove.Up;
                sNodeUp.ArrayStateInt        = HashConvertToInt(sNodeUp.StateArray2);
                sNodeUp.CostOfMovesFromStart = sNodeUp.CostOfMove + sNode.CostOfMovesFromStart;
                // move right
                sNodeRight.StateArray2          = SwapNums3(sNodeRight.StateArray2, 5, 4);
                sNodeRight.CostOfMove           = sNodeRight.StateArray2[5];
                sNodeRight.direction            = StateNode.DirectionOfMove.Right;
                sNodeRight.ArrayStateInt        = HashConvertToInt(sNodeRight.StateArray2);
                sNodeRight.CostOfMovesFromStart = sNodeRight.CostOfMove + sNode.CostOfMovesFromStart;

                sNodeLeft = null;
                break;

            case 6:
                //  Console.WriteLine("Case6\n\n");
                // move down
                sNodeDown.StateArray2          = SwapNums3(sNodeDown.StateArray2, 6, 3);
                sNodeDown.CostOfMove           = sNodeDown.StateArray2[6];
                sNodeDown.direction            = StateNode.DirectionOfMove.Down;
                sNodeDown.ArrayStateInt        = HashConvertToInt(sNodeDown.StateArray2);
                sNodeDown.CostOfMovesFromStart = sNodeDown.CostOfMove + sNode.CostOfMovesFromStart;
                // move left
                sNodeLeft.StateArray2          = SwapNums3(sNodeLeft.StateArray2, 6, 7);
                sNodeLeft.CostOfMove           = sNodeLeft.StateArray2[6];
                sNodeLeft.direction            = StateNode.DirectionOfMove.Left;
                sNodeLeft.ArrayStateInt        = HashConvertToInt(sNodeLeft.StateArray2);
                sNodeLeft.CostOfMovesFromStart = sNodeLeft.CostOfMove + sNode.CostOfMovesFromStart;

                sNodeUp    = null;
                sNodeRight = null;
                break;

            case 7:
                //  Console.WriteLine("Case7\n\n");
                // move right
                sNodeRight.StateArray2          = SwapNums3(sNodeRight.StateArray2, 7, 6);
                sNodeRight.CostOfMove           = sNodeRight.StateArray2[7];
                sNodeRight.direction            = StateNode.DirectionOfMove.Right;
                sNodeRight.ArrayStateInt        = HashConvertToInt(sNodeRight.StateArray2);
                sNodeRight.CostOfMovesFromStart = sNodeRight.CostOfMove + sNode.CostOfMovesFromStart;
                // move left
                sNodeLeft.StateArray2          = SwapNums3(sNodeLeft.StateArray2, 7, 8);
                sNodeLeft.CostOfMove           = sNodeLeft.StateArray2[7];
                sNodeLeft.direction            = StateNode.DirectionOfMove.Left;
                sNodeLeft.ArrayStateInt        = HashConvertToInt(sNodeLeft.StateArray2);
                sNodeLeft.CostOfMovesFromStart = sNodeLeft.CostOfMove + sNode.CostOfMovesFromStart;
                // move down
                sNodeDown.StateArray2          = SwapNums3(sNodeDown.StateArray2, 7, 4);
                sNodeDown.CostOfMove           = sNodeDown.StateArray2[7];
                sNodeDown.direction            = StateNode.DirectionOfMove.Down;
                sNodeDown.ArrayStateInt        = HashConvertToInt(sNodeDown.StateArray2);
                sNodeDown.CostOfMovesFromStart = sNodeDown.CostOfMove + sNode.CostOfMovesFromStart;

                sNodeUp = null;
                break;

            case 8:
                // Console.WriteLine("Case8\n\n");
                // move right
                sNodeRight.StateArray2          = SwapNums3(sNodeRight.StateArray2, 8, 7);
                sNodeRight.CostOfMove           = sNodeRight.StateArray2[8];
                sNodeRight.direction            = StateNode.DirectionOfMove.Right;
                sNodeRight.ArrayStateInt        = HashConvertToInt(sNodeRight.StateArray2);
                sNodeRight.CostOfMovesFromStart = sNodeRight.CostOfMove + sNode.CostOfMovesFromStart;
                // move down
                sNodeDown.StateArray2          = SwapNums3(sNodeDown.StateArray2, 8, 5);
                sNodeDown.CostOfMove           = sNodeDown.StateArray2[8];
                sNodeDown.direction            = StateNode.DirectionOfMove.Down;
                sNodeDown.ArrayStateInt        = HashConvertToInt(sNodeDown.StateArray2);
                sNodeDown.CostOfMovesFromStart = sNodeDown.CostOfMove + sNode.CostOfMovesFromStart;
                sNodeUp   = null;
                sNodeLeft = null;
                break;
            }



            //Now determine all the next possible states the puzzle can be in
            //  ALL THE NEXT STATES in a queue and send it to main loop
            if (sNodeDown != null)
            {
                qNodes.Enqueue(sNodeDown);
                //Console.WriteLine("\nStateArrayDown\n");
                //foreach (var item in sNodeDown.StateArray2)
                //{


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

                //}
                //Console.WriteLine("CostOfMove = " + sNodeDown.CostOfMove);
            }
            if (sNodeUp != null)
            {
                qNodes.Enqueue(sNodeUp);
                //Console.WriteLine("\nStateArrayUp\n");
                //foreach (var item in sNodeUp.StateArray2)
                //{


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

                //}
                //Console.WriteLine("CostOfMove = " + sNodeUp.CostOfMove);
            }
            if (sNodeLeft != null)
            {
                qNodes.Enqueue(sNodeLeft);
                //Console.WriteLine("\nStateArrayLeft\n");
                //foreach (var item in sNodeLeft.StateArray2)
                //{


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

                //}
                //Console.WriteLine("CostOfMove = " + sNodeLeft.CostOfMove);
            }
            if (sNodeRight != null)
            {
                qNodes.Enqueue(sNodeRight);
                //Console.WriteLine("\nStateArrayRight\n");
                //foreach (var item in sNodeRight.StateArray2)
                //{


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

                //}
                //Console.WriteLine("CostOfMove = " + sNodeRight.CostOfMove);
            }

            // return the queue
            return(qNodes);

            //switch statement for each index number of the array the zero can be in.
        }
コード例 #16
0
        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();
        }
コード例 #17
0
 public void baseAddNode(StateNode node)
 {
 }
コード例 #18
0
 public void baseRemoveNode(StateNode node)
 {
 }