The Agent-Behavior Pattern

An artificial society consists of an environment inhabited by agents. Agents represent people, animals, robots, or any other type of social, goal-directed active object. An agent can play many roles in a society. Parent, employee, and spouse are examples. An agent's behavior depends on the role it currently plays.

We can think of an agent as a special type of actor that perpetually loops through a list of roles executing one step from each role. A role can be viewed as a finite state machine. Each time a role's execute method is called, it performs a state transition. When it reaches a final state the role is removed from the agent's list. Agents can send messages to other agents.

A. Translate the concepts Environment, Agent, and Role into a UML class diagram.

B. Translate the class diagram from A into a Java or C++ framework for building artificial societies.

C. Customize the framework from B with the following scenario:

The roles in an auction are auctioneer, bidder, and seller. The auctioneer broadcasts a current highest bid to all bidders. Each bidder sends a new bid back to the auctioneer.

 

 

 

 

In the bi-directional case the role specifies a behavior the actor must execute. This situation commonly appears in agent-based systems such as Jade:

A composite behavior is a finite state machine in which each state represents a micro-behavior and a transition represents the flow of execution from one micro-behavior to the next.

Examples of behaviors include:

Auctioneer, Bidder, Seller, Requestor, Provider, Publisher, Broker

The Actor-Role and Agent-Behavior may be seen as formalizations of some basic ideas from Sociology.

Implementation

An agent is an active object that perpetually cycles through a schedule of behaviors:

class Agent extends Thread {
   private List<Behavior> schedule
      = new ArrayList<Behavior>();
   public void add(Behavior b) {
      b.setAgent(this);
      schedule.add(b);
   }
   public void run() {
      int next = 0;
      while(0 < schedule.size()) {
         Behavior b = schedule.get(next);
         if (!b.execute()) {
            schedule.remove(b);
            if (schedule.size() == 0) break;
         }
         next = (next + 1) % schedule.size();
      }
   }
}

A cyclic behavior is a simple behavior that executes some task a fixed number of times:

class Behavior {
   private Agent agent;
   private int cycle = 0;
   private int maxCycle;
   public Behavior(Agent a, int max) {
      agent = a;
      maxCycles = max;
   }
   public void setAgent(Agent a) {
      agent = a;
   }
   public boolean execute() {
      if (maxCycle <= cycle++) return false; // done
      System.out.println("cycle = " + cycle); // the task
      return true; // call again
   }
}