如何在询问其他海龟[ ]块中为自己变量设置值



所以我有这个当前的设置

 ask turtles [                               ;i want this turtle (myself)
    ask other turtles [                      ;to ask other turtle, one by one (self)
      if Smin < Sim myself self [            ;to run a function wherein 
       ifelse Sim myself self < Smax         ;if Smin < Sim myself self < Smax
       [ ;if block ]                         ;self will be assigned to the variable
       [ ;else block ]                       ;of myself called 'Ac'
      ]
    ]
  ]

我该怎么做?

好吧,你可以ask myself [ set Ac myself ],但这有点令人困惑。(每次输入ask块时,myself的指称都会发生变化,因此myself被使用两次来指代两个不同的代理。

我的建议是为您的代理分配更明确的变量名称。 self/myself对于简单的代码来说很方便,但你不必一直使用它们:

ask turtles [              
  let t1 self
  ask other turtles [      
    let t2 self
    if Smin < Sim t1 t2 [  
      ifelse Sim t1 t2 < Smax   
        [ ask t1 [ set Ac t2 ] ]
        [ ] ; do something else?
    ]
  ]
]

相关内容

最新更新