Raj*_*Raj 2 charts dynamicquery netlogo
在我的模型中,海龟的数量是动态的,基于用户使用滑块定义的值。滑块可以取 2 到 10 之间的值。每只海龟都有自己的一组坐标和特征,因此我使用以下代码来创建它们。
create-parties 1
[set color red set label-color red set label who + 1 set size 3 setxy party1-left-right party1-lib-con ]
create-parties 1
[set color green set label-color red set label who + 1 set size 3 setxy party2-left-right party2-lib-con ]
if Num-of-parties >= 3
[ create-parties 1
[set color blue set label-color red set label who + 1 set size 3 setxy party3-left-right party3-lib-con ] ]
Run Code Online (Sandbox Code Playgroud)
我重复上述操作直到 Num-of-partys =10。
在其中一个模块中,我创建了一个条件,如果乌龟的某个值达到 0,它就会死亡。
在模型的后面部分,我使用 set-current-plot 使用以下代码创建图表:
set-current-plot "Voter Support"
set-current-plot-pen "Party1"
plot 100 * [my-size] of turtle 0 / sum[votes-with-benefit] of patches
set-current-plot-pen "Party2"
plot 100 * [my-size] of turtle 1 / sum[votes-with-benefit] of patches
if Num-of-parties >= 3 [ set-current-plot-pen "Party3"
plot 100 * [my-size] of turtle 2 / sum[votes-with-benefit] of patches ]
Run Code Online (Sandbox Code Playgroud)
对于所有十种可能的海龟,依此类推。
问题是,如果用户定义了 5 只海龟,而海龟 3 在刻度 10 处死亡,则代码的图表部分会抛出错误,因为没有海龟 3,但用户定义的海龟数量滑块的值为 5 。
请告知如何解决这个问题。谢谢,感谢帮助。
问候
在编写模型代码时,您应该尝试应用 DRY 原则:不要重复自己。单独创建每只乌龟,然后尝试通过分别将它们分别称为turtle 0
、turtle 1
等来对它们执行某些操作,将导致各种问题。您所经历的绘图只是冰山一角。
幸运的是,NetLogo 为您提供了处理“动态”数量的海龟所需的所有设施。ask
是您最常使用的原语,但是还有许多其他处理整个代理集的原语。您可以在编程指南中阅读有关主体集的更多信息。
在策划的情况下,你可以ask
为双方创建一支“临时策划笔”。我们将使用该who
编号为每支笔指定一个唯一的名称。who
(这是NetLogo 中该数字极少数合法用途之一。)
将此代码放入绘图的“绘图设置命令”字段中:
ask parties [
create-temporary-plot-pen (word "Party" (who + 1))
set-plot-pen-color color ; set the pen to the color of the party
]
Run Code Online (Sandbox Code Playgroud)
(请注意,您将不再需要之前定义的绘图笔:您可以将它们删除。每次设置绘图时都会动态创建新的绘图笔。)
为了进行实际的绘图,我们可以使用非常相似的代码。将此代码放入绘图的“绘图更新命令”字段中:
ask parties [
set-current-plot-pen (word "Party" (who + 1))
plot 100 * my-size / sum [ votes-with-benefit ] of patches
]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
421 次 |
最近记录: |