我非常喜欢在Netlogo中创建链接。
基本上我想在Netlogo中的两个不同品种之间添加一个单一链接,其中定义了长度'粒子长度'。此链接需要连接到其旁边的其他品种。 目前,此代码创建了一组体积独特的海龟,有3个品种。一个是随机分散的,另外两个是在一个盒子里面。我认为盒子内两种不同品种的初始排列也需要固定,所以每个绿色旁边都有紫色。 最终我需要50个绿色(大)和50个紫色(小),它们通过一个链接(绿色到紫色)连接,链接长度为粒子长度为1.
注意water-number,Big-number和small-number是在值为50的滑块上定义的。
Netlogo代码:
breed [Bigs Big]
breed [Smalls Small]
breed [ waters water ]
to setup
clear-all
set particle-length 1.0
set-default-shape turtles "square"
;;To set up the bigs and smalls inside a central box
ask patches with [ abs (min-pxcor - pxcor) > 22
and abs (min-pxcor - pxcor) < 23 + floor (sqrt ( Big-number + Small-number ))
and abs (max-pycor - pycor) > 22
and abs (max-pycor - pycor) < 23 + floor (sqrt (Big-number + Small-number))]
[sprout-Bigs 1 [ set color green ] ]
;;Now make half of the Bigs into Smalls
ask n-of Big-number Bigs [ set breed Smalls]
ask Smalls [set color violet]
;;randomly makes volume exclusive Waters where there are no Bigs or Smalls
ask n-of water-number patches with [ not any? turtles-here]
[sprout-waters 1 [ set color blue ]]
ask Bigs with [any? Smalls-on neighbors4] [create-link-with one-of Smalls-on neighbors4]
reset-ticks
end
答案 0 :(得分:2)
抛出错误的代码是
询问n-of(Big-number / 2)Bigs [create-link-with-of-Smalls-on neighbors4]
一个简单的原因是,至少有一个被问到的大个子在邻居身上没有什么小事.4。
将其更改为
ask n-of ( Big-number / 2 ) Bigs
[
If any? Smalls-on neighbors4
[create-link-with one-of Smalls-on neighbors4]
]
会阻止错误,但可能不是您想要的行为。可替代地
询问(大数/ 2)Bigs与[任何?小型邻居4] [创建链接与一个小型邻居4]