terriblecontact7
terriblecontact7

Reputation: 11

Error with n-of in a SEIR model of Netlogo

I was trying to implement social-distancing into my code for NetLogo and I keep getting an error when I run the code with n-of: "Requested 500 random agents from a set of only 0 agents. error while observer running N-OF called by procedure SETUP called by Button 'setup'"

I added a slider called "social-distancing and currently have it set to 0.5"

Here is my setup:

globals [susceptible-code exposed-code infectious-code recovered-code distancing-yes-code distancing-no-code population total-dead]
turtles-own [epi-state distancing] ;; each turtle has an epidemiological state


;; Creating the  the initial configuration of the model
to setup
  clear-all
  set susceptible-code "susceptible"
  set infectious-code "infectious"
  set exposed-code "exposed"
  set recovered-code "recovered"
  set distancing-yes-code "distancing"
  set distancing-no-code "not-distancing"

  create-turtles 1000 [
    set epi-state susceptible-code ;; setting the turtle state as susceptible
    set color blue
    set size 0.4
    set shape "circle"
    set xcor random-xcor
    set ycor random-ycor
    set distancing-no-code "not-distancing"
  ]

  ;; makeing one turtle infectious

  let initial-no-of-sd count turtles * social-distancing

  ask one-of turtles [
    set epi-state infectious-code
    set color red ;; we color infectious turtles in red
  ]

  ask n-of initial-no-of-sd turtles with [distancing = distancing-yes-code] [
    set distancing distancing-yes-code
    set color yellow
  ]

  set population count turtles
  reset-ticks
end

Upvotes: 0

Views: 196

Answers (1)

JenB
JenB

Reputation: 17678

The error message is telling you that you asked 500 turtles from a set to do something but there are no turtles in that set. My guess is that it is this line:

ask n-of initial-no-of-sd turtles with [distancing = distancing-yes-code]

You haven't actually set the distancing-yes-code yes, so there are no turtles that satisfy the condition. In fact, you set that variable within the code block so I expect you want:

 let initial-no-of-sd count turtles * social-distancing
 ask n-of initial-no-of-sd turtles [
    set distancing distancing-yes-code
    set color yellow
  ]

Note that I also moved the let statement so you don't have creating the variable and using it separated by the code that is about something different (in this case creating an infectious turtle). This is good practice so that debugging is easier as the model gets more complicated.

Upvotes: 2

Related Questions