Esempio n. 1
0
        private void RandomWorld()
        {
            int tmpDim = rand.Next(2, 6);

            rows    = tmpDim;
            columns = tmpDim;
            columns = rows; rows = columns;

            DimValue.Text = rows.ToString();
            PValue.Text   = p.ToString();

            world = new GridWorld(tmpDim, tmpDim, mainFrame.Width, mainFrame.Height, p);

            int[] coord = world.GetGoalCoord();
            GoalSpaceValue.Text = ((coord[0] * rows) + coord[1]).ToString();
            world.SetGoal(coord[0], coord[1]);

            coord = world.GetStartCoord();
            StartSpaceValue.Text = ((coord[0] * rows) + coord[1]).ToString();
            world.SetStart(coord[0], coord[1]);

            mainFrame.Child           = world;
            TimeStepsButton.IsEnabled = false;
            CostLabel.Content         = "Total Cost: ";
            SolvableLabel.Content     = "";
            PathText.Text             = "";

            timeStep = 0;
        }
Esempio n. 2
0
 public Agent(GridWorld world, Heuristic heuristic, bool repeated)
 {
     cost             = 0;
     this.repeated    = repeated;
     timeStepStrings  = new string[world.Rows * world.Rows];
     timeSteppedPaths = new List <GridWorldSpace> [world.Rows * world.Rows];
     path_string      = "";
     this.world       = world;
     this.heuristic   = heuristic;
     rand             = new Random();
     TT = new TernaryTree();
 }
Esempio n. 3
0
        // Sets the row value and resets the GridWorld
        private void DimValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!Int32.TryParse(DimValue.Text, out rows))
            {
                rows = defaultRowSize;
            }
            columns = rows;

            world                     = new GridWorld(rows, columns, mainFrame.Width, mainFrame.Height);
            mainFrame.Child           = world;
            TimeStepsButton.IsEnabled = false;
            PathText.Text             = "";
            timeStep                  = 0;
        }
Esempio n. 4
0
 // If you want to run the same map repeatedly without having to put it in every time,
 // put the index numbers of the nontraversible spaces in the map array and set the
 // goal and start spaces here.
 // ex. you have a 5x5 with spaces A1 and B4 nontraversible, put { 0, 8 } in the map array
 public void InitialWorld()
 {
     //int[] map = { 2, 3, 5};
     int[] map = { };
     if (map.Length == 0)
     {
         RandomWorld();
     }
     else
     {
         rows    = 3;
         columns = 3;
         world   = new GridWorld(rows, columns, mainFrame.Width, mainFrame.Height, map);
         world.SetStart(0, 0);
         world.SetGoal(2, 2);
     }
 }
Esempio n. 5
0
        // Run and output the search algorithm when button is clicked
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // TODO: need to run checks to make sure everything is filled in

            world.Refresh();

            Heuristic heuristic = Heuristic.Manhattan;

            switch (HeuristicValue.Text)
            {
            case "Euclidean":
                heuristic = Heuristic.Euclidean;
                break;

            case "Manhattan":
                heuristic = Heuristic.Manhattan;
                break;

            case "Chebyshev":
                heuristic = Heuristic.Chebyshev;
                break;
            }

            agent = new Agent(world, heuristic, repeated);
            world = agent.Traverse();

            if (agent.Solvable)
            {
                SolvableLabel.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                SolvableLabel.Content    = "SOLVABLE";
            }
            else
            {
                SolvableLabel.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                SolvableLabel.Content    = "UNSOLVABLE";
            }

            CostLabel.Content = "Total Cost: " + agent.Cost.ToString();

            PathText.Text             = "Path: " + agent.Path;
            mainFrame.Child           = world;
            TimeStepsButton.IsEnabled = true;
            timeStep = 0;
        }
Esempio n. 6
0
        private List <GridWorldSpace> RepeatedAStar()
        {
            int timeSteps = 0;

            GridWorld currWorld = new GridWorld(world.Rows, world.Rows,
                                                world.Width, world.Height);

            currWorld.SetStart(0, 0);
            currWorld.SetGoal(world.Rows - 1, world.Rows - 1);

            int  size     = (world.Rows * world.Rows) + 1;
            Node currNode = world.GetSpaceByCoord(world.GetStartCoord()).TreePointer;
            Node goalNode = world.GetSpaceByCoord(world.GetGoalCoord()).TreePointer;
            List <GridWorldSpace> repeatedPath = new List <GridWorldSpace>();
            List <GridWorldSpace> tmpPath      = new List <GridWorldSpace>();
            CoordHeap             openList     = new CoordHeap();

            while (currNode.key != goalNode.key)
            {
                repeatedPath.Add(currNode.key);
                currNode.key.G = 0;
                currNode.key.S = timeSteps;
                goalNode.key.G = size;
                goalNode.key.S = timeSteps;
                currNode.key.F = currNode.key.G + currNode.key.H;

                openList.Insert(currNode.key);
                tmpPath = GetPath(currWorld);

                currNode = openList.GetLeastF().TreePointer;
                timeSteps++;
            }

            cost = timeSteps;
            return(repeatedPath);
        }
Esempio n. 7
0
 public void SetGridWorld(GridWorld world)
 {
     this.world = world;
 }
