Caputring an agent's trajectory, movement over space and time in csv or GIS file

193 views
Skip to first unread message

dimitris....@gmail.com

unread,
Jan 26, 2015, 11:43:59 AM1/26/15
to gama-p...@googlegroups.com
Dear all,

I can see this is a really nice community : ) It's nice to see all of you.
It's been few months since I decided to work with GAMA. I started reading GAMA documentation, following some Java lectures online and of course checking some tutorials like the Road Traffic ones. My Java programming skills are still at a novice level this is why I'd like to ask for some help with gaml.

I started creating a new model by using one of the Road Traffic tutorial models and making some extra additions. Basically I added 3 groups of people (children, adults, elderly) and 3 types of buildings (industrial, residential and schools).
(Agents are moving either from a residential place to a school if they're children, from a residential place to an industrial one if they're adults or they remain at their homes if they belong to the "eldrely" group.)

Is it possible to export the agents trajectories after a certain number of runs? I'm interested in having an output file (plus a graph if that's feasible) where one can see an agent's movement over space and time. Ideally I'd be interested in creating a GIS file with this kind of data but I guess a first step would be a csv file with time and coordinates of an agent.
I saw some topics (like here and here) that were talking about capturing an agent's trajectory which is what I want.. but to be honest it looks like I failed to put the new lines of code at the right spot so it didn't work. Could you please help me? I would be grateful.

I'm using GAMA 1.6.1 on Windows 7 64bit
Here is my model script if you'd like to have a look:

/**
 *  dim_traffic_example
 *  Author: Dimitris
 *  Description: evolution of a traffic model
 */


model dim_traffic_example

global {
    file shape_file_buildings
<- file("../includes/batiments2.shp");
    file shape_file_roads
<- file("../includes/routes.shp");
//    file shape_file_bounds <- file('../includes/bounds.shp');
//  instead of indicating bounds we give the geometry shape > we use a shapefile to automatically define it by computing its envelope
    geometry shape
<- envelope(shape_file_roads);
//  
// A "reflex" is a sequence of statements that can be executed, at each time step, by the agent
// A "Init" is evaluated only once when the agent is created, after the initialization of its variables
// and before it exectutes any "reflex"    
   
int nb_adults <- 100;
   
   
int nb_children <- 60;
   
   
int nb_elderly <- 60;

// The value of the day_time variable is automatically computed at each simulation step and is equals to
// "time (the simulation step) modulo 24" (one day is decomposed in 24 simulation steps)Therefore every step is 1 hour.        
   
int day_time update: time mod 24 ;
   
int min_work_start_a <- 6;
   
int max_work_start_a <- 10;
   
int min_work_end_a <- 14;
   
int max_work_end_a <- 22;
   
   
int min_work_start_c <- 9;
   
int max_work_start_c <- 9;
   
int min_work_end_c <- 14;
   
int max_work_end_c <- 14;
   
// The value of the day_time variable is automatically computed at each simulation step and is equals to
// "time (the simulation step) modulo 144" (one day is decomposed in 144 simulation steps).          
//    int day_time update: time mod 144 ;
//    int min_work_start <- 36;
//    int max_work_start <- 60;
//    int min_work_end <- 84;
//    int max_work_end <- 132;
   
float min_speed <- 50.0;
   
float max_speed <- 100.0;
    graph the_graph
;
   
    init
{
        create building
from: shape_file_buildings with: [type::string(read ('NATURE'))] {      
           
if type='Industrial' {
                color
<- rgb('pink') ;
           
}  
           
if type='School' {
                color
<- rgb('black') ;
           
}
         
}
     
        create road
from: shape_file_roads ;
        map
<road,float> weights_map <- road as_map (each:: (shape.perimeter));
        the_graph
<- as_edge_graph(road) with_weights weights_map;
       
        list
<building> residential_buildings <- building where (each.type='Residential');
        list
<building>  industrial_buildings <- building where (each.type='Industrial') ;
        list
<building>  school_buildings <- building where (each.type='School') ;
       
        create adults number
: nb_adults {
             speed
<- min_speed + rnd (max_speed - min_speed) ;
             start_work
<- min_work_start_a + rnd (max_work_start_a - min_work_start_a) ;
             end_work
<- min_work_end_a + rnd (max_work_end_a - min_work_end_a) ;
             living_place
<- one_of(residential_buildings) ;
             working_place
<- one_of(industrial_buildings) ;
             location
<- any_location_in (living_place);  
       
}  
       
        create children number
: nb_children {
        speed
<- min_speed + rnd (max_speed - min_speed) ;
        start_work
<- min_work_start_c + rnd (max_work_start_c - min_work_start_c) ;
        end_work
<- min_work_end_c + rnd (max_work_end_c - min_work_end_c) ;
        living_place
<- one_of(residential_buildings) ;
        working_place
<- one_of(school_buildings) ;
        location
<- any_location_in (living_place);  
       
}  
       
        create elderly number
: nb_elderly {
        living_place
<- one_of(residential_buildings) ;
        location
<- any_location_in (living_place);  
       
}
       
   
}
   
    reflex update_graph
{
        map
<road,float> weights_map <- road as_map (each:: (each.shape.perimeter));
        the_graph
<- the_graph with_weights weights_map;
   
}
}


