Class BasicAgent

java.lang.Object
nl.uu.cs.aplib.mainConcepts.BasicAgent
Direct Known Subclasses:
AutonomousBasicAgent

public class BasicAgent extends Object
This is the root class of all agents in aplib. As the name suggests, this class provides you with basic but working agents. An instance of BasicAgent is an agent. You can give it a goal (to be more precise, an instance of GoalStructure. Assuming a strategy has been supplied along with the goal, you can then execute the agent, which in turn will execute the strategy towards solving the goal. A BasicAgent uses a very simplistic deliberation scheme. When in the current state (of the agent) the given strategy leads to multiple possible actions (Action) to choose, the agent's internal process to decide which one to choose is called deliberation. A BasicAgent simply chooses randomly. A BasicAgent is suitable to be used as a so-called subservient agent. In particular, it lacks the ability to run as an autonomous agent. It also lacks the ability to exchange messages with other agents. If you need autonomous and communicating agents, you can use a subclass called AutonomousBasicAgent.

After creating a blank agent using the constructor, you will need to attach a state to it. This may sound contradictory to you. Since we are in Java, the just created agent would be an object, and it would have some methods. This object already has a state, composed of the values of its fields; so the statement of 'attaching' a state would be wierd. It is true that any instance of BasicAgent will come with its own fields etc that form its state. However this state would have no information about the domain of the goal that you try to solve with the agent. For example if you want the agent to play the Monopoly game, its built-in state would have no knowledge of how a the Monopoly board looks like. Without this knowledge we can't expect it to be able to play Monopoly. You would first need to provide a representation of this board as a subclass of SimpleState and then attach an instance of this board to your agent so that it can use it to at least track the state of the game. Use the method attachState(SimpleState) to attach a state. After this, the agent is ready to do work for you.

Use the method setGoal(GoalStructure) to give a goal to a BasicAgent. It is assumed, that when you provide a goal, you also attach a Tactic to it that the agent can use to solve the goal. To execute this strategy you can invoke the method update(), which will execute the strategy for a single tick. Call the method repeatedly until the goal is solved, or until the agent exhaust the computation budget that you may have specified for the goal:

   while (topgoal.getStatus().inProgress()) {
      agent.update();
      ... // do whatever else between agent's updates
   }
 

Between calls to update() the agent will not do anything (though the Environment on which it operates might do a lot of things). Each call to update() is also called a tick. The above way of running an agent puts you in full control of when to give a tick to the agent, hence invoking its behavior. An agent that is used in this way is called subservient, as opposed to autonomous agents that control the ticking themselves. Autonomous agents would run on their own threads, whereas a subservient agent can stay in the same thread is your main thread. See AutonomousBasicAgent for a base class to create autonomous agents.

Author:
Wish
  • Field Details

    • id

      protected String id
    • role

      protected String role
    • state

      protected SimpleState state
    • goal

      protected GoalStructure goal
      The current topgoal the agent has.
    • lastHandledGoal

      protected GoalStructure lastHandledGoal
      The last goal handled by this agent before it was detached.
    • currentGoal

      protected GoalStructure.PrimitiveGoal currentGoal
      A topgoal may consists of multiple subgoals, which the agent will work on one at a time. This field will point to the current subgoal the agent is working on.
    • currentTactic

      protected Tactic currentTactic
      The tactic the agent is currently using to solve its currentGoal.
    • logger

      protected Logger logger
    • mytime

      protected Time mytime
      A time tracker used to calculated the agent's actions' execution time for the purpose of budget calculation. This is declared as an explicit field so that it can be conveniently mocked during testing (you have to test from the same package).
    • deliberation

      protected Deliberation deliberation
      An instance of Deliberation is responsible for, as the name says, executing a deliberation process for this agent. .
    • costFunction

      protected CostFunction costFunction
      Specify how to calculate the cost of a single invocation of an action. The default is that each action invocation costs 1.0.
  • Constructor Details

    • BasicAgent

      public BasicAgent()
      Create a blank agent. You will need to at least attach a SimpleState and a GoalStructure to it before it can be used to do something.
    • BasicAgent

      public BasicAgent(String id, String role)
      Create a blank agent with the given id and role. You will need to at least attach a SimpleState and a GoalStructure to it before it can be used to do something. The id should be unique. Multiple agents can have the same role. For BasicAgent, Id and role do not have any influence. For AutonomousBasicAgent they are important to identify where messages should be sent to.
  • Method Details

    • setGoal

      public BasicAgent setGoal(GoalStructure g)
      Set a goal for this agent. The method returns the agent itself so that this method can be used in the Fluent Interface style.
    • setGoal

      public BasicAgent setGoal(double budget, GoalStructure g)
      Set a goal for this agent, with the specified initial budget. The method returns the agent itself so that this method can be used in the Fluent Interface style.
    • budget

      public BasicAgent budget(double b)
      Set initial computation budget for this agent. The agent must have a goal set. This method should not be called when the agent is already working on its goal.
    • withCostFunction

      public BasicAgent withCostFunction(CostFunction f)
      Set f as this agent cost-function. Return the agent itself so that this method can be used in the Fluent Interface style.
    • getId

      public String getId()
      Return the agent's id, if it was set.
    • getRole

      public String getRole()
      Return the agent's role, if it was set.
    • attachState

      public BasicAgent attachState(SimpleState state)
      Attach a state structure to this agent. The method returns the agent itself so that this method can be used in the Fluent Interface style.
    • state

      public SimpleState state()
      Return the agent's state.
    • attachEnvironment

      public BasicAgent attachEnvironment(Environment env)
      Attach an Environment to this agent. To be more precise, to attach the Environment to the state structure of this agent. The method returns the agent itself so that this method can be used in the Fluent Interface style.
    • env

      public Environment env()
      Return the environment attached to this agent.
    • useDeliberation

      public BasicAgent useDeliberation(Deliberation delib)
      Replace the agent's deliberation module with the one given to this method. The method returns the agent itself so that this method can be used in the Fluent Interface style.
    • detachgoal

      protected void detachgoal()
      As the name says, this will detach the current topgoal and subgoal from the agent. So, setting the goal and currentGoal fields to null.
    • getLastHandledGoal

      public GoalStructure getLastHandledGoal()
      Return the goal-structure that was last detached by this agent. A goal is detached when it is declared successful or failed.
    • restart

      public void restart()
      Currently unimplemented.
    • log

      protected void log(Level level, String s)
      Write the string to this agent logger, with the specified logging level.
    • setTopGoalToSuccess

      protected void setTopGoalToSuccess(String info)
      For marking the agent's topgoal as succees, and adding the given info string to it.
    • setTopGoalToFail

      protected void setTopGoalToFail(String reason)
      Set topgoal to fail, with the given reason. Then the goal is detached.
    • addAfter

      public void addAfter(GoalStructure G)
      Insert the goal-structure G as the next direct sibling of the current goal. However, if the parent of this goal-structure is REPEAT (which can only have one child), a SEQ node will first be inserted in-between, and the G is added as the next sibling of this goal-structure.

      Fail if the current goal is null or if it is the top-goal.

    • addAfterWithAutoRemove

      public void addAfterWithAutoRemove(GoalStructure G)
      As addAfter(GoalStructure), but the inserted goal is set with the auto-remove flag turned on. This means that after G is achieved or failed, it will also be removed from the goal-structure that it is part of.
    • addBefore

      @Deprecated public void addBefore(GoalStructure G)
      Deprecated.
    • simpleAddBefore

      public void simpleAddBefore(GoalStructure G)
      Insert the goal-structure G as a sibling before the current goal. However, if the parent of this goal-structure is REPEAT (which can only have one child), a SEQ node will first be inserted in-between, and the G is added as the pre-sibling of this goal-structure.

      Fail if the current goal is null or if it is the top-goal.

    • addBeforeWithAutoRemove

      public void addBeforeWithAutoRemove(GoalStructure G)
      As simpleAddBefore(GoalStructure), but the inserted goal is set with the auto-remove flag turned on. This means that after G is achieved or failed, it will also be removed from the goal-structure that it is part of.
    • remove

      public void remove(GoalStructure G)
      Remove the goal-structure G from this agent root goal-structure. Fail if the agent has no goal or if G is an ancestor of the current goal.
    • lockEnvironment

      protected void lockEnvironment()
      You should not need to use this, unless you want to implement your own Agent by overriding this class, and you need your own custom way to lock and unlock access to the Environment.
    • unlockEnvironment

      protected void unlockEnvironment()
      You should not need to use this, unless you want to implement your own Agent by overriding this class, and you need your own custom way to lock and unlock access to the Environment.
    • update

      public void update()
      Look for enabled actions within the current strategy that would be enabled on the current agent state. If there are none, the method returns. If there are multiple, the deliberation policy set by the method useDeliberation(Deliberation) is used to choose one (the default is to just choose randomly). The chosen Action will then be executed for a single tick. If the Action returns a non-null proposal, this method will check is this proposal solves the current subgoal, it will be marked as such, and the next subgoal will be search. If there is none, then the topgoal is solved.

      This method also keeps track of the computation time so far used to work on the topgoal as well as the current subgoal. If this exceeds the allocated time, the corresponding topgoal/subgoal will be marked as failed.