Boyd Van Leeuwaarde
Boyd Van Leeuwaarde

Reputation: 11

get-infected method for netlogo simulation not functioning

The following code is a simple simulation for a virus spreading. It isn't working and I can't find out why. ziek becomes true when an agent mutates (which is in a different method, but is known to be working). The agent also becomes lime. However my agents only mutate and won't get infected. What am I doing wrong?

to get-infected
  ask other turtles [
  if color = gray and turtles-here = ziek
    [set color lime 
      set ziek true]]

end

Upvotes: 1

Views: 55

Answers (1)

Jasper
Jasper

Reputation: 2780

So turtles-here reports an agent set of turtles on the caller's patch (the caller can be another turtle or a patch), per the NetLogo dictionary. As such comparing turtles-here = ziek doesn't make a lot of sense if ziek is true or false, as those values will never be the same (=) an agent set of turtles.

To check if any member of the turtles-here agent set is sick, you'd want to use any? along with the with primitives. I think you want something like this:

to get-infected
  ask other turtles [
    if color = gray and any? turtles-here with [ziek] [
      set color lime 
      set ziek true
    ]
  ]
end

But you could also flip this around - instead of having turtles infect themselves when they see other sick turtles, you can have sick turtles do the infecting. To me this setup makes more sense.

to go
  ; ... other go code to do turtle actions goes here
  ask turtles with [ziek] [
    infect-others
  ]
  ; ... more code here
end

to infect-others
  ask other turtles-here with [not ziek] [
    set color lime 
    set ziek true
  ]
end

Upvotes: 2

Related Questions