entities
{
    species building
{
       
string type;
        rgb color
<- rgb('blue')  ;
        aspect
base {
            draw shape color
: color ;
       
}
   
}
    species road  
{
        rgb color
<- rgb('black')  ;
        aspect geom
{
            draw  shape color
: color ;
       
}
   
}

    species adults skills
: [moving]{
        rgb color
<- rgb('yellow') ;
        building living_place
<- nil ;
        building working_place
<- nil ;
       
int start_work ;
       
int end_work  ;
       
string objective ;
        point the_target
<- nil ;
       
        reflex time_to_work
when: day_time = start_work {
             objective
<- 'working' ;
             the_target
<- any_location_in (working_place);
       
}
        reflex time_to_go_home
when: day_time = end_work {
             objective
<- 'go home' ;
             the_target
<- any_location_in (living_place);
       
}  
        reflex move
when: the_target != nil {
            path path_followed
<- self goto [target::the_target, on::the_graph, return_path:: true];
            list
<geometry> segments <- path_followed.segments;
                    loop line over
: segments {
                       
float dist <- line.perimeter;
                        road ag
<- road(path_followed agent_from_geometry line);
                   
}
                   
switch the_target {
                        match location
{ the_target <- nil ;}
                   
}
               
}
            aspect
base {
                draw circle
(7) color: color;
           
}
   
}
   
        species children skills
: [moving]{
        rgb color
<- rgb('red') ;
        building living_place
<- nil ;
        building working_place
<- nil ;
       
int start_work ;
       
int end_work  ;
       
string objective ;
        point the_target
<- nil ;
       
        reflex time_to_work
when: day_time = start_work {
             objective
<- 'working' ;
             the_target
<- any_location_in (working_place);
       
}
        reflex time_to_go_home
when: day_time = end_work {
             objective
<- 'go home' ;
             the_target
<- any_location_in (living_place);
       
}  
        reflex move
when: the_target != nil {
            path path_followed
<- self goto [target::the_target, on::the_graph, return_path:: true];
            list
<geometry> segments <- path_followed.segments;
                    loop line over
: segments {
                       
float dist <- line.perimeter;
                        road ag
<- road(path_followed agent_from_geometry line);
                   
}
                   
switch the_target {
                        match location
{ the_target <- nil ;}
                   
}
               
}
            aspect
base {
                draw circle
(5) color: color;
           
}
   
}
   
        species elderly
{
        rgb color
<- rgb('green') ;
        building living_place
<- nil ;
       
string objective ;
        point the_target
<- nil ;

            aspect
base {
                draw circle
(5) color: color;
           
}
   
}    
   
   
}

