Sei sulla pagina 1di 31

Formation à la modélisation à base d'agents, introduction aux plates-formes GAMA et PAMS

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms
26, 27 & 28 oct. 2009, Hanoi, Vietnam

An introduction to GAMA
Alexis Drogoul, IRD

Reasons for developing GAMA


• Improving the reusability of individual developments in different models.

• Structuring the research team around a common realisation.

• Sharing and abstracting the knowledge accumulated in different


domains of modeling.

• Supporting lectures on agent-based modeling and simulation.

• Offering new research opportunities, especially on modeling languages


and coupling of modeling techniques.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 2
Background
• Work on GAMA began late 2006, as a framework (meta-model on top of Repast J) able to
support the requirements of 3 research projects (avian flu propagation, by E. Amouroux,
urban emergencies, by Q. Chu Thanh and household daily activities, by F. Sempé)
• The idea of translating the concepts shared by these projects into computational
abstractions manipulated by a script language gained momentum, and GAML was officially
born in mid-2007. A. Vo Duc joined the team to port it onto the Eclipse RCP environment.
• In 2008, GAMA began to be used as a training tool for the IFI Master students. The addition
of batch controllers and several developments targeting its usability, stability and
performances achieved to transform it into a more versatile platform.
• In 2009, it was released as an open-source project and used for the Master lectures at the
Univ. of Can Tho. Since then, with the help of P. Taillandier, work has begun on its second
incarnation (GAMAv2). Two new projects running on top of GAMA (strategies against rice
pests) have been launched in 2009.
• GAMA is now both a simulation environment, which we use in 6 projects, a pedagogical
tool, used in our master lectures, and the object of ongoing researches, notably on
modeling languages for describing emergent phenomena.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 3

Original purpose of GAMA


• Extend Repast J (and Repast S) with a
strong metamodel and a modeling
language.

• Be as easy to use as NetLogo, but as


open as Repast (GIS, outputs,
exploratory modes, inheritance, reuse,
etc.)

• Be as efficient as Mason (hundreds of


thousands of agents on one computer).

• Allow the simultaneous use of different


environments and control architectures.

• Release 1 available here has achieved


some of these goals (but not all) :
http://www1.ifi.auf.org/mediawiki/index.php/GAMA

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 4
Internal architecture
GAML
Compiler Language
• Explicit meta-model to guide ANTLR grammar
Plug-in architecture
Based on the meta-model
Extensible at runtime

programmers and modelers


Controller Meta-model
Architecture
• Libraries of behaviors/control Batch
Calibration and parameters Abstract, situated agents
Scheduling
architectures/environments (GIS, exploration

grids, networks, etc.) Remote


Web-based control (PAMS
Decision
Automata, reflexes,
project) machine learning

• Semi-compiled modeling language Interactive Skills


(GAML) aiming at supporting the Graphical User Interface
Input & Output
Behavioral libraries,
plug-in architecture

development of DSLs
Kernel
• Advanced support for simulation/ Simulation
Environments
GIS, continuous,
experimentation/visualization Scheduler, inputs, outputs
grids, networks

(controllers)
Eclipse RCP Repast OpenMap

Java libraries
Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 5

Architectural choices: scheduler


• The scheduler is responsible for running the simulation. It repeats the
sequence below until the simulation halts (or is stopped):
Ⴡ Maintain the list of agents : adding new agents and removing dead ones
Ⴡ Updating global attributes and running global dynamics
Ⴡ Updating the different environments
Ⴡ Asking agents to update their attributes
Ⴡ Asking agents to execute their behaviors
Ⴡ Shuffling the list of agents
Ⴡ Updating the outputs (views, files, etc.)
Ⴡ Incrementing time

• GAMA is a discrete-time, pseudo-parallel simulator.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 6
UI Controller
• Based on Eclipse RCP and used for designing models

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 7

Batch Controller
Sequential batch
• Allows to explore the parameters space of <batch
type="sequence"
models by running simulations and controlling sameSeed="true"
repeat="3"
stopSimWhen="food = 0"
their input parameters and outcomes. >
<param
name="evaporation_rate"

• Two possible modes:


min="0.05"
max="0.5"
step="0.05"/>

Ⴡ Sequential: the exploration is entirely


<param
name="ants_number"
values="100"
determined by the user (ranges of parameters, random="2"
/>

stop conditions, etc.) </batch>

