我正在使用这些变量解决以下问题:
set variablex .5
set threshhold-list [0 .3 .6]
set variable-list [0 0 1]
我有三个agenttypes 0,1,2,分别对应threshhold-list和variable-list的索引位置。所以智能体 0 的阈值为 0 和变量 0,智能体 1 的阈值为 .3 和变量 0,智能体 2 的阈值为 .6 和变量 1。
我想做的是检查是否有任何代理的阈值大于零且小于变量 x。如果是,则将变量列表中该代理的变量更新为 variablex。也就是说,对于上面的变量,我想运行产生一个像这样的新变量列表的逻辑:
variable-list [0 .5 1]
但是如果变量 x 是 .7,它会产生 [0 .7 .7]。
我有一些我一直在破解的代码,但我觉得它比问题复杂得多,所以我想知道是否有人可以为我指明正确的方向。非常感谢!
最佳答案
有几种不同的方法来解决这个问题,但如果我遇到你的情况,我会先写一个小记者,给我应该存储在每个索引中的值:
to-report new-value [ i ]
let t item i threshhold-list
report ifelse-value (t > 0 and t < variablex)
[ variablex ] ; the variable's new value should be variable x
[ item i variable-list ] ; the variable's value should not change
end
一旦你有了它,你就可以使用 foreach
或 map
来改变你的变量列表:
to update-variables-with-foreach
foreach range length variable-list [ i ->
set variable-list replace-item i variable-list new-value i
]
end
to update-variables-with-map
set variable-list map new-value range length variable-list
end
这是一个有点冗长的测试,用于检查两个版本是否都能给您带来预期的结果:
globals [
variablex
threshhold-list
variable-list
]
to test
clear-all
set threshhold-list [0 .3 .6]
set variablex .5
set variable-list [0 0 1]
update-variables-with-foreach
print variable-list
set variablex .5
set variable-list [0 0 1]
update-variables-with-map
print variable-list
set variablex .7
set variable-list [0 0 1]
update-variables-with-foreach
print variable-list
set variablex .7
set variable-list [0 0 1]
update-variables-with-map
print variable-list
end
话虽这么说,尽管我认为玩列表很有趣,但我认为您正在以一种非常不符合网络标志的方式来解决您的问题。
NetLogo 的世界是海龟、补丁和链接的世界,而不是数组、索引和数字的世界。
你可以按照以下方式做一些事情:
globals [
variable-x
]
turtles-own [
threshhold
variable
]
to setup
clear-all
set variable-x .5
(foreach [0 .3 .6] [0 0 1] [ [t v] ->
create-turtles 1 [
set threshhold t
set variable v
]
])
ask turtles [ update-variable ]
ask turtles [ show variable ]
end
to update-variable ; turtle procedure
if threshhold > 0 and threshhold < variable-x [
set variable variable-x
]
end
我不知道你最终想要达到什么目的,但如果我能提供一般性建议,那就是尝试接受 NetLogo 的思维方式。每次您想要在您的代码中使用某种索引时,请退后一步并重新思考:可能有更好的(如“more netlogoish”)方式来做到这一点。
关于netlogo - 在 Netlogo 中处理列表中的索引位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306103/