experiment dim_traffic_example type
: gui {
    parameter
'Shapefile for the buildings:' var: shape_file_buildings category: 'GIS' ;
    parameter
'Shapefile for the roads:' var: shape_file_roads category: 'GIS' ;
//    parameter 'Shapefile for the bounds:' var: shape_file_bounds category: 'GIS' ;
    parameter
'Number of adults agents' var: nb_adults category: 'adults' ;
    parameter
'Number of children agents' var: nb_children category: 'children' ;
    parameter
'Number of elderly agents' var: nb_elderly category: 'elderly' ;
   
    parameter
'Earliest hour to start work' var: min_work_start_a category: 'adults' ;
    parameter
'Latest hour to start work' var: max_work_start_a category: 'adults' ;
    parameter
'Earliest hour to end work' var: min_work_end_a category: 'adults' ;
    parameter
'Latest hour to end work' var: max_work_end_a category: 'adults' ;
    parameter
'minimal speed' var: min_speed category: 'adults' ;
    parameter
'maximal speed' var: max_speed category: 'adults' ;
   
    parameter
'Earliest hour to start work' var: min_work_start_c category: 'children' ;
    parameter
'Latest hour to start work' var: max_work_start_c category: 'children' ;
    parameter
'Earliest hour to end work' var: min_work_end_c category: 'children' ;
    parameter
'Latest hour to end work' var: max_work_end_c category: 'children' ;
    parameter
'minimal speed' var: min_speed category: 'adults' ;
    parameter
'maximal speed' var: max_speed category: 'adults' ;

    output
{
        display dim_traffic_example refresh_every
: 1 {
            species building aspect
: base ;
            species road aspect
: geom ;
            species adults aspect
: base ;
            species children aspect
: base ;
            species elderly aspect
: base ;
       
}
        display chart_display refresh_every
: 10 {
            chart name
: 'adults Objectif' type: pie background: rgb('lightGray') style: exploded size: {1, 1} position: {0, 0} {
                data name
:'Working' value: adults count (each.objective='working') color: rgb('green') ;
                data name
:'Staying home' value: adults count (each.objective='go home') color: rgb('blue') ;
           
}
           
            chart name
: 'children Objectif' type: pie background: rgb('lightGray') style: exploded size: {1, 1} position: {0, 0} {
                data name
:'Working' value: adults count (each.objective='working') color: rgb('green') ;
                data name
:'Staying home' value: adults count (each.objective='go home') color: rgb('blue') ;
           
}
           
           
       
}
       
   
}
}





Patrick Taillandier

unread,
Jan 26, 2015, 10:24:40 PM1/26/15
to gama-p...@googlegroups.com
Hi,

I add the lines to your models (attached to this mail). Note I made some "improvement" of your model :) :
  - remove the entities block (not necessary anymore) ;) 
  - change rgb("a_color") by #color : new way of defining color 
  - change children as a child species of adult: allow to inherit from all the variables/reflexes/actions/aspects of a species and just overriding what is necessary : see for example https://code.google.com/p/gama-platform/wiki/Tutorial__PredatorPreyTutorial_step5

For the save of data, I have created a "trajectory" species, asked the adults and children to save their trajectory in a variable (list of geometries) and just create the trajectory agents from the adults/children variable (and finally save them in a shapefile).

Cheers,

Patrick

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To post to this group, send email to gama-p...@googlegroups.com.
Visit this group at http://groups.google.com/group/gama-platform.
For more options, visit https://groups.google.com/d/optout.

dimitris.zip

dimitris....@gmail.com

unread,
Jan 28, 2015, 9:59:41 AM1/28/15
to gama-p...@googlegroups.com
Dear Patrick, thank you so much!
This is really really helpful indeed. Your quick help encourages me to keep playing with a concept that I have in mind.

I'd like to ask one thing (and please bear with me)
What is the frequency with which an agent's set of coordinates is being captured?

Let me explain.. in my example time is set to a modulo 24 cycle, that means that the step is set to 1h, right? (please do correct me if I'm wrong, I'm new at this : ) )
Based on your code, "cycle_to_save" was set to 50 cycles. That means that we capture the agent trajectory for a total time of 50 hours (since time = cycle * step = 50 * 1h), right?
Opening the exported shapefile in ArcMap, I was expecting something like 50 entries per agent, with the agent's set of coordinates recorded for every step. It looks like I got this wrong since different agents had different number of "recording" entries. I probably missed something..

