海龟在netlogo中找到合作伙伴

时间:2017-04-06 19:50:53

标签: netlogo

我正在创建一个代码,海龟需要找到合作伙伴。这是程序:

patches-own [occupied?]
turtles-own [partner fed?]

to find-partners
 let singles turtles with [partner = nobody]
 if not any? singles [ stop ]

  ask singles  
  [ lt random 50 
    rt random 50
    fd 1 ]

 ask turtles 
 [
  if (partner = nobody) and (any? other turtles in-radius 1 with [partner = nobody])
  [ set partner one-of other turtles in-radius 1 with [partner = nobody] 

    ask partner [ 
    set partner myself

]]]

end

我希望这个程序在1个刻度中进行,但需要约500个。我怎么能纠正这个?

1 个答案:

答案 0 :(得分:1)

很难说没有你的设置,但似乎while就是你想要的。尝试将if not any? singles [stop]替换为while any? singles [

这个想法是,虽然有任何单打,但继续运行这个循环。要小心,因为如果"同时"条件永远不会满足,模型将卡在while循环中。因此,您还需要在while循环中包含行set singles turtles with [ partner = nobody ]。总而言之,就像

一样
to find-partners
     let singles turtles with [partner = nobody]
     while [ any? singles ] [
      ask singles  
      [ lt random 50 
        rt random 50
        fd 1 ]

     ask turtles 
     [
      if (partner = nobody) and (any? other turtles in-radius 1 with [partner = nobody])
      [ set partner one-of other turtles in-radius 1 with [partner = nobody] 

        ask partner [ 
        set partner myself

    ]]]

    set singles turtles with [partner = nobody]

    ]

end