0

我对 Netlogo 和 stackoverflow 都是新手,但是您的其他帖子已经对我有很大帮助。

我目前正在尝试编写一个模型,其中代理随机游荡一个空间并让他们在遇到时停下来。“相遇”在这里的意思是“互相擦肩而过in-radius 2”。他们应该face互相,等待 2 个滴答声,然后继续移动,直到找到下一个代理。

我试图在计时器上使用 NzHelen 的问题,但并没有真正成功。

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

到目前为止的代码下方。

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   
4

2 回答 2

1

在一位同事的帮助下,我设法解决了我的计时器问题。正如 Seth 指出的那样wait,这不是正确的命令,太多的to-end-loops 也让我的乌龟感到困惑。代码现在如下所示并且可以正常工作。海龟靠得很近,面对面,变成星星,等三个滴答声,然后朝相反的方向跳跃。

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 
于 2014-03-10T17:49:50.250 回答
0

您链接到 Nzhelen 的问题是正确的。基本上你的问题的答案是你需要做同样的事情。当您尝试这样做时,您就走在了正确的轨道上。我建议再试一次,如果你卡住了,请告诉我们你卡在哪里。

于 2014-03-09T13:31:14.133 回答