JADE: Parallelism - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

JADE: Parallelism

Description:

... an object of a class that extends jade.core.behaviours.Behaviour. A class extending Behaviour must implement ... sleep(dt) suspends the execution of the thread ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 37
Provided by: will178
Category:

less

Transcript and Presenter's Notes

Title: JADE: Parallelism


1
JADE Parallelism Behaviours
  • Sensor Web TITAN Research
  • 3/12/2009

2
Behaviours
  • Agents operate independently, execute in parallel
  • Assign a Java Thread to each agent
  • But also need parallelism within each agent
  • An agent may be involved in several tasks
  • Possible solution additional threads for each
    concurrent agent activity
  • But Java Threads werent designed for large-scale
    parallelism

3
Behaviours
  • To support efficiently parallel activities within
    an agent, Jade introduced Behaviours
  • A behaviour is basically an Event Handler,
    describing how an agent reacts to an event
  • An event is a relevant change of state
  • E.g., a reception of a message or a Timer
    interrupt

4
Implementing a behaviour
  • A behaviour is an object of a class that extends
    jade.core.behaviours.Behaviour.
  • A class extending Behaviour must implement some
    methods
  • action() defines the operations to be performed
    when the behaviour is in execution
  • done() specifies whether a behaviour has
    completed, through the boolean value it returns

5
Behaviours (Important Notes)
  • Behaviour actions are methods
  • Executed one after the other by the agent's
    thread after events
  • Cant pause without blocking all other activity
    within the agent
  • So each Behaviour execution corresponds to ONE
    SINGLE instantaneous active phase
  • To implement long-term activities (e.g., a
    negotiation), must provide as many Behaviours as
    there are active phases

6
Implementing a behaviour
  • To implement an agent-specific task
  • define 1 or more Behaviour subclasses,
  • instantiate them, and
  • add the behaviour objects to the agents task
    list
  • using addBehaviour(Behaviour)method

7
Agent1 Example
  • 2 identical behaviours that show parallelism
  • Prints a message with the elapsed time and agent
    name
  • Create instance of the behaviour
  • Add behaviour using addBehaviour(Behaviour)
  • addBehaviour( new Looper( this, 300 ) )

8
Agent1 Example (code)
  • public class Agent1 extends Agent
  • protected void setup()
  • addBehaviour( new Looper( this, 300 ) )
    addBehaviour( new Looper( this, 500 ) )
  • // Action method of Looper behaviour
  • public void action()
  • System.out.println( tab (System.currentTimeMill
    is()- t0)/1010 " " myAgent.getLocalName()
    )
  • block( dt )
  • n

9
Complete CodeFile Agent1.java
  • import jade.core.Agent
  • import jade.core.behaviours.
  • public class Agent1 extends Agent
  • protected void setup()
  • addBehaviour( new Looper( this, 300 ) )
  • addBehaviour( new Looper( this, 500 ) )

10
Complete CodeLooper.java
  • import jade.core.Agent
  • import jade.core.behaviours.
  • class Looper extends SimpleBehaviour
  • static String offset ""
  • static long t0 System.currentTimeMillis()
  • String tab ""
  • int n 1
  • long dt

Continued
11
  • public Looper( Agent a, long dt)
  • super(a)
  • this.dt dt
  • offset " "
  • tab new String(offset)
  • public void action()
  • System.out.println( tab
  • (System.currentTimeMillis()-t0)/1010 " "
  • myAgent.getLocalName() )
  • block( dt )
  • n
  • public boolean done() return ngt6

12
Output from Agent1
  • gt java jade.Boot aaaAgent1
  • 0 aaa
  • 0 aaa
  • 300 aaa
  • 510 aaa
  • 620 aaa
  • 920 aaa
  • 1010 aaa
  • 1220 aaa
  • 1520 aaa
  • 1530 aaa
  • 2020 aaa
  • 2530 aaa

