在Netlogo中使用列表上的索引位置
我正在使用这些变量解决以下问题:在Netlogo中使用列表上的索引位置,netlogo,Netlogo,我正在使用这些变量解决以下问题: set variablex .5 set threshhold-list [0 .3 .6] set variable-list [0 0 1] 我有三个AgentType 0、1、2,它们对应于Threshold list和variable list的索引位置。代理0有阈值0和变量0,代理1有阈值3和变量0,代理2有阈值6和变量1 我想做的是检查是否有任何代理的阈值大于零且小于variablex。如果是,请将变量列表中该代理的变量更新为variablex。也
set variablex .5
set threshhold-list [0 .3 .6]
set variable-list [0 0 1]
我有三个AgentType 0、1、2,它们对应于Threshold list和variable list的索引位置。代理0有阈值0和变量0,代理1有阈值3和变量0,代理2有阈值6和变量1
我想做的是检查是否有任何代理的阈值大于零且小于variablex。如果是,请将变量列表中该代理的变量更新为variablex。也就是说,对于上面的变量,我想运行逻辑,生成一个新的变量列表,如下所示:
variable-list [0 .5 1]
但如果variablex为0.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
下面是一个有点冗长的测试,用于检查两个版本是否都能提供预期的结果:
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
globals[
变量x
]
乌龟自己的[
脱粒
变量
]
设置
清除所有
设置变量-x.5
(foreach[0.3.6][0.1][[t v]>
创造海龟1[
设置阈值
设置变量v
]
])
询问海龟[更新变量]
问海龟[显示变量]
结束
更新变量;海龟手术
如果threshold>0且threshold
我不知道你们最终想要实现什么,但如果我能提供一般性的建议,那就是试着接受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