简化NetLogo中的代理行为

时间:2013-12-12 05:10:34

标签: netlogo

我正试图在NetLogo中模拟从人类代理中避免使用动物因子。首先,我问一个单一的捕食者,以避免人们使用两种行为,“警惕”和“害怕”。这很好用。但后来我问猎物动物(现在有168个人,但可能还有更多)做同样的事情,模型已经放慢了蜗牛的速度。由于我对NetLogo很陌生,我确信有一种更有效的方法来编码这种行为。有关如何简化此流程的任何建议?我确信有更好的方法来做到这一点。谢谢!

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 

;;人类附近和人类太近是功能 ;;警报距离和飞行起始距离是捕食者的滑块,但是是猎物的设定值

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

;;这使动物保持在热带森林中,远离人类居住 ;; Max-separate-turn是一个滑块,指示捕食者远离人类的角度

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

1 个答案:

答案 0 :(得分:1)

我不明白你的代码,但这是你想做的事情的一种方式,我不确定代理人如果害怕或者他们谨慎行事应该如何表现,但你可以轻易地改变这些:

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

*更新:

你的问题是让对方互相询问,我很高兴你的模型现在运行得更快。