user2359494
user2359494

Reputation: 733

Streamline agent behavior in NetLogo

I'm trying to model the avoidance of animal agents from human agents in NetLogo. First, I asked a single predator to avoid people using two behaviors, "wary" and "scared". This worked nicely. But then I asked the prey animals (168 individuals right now but potentially many more) to do the same and the model has slowed down to a snail's pace. As I'm pretty new to NetLogo I'm sure that there is a more efficient way to code this behavior. Any suggestions on how to streamline this process? I'm sure there is a better way to do it. Thanks!

to avoid-people ;; test if people too close to predator and prey and animals moves away if is.

   ask predator [
   ifelse ticks mod 24 >= 5 and ticks mod 24 < 18 [ ;makes sure the animals respond to people during the daytime
   humans-near
   ifelse any? wary
   [ fd 0 ]
   [ ]
   humans-too-near
   if any? scared
   [run-away]
   ] [set wary 0 set scared 0]]

   ask preys [
   ifelse ticks mod 24 >= 5 and ticks mod 24 < 18 [
   humans-near
   ifelse any? wary
   [ fd 0 ]
   [ ]
   humans-too-near
   if any? scared
   [run-away]
   ] [set wary 0 set scared 0]]

end 

;;Humans-near and humans-too-near are functions ;;Alert-distance and flight initiation distance are sliders for the predator but are set values for prey

to humans-near    ;;adds all humans in alert-distance radius of animal to an agent subset for that agent.
   ask predator [
   set wary humans in-radius alert-distance]
   ask preys [
   set wary humans in-radius 10]
end

to humans-too-near  ;;adds all humans in flight-initiation-distance radius of animal to an agent subset for that agent.
   ask predator [
   set scared humans in-radius flight-initiation-distance]
   ask preys [
   set scared humans in-radius 5]
end

to run-away ;;Make animal avoid the human closest to it.
   set nearest-human min-one-of scared [distance myself]
   turn-away ([heading] of nearest-human) max-separate-turn
end

;;this keeps the animals inside the tropical forest and away from human settlement.
;;Max-separate-turn is a slider dictating the angle that the predator runs away from the human

to turn-away [new-heading max-turn]  
   turn-at-most (subtract-headings heading new-heading) max-turn
   ifelse [habitat = typeTrop] of patch-ahead run-distance
   [fd run-distance] [turn-away ([heading] of nearest-human) max-separate-turn]
end

to turn-at-most [turn max-turn]  
   ifelse abs turn > max-turn
   [ ifelse turn > 0
    [ rt max-turn ]
    [ lt max-turn ] ]
   [ rt turn ]
end

Upvotes: 2

Views: 293

Answers (1)

Marzy
Marzy

Reputation: 1894

I did not understand your code, but this is one way to do what you want, I am not sure how agents should behave if they are scared or they move with wary, but you can change these easily :

Breed [predators predator]
Breed [Humans Human]
Breed [Preys Prey]

turtles-own [
  wary
  scared
]


to setup
  Clear-all
  Create-humans 5 [Set color orange set shape "person" move-to patch random 30 random 30]
  Create-Preys 5[Set color white Set shape "Sheep" move-to patch random 30 random 30]
  Create-predators 5 [set color red Set shape "wolf" move-to patch random 30 random 30]
  ask turtles
  [set Wary false
    Set Scared False
  ]
  reset-ticks
end


to go
  ask turtles
  [rt random 5
    fd 0.3]
  avoid-people
  tick
end

to avoid-people
  ifelse is-day?
  [
    ask predators 

    [ if humans-near?
      [
        set wary true
        if humans-too-near? [Set Scared true]
        set label  (word wary "," Scared )
      ]
    ]
    Ask Preys 
    [ if humans-near?
      [
        set wary true
        if humans-too-near? [Set Scared true]
        set label  (word wary "," Scared )
      ]
    ]


  ]


  [; what they should do when its night time
    ] 



end 

to-report humans-too-near?
  report any? humans in-radius 2
end


to-report humans-near?

  report any? humans in-radius 5
end

to-report is-day?
  report (ticks mod 24 >= 5 and ticks mod 24 < 18)
end

*Update:

Your problem was in having 2 ask inside each other , I am glad your model now runs faster.

Upvotes: 1

Related Questions