Count the number of rodent agents on a land cover shapefile

69 views
Skip to first unread message

Quentin Devalloir

unread,
Jan 6, 2025, 2:13:11 AM1/6/25
to GAMA
Hi everyone, 

I'm struggling to perform a simple count of agents (rodents) on a given patch (pastures in a land cover shapefile). 

I tried first with a simple counting of all agents overlapping the shapefile ("habitats") as follow : 

global{

file habitat <- file("XXXX.shp");

file world_enveloppe <- file("XXXX.shp");

image_file my_icon <- file("XXXX.png");


init

{

step <- 1 #day;

create world_boundaries from: world_enveloppe

{}

world_boundaries_shape <- first(world_boundaries).shape;

create habitats from:habitat with:[DN::int(read("Validmean"))]

{}

create prey number: preyinit

{

location <- any_location_in(world_boundaries_shape ); // location anywhere within the group perimeter

}

}

}


species world_boundaries

{}

species habitats

{

int DN;

aspect default{

switch (DN)

{

match 12 {draw shape color:rgb([0, 128, 0]); } // pasture

match 13 {draw shape color:rgb([0, 255,0]); } // crops

match 14 {draw shape color:rgb([128, 128, 0]);} // vineyard

match 15 {draw shape color:rgb([255, 128, 0]); } // cities

}

}


reflex rodent_in_habitas

{

list<prey> rod_in_habitats <- agents_overlapping (self);

rod_sum <- sum (sample(rod_in_habitats));

}


}


species rodent skills: [moving]

{

float speed <- 1#m/#hour; // test value

reflex move

{

do wander speed:speed amplitude:60.0 bounds: geometry(world_boundaries_shape);

}

}


   }

Do you know what is wrong with the reflexe highlited above  and how can I fix it ? 

Many thanks in advance,

All the best 

Quentin 

Alan Lam

unread,
Jan 8, 2025, 7:30:02 AM1/8/25
to GAMA
Hi Quentin,

Have you tried agent_inside(self) or agents_covering(self) instead of agents_overlapping?

Quentin Devalloir

unread,
Jan 13, 2025, 6:42:29 AM1/13/25
to GAMA
Hi, 

Thank you  for your help. 

I managed to fix it with 'agents_overlapping' and 'agents_inside' and then in instead of sum I use 'length' for prey in habitat.

But now I met another difficulty..

I would like to get a data frame with a value of thetime spent for each agent in a given habitat. In other word, to determine the time of the overlapping for each agent.

Do you have any idea or suggestion to do it ? 

Many thanks in advance.

All the best  

Alan Lam

unread,
Jan 13, 2025, 8:22:01 AM1/13/25
to GAMA
Hi Quentin,

Congratulations on figuring out the correct code. Really happy for you :)
For your other question, how many habitat are there in your model? If it is just a few, my not-so-elegant guess would be to create separate int variable for each habitat as a cycle counter:

species rodent {
     int time_in_habitat0 <- 0;
     int time_in_habitat1 <- 0 ;
     int time_in_habitat2 <- 0 ;   
...
}

Then use a reflex under rodent to update the corresponding time_in_habitat at each cycle, based on the location of the agent:

reflex update_time_in_habitat {
   if location overlaps habitat[0].shape { time_in_habitat0 <- time_in_habitat0 + 1} //not 100% sure about the syntax, but you get the idea
   ....
   ....
}

But if you have hundreds of habitats, then the coding is really beyond me (I am just a beginner myself); perhaps you can create a map of habitat id and use a loop to add 1 to the corresponding value in the map? I am sure others on this group will have a better suggestion.

Alan

Quentin Devalloir

unread,
Jan 14, 2025, 4:43:10 AM1/14/25
to GAMA
Thank you very much Alan ! 

It works pretty good that way but the only way to see the time counter for a given habitat is to use 'write time_in_habitat ;' in the reflex (see code below) 

reflex update_time_in_habitat {

if location overlaps habitats[1].shape {

time_in_hab <- time_in_hab +1;

write time_in_hab;

}

 Do you know how can I "extract"  time_in_hab to monitor it displays it on a chart and save the results for each rodent ? 

Many thanks in advance

All the best 

Quentin 

Alan Lam

unread,
Jan 17, 2025, 11:05:54 PM1/17/25
to GAMA
I suppose you can export the value of time_in_hab to a csv in the reflex statement using the save function. You can add the agent attributes (e.g., name) and number of cycle in csv (refers to the GAMA documentation). You can then plot charts and do further analysis on the csv in a different software (e.g. excel or R).

Hope it helps.

Alan

Quentin Devalloir

unread,
Jan 20, 2025, 2:06:41 PM1/20/25
to GAMA
Hi Alan, 

Thank you very much again for replying. 
Indeed ! This is exactly what I've done and it works very well !


All the best 

Quentin  

Alan Lam

unread,
Jan 20, 2025, 10:04:49 PM1/20/25
to GAMA
That's awesome Quentin!  I'm glad I could assist.

Best of luck with your project.

Alan
Reply all
Reply to author
Forward
0 new messages