Class BasicAgent
- Direct Known Subclasses:
AutonomousBasicAgent
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 Summary
Modifier and TypeFieldDescriptionprotected CostFunction
Specify how to calculate the cost of a single invocation of an action.protected GoalStructure.PrimitiveGoal
A topgoal may consists of multiple subgoals, which the agent will work on one at a time.protected Tactic
The tactic the agent is currently using to solve its currentGoal.protected Deliberation
An instance of Deliberation is responsible for, as the name says, executing a deliberation process for this agent.protected GoalStructure
The current topgoal the agent has.protected String
protected GoalStructure
The last goal handled by this agent before it was detached.protected Logger
protected Time
A time tracker used to calculated the agent's actions' execution time for the purpose of budget calculation.protected String
protected SimpleState
-
Constructor Summary
ConstructorDescriptionCreate a blank agent.BasicAgent(String id, String role)
Create a blank agent with the given id and role. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Insert the goal-structure G as the next direct sibling of the current goal.void
AsaddAfter(GoalStructure)
, but the inserted goal is set with the auto-remove flag turned on.void
Deprecated.void
AssimpleAddBefore(GoalStructure)
, but the inserted goal is set with the auto-remove flag turned on.attachEnvironment(Environment env)
Attach an Environment to this agent.attachState(SimpleState state)
Attach a state structure to this agent.budget(double b)
Set initial computation budget for this agent.protected void
As the name says, this will detach the current topgoal and subgoal from the agent.env()
Return the environment attached to this agent.getId()
Return the agent's id, if it was set.Return the goal-structure that was last detached by this agent.getRole()
Return the agent's role, if it was set.protected void
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.protected void
Write the string to this agent logger, with the specified logging level.void
remove(GoalStructure G)
Remove the goal-structure G from this agent root goal-structure.void
restart()
Currently unimplemented.setGoal(double budget, GoalStructure g)
Set a goal for this agent, with the specified initial budget.setGoal(GoalStructure g)
Set a goal for this agent.protected void
setTopGoalToFail(String reason)
Set topgoal to fail, with the given reason.protected void
setTopGoalToSuccess(String info)
For marking the agent's topgoal as succees, and adding the given info string to it.void
Insert the goal-structure G as a sibling before the current goal.state()
Return the agent's state.protected void
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.void
update()
Look for enabled actions within the current strategy that would be enabled on the current agent state.useDeliberation(Deliberation delib)
Replace the agent's deliberation module with the one given to this method.Set f as this agent cost-function.
-
Field Details
-
id
-
role
-
state
-
goal
The current topgoal the agent has. -
lastHandledGoal
The last goal handled by this agent before it was detached. -
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
The tactic the agent is currently using to solve its currentGoal. -
logger
-
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
An instance of Deliberation is responsible for, as the name says, executing a deliberation process for this agent. . -
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 aSimpleState
and aGoalStructure
to it before it can be used to do something. -
BasicAgent
Create a blank agent with the given id and role. You will need to at least attach aSimpleState
and aGoalStructure
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. ForAutonomousBasicAgent
they are important to identify where messages should be sent to.
-
-
Method Details
-
setGoal
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
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
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
Set f as this agent cost-function. Return the agent itself so that this method can be used in the Fluent Interface style. -
getId
Return the agent's id, if it was set. -
getRole
Return the agent's role, if it was set. -
attachState
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
Return the agent's state. -
attachEnvironment
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
Return the environment attached to this agent. -
useDeliberation
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 thegoal
andcurrentGoal
fields to null. -
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
Write the string to this agent logger, with the specified logging level. -
setTopGoalToSuccess
For marking the agent's topgoal as succees, and adding the given info string to it. -
setTopGoalToFail
Set topgoal to fail, with the given reason. Then the goal is detached. -
addAfter
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
AsaddAfter(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. -
simpleAddBefore
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
AssimpleAddBefore(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
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 methoduseDeliberation(Deliberation)
is used to choose one (the default is to just choose randomly). The chosenAction
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.
-