13
Output from two Agent1 agents
  • gt java jade.Boot aaAgent1 zzzzzAgent1
  • 0 zzzzz
  • 0 aa
  • 10 zzzzz
  • 10 aa
  • 300 zzzzz
  • 310 aa
  • 510 zzzzz
  • 510 aa
  • 610 zzzzz
  • 610 aa
  • 910 zzzzz
  • 920 aa

14
Output from two Agent1 agents (cont.)
  • 1020 zzzzz
  • 1020 aa
  • 1220 zzzzz
  • 1220 aa
  • 1520 zzzzz
  • 1520 aa
  • 1520 zzzzz
  • 1530 aa
  • 2030 zzzzz
  • 2030 aa
  • 2530 zzzzz
  • 2530 aa

15
block(dt) vs. sleep(dt)
  • block(dt) sets a delay for the behaviours next
    execution
  • Does not block the execution of the behaviour
  • sleep(dt) suspends the execution of the thread
  • If that thread is put to sleep, then all activity
    for that agent stops

16
Bad1 Example
  • Try to solve a more complex sequencing problem in
    a variety of ways
  • Combine a sequence of 2 actions with the above
    Looping behaviour
  • In the sequential part, we want our agent to
  • wait 0.25 sec then print out a first message
  • wait 0.5 sec and print out a second message
  • then terminate
  • The next implementation is an incorrect example
    of parallelism
  • Uses block(dt) along with a Looper behaviour

17
Bad1 Example (code)
  • public class Bad1 extends Agent
  • protected void setup()
  • addBehaviour( new TwoStep() )
  • addBehaviour( new Looper( this, 300 ) )
  • // TwoStep Behaviour
  • import jade.core.behaviours.SimpleBehaviour
  • class TwoStep extends SimpleBehaviour
  • public void action()
  • block(250)
  • System.out.println( "--- Message 1 --- "
    )
  • block(500)
  • System.out.println( " - message 2 " )

18
Output from Bad1
  • gt java jade.Boot johnBad1
  • --- Message 1 ---
  • message 2
  • 10 john
  • 310 john
  • 620 john
  • 920 john
  • 1220 john
  • 1530 john

19
Block2 Example
  • A single behaviour, BlockTwice
  • 2 block statements
  • Prints out the clock at the beginning of the
    action and after each block()

20
Block2 Example (code)
  • public class Block2 extends Agent
  • protected void setup()
  • addBehaviour( new BlockTwice() )
  • class BlockTwice extends SimpleBehaviour
  • static long t0 System.currentTimeMillis()
  • public void action()
  • System.out.println( "Start "
    (System.currentTimeMillis()-t0) )
  • block(250)
  • System.out.println( " after block(250) "
    (System.currentTimeMillis()-t0) )
  • block(1000)
  • System.out.println( " after block(1000) "
    (System.currentTimeMillis()-t0) )
  • System.out.println()

21
Output from Block2
  • gt java jade.Boot tomBlock2
  • Start 1
  • after block(250) 7
  • after block(1000) 9
  • Start 258
  • after block(250) 261
  • after block(1000) 263
  • Start 512
  • after block(250) 515
  • after block(1000) 517
  • Start 767
  • after block(250) 769
  • after block(1000) 771

22
Bad3 Example with sleep()
  • Incorrect example of parallelism
  • Uses sleep(dt)method
  • An agent only has 1 Thread, shared by the
    behaviours
  • If that Thread is put to sleep anywhere, then all
    activity for that agent stops