Esempio n. 8
0
        private List <GridWorldSpace> GetPath(GridWorld currWorld)
        {
            int timeSteps = 0;

            CoordHeap             openList   = new CoordHeap();
            List <GridWorldSpace> closedList = new List <GridWorldSpace>();
            List <Node>           backTrack  = new List <Node>();
            GridWorldSpace        currSpace  = currWorld.GetSpaceByCoord(currWorld.GetStartCoord());
            GridWorldSpace        goalSpace  = currWorld.GetSpaceByCoord(currWorld.GetGoalCoord());

            Node starting = new Node(currSpace);

            backTrack.Add(starting);
            openList.Insert(currSpace);
            ComputeValues(currSpace, timeSteps);

            while (!openList.isEmpty() && currSpace != goalSpace)
            {
                CardinalDir[] cardinals = { CardinalDir.UP,
                                            CardinalDir.DOWN,
                                            CardinalDir.LEFT,
                                            CardinalDir.RIGHT };

                int isBlocked = 0;

                for (int i = 0; i < cardinals.Length; i++)
                {
                    if (currWorld.IsTraversible(currSpace, cardinals[i]))
                    {
                        GridWorldSpace tmpSpace = GetAdjacentSpace(currSpace, cardinals[i]);

                        bool tmpbool = tmpSpace.isTraversible();

                        leastSpace = backTrack.First();
                        for (int it = 0; it < backTrack.Count; it++)
                        {
                            if (leastSpace.key.F > backTrack[it].key.F)
                            {
                                leastSpace.key = backTrack[it].key;
                            }
                        }

                        Node parent1 = leastSpace;
                        ComputeValues(tmpSpace, timeSteps);
                        if (tmpbool && !openList.Contains(tmpSpace) && !closedList.Contains(tmpSpace))
                        {
                            ComputeValues(tmpSpace, timeSteps);
                            openList.Insert(tmpSpace);

                            //Initialized a Node Object for tmpSpace
                            child = new Node(tmpSpace);

                            TT.Insert(parent1, child);
                            backTrack.Add(child);

                            if (child.key == goalSpace)
                            {
                                break;
                            }
                        }

                        if (tmpbool && openList.Contains(tmpSpace) && (tmpSpace.F < openList.SpaceF(tmpSpace.Id)))
                        {
                            child = new Node(tmpSpace);

                            openList.RemoveSpecific(tmpSpace.Id); //Remove anything with this ID
                            openList.Insert(tmpSpace);

                            for (int x = 0; x < backTrack.Count; x++)
                            {
                                if (tmpSpace.Id == backTrack[x].key.Id)
                                {
                                    backTrack.Remove(child); //Remove anything with this ID
                                }
                            }

                            backTrack.Add(child);
                            TT.Insert(parent1, child);

                            if (child.key == goalSpace)
                            {
                                break;
                            }
                        }

                        else
                        {
                            isBlocked++;
                        }
                    }
                    else
                    {
                        isBlocked++;
                    }

                    if (isBlocked == 4)
                    {
                        currSpace.Blocked = true;
                    }
                }

                openList.Remove(currSpace);
                closedList.Add(currSpace);
                if (!openList.isEmpty())
                {
                    currSpace = openList.GetLeastF();
                }
                backTrack.Remove(leastSpace);

                timeSteppedPaths[timeSteps] = new List <GridWorldSpace>();
                foreach (GridWorldSpace tmp in closedList)
                {
                    timeSteppedPaths[timeSteps].Add(tmp);
                }

                timeStepStrings[timeSteps] = "";
                Debug.WriteLine("");
                Debug.WriteLine("Timestep: " + timeSteps);
                Debug.WriteLine("Current Node: " + currSpace.Id);
                Debug.WriteLine("Closed List:");
                foreach (GridWorldSpace tmp in closedList)
                {
                    Debug.Write(tmp.Id);
                    timeStepStrings[timeSteps] += tmp.Id;
                    timeStepStrings[timeSteps] += "->";
                    Debug.Write("->");
                }
                Debug.WriteLine("");
                openList.PrintHeap();

                timeSteps++;
            }

            if (currSpace.isGoal())
            {
                timeStepStrings[timeSteps] += timeStepStrings[timeSteps - 1];
                timeStepStrings[timeSteps] += currSpace.Id;

                timeSteppedPaths[timeSteps] = new List <GridWorldSpace>();
                foreach (GridWorldSpace tmp in closedList)
                {
                    timeSteppedPaths[timeSteps].Add(tmp);
                }

                solvable = true;
            }
            else
            {
                timeSteps = timeSteps - 1;
                solvable  = false;
            }

            List <GridWorldSpace> finalPath = new List <GridWorldSpace>();

            //track parent pointers all the way up to Start
            Node curr = child; //Go to Goal, where child = goal

            while (curr != null)
            {
                finalPath.Add(curr.key);
                curr = curr.parent;
            }

            finalPath.Reverse();
            Debug.WriteLine("Final list: ");
            foreach (GridWorldSpace tmp in finalPath)
            {
                Debug.Write(tmp.Id);
                path_string += tmp.Id;
                if (tmp.isGoal())
                {
                    Debug.Write("(GOAL)");
                    path_string += "(GOAL)";
                }
                else
                {
                    Debug.Write("->");
                    path_string += "->";
                }
            }

            cost = timeSteps;
            return(finalPath);
        }