Assign age based on percentage to breed

42 views
Skip to the first unread message

Michèle Grindat

unread,
24 Jun 2022, 09:07:1024/06/2022
to netlogo-users
Hi!
I'm quite new to NetLogo (and to programming in general) so I don't know too much about scheduling of the code. Now I ran in to a challenge that I don't know how to fix so far. 

I have a breed called "evacuees" to which I want to assign the evacuees-own variable "age". I have 5 different age categories and the corresponding percentage of people in that age group, e.g. 27% of men should get the age "20-39" assigned.
The number of evacuees is defined by a slider on the interface. 
Now I want to create different agentsets (based on the percentage) to which I can assign the age to. I tried using the approch with "n-of":

set men-0-14 n-of (count evacuees with [sex = "male"] * 0.11) evacuees
ask men-0-14 [set age "0-14"]
ask evacuees [if not member? self men-0-14 [set men-15-19 n-of (count evacuees with [sex = "male"] * 0.04) evacuees with [sex = "male" AND not member? self men-0-14]] ]
ask men-15-19 [set age "15-19"]

.. and so on. 
But the problem is that in the end I still have male evacuees that haven't gotten an age assigned. I assume that this is because of the scheduling of NetLogo. Maybe the "ask evacuees" or the "n-of" still considers all evacuees, even with the  added if-statement, because the turtle only will be assigned to men-0-14 definitely after the whole procedure is done, until then they are still in the pool that gets called with n-of. Does someone know how I can avoid this?

Greetings
Michèle


Pradeesh Kumar K V

unread,
25 Jun 2022, 00:48:1425/06/2022
to netlogo-users
Hello  Michèle,

Following is a sample code that assigns turtles to five age groups:

turtles-own [age]

to setup
  ca
  reset-ticks
  create-turtles 100
  [
    set xcor random-xcor
    set ycor random-ycor
    set age -1 ;set an initial value for age. This can also be '0'
  ]

  let i 0
  let prop (list 0.1 0.2 0.3 0.2 0.2) ;create list of age group proportions in the same order as groups are listed in the 'age-group' list below
  let age-group (list "group1" "group2" "group3" "group4" "group5") ;create list of age groups
  repeat 5 ; 5 as there are five age groups
  [
    let m turtles with [age = -1] ;create set of turtles with age = -1. This set will progressively grow smaller after each iteration
    ask n-of (100 * item i prop) m ;select specific number of turtles from m. It will start with 100*0.1 in first iteration, 100*0.2 in second iteration and so on. 100 because there are 100 turtles.
    [
      set age item i age-group ;update age
    ]
    set i i + 1 ;increment i
  ]
 
end

Best,

Pradeesh

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/1ed074de-8102-471f-a0cd-5dbe81493335n%40googlegroups.com.

James Steiner

unread,
27 Jun 2022, 10:44:5427/06/2022
to netlogo-users
Hey Michele.

The scheduling in netlogo is very simple: Things happen one at a time.

One source of confusion for some people is CREATE-TURTLES (and it's relatives, HATCH and SPROUT).

The key is that all the turtles are created, then the turtles, in random order, run the code block, if there is one.

Consider the command

CREATE-TURTLES 10
[ if any? turtles-here [ jump count turtles-here ]
]

First, ALL 10 turtles are created, THEN each turtle, one at a time, runs all the code is the code block. (back in the day, there was this simulated concurrency thing--that is no longer the default and you can ignore it)

The first turtle that runs finds 10 turtles, the second, 9, the third 8, and so on.




Michèle Grindat

unread,
28 Jun 2022, 15:53:1428/06/2022
to netlogo-users
Thank vou very much Pradeesh!

I adapted my code a bit because I realized that there were always a few agents with the age of -1 left. This is because when I calculate the number of evacuees that get a certain age group assigned, NetLogo rounds the numbers like so that they might not match 100 % at the end. As a quick fix I just let the loop run 5 times instead of 6 and asked all the remaining evacuees to set their age to 65+ so that none is left. This changes the proportions in the age category a bit, but in an extent that is not relevant to me. 

ask evacuees [ set age -1 ]
let i 0
let prop (list 0.11 0.04 0.26 0.27 0.06 0.26)
let age-group (list "0-14" "15-19" "20-39" "40-59" "60-64" "65+")
repeat 5
[
 set m evacuees with [age = -1]
 ask n-of (round(number-of-evacuees * item i prop)) m 

   [
     set age item i age-group
   ]
   set i i + 1
 ]
 ask m [set age "65+"]

Best
Michèle

pradeeshkkv schrieb am Samstag, 25. Juni 2022 um 06:48:14 UTC+2:
Hello  Michèle,

Following is a sample code that assigns turtles to five age groups:

turtles-own [age]

to setup
  ca
  reset-ticks
  create-turtles 100
  [
    set xcor random-xcor
    set ycor random-ycor
    set age -1 ;set an initial value for age. This can also be '0'
  ]

  let i 0
  let prop (list 0.1 0.2 0.3 0.2 0.2) ;create list of age group proportions in the same order as groups are listed in the 'age-group' list below
  let age-group (list "group1" "group2" "group3" "group4" "group5") ;create list of age groups
  repeat 5 ; 5 as there are five age groups
  [
    let m turtles with [age = -1] ;create set of turtles with age = -1. This set will progressively grow smaller after each iteration
    ask n-of (100 * item i prop) m ;select specific number of turtles from m. It will start with 100*0.1 in first iteration, 100*0.2 in second iteration and so on. 100 because there are 100 turtles.
    [
      set age item i age-group ;update age
    ]
    set i i + 1 ;increment i
  ]
 
end

Best,

Pradeesh

Michèle Grindat

unread,
28 Jun 2022, 15:53:1528/06/2022
to netlogo-users
Hi Gregor

Thank you very much for your explanation. Now it is clear to me how the scheduling works!

Greetings
Michèle

Reply all
Reply to author
Forward
0 new messages