Exploratory batch
Ⴡ Exploratory: optimization techniques are used <batch
to find “interesting” sets of parameters that type="optimize"
sameSeed="true"
repeat="3"
produce the desired result. stopSimWhen="time > 200"
>
<param
◦ Simulated annealing name="evaporation_rate"
min="0.05"
max="0.7"
◦ Ant Colony Metaheuristic step="0.01"
/>
<optimize minimize="foodLeft"/>
</batch>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam
Metamodel
manages >

defines >

* * *
Control
Skill Attribute Behavior Interaction Communication
Architecture
* *
1 * *
*

* *
* 1 specifies > * controls > 1 1
Species Agent Body

1 1

World

* *
1..n
*
Environment Place Action Perception

< changes

< uses

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 9

Use of the meta-model


Modellers

Databases OpenMap
GIS data foundation

UI control
GAML Language
Batch experiments
manages >

defines >

* * *
Control
Skill Attribute Behavior Interaction Communication
Architecture
1 * *
* *
*

* *
* 1 specifies > * controls > 1 1
Species Agent Body

1 1

Experts World

* *
Modellers
Users
1..n
*
Environment Place Action Perception

< changes

< uses

Eclipse RCP foundation,


Java annotations
RepastJ DisplaySurface
XML parser
foundation,
GAML parser
RepastJ Controller/Sim
Partial Java compiler
framework

GAMA Java
Framework
Java subclassing
Abstract/
Java reuse
Concrete
classes

Developers

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 10
Meta-model
• The meta-model allows:
Ⴡ To extend GAMA in Java by defining new (concrete) subclasses to anything : agents,
control architectures, etc. The meta-model defines all the relationships needed.
Ⴡ To provide a clean interface for manipulating GAMA from outside (example with
PAMS)

• In GAMA, modellers have the choice between :


Ⴡ Extending the abstraction layer provided and building models directly in Java
Ⴡ Creating models using a dedicated (specialized for modelling) language, GAML,
which masks the complexity of the underlying architecture.
Ⴡ Or, they can do both : write the applicative part in GAML, but abstract and develop in
Java the core techniques for future reuse in other applications.

• The link between GAML and Java, and the possibility to develop hybrid models, is
realized through the use of Java meta-informations (annotations) and dedicated
parsers and compilers.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 11

Ex: coding the GAML "or" function in Java

Meta-annotations, once
interpreted, directly
translate into structures GAML Language

available to GAML Control


manages >

defines >

* * *

Skill Attribute Behavior Interaction Communication


Architecture
1 * *
*

/**
* *

* *
* 1 specifies > * controls > 1 1

* The OR expression
Species Agent Body

1 1

Subclassing of existing
World

*
* *
1..n
*
Environment Place Action Perception

classes in the framework


< changes

*/
< uses

Java annotations
@left_operand_types( { GamlType.BOOL }) XML parser
@right_operand_types( { GamlType.BOOL }) GAML parser
Partial Java compiler
@return_type(GamlType.BOOL)
@keyword( { "or", "|" }) GAMA Java
public class BinaryOpOrExpr extends AbstractBinaryOpExpr { Java subclassing
Framework
Abstract/
Java reuse
Concrete
public Boolean value(final BasicEntity agent) { classes
return boolValue(agent);
}

public boolean boolValue(final BasicEntity agent) {


return left.boolValue(agent) ? true : right.boolValue(agent);
}

Reuse of existing interfaces/classes of the framework

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 12
Ex: adding new entities to GAML
• Messages are bits of information exchanged between agents, that
follow some protocols inside a
conversation. @species_name("message")
@vars( { "sender", "receivers", "performative", "content", "unread",
"conversation", "protocol", "timestamp" })
public class Message extends GamaObject implements IMessage {

private final IMessage reference;


@species("conversation")
@vars( { "messages", "protocol", "initiator", "participants",
"ended" }) private boolean unread = true;
public class Conversation extends GamaObject { ...
@getter(var = "sender")
protected FIPAProtocol protocol;
public BasicEntity getSender() {
/** The owner of this conversation. */ return reference.getSender();
protected BasicEntity initiator; }

/** Other Agent in the conversations. */


protected List<BasicEntity> participants = new
@setter("sender")
FastList<BasicEntity>(); public void setSender(final BasicEntity sender) {
reference.setSender(sender);
protected Map<BasicEntity, ProtocolNode>
}
protocolNodeParticipantMap = new FastMap<BasicEntity,
ProtocolNode>();
@getter(var = "conversation", species = "conversation")
protected Map<BasicEntity, IMessage> public BasicEntity getConversation() {
noProtocolNodeParticipantMap = new FastMap<BasicEntity,
return reference.getConversation();
IMessage>();
}
}
Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 13

Ex: adding an action to an agent or a skill

/**
* Action: wander
* @param args
* the args : speed (meter/sec) : the speed with which the agent
* wants to move ; distance (meter) : the distance the agent want
* to cover in one step ; amplitude (in degrees) : 360 or 0 means
Once defined, an action caled * completely random move, while other values, combined with the
"wander" becomes available for the * heading of the agent, defines the cone in which the agent will
agents that use/implement this skill * choose a new place.
*/

@action("wander")
@facets( {
@facet(name = "amplitude", type = GamlType.INT, optional = true),
@facet(name = "speed", type = GamlType.FLOAT, optional = true),
@facet(name = "distance", type = GamlType.FLOAT, optional = true) })
public CommandStatus primMoveRandomly(final Arguments args) {
...
}

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 14
Training session
• This training session will not deal with these aspects. No programming
in Java (unless you want to correct a bug) will be required, as we will
focus on the modeling part of the platform, leaving aside its role as a
framework.

• Next part makes you discover the User Interface controller of GAMA,
and some bits of the GAML language.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 15

A quick tour of GAMA UI


• First things first, launch the program. It opens an empty window with
four controls.

Open and run a


simulation of a model

Open and run several


simulations (batch) of a model

Open and edit a model file

Create a new model file

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 16
Opening and running a model
Models are simple
XML text files

Allows to open, compile


and run a model in
simulation mode

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 17

Progress bar
• Progress bar during the opening of the model
Ⴡ Parsing -> Compilation -> Agents Creation -> UI Opening
Ⴡ Currently not cancellable (but interrupted in case of errors !)

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 18
GAMA User Interface
• Once a model has
been loaded,
compiled and
launched, the window
display several views
that depend on the
outputs defined in it.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 19

GAMA User Interface


• Views can be rearranged at will

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 20
Running a model
• To run a model already loaded, two buttons can be used. Each
of them causes the views to be updated.

"Run" launches the simulation, until it


stops by itself or the user pauses it.

"Step" launches the simulation, then pauses


it after one step, waiting for user input.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 21

Monitors
• Monitors : a way to follow arbitrary values during a simulation.
At which frequency (in
simulation steps) should
the value be updated.

Opens a dialog to let the user


enter the expression to
monitor

Expressions are written in GAML


(here, “time” simply means we
want to monitor the evolution of
this -global- variable)

The expression can be evaluated,


In order to catch any error beforehand

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 22
Monitors
• Monitors

Pauses the monitor Closes the monitor Re-edit the monitor

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 23

Monitors
• New monitors can be added at will

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 24
Monitors
• The monitors view can accomodate for an arbitrary number of monitors
(they can be described programmatically in the model, and saved at
once for future reuse in the model)

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 25

Using GAMA
• Errors in
expressions
are caught at
run-time and
prevent faulty
monitors to
be created

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 26
Using GAMA
• All dynamical views in GAMA share a same set of controls
Pause the update of the view (NOT the simulation)

Edit the refresh rate of the view

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 27

Using GAMA
• Graphical views (charts, displays) add the possibility to save snapshots

Saves a snapshot in the current folder of the


model, under a sub-folder called "snapshots"

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 28
Using GAMA
• Parameters can be visualized and edited from the Parameters view
category name of the parameter current value (editable) bounds type

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 29

Using GAMA
• Specialized editors exist for booleans, lists and colors

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 30
Parameters
• Parameters modified by the user are indicated in red. They can be
reverted to their original values using the appropriate button.

Any modification can be cancelled using this button

The value of "Number of colors" has been modified

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 31

Parameters
• Simulations can be relaunched with the modified set of parameters

"Number of colors" will be equal to 2 for subsequent simulations.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 32
Parameters

"Number of colors" is correctly still indicated as


modified from the original model.

The model is now correctly initialized with 2


(instead of 5) colors

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 33

Parameters
• Modified parameters (and user-created monitors) can be saved for
future reuse.

Parameters and monitors are


saved in a new model that
"includes" the previous one.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 34
Parameters
• This new model is available as any other model and can be run in the
same conditions as the others

The new file is available,


together with the original one

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 35

Parameters

Two model files form the


"new" model. The original
one, complemented by the
modified parameters

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 36
Edition mode
• Models can be edited directly from GAMA (although any other text or
XML editor can be used).

Edit any model file Edit all the model files composing the current simulation

Create a new model file

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 37

Edition Mode

Outline of the frontmost model file XML Editor

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 38
Edition mode

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 39

Edition mode
• Going back and forth the simulation and the edition modes
Does not relaunch the model

Launch or relaunch the current frontmost model

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 40
Overview
• GAML is a programming language, interpreted (actually, semi-compiled)
by GAMA, and dedicated to agent-based modeling. It does not replace
Java, but is sufficient for most models (even complex ones).

• Allows the modeler to directly specify and manipulate most of the


components of the metamodel of GAMA.

• Allows to use extensions to the metamodel (such as communication, for


instance, or different control architectures) if they include the necessary
meta-information.

• GAML files are currently structured as XML files.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 41

GAML Documentation
• A limited (and incomplete) documentation is available here:
Ⴡ http://www1.ifi.auf.org/mediawiki/index.php/New_programming_guide

• From there, you have access to the definition of the most common
features of GAML (commands, operators, variables, types, skills, species,
actions, etc.)

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 42
Structure of a model : 4 sections
<model_name>

<global> Inside are described the attributes Important sub-sections


and behaviors of the global agent
...
(the "world")
</global>

Inside are described the <gis ... >


<environment>
environments in which the Inside are described the
...
agents behave <grid ...> attributes and behaviors of
</environment>
each cell-agent of the grid(s)

<entities> Inside are described the <species 1... > Inside are described the
... different species of agents attributes and behaviors of
</entities> involved in the model <species 2...> each agent of the species

<monitor ... >


<output> Inside are described the
... various outputs of a <display ... >
</output> simulation
<file ... >

</model_name>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 43

Species
• The heart of the GAML model is the description of the structure and
dynamics of the global level, the environment(s) and the species of
agents. For global, grid and species, the definition uses the same format
of a generic species. They specify three things :

<species name="my_agents" skills="moving, visible">




<rgb name="color" init="rgb 'red'" const="true"/>
What their agents know (attributes)
<float name="size" init="rnd 20" min="1"/>

<action name="increase_size">
<set name="size" value="size+1"/> What their agents can do (actions)
</action>

<reflex>
<do action="increase_size"/> What their agents actually do (behaviors)
</reflex>


</species>
Additionally, skills provide built-in
attributes and actions (coded in Java)

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 44
Attributes
Attributes are defined by a type (9 predefined) and some facets : name (the
only mandatory one), init (an initial value), value (an update function), and
some specific facets (max, min, of, size, etc.).
Each facet (save name) contains an expression in GAML.

Predefined types: int, float, bool, string, rgb, list, matrix, point, agent
Ⴡ More types become available as species are created : the species
themselves can be used to provide a type for attributes.

• Examples :
<float name="i" init="0.1" value="i+0.01" max="100"/>

<list name="liste" init="[ ]" of="agent"/>

<int name="longueur" value="length liste"/>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 45

Attributes and parameters


In the global definition, attributes can have two additional facets if they are
to be considered as input parameters of the model : parameter and category

Example:
<int name="na" init="100" max="1000" min="1" parameter="Number of agents:" category="Agents" />

<int name="nc" init="5" max="9" parameter="Number of colors:" category="Environment"/>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 46
Expressions: constants
• Constant literals :
‘aaa’ (a string); 10 (an int); 10.23 (a float); true (a bool); [‘ab’, 1, 32.2] (a
list); {12.0,12.23} (a point)

rgband matrix cannot be declared as literals and are created by specific


operators.

• Other pseudo-constants (or pseudo-attributes) :


self : always represents the agent that is acting (see also myself)
each : represents each element of a list in list-related operators
nil : represents the absence of information on an agent or a value

• Other constants :
Units : m, km, meter, decimeter, second, s, sec, hour, month, etc.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 47

Expressions: variables
• If a variable is an attribute of the agent, or a global attribute, the name of
this variable can be used directly in an expression.

If it belongs to another agent, the syntax to use is: agent_name.attribute_name


Note that self.attribute_name is the same as attribute_name

Example:
Access to the global
attribute "time"
<species name="my_species" > Direct access to
own attribute
"date_of_birth"
<int name="date_of_birth" init="time" />
Use of <int name="age" value="time - date_of_birth"/>
"my_species"
as a type <my_species name="my_friend"/>
<bool name="older" value="age > my_friend.age"/>

</species>
Direct access to own attribute "age" Access to the attribute "age" of "my_friend"

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 48
Expressions: unary operators
• Unary operators: functions that only take one argument.
Syntax: operator expression

• Examples:
sum [10,20,30] -> 60; round 19.54 -> 20; length ‘abc’ -> 3; empty [ ] -> true;
max [10,20,30] -> 30 ; reverse [10,20,30] -> [30,20,10];
shuffle [‘a’,’b’,’c’] -> [‘c’, ‘a’, ‘b’].

• Casting operators : types can be used as operators to «cast» the


expression
int 19.54 -> 19 ; list species_name -> list of the agents of the species ;
list ‘abc’ -> [‘a’,’b’,’c’]; list 12 -> [12] ;
rgb [255,255,255] -> white color; rgb ‘black’ -> black color;

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 49

Expressions: binary operators


• Binary operators: functions that take two arguments.
Syntax: expression1 operator expression2

• Examples
10 + 23 ; [10,20,30] at 1 -> 20 ; 2 among [1,2,3,4,5] -> [3,5]

• Powerful list (and matrix) operators: where, collect, first_with, count,


last_with, with_max_of, with_min_of, sort_by, min_of, max_of

• Example:
3 agents a1, a2, a3 with attribute size = 1, 2, 3 and a list la = [a2,a1,a3]
la where each.size > 1 -> [a2,a3]; la with_max_of each.size -> a3 ; la sort_by
each.size -> [a1,a2,a3] ; la min_of each.size -> 1

[32,10,1,23] sort_by each -> [1,10,23,32]


[32,10,1,23] collect (each + 10) -> [42,20,11,33]

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 50
Expressions: ternary operator
• Only one function with three arguments ("functional if").
Syntax: bool_expression ? expression1 : expression2

Example:
2 > 12 ? 1 : 2 -> 1
la collect ((each.size > mean (la collect each.size)) ? ‘taller’ : ‘smaller’))
-> [‘smaller’,‘smaller’,‘taller than average’]

Precedence between operators of the same arity is not ensured (even


arithmetic ones: 1 + 2 * 3 is not correctly computed). Use parentheses !
However, unary operators always take precedence over binary/ternary ones :
length la + length lb is correctly understood.

XML disallows some keywords. For instance, the less-than operator '<' has
to be written &lt; (the corresponding HTML entity).
Ⴡ The whole reference to expressions is available on the documents and the wiki.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 51

Actions
• Actions describe what an agent of the species can do. It can take
arguments, and consists in a sequence of commands.
<action name="my_action">
[arguments]*
[commands]*
</action>

Arguments are defined by the arg keyword. They accept two facets : a name
and a possible default value.

<action name="my_action">
<arg name="arg1" default="0"/>
<arg name="arg2"/>
[commands]*
</action>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 52
Commands
• There are 9 basic commands in GAML
let, set, do, return, if, else, loop, create and ask

• Temporary variables
let allows to create and initialize a temporary variable. This variable is
local to the scope in which it is created (i.e. If it is created within an
action, for instance, it cannot be used outside of this action). The type of
the variable is not declared (it is inferred from the value).

<let name="temp" value="10"/>


<let name="temp2" value="temp + 23"/>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 53

Commands: set
• Assignment
setallows to assign a new value to an attribute, a temporary variable, a
location in a list, matrix or point.

<set name="size" value="10"/>

if the types do not match, the value is casted using that of the variable :

<set name="color" value="'white'"/>

• Also allowed is to assign a value to an attribute of another agent


<set name="a1.size" value="10"/>

Can be used to modify composite attributes, like list or matrix:

<set name="la at 0" value="10"/>


<set name="matrix1 at {10,10}" value="10"/>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 54
Commands: do & return
• Action calls and returns
do allows the agent to perform an action (built-in or defined in the species).
The optional facet result declares a temporary variable that will be assigned
with the result of the execution (if any).
In an action, return allows to describe which value to return (if any) at the
end of its execution.

<action name="my_action1"> <arg name="arg1" default="0"/>


<return value="arg1"/>
</action> temp1 is assigned with 0

<action name="my_action2">
temp2 is assigned with 100
<do action="my_action1" result="temp1"/>
<do action="my_action1">
<arg name="arg1" value="temp1 + 100" result="temp2"/>
</do>
</action>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 55

Commands: if
• Conditional statements
if allows to conditionally execute another command (or a sequence of) if a
condition evaluates to true

<if condition="a_bool_expression">
[command]*
</if>

elsecan only be employed inside if, and encloses the command(s) to


execute when the condition evaluates to false
<if condition="a_bool_expression">
[command]*
<else>
[command]*
</else>
</if>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 56
Commands: loop
• Repetitions
Loopallows to execute several times a sequence of commands, either
conditionnaly or within certain bounds. It takes several forms :

<loop while="a_bool_expression">
[command]*
</loop>

<loop times="an_int_expression">
[command]* temp is assigned, in turn, with each integer in the
sequence between int1 and int2
</loop>

<loop for="an_int1" to="an_int2" var="temp">


[command]*
</loop>

<loop over="a_list_expression" var="temp">


[command]*
temp is assigned, in turn, with each value in the list
</loop>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 57

Commands : ask
• Remote execution
askis a way for an agent to "control" other agents by making them execute a
sequence of commands.
<ask target="an_agent | a_list_of_agents" [as="a_species"]>
[command]*
</ask>

Commands are executed in the context of the target. If the calling agent wants
to transmit on some data of its own, it needs to use the keyword myself.
<ask target="a1">
<set name="size" value="myself.size"/>
</ask>

<action name="invite_friends">
<ask target="all_my_friends" as="species self">
<do action="come"> <arg name="location" value="myself.home"/> </do>
</ask>
</action>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 58
Commands: create
• Instantiation of agents
create allows to instantiate new agents of a given species. Optionnaly it can
contain a sequence of commands that will be executed by the agents
created immediately after their "birth" (just like ask). It can also contain a
facet result, that assigns to the temporary variable declared the list of newly
created agents.

<create species="a_species" number="an_int_expression" [result="temp"]>


[command]*
</create>

<create species="human" number="100" result="univ_staff">


<set name="age" value="rnd 50 + 18"/>
<set name="occupation" value="one_of ['researcher', 'student', 'professor']"/>
<set name="hobbies" value="2 among ['tennis', 'lecture', 'music', 'computers']"/>
</create>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 59

Behaviors : reflexes & init


• Behaviors describe what the agents will do after they are created.
The most basic behavior is the reflex, a sequence of commands continuously
executed by the agents. It can be given a condition, in which case the reflex is
only executed when it is true. Species can define as many reflexes as they need.
A special reflex, called init, is executed at the birth of the agent (and discarded
after).

<species name="human">
<int name="sleepless_hours" init="0" value="in_bed ? sleepless_hours - 1 : sleepless_hours + 1"/>
<bool name="in_bed"/>
<init>
<set name="in_bed" value="flip 0.5"/>
</init>
<reflex name="go_to_bed" when="sleepless_hours > 12">
<set name="in_bed" value="true"/>
</reflex>
<reflex name="wake_up" when="sleepless_hours &lt; 0">
<set name="in_bed" value="false"/>
</reflex>
</species>

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 60
Specific declarations
• Species can inherit their attributes, actions and behaviors from another
one (and redefine them). This is specified using the facet parent:
<species name="human" parent="mammal">

• "Grid" species can specify their width and height, how many neighbours
each cell has, and whether or not they are to be considered as toroidal
<grid name="my_grid" width="30" height="30" torus="false" neighbours="8">
<rgb name="color" value="rgb [rnd 255, rnd 255, rnd 255]"/>
</grid>

Their cell agents are automatically created, and


automatically provided with specific attributes,
like color, neighbours, multiagent, location, etc.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 61

Time to play !

• Many other notions (advanced behaviors, commu-


nication between agents, use of GIS environments,
chart outputs, etc.) will be discovered during the tutorials,
together with examples of what we have just seen.

• Keep in mind that GAMA is not a polished software. It is


sometimes necessary to force-quit and restart it. It has bugs,
unfinished parts, unimplemented functions, awkward behaviors.
Just be patient : it can only improve over time, with your help.

• Despite all this, we hope you'll find GAMA as useful as we do


in our researches, and that you'll be interested to contribute to it (or
simply use it) in the future. We'll discuss this at the end of the training.

Training session on agent-based modeling, introduction to the GAMA and PAMS platforms, IRD, UMI UMMISCO 209, MSI Team, 26, 27 & 28 oct. 2009, Hanoi, Vietnam 62

Potrebbero piacerti anche