Exemplo n.º 1
0
        private void bt_q3_Click(object sender, EventArgs e)
        {
            //Invoked for Q3
            Constants.newHValues();
            solutionPath = new Queue <int[]>();
            solutionPath.Enqueue(new int[] { Constants.start.X, Constants.start.Y });   //adding the start state to the solutionPath
            Constants.Corners.Add(new int[] { Constants.finish.X, Constants.finish.Y });
            LRTA lrta = new LRTA();
            int  HVal = 0;

            int[] currentPos    = new int[] { Constants.start.X, Constants.start.Y }; //setting the start state as the current positiion
            bool  solutionFound = false;

            //navigating to first corner
            List <int[]> percept = lrta.getPercept(world.array, currentPos[0], currentPos[1]);   //getting the percept
            SimplePriorityQueue <int[]> priorityQueue = new SimplePriorityQueue <int[]>();

            //getting the heuristic values for the percept
            foreach (int[] x in percept)
            {
                HVal = lrta.calculateHeuristicValue(x[0], x[1]);
                try
                {
                    Constants.Hvalues.Add(x, HVal);
                }
                catch { }
                //Goal Test
                if (HVal == 0)
                {
                    solutionPath.Enqueue(x);
                    solutionFound = true;
                    break;
                }
                priorityQueue.Enqueue(x, Constants.Hvalues[x]);
            }
            int[] lastPos = new int[] { Constants.start.X, Constants.start.Y };
            Constants.Hvalues.Add(lastPos, lrta.calculateHeuristicValue(lastPos[0], lastPos[1]));

            if (!solutionFound)
            {
                currentPos = priorityQueue.Dequeue();
                solutionPath.Enqueue(currentPos);
            }
            //now we have last and current so find next and check if it is in a local minima
            while (!solutionFound)
            {
                //finding the percept for the current state
                percept       = lrta.getPercept(world.array, currentPos[0], currentPos[1]);
                priorityQueue = new SimplePriorityQueue <int[]>();
                //finding all the heuristic values
                foreach (int[] x in percept)
                {
                    if (x != currentPos)
                    {
                        HVal = lrta.calculateHeuristicValue(x[0], x[1]);
                        try
                        {
                            Constants.Hvalues.Add(x, HVal);
                        }
                        catch { }
                        //Goal Test
                        if (HVal == 0)
                        {
                            solutionPath.Enqueue(x);
                            solutionFound = true;
                            break;
                        }
                        priorityQueue.Enqueue(x, Constants.Hvalues[x]);
                    }
                }
                //Implementing the 30-70% probability
                if (!solutionFound)
                {
                    Random rand        = new Random();
                    int    probability = rand.Next(10); //random number for probability
                    if (probability > 2)
                    {
                        //Take the correct path and implement it as in Q1
                        int[] nextPos = priorityQueue.Dequeue();
                        if (Constants.Hvalues[currentPos] <= Constants.Hvalues[nextPos] && Constants.Hvalues[currentPos] <= Constants.Hvalues[lastPos])
                        {
                            //stuck in local minima
                            Constants.Hvalues[currentPos] = (Constants.Hvalues[nextPos] < Constants.Hvalues[lastPos] ? Constants.Hvalues[nextPos] : Constants.Hvalues[lastPos]) + 1;
                            int[] tempPos = lastPos;
                            lastPos    = currentPos;
                            currentPos = Constants.Hvalues[nextPos] < Constants.Hvalues[lastPos] ? nextPos : tempPos;   //moving it to the next best value
                        }
                        else
                        {
                            lastPos    = currentPos;
                            currentPos = nextPos;
                        }
                        solutionPath.Enqueue(nextPos);
                    }
                    else
                    {
                        //Robot went to a wrong position
                        //Implementing a Stack to backtrack to the original position
                        Stack <int[]> returnToOriginalPlan = new Stack <int[]>();
                        int[]         actualNextPos        = currentPos;
                        int[]         randomNextPos        = new int[] { 0, 0 };
                        returnToOriginalPlan.Push(currentPos);  //Pushing the current position to the stack
                        do
                        {
                            int times = rand.Next(priorityQueue.Count);
                            for (int i = 0; i < times; i++)     //selecting a random position to go
                            {
                                randomNextPos = priorityQueue.Dequeue();
                            }

                            percept = lrta.getPercept(world.array, randomNextPos[0], randomNextPos[1]); //getting percept of that position to try to get back
                            priorityQueue.Clear();
                            //getting heuristic values
                            foreach (int[] x in percept)
                            {
                                if (x != randomNextPos)
                                {
                                    int Hval = calcHeurToGetBackToOriginalPlan(x, actualNextPos);
                                    priorityQueue.Enqueue(x, Hval);
                                }
                            }

                            probability = rand.Next(10);    //again checking the probability for going back
                            if (probability > 2)
                            {
                                //correct path pop from stack and update the percept
                                int[] a = returnToOriginalPlan.Pop();
                                solutionPath.Enqueue(a);
                                //priority queue update
                                percept = lrta.getPercept(world.array, a[0], a[1]); //getting percept of that position to try to get back
                                priorityQueue.Clear();
                                foreach (int[] x in percept)
                                {
                                    if (x != a)
                                    {
                                        int Hval = calcHeurToGetBackToOriginalPlan(x, actualNextPos);
                                        priorityQueue.Enqueue(x, Hval);
                                    }
                                }
                            }
                            else
                            {
                                //wrong path go to the new wrong positino and pust the current position onto the stack
                                solutionPath.Enqueue(randomNextPos);        //going to that position
                                returnToOriginalPlan.Push(randomNextPos);
                                actualNextPos = randomNextPos;
                            }
                        } while (returnToOriginalPlan.Count != 0);
                    }
                }
            }
            lbl_perf.Text = "Performance : " + (1001 - solutionPath.Count);
            pbCamvas.Invalidate();
        }
