如何让乌龟面对面,等待3个蜱然后继续徘徊?

时间:2014-03-05 10:23:16

标签: timer netlogo interaction agent-based-modeling

我对Netlogo和stackoverflow都很陌生,但你的其他帖子已经帮了我很多。

我目前正在尝试编写一个模型,代理人随机漫游一个空间并让它们在遇到时停止。 “会议”在这里意味着“相互传递in-radius 2”。他们应该face彼此,等待2个滴答,然后继续前进,直到找到下一个代理。

我尝试使用NzHelen's question on a timer,但没有真正成功。

到目前为止,我设法让他们面对面。我无法将tick - 命令放在我的代码中的正确位置。 (编辑:通过取出wait - 命令解决了这个问题,感谢Seth。 - >而且我不希望所有的海龟都停止移动,而只是那些彼此相遇的海龟。 我正在努力的另一件事是他们会面的某种视觉表现形式,例如当他们见面时补丁闪烁或者当他们见面时出现在他们周围的圆圈。使用wait - 命令,一切都会再次停止,我想阻止它。

到目前为止的代码下方。

to go 

  tick  

  ask turtles 
  [
   wander
   find-neighbourhood
  ] 

 ask turtles with [found-neighbour = "yes"]
  [
    face-each-other
  ]

 ask turtles with [found-neighbour = "no" or found-neighbour = "unknown"]
 [ wander ]

  end

;-------
;Go commands      

to wander
      right random 50
      left random 50
      forward 1   
end 

 to find-neighbourhood
     set neighbourhood other turtles in-radius 2
     if neighbourhood != nobody [wander]
     find-nearest-neighbour
  end 

  to find-nearest-neighbour
  set nearest-neighbour one-of neighbourhood with-min [distance myself]
  ifelse nearest-neighbour != nobody [set found-neighbour "yes"][set found-neighbour "no"]
            end 

to face-each-other                             ;;neighbour-procedure
  face nearest-neighbour
  set found-neighbour "no"
  ask patch-here [                             ;; patch-procedure
    set pcolor red + 2
    ;wait 0.2
    set pcolor grey + 2
        ]
    if nearest-neighbour != nobody [wander]
  rt 180
  jump 2

  ask nearest-neighbour 
[
    face myself 
    rt 180
    jump 2
    set found-neighbour "no"
  ]  
  end   

2 个答案:

答案 0 :(得分:1)

在同事的帮助下,我设法解决了我的计时器问题。正如Seth所指出的那样wait不是正确的命令而且to-end太多了 - 循环也困扰了我的乌龟。代码现在看起来像以下和工作。海龟彼此靠近,彼此面对,将它们的形状改变为星星,等待三个蜱,然后向相反方向跳跃。

to setup

  clear-all 
 ask turtles 
   [
     set count-down 3
     ]
reset-ticks

end 
;---------
to go

 ask turtles 
  [    
   if occupied = "yes" [
     ifelse count-down > 0 
[
       set count-down (count-down - 1)
       set shape "star"
     ][
       set shape "default"
       rt 180
       fd 2
       set occupied "no"
       set count-down 3
     ] 
   ]

   if occupied = "no" [
     ; Wandering around, ignoring occupied agents

     set neighbourhood other turtles in-radius 2

     ; If someone 'free' is near, communicate!

     set nearest-neighbour one-of neighbourhood with-min [distance myself]
     ifelse nearest-neighbour != nobody [
         if ([occupied] of nearest-neighbour = "no") [
            face nearest-neighbour            
            set occupied "yes"
            ask nearest-neighbour [ 
              face myself              
              set occupied "yes"
           ]]
     ][
       ; No one found, keep on wandering
       wander
     ]]] 
   tick 
   end
;-------
;Go commands      

to wander
      right random 50
      left random 50
      forward 1   
end 

答案 1 :(得分:0)

您可以链接到Nzhelen的问题。基本上你的问题的答案是你需要做同样的事情。当你试图这样做时,你是在正确的轨道上。我建议再试一次,如果你遇到困难,请告诉我们你到底陷入困境的地方。