Netlogo:如何从现有(字符串)列表中为变量赋值
Netlogo: How to assign a value to a variable from an existing (string) list
我在尝试为(加盟商)超市分配位置时遇到问题。我有一个每个超市品牌的列表,其中的城市重复次数与该超市品牌在给定城市的门店数量一样多。
但是,使用我一直使用的代码,我无法分配我列表中的确切城市数量,它每次都是随机变化的。
;to create coop franchisees
create-Fs 32 [
set HQ-brand "Coop"
set fcoop-location-outlet ["Zuidlaren" "Zuidlaren" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Ba neveld" "Barneveld" "Doetinchem" "Doetinchem" "Doetinchem" "Zutphen" "Huissen" "Amstelveen" "Amstelveen" "Amstelveen" "Zwolle" "Zwolle" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam"]
]
有了上面的列表后,我尝试了以下方法,为每个加盟商(每个品牌)分配一个城市分店
试用 1
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [ set location-outlet one-of fcoop-location-outlet ]
试验 2
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [ foreach fcoop-location-outlet [set location-outlet one-of fcoop-location-outlet] ]
试用 3
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [ set location-outlet n-of 32 fcoop-location-outlet ]
我认为使用 one-of 我会将 Coop 位置列表中的一项分配给我的 32 个 Coop 网点中的每一个,但它是随机的。对于 n-of 它不起作用,我认为也许 foreach 会起作用,但它与我编码它的一种方式相同。
我如何编写代码,以便每次我都能将列表中的所有项目与城市分配到我的 Coop 网点之一?一定有一个简单的方法可以做到这一点,但我一直在谷歌上搜索它,但无法弄清楚。
(我正在为其他 8 家超市这样做)。
你没有足够的代码让我写一些实际可行的东西。我不想修改列表,所以创建一个迭代器。这样的事情应该有效。它利用 ask
是随机顺序这一事实,因此您不需要随机化列表顺序。我从 -1 开始,所以我可以在分配之前递增(所以它不会离开最后一只乌龟的列表末尾)。
to assign-locations
let iterator -1
ask turtles
[ set iterator iterator + 1
set my-location item iterator fcoop-location-outlet
]
end
我找到方法了!一位朋友帮我解决了这个问题
所以,首先是包含所有网点位置的列表:
fcoop-location-outlet
必须将其设置为全局变量而不是每个插座的变量。
那么,代码应该是这样的:
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [
set location-outlet item 0 fcoop-location-outlet
set fcoop-location-outlet remove-item 0 fcoop-location-outlet
]
这样,添加到 location-outlet 变量的项目(位置)将从主列表中删除,因此不会重复。
我在尝试为(加盟商)超市分配位置时遇到问题。我有一个每个超市品牌的列表,其中的城市重复次数与该超市品牌在给定城市的门店数量一样多。
但是,使用我一直使用的代码,我无法分配我列表中的确切城市数量,它每次都是随机变化的。
;to create coop franchisees
create-Fs 32 [
set HQ-brand "Coop"
set fcoop-location-outlet ["Zuidlaren" "Zuidlaren" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Nijmegen" "Ba neveld" "Barneveld" "Doetinchem" "Doetinchem" "Doetinchem" "Zutphen" "Huissen" "Amstelveen" "Amstelveen" "Amstelveen" "Zwolle" "Zwolle" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam" "Rotterdam"]
]
有了上面的列表后,我尝试了以下方法,为每个加盟商(每个品牌)分配一个城市分店
试用 1
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [ set location-outlet one-of fcoop-location-outlet ]
试验 2
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [ foreach fcoop-location-outlet [set location-outlet one-of fcoop-location-outlet] ]
试用 3
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [ set location-outlet n-of 32 fcoop-location-outlet ]
我认为使用 one-of 我会将 Coop 位置列表中的一项分配给我的 32 个 Coop 网点中的每一个,但它是随机的。对于 n-of 它不起作用,我认为也许 foreach 会起作用,但它与我编码它的一种方式相同。
我如何编写代码,以便每次我都能将列表中的所有项目与城市分配到我的 Coop 网点之一?一定有一个简单的方法可以做到这一点,但我一直在谷歌上搜索它,但无法弄清楚。
(我正在为其他 8 家超市这样做)。
你没有足够的代码让我写一些实际可行的东西。我不想修改列表,所以创建一个迭代器。这样的事情应该有效。它利用 ask
是随机顺序这一事实,因此您不需要随机化列表顺序。我从 -1 开始,所以我可以在分配之前递增(所以它不会离开最后一只乌龟的列表末尾)。
to assign-locations
let iterator -1
ask turtles
[ set iterator iterator + 1
set my-location item iterator fcoop-location-outlet
]
end
我找到方法了!一位朋友帮我解决了这个问题
所以,首先是包含所有网点位置的列表:
fcoop-location-outlet
必须将其设置为全局变量而不是每个插座的变量。
那么,代码应该是这样的:
ask Fs with [(HQ-brand = "Coop") and (location-outlet = 0)] [
set location-outlet item 0 fcoop-location-outlet
set fcoop-location-outlet remove-item 0 fcoop-location-outlet
]
这样,添加到 location-outlet 变量的项目(位置)将从主列表中删除,因此不会重复。