Exemplo n.º 2
0
        private void bt_q4_2_Click(object sender, EventArgs e)
        {
            //Implements Q4
            Constants.newHValues();
            solutionPath = new Queue <int[]>();
            solutionPath.Enqueue(new int[] { Constants.start.X, Constants.start.Y });
            Constants.Corners.Add(new int[] { Constants.finish.X, Constants.finish.Y });
            LRTA lrta = new LRTA();
            int  HVal = 0;

            int[] currentPos    = new int[] { Constants.start.X, Constants.start.Y };
            bool  solutionFound = false;

            //navigating to first corner
            List <int[]> percept = lrta.getPercept(world.array, currentPos[0], currentPos[1]);
            //getting the percept and calculating the heuristic values for all tha points in the percept
            SimplePriorityQueue <int[]> priorityQueue = new SimplePriorityQueue <int[]>();

            foreach (int[] x in percept)
            {
                HVal = lrta.calculateHeuristicValue(x[0], x[1]);
                try
                {
                    Constants.Hvalues.Add(x, HVal);
                }
                catch { }
                //goal test
                if (HVal == 0)
                {
                    solutionPath.Enqueue(x);
                    solutionFound = true;
                    break;
                }
                priorityQueue.Enqueue(x, Constants.Hvalues[x]); //pushing the heuristic values in the queue
            }
            int[] lastPos = new int[] { Constants.start.X, Constants.start.Y };
            Constants.Hvalues.Add(lastPos, lrta.calculateHeuristicValue(lastPos[0], lastPos[1]));

            if (!solutionFound)
            {
                //adding the current position in the output
                currentPos = priorityQueue.Dequeue();
                solutionPath.Enqueue(currentPos);
            }
            //now we have last and current so find next and check if it is in a ditch
            while (!solutionFound)
            {
                //getting the percept
                percept = lrta.getPercept(world.array, currentPos[0], currentPos[1]);
                priorityQueue.Clear();
                //calculating heuristic values
                foreach (int[] x in percept)
                {
                    if (x != currentPos)
                    {
                        HVal = lrta.calculateHeuristicValue(x[0], x[1]);
                        try
                        {
                            Constants.Hvalues.Add(x, HVal);
                        }
                        catch { }
                        //goal test
                        if (HVal == 0)
                        {
                            solutionPath.Enqueue(x);
                            solutionFound = true;
                            break;
                        }
                        priorityQueue.Enqueue(x, Constants.Hvalues[x]);
                    }
                }
                if (!solutionFound)
                {
                    //implementing the probability 30-70%
                    Random rand        = new Random();
                    int    probability = rand.Next(10);
                    if (probability > 2)
                    {
                        //Take the correct path as in Q1
                        int[] nextPos = priorityQueue.Dequeue();
                        if (Constants.Hvalues[currentPos] <= Constants.Hvalues[nextPos] && Constants.Hvalues[currentPos] <= Constants.Hvalues[lastPos])
                        {
                            //stuck in local minima
                            Constants.Hvalues[currentPos] = (Constants.Hvalues[nextPos] < Constants.Hvalues[lastPos] ? Constants.Hvalues[nextPos] : Constants.Hvalues[lastPos]) + 1;
                            int[] tempPos = lastPos;
                            lastPos    = currentPos;
                            currentPos = Constants.Hvalues[nextPos] < Constants.Hvalues[lastPos] ? nextPos : tempPos;   //moving it to the next best value
                        }
                        else
                        {
                            //not in local minima
                            lastPos    = currentPos;
                            currentPos = nextPos;
                        }
                        solutionPath.Enqueue(nextPos);
                    }
                    else
                    {
                        //wrong path- find a new path to the goal from this new position
                        lastPos = currentPos;
                        int i = rand.Next(priorityQueue.Count);
                        for (int k = 0; k < i; k++)
                        {
                            priorityQueue.Dequeue();
                        }
                        currentPos = priorityQueue.Dequeue();
                        solutionPath.Enqueue(currentPos);
                    }
                }
            }
            lbl_perf.Text = "Performance : " + (1001 - solutionPath.Count);
            pbCamvas.Invalidate();
        }
