Figure 5. The green turtles are contaminated by the red ones
In this simulation, we want to simulate the transmission of a virus in a population and observe it. To do this, we have created a kind of turtle (Virus class) that can take two behaviors: infected or non infected (red and green). The all turtles just walk around and the red ones contaminate the others when cross them on a patch.
We have created two classes of Virus to simulate two ways to transmit the virus. The first way is to use real MadKit agent messages: when a red turtle cross turtles that are green, it gets their AgentAddress and sends them a VirusMessage. So if a sane turtle has its mailbox not empty, it changes of behavior and becomes a red turtle. (Just like an email virus: we can imagine another version of this simulation where a turtle might be capable of being careful when reading its mailbox.
public String red() { wiggle(); Turtle[] ts = turtlesHere(); //returns the other turtles that are on the same patch if (ts != null) for (int i=0; i < ts.length;i++) if (ts[i].getColor() == Color.green) sendMessage(ts[i].getAddress(),new VirusMessage()); return("red"); } } public String green() { if (nextMessage() != null) //check mailbox { setColor(Color.red); playRole("infected"); return ("red"); } else { wiggle(); return ("green"); } } |
The second way (Virus2) consists in directly interact with the other turtles by changing their color to simulate the transmission. So a sane turtle always check its color and adopts the corresponding behavior: red or green.
public String red() { wiggle(); Turtle[] ts = turtlesHere(); if (ts != null) for (int i=0; i < ts.length;i++) if (ts[i].getColor() == Color.green) ts[i].setColor(Color.red); return("red"); } public String green() { if (getColor() == Color.red) { playRole("infected"); return ("red"); } else { wiggle(); return ("green"); } } |
Figure 6. The curve looks like an exponential one
The Observer of this simulation, the VirusWatcher, just creates a TurtleTable with the role infected and displays the corresponding number using a special GUI (a madkit.lib.simulation.SimplePlotPanel) suited to draw mathematic curves.
/** initialize the variable infectedTurtles (a Turtle[]) using getTurtleWithRole*/ public void initializeTurtleTables() { infectedTurtles = getTurtleWithRole("infected"); } /**this method overrides watch in the class Observer. So it will be invoked for each simulation step*/ step public void watch() { plot.addPoint(infectedTurtles.length); //plot is the GUI } |
A Turtle is a real MadKit agent: it owns all the possibilities of any AbstractAgent of MadKit. Specially, interesting points are the usage of group, roles and AgentAddress concepts to structure the application logics and identify the agents.