23
Bad3 Example with sleep() (code)
  • class TwoStep extends SimpleBehaviour
  • public void action()
  • try
  • System.out.println( "--- TwoStep
    start " ...time )
  • Thread.sleep(200)
  • System.out.println( " -- Message 1
    --- " ...time )
  • Thread.sleep(500)
  • System.out.println( " - message 2
    " ...time )
  • catch (Exception e)
  • private int n 0
  • public boolean done() return n gt 2

24
Output from Bad3
  • gt java jade.Boot maryBad3
  • --- TwoStep start 20
  • -- Message 1 --- 220
  • - message 2 720
  • 700 mary
  • --- TwoStep start 730
  • -- Message 1 --- 930
  • - message 2 1430
  • --- TwoStep start 1440
  • -- Message 1 --- 1640
  • - message 2 2140
  • 2120 mary
  • 2430 mary
  • 2730 mary
  • 3040 mary
  • 3340 mary

25
Updated Agent2
  • If an activity is made up of a sequence of active
    phases with pauses in between, use 1 Behaviours
    for every active phase
  • Need 2 behaviours to print out the 2 messages
  • Ideally, schedule newly created behaviours for
    some future time
  • In the behaviour which prints out Msg1, we
    schedule the behaviour which prints out Msg2 to
    occur 0.5 sec later
  • But block() can't be used to schedule other
    behaviours
  • Just postpones the next execution of the current
    behaviour
  • Solution Use a state variable to distinguish
  • the 1st execution of a behaviour (when we execute
    block(dt)) from
  • the next execution (which occurs dt msec later)

26
Updated Agent2 (code)
  • class Step1 extends SimpleBehaviour
  • int state 0
  • public void action()
  • if (state0) block( 200 )
  • else if (state1)
  • System.out.println( "--- Message
    1 --- " )
  • addBehaviour( new Step2() )
  • state
  • public boolean done() return state gt
    1

27
Updated Agent2 (code) - continued
  • class Step2 extends SimpleBehaviour
  • int state 0
  • public void action()
  • if (state0)
  • block( 600 )
  • else
  • System.out.println( " -
    message 2 " )
  • doDelete() // applies to the
    Agent
  • state
  • public boolean done() return stategt1

28
Output from Updated Agent2
  • gt java jade.Boot harryAgent2
  • 0 harry
  • --- Message 1 ---
  • 340 harry
  • 640 harry
  • - message 2

29
Agent3 (Final version)
  • Creates a finite state machine
  • Uses a switch to select the actions corresponding
    to each state

30
Agent3 (Final version) (code)
  • class TwoSteps extends SimpleBehaviour
  • int state 1
  • public void action()
  • switch( state )
  • case 1
  • block( 200 )
  • break
  • case 2
  • System.out.println( "--- Message
    1 --- " )
  • block( 800 )
  • break
  • case 3
  • System.out.println( " -- message
    2 --" )
  • finished true

31
Output from Agent3
  • gt java jade.Boot aliceAgent3
  • 0 alice
  • --- Message 1 ---
  • 340 alice
  • 640 alice
  • 950 alice
  • -- message 2 --

32
Complete CodeAgent3.java
  • The program ends normally get back to the shell
    prompt without CTL-C
  • Provided the agent with a takeDown() method
  • Called automatically when an agent is deleted
  • Most often, this is where an agent removes its
    entries from various directories
  • Here we just stop the container and terminate the
    program with System.exit(0)

33
  • import jade.core.Agent
  • import jade.core.behaviours.
  • public class Agent3 extends Agent
  • protected void setup()
  • addBehaviour( new TwoSteps() )
  • addBehaviour( new Looper( this, 300 ) )
  • protected void takeDown()
  • System.exit(0)

Continued
34
  • class TwoSteps extends SimpleBehaviour
  • int state 1
  • public void action()
  • switch( state)
  • case 1
  • block( 200 )
  • break
  • case 2
  • System.out.println( "--- Message
    1 --- " )
  • block( 800 )
  • break

Continued
35
  • case 3
  • System.out.println( " -- message
    2 --" )
  • finished true
  • doDelete() // applies to the
    Agent
  • state
  • private boolean finished false
  • public boolean done() return
    finished

36
Negotiation example w/ Behaviours
  • Consider a negotiation (sending offers, waiting
    for counter-offers and finally reaching
    agreement)
  • This activity consists of an alternation of
  • active phases - when the agent decides what to
    do and sends messages
  • passive phases - when the agent waits for an
    answer.
Write a Comment
User Comments (0)
About PowerShow.com