Exemplo n.º 3
0
        private void LRTAFindPath()
        {
            //defining the variables
            Constants.newHValues();
            solutionPath = new Queue <int[]>();
            solutionPath.Enqueue(new int[] { Constants.start.X, Constants.start.Y });    //adding start to solution path
            Constants.Corners.Add(new int[] { Constants.finish.X, Constants.finish.Y }); //adding finish state as one of the corners
            LRTA lrta = new LRTA();
            int  HVal = 0;

            int[] currentPos    = new int[] { Constants.start.X, Constants.start.Y };
            bool  solutionFound = false;

            //navigating to first corner
            List <int[]> percept = lrta.getPercept(world.array, currentPos[0], currentPos[1]);
            SimplePriorityQueue <int[]> priorityQueue = new SimplePriorityQueue <int[]>();

            //finding the Heuristic value for all the corners
            foreach (int[] x in percept)
            {
                HVal = lrta.calculateHeuristicValue(x[0], x[1]);
                try
                {
                    Constants.Hvalues.Add(x, HVal);
                }
                catch { }
                //Goal Test
                if (HVal == 0)
                {
                    solutionPath.Enqueue(x);
                    solutionFound = true;
                    break;
                }
                priorityQueue.Enqueue(x, Constants.Hvalues[x]);
            }
            int[] lastPos = new int[] { Constants.start.X, Constants.start.Y };
            Constants.Hvalues.Add(lastPos, lrta.calculateHeuristicValue(lastPos[0], lastPos[1]));
            //Appending the best node to the priority Queue
            if (!solutionFound)
            {
                currentPos = priorityQueue.Dequeue();
                solutionPath.Enqueue(currentPos);
            }
            //now we have last and current so find next and check if it is in a local minima
            while (!solutionFound)
            {
                //Getting the percept for current pos
                percept       = lrta.getPercept(world.array, currentPos[0], currentPos[1]);
                priorityQueue = new SimplePriorityQueue <int[]>();
                foreach (int[] x in percept)
                {
                    //finding Heuristic values for the current percept
                    if (x != currentPos)
                    {
                        HVal = lrta.calculateHeuristicValue(x[0], x[1]);
                        try
                        {
                            Constants.Hvalues.Add(x, HVal);
                        }
                        catch { }
                        //Goal Test
                        if (HVal == 0)
                        {
                            solutionPath.Enqueue(x);
                            solutionFound = true;
                            break;
                        }
                        priorityQueue.Enqueue(x, Constants.Hvalues[x]);
                    }
                }
                if (!solutionFound)
                {
                    int[] nextPos = priorityQueue.Dequeue();
                    if (Constants.Hvalues[currentPos] <= Constants.Hvalues[nextPos] && Constants.Hvalues[currentPos] <= Constants.Hvalues[lastPos])
                    {
                        //stuck in local minima hence update the herustic value of that position
                        Constants.Hvalues[currentPos] = (Constants.Hvalues[nextPos] < Constants.Hvalues[lastPos] ?
                                                         Constants.Hvalues[nextPos] : Constants.Hvalues[lastPos]) + 1;
                        int[] tempPos = lastPos;
                        lastPos = currentPos;
                        //moving it to the next best value
                        currentPos = Constants.Hvalues[nextPos] < Constants.Hvalues[lastPos] ? nextPos : tempPos;
                    }
                    else
                    {
                        //best position found which is not a local minima
                        lastPos    = currentPos;
                        currentPos = nextPos;
                    }
                    //adding the path to the solution
                    solutionPath.Enqueue(nextPos);
                }
            }
        }