Search code examples
netlogo

checking condition for two different breed of agents at once


I have two agents mosquito and human. they both have different states like susceptible and infected. I want to check states for my agents and do the action I want to check if mosquito is infected and human susceptible then infect human and vice versa. please help on the codes

to go
  ask human
  [
    set heading  random 360
    forward random 2
      
      ]
  **if any? human-here infected? true and any? mosquito-here infected? false
ask mosquito [ set infected? true
    set color red]
  tick** 
end

Solution

  • You first need to decide which agents you want to ask to take the action necessary to infect or be infected. For that, I suggest using the "with" primitive that allows you to create a subset of the agentsets for which it makes sense to check if they can be infected. humans with [susceptible? = true and infected? = false]

    After that you can ask this group to check for the conditions necessary to become infected themselves. if any? mosquitos-here with [infected? = true] [set infected? true]

    I suggest doing this separately for both humans and mosquitos as your conditions might differ for both. Combined, your code could look something like this:

    to go
      ask humans
      [
        set heading  random 360
        forward random 2
          ]
      
      ask mosquitos
      [
        set heading  random 360
        forward random 2
          ]  
      
      ask humans with [susceptible? = true and infected? = false] [
        if any? mosquitos-here with [infected? = true] [set infected? true set color red]
    ]
      
      ask mosquitos with [susceptible? = true and infected? = false] [
        if any? humans-here with [infected? = true] [set infected? true set color red]
    ]
      
      tick
    end
    

    If infection happens in a symmetric way, you could also use something like the following but I wouldn't suggest it since I assume you want some distinction between humans and mosquitos

      ask turtles with [susceptible? = true and infected? = false] [
        let my-breed breed
        if any? turtles-here with [infected? = true and breed != my-breed] [set infected? true set color red]
    ]