public override void Initialise(Percepts percepts)
        {
            Name = "Greedy-Best First Agent";

            base.Initialise(percepts);

            _frontierList.Add(AgentData.RootNode);
        }
Example #2
0
        public virtual void Initialise(Percepts percepts)
        {
            _agentData = new AgentData();
            _state     = AgentState.Searching;

            AgentData.RootNode          = new Node <TileType>(TileType.Start, null, percepts.AgentPos);
            AgentData.RootNode.IsOnPath = true;
            AgentData.SearchedPos.Add(AgentData.RootNode.Pos);
        }
        public override int HeuristicFunction(Vector2i Pos, Percepts percepts)
        {
            int manhattanDistance = 0;

            // Manhanttan Distance to the FIRST goal

            manhattanDistance = Math.Abs(percepts.GoalPositions[0].X - Pos.X) +
                                Math.Abs(percepts.GoalPositions[0].Y - Pos.Y);

            return(manhattanDistance);
        }
        public override int CostFunction(Vector2i Pos, Percepts percepts)
        {
            //Dijkstra's Algorithm is to determine the cost to get up to this node, using a distance function.
            // In this case, it'll be the manhattan distance to this node location

            int manhattanDistance = 0;

            manhattanDistance = Math.Abs(Pos.X - AgentData.RootNode.Pos.X) +
                                Math.Abs(Pos.Y - Pos.X - AgentData.RootNode.Pos.Y);

            return(manhattanDistance);
        }
Example #5
0
 protected virtual bool WithinMap(Vector2i pos, Percepts percepts)
 {
     if ((0 <= pos.X) && (pos.X < percepts.MapMatrix.GetLength(0)))
     {
         if ((0 <= pos.Y) && (pos.Y < percepts.MapMatrix.GetLength(1)))
         {
             if (percepts.MapMatrix[pos.X, pos.Y] != TileType.Wall)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        public override int CostFunction(Vector2i Pos, Percepts percepts)
        {
            // Get the current latest node
            // GBFS should only expand one node at a time...
            Node <TileType> currentNode = AgentData.SearchedNodes[AgentData.SearchedNodes.Count - 1];

            // Determine layers of parents
            //  This gives us the "cost" to get up to this point
            int             nodeDistance = 0;
            Node <TileType> parentNode   = currentNode.Parent;

            while (parentNode != null)
            {
                nodeDistance++;
                parentNode = parentNode.Parent;
            }

            return(nodeDistance);
        }
        public override AgentActions next(Percepts percepts)
        {
            AgentDelay--;
            if (AgentDelay < 0)
            {
                AgentDelay = AgentData.AgentDelay;
            }
            else
            {
                return(AgentActions.Search);
            }

            UpdateInternalHeap(_frontierList);

            switch (State)
            {
            case AgentState.Searching:

                if (_frontierList.Count > 0)
                {
                    Node <TileType> currentNode = _frontierList[_frontierList.Count - 1];
                    int             lowestCost  = 5000;

                    // This is reversed such that it provides a DFS under all equal circumstances
                    //  this implies in a worst-case scenario, A* behaves as  GBFS
                    for (int i = _frontierList.Count - 1; i >= 0; i--)
                    {
                        Node <TileType> listNode = _frontierList[i];
                        if (listNode.Cost < lowestCost)
                        {
                            lowestCost  = listNode.Cost;
                            currentNode = listNode;
                        }
                    }

                    _frontierList.Remove(currentNode);

                    if (IsGoalNode(currentNode))
                    {
                        State = AgentState.Moving;

                        AgentData.NodePath          = DetermineAgentPath(AgentData.RootNode, currentNode);
                        AgentData.DeterminedMoveSet = DetermineMoveSet();
                        State = AgentState.Moving;
                        AgentData.PosToSearch.Clear();
                        break;
                    }

                    AgentData.PosToSearch = new List <Vector2i>();
                    AgentData.PosToSearch.Add(currentNode.Pos);
                    AgentData.SearchedNodes.Add(currentNode);


                    List <Node <TileType> > bestNodes = new List <Node <TileType> >();

                    List <Node <TileType> > surroundingNodes = SearchSurroundingNodes(currentNode, percepts);

                    for (int i = 0; i < surroundingNodes.Count; i++)
                    {
                        Node <TileType> subnode = surroundingNodes[i];

                        if (!AgentData.SearchedPos.Contains(subnode.Pos))
                        {
                            _frontierList.Add(subnode);

                            AgentData.SearchedPos.Add(subnode.Pos);
                            AgentData.SearchedNodes.Add(subnode);
                        }
                    }
                }
                else
                {
                    State = AgentState.Lost;
                    return(AgentActions.Lost);
                }

                return(AgentActions.Search);


            case AgentState.Moving:

                AgentData.Path.Add(percepts.AgentPos);

                if (AgentData.DeterminedMoveSet.Count > 0)
                {
                    return(AgentData.DeterminedMoveSet.Dequeue());
                }
                State = AgentState.Finished;
                break;

            default:
                break;
            }

            return(AgentActions.Search);
        }
        public override void Initialise(Percepts percepts)
        {
            base.Initialise(percepts);

            Name = "Dijkstra's Algorithm Agent";
        }
        public override void Initialise(Percepts percepts)
        {
            base.Initialise(percepts);

            Name = "A-Star Agent";
        }
Example #10
0
        protected virtual List <Node <TileType> > SearchSurroundingNodes(Node <TileType> currentNode, Percepts percepts)
        {
            Vector2i[] searchNodes             = new Vector2i[4]; // 4 surrounding nodes
            List <Node <TileType> > foundNodes = new List <Node <TileType> >();

            // Priorities: Goes UP before LEFT then DOWN then RIGHT
            searchNodes[0] = new Vector2i(currentNode.Pos.X, currentNode.Pos.Y - 1);
            searchNodes[1] = new Vector2i(currentNode.Pos.X - 1, currentNode.Pos.Y);
            searchNodes[2] = new Vector2i(currentNode.Pos.X, currentNode.Pos.Y + 1);
            searchNodes[3] = new Vector2i(currentNode.Pos.X + 1, currentNode.Pos.Y);



            for (int i = 0; i < searchNodes.Length; i++)
            {
                Vector2i newPos = searchNodes[i];
                if (WithinMap(newPos, percepts))
                {
                    if (!AgentData.SearchedPos.Contains(newPos))
                    {
                        Node <TileType> newNode = new Node <TileType>(
                            percepts.MapMatrix[newPos.X, newPos.Y], currentNode, newPos);
                        newNode.Cost = HeuristicFunction(newPos, percepts) + CostFunction(newPos, percepts);
                        if (AgentData.DirectionalMovementCost.Count != 0)
                        {
                            newNode.Cost += AgentData.DirectionalMovementCost[newPos - currentNode.Pos];
                        }
                        foundNodes.Add(newNode);
                        currentNode.Children.Add(newNode);
                    }
                }
            }

            return(foundNodes);
        }
Example #11
0
 public virtual int CostFunction(Vector2i Pos, Percepts percepts)
 {
     return(0);
 }
Example #12
0
 public virtual int HeuristicFunction(Vector2i Pos, Percepts percepts)
 {
     return(0);
 }
Example #13
0
 public abstract AgentActions next(Percepts percepts);