Now ideally what I want is to capture the agent's location in every simulation step (user defined, in this case every 1h) so when I open the shapefile's attribute table, I'd have columns in dbf such as:
- agent_name (e.g adult#1) - we have that already thanks to your help
- Coordinates (or polygon) - we have that already thanks to your help
- Cycle (0,1,2,3... till we reach the number of cycles as indicated in "cycle_to_save" variable) - could  you please help me with that so that I have an estimation of time for every location..
- Place (whether the agent is at a residential/industrial/school place or moving in the road)

By now you're probably tired with all these descriptions, I'm sorry.
Any hint would mean a lot.

Thanks again so much,
Have a nice day,
Dimitris
...

Patrick Taillandier

unread,
Feb 2, 2015, 7:52:33 AM2/2/15
to gama-p...@googlegroups.com
Hi,

What is the frequency with which an agent's set of coordinates is being captured?
They are captured at every time step. But to be honest, I am a bit lost with your model and the duration of a simulation step. I mean, in the road traffic tutorial, the duration of time step was 10 minutes. To specify the duration of a simulation step, you just have to define the value of the step variable (by default, 1 simulation step = 1 second).
For example, in the road traffic tutorial, I defined in the global section: https://code.google.com/p/gama-platform/wiki/Tutorial__RoadTraficModel_step1#time_step

float step <- 10 #mn;


So, in your model, if you want to specify that a time step is 1h, you should write:
float step <- 1 #h;

Note that in this case, you have to define the unity of the speed of your agents (for example : min_speed <- 50.0 #km/#h;)

Based on your code, "cycle_to_save" was set to 50 cycles. That means that we capture the agent trajectory for a total time of 50 hours (since time = cycle * step = 50 * 1h), right?
Opening the exported shapefile in ArcMap, I was expecting something like 50 entries per agent, with the agent's set of coordinates recorded for every step. It looks like I got this wrong since different agents had different number of "recording" entries. I probably missed something.. 

Not exactly as I store in the trajectories of my agents the segments and for each segment (I can have several segments per simulation steps), I create one trajectory agents : so I can have more than 50 geometries for one agents. In the same way, if the path between the origin and the destination is very short, the number of simulation step (and segments) to reach the destination can be lower than 50.

Now ideally what I want is to capture the agent's location in every simulation step (user defined, in this case every 1h) so when I open the shapefile's attribute table, I'd have columns in dbf such as:
- agent_name (e.g adult#1) - we have that already thanks to your help
- Coordinates (or polygon) - we have that already thanks to your help
- Cycle (0,1,2,3... till we reach the number of cycles as indicated in "cycle_to_save" variable) - could  you please help me with that so that I have an estimation of time for every location..
- Place (whether the agent is at a residential/industrial/school place or moving in the road)

I have some ideas of how to do that, but also some questions to be sure to give you good advices ;) :
   - when an agent stay at home for 3 simulation cycles, do want you?
           -> nothing, only movements interest you
           -> one point geometry with for cycle, the first "cycle" of its staying
           -> 3 points

Cheers,

Patrick
 

2015-01-28 21:59 GMT+07:00 <dimitris....@gmail.com>:
Dear Patrick, thank you so much!
This is really really helpful indeed. Your quick help encourages me to keep playing with a concept that I have in mind.

I'd like to ask one thing (and please bear with me)
What is the frequency with which an agent's set of coordinates is being captured?

Let me explain.. in my example time is set to a modulo 24 cycle, that means that the step is set to 1h, right? (please do correct me if I'm wrong, I'm new at this : ) )
Based on your code, "cycle_to_save" was set to 50 cycles. That means that we capture the agent trajectory for a total time of 50 hours (since time = cycle * step = 50 * 1h), right?
Opening the exported shapefile in ArcMap, I was expecting something like 50 entries per agent, with the agent's set of coordinates recorded for every step. It looks like I got this wrong since different agents had different number of "recording" entries. I probably missed something..

Now ideally what I want is to capture the agent's location in every simulation step (user defined, in this case every 1h) so when I open the shapefile's attribute table, I'd have columns in dbf such as:
- agent_name (e.g adult#1) - we have that already thanks to your help
- Coordinates (or polygon) - we have that already thanks to your help
- Cycle (0,1,2,3... till we reach the number of cycles as indicated in "cycle_to_save" variable) - could  you please help me with that so that I have an estimation of time for every location..
- Place (whether the agent is at a residential/industrial/school place or moving in the road)

By now you're probably tired with all these descriptions, I'm sorry.
Any hint would mean a lot.

--

dimitris....@gmail.com

unread,
Feb 27, 2015, 11:09:04 AM2/27/15
to gama-p...@googlegroups.com
Hey Patrick thanks again,



I am a bit lost with your model and the duration of a simulation step.
Alright : ) I'm still trying to make sure I get this right.
Let's say that I want a simulation to last for a single day with a step of 10 min.
That means that I have to define the step to be 10 minutes long (as you said) and I guess I have to set the cycle_to_save to 144, right?
(since time=cycle*step = 144*10 = 1440min = 1 day)

I also need to define the time an agent starts/ends working and also to define speed.
Here's my lines of code on that.. Am I doing something wrong?

   float step <- 10 #mn;

   int cycle_to_save <- 144;

  
   reflex end_simulation when: cycle = 1440 {
       
do pause;
   
}

   //int day_time update: time mod 24;
   int current_hour update: (time /#h) mod 24;
   

  
int min_work_start_a <- 6;

  
int max_work_start_a <- 10;
  
int min_work_end_a <- 14;
  
int max_work_end_a <- 22;
  
int min_work_start_c <- 9;
  
int max_work_start_c <- 9;
  
int min_work_end_c <- 14;
  
int max_work_end_c <- 14;


   float min_speed <- 50.0  #km/#h;
   float max_speed <- 100.0 #km/#h;


Not exactly as I store in the trajectories of my agents the segments and for each segment (I can have several segments per simulation steps), I create one trajectory agents : so I can have more than 50 geometries for one agents. In the same way, if the path between the origin and the destination is very short, the number of simulation step (and segments) to reach the destination can be lower than 50.
Terribly sorry but I'm not sure I got what you say. I want to create a time and location dependent model (not just a location dependent one) and the ideal thing for me was to have the agents's set of coordinates recorded for every cycle,, so 1 segment for every cycle


I have some ideas of how to do that, but also some questions to be sure to give you good advices ;) :
   - when an agent stay at home for 3 simulation cycles, do want you?
           -> nothing, only movements interest you
           -> one point geometry with for cycle, the first "cycle" of its staying
           -> 3 points
I'd choose your last option (3 points), I'd like to have the time and coordinates for every cycle no matter what the agent is doing (moving or working)


Your hints are a  treasure : )
Enjoy the weekend
Yours

Dimitris
...

Patrick Taillandier

unread,
Mar 4, 2015, 9:33:01 AM3/4/15
to gama-p...@googlegroups.com
Hi,

Let's say that I want a simulation to last for a single day with a step of 10 min. 
That means that I have to define the step to be 10 minutes long (as you said) and I guess I have to set the cycle_to_save to 144, right?
(since time=cycle*step = 144*10 = 1440min = 1 day)
You are right ! Not that you can use the built-in variable time as well.
In your case (if you have define that step = 10 mn), you can use:
 reflex save_result when: cycle = 144 {....}
 reflex save_result when: time= 1 #day {....}
-------

I also need to define the time an agent starts/ends working and also to define speed. 
Here's my lines of code on that.. Am I doing something wrong?
seems ok

-----------------

Terribly sorry but I'm not sure I got what you say. I want to create a time and location dependent model (not just a location dependent one) and the ideal thing for me was to have the agents's set of coordinates recorded for every cycle,, so 1 segment for every cycle

I have some ideas of how to do that, but also some questions to be sure to give you good advices ;) :
   - when an agent stay at home for 3 simulation cycles, do want you?
           -> nothing, only movements interest you
           -> one point geometry with for cycle, the first "cycle" of its staying
           -> 3 points

Please have a look at the attached model ;).


Cheers,

Patrick

--
dimitiris.gaml

dimitris....@gmail.com

unread,
Mar 9, 2015, 10:29:40 AM3/9/15
to gama-p...@googlegroups.com
Hey Patrick, thank you so much.
That's exactly what I was looking for : )
I'll keep on playing with this example (so who knows I might pose a question in the future) but so long your help was really valuable, thanks
...
Reply all
Reply to author
Forward
0 new messages