如何使海龟彼此面对,等待3个滴答,然后继续徘徊? [英] How to make turtles face each other, wait 3 ticks and then keep wandering?

查看:126
本文介绍了如何使海龟彼此面对,等待3个滴答,然后继续徘徊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to both, Netlogo and stackoverflow, but your other posts have already helped me a lot.

我目前正在尝试编程一个模型,在该模型中,特工随机漫步一个空间,让他们在见面时停下来.这里的开会"是指彼此in-radius 2".他们应该互相face,等待2个滴答声,然后继续移动直到找到下一个特工.

I am currently trying to program a model, where agents randomly wander a space and have them stop whenever they meet. "Meeting" here means "passing each other in-radius 2". They should face each other, wait for 2 ticks and then keep moving until they find the next agent.

我尝试使用

I tried to use NzHelen's question on a timer, but did not really succeed.

到目前为止,我设法让他们彼此面对.我很难将tick-命令放在代码中的正确位置. (感谢Seth,这是通过删除wait-命令解决的.->而且我不希望所有海龟都停止移动,而只希望彼此相遇的海龟停止移动). 我正在争取的另一件事是他们开会的某种视觉表示,例如,他们开会时让补丁闪烁,或者当他们开会时在他们周围出现一个圆圈.使用wait命令,一切都会再次停止,这是我要防止的.

So far, I managed to have them face each other. I have trouble putting the tick-command at the right place in my code. ( This got solved by taking out the wait-command, thanks to Seth. --> And I don't want all turtles to stop moving, but only the ones which are meeting each other). One other thing which I am striving for is some kind of visual representation of them meeting, for instance have the patch blink for the time when they are meeting or a circle which shows up around them when they meet. With the wait-command, everything stops again, which I want to prevent.

到目前为止,下面的代码.

Below the code so far.

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   

推荐答案

在同事的帮助下,我设法解决了计时器问题.正如Seth所指出的,wait不是正确的命令,太多的to-end -loop也使我的海龟感到困惑.现在,代码如下所示并可以正常工作.海龟彼此靠近,彼此面对,将它们的形状改变为星形,等待三个刻度,然后向相反的方向跳跃.

With the help of a colleague I managed to solve my timer-issue. As Seth pointed out wait was not the right command and too many to-end-loops confused my turtles as well. The code now looks like the following and works. The turtles get close to each other, face each other, change their shape to stars, wait three ticks and then jump in the opposite directions.

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 

这篇关于如何使海龟彼此面对,等待3个滴答,然后继续徘徊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