Using who
索引海龟的数字可能会导致问题 - 在这种情况下,您将遇到无法真正动态更新列表的问题,因为hous
and carr
数字仅基于其创建顺序。如果可能的话,最好将海龟直接存储在列表中。使用您的设置的修改版本查看此示例:
globals [ route-vector ]
breed [carr car]
breed [hous housess]
breed [spawns spawn]
carr-own [ route route-counter spawn-target target]
to setup
clear-all
create-carros
create-casas
make-routes
reset-ticks
end
to create-carros
create-carr 3 [ set color green ]
ask carr [
set size 1.5
setxy random-xcor random-ycor
; set up a 'route-counter' to act as an index for a car's route
set route-counter 0
set target nobody
set route []
pd
]
; hatch a 'spawn-target' turtle that can be used to return
; the carr back to their starting position
ask carr [
hatch 1 [
set breed spawns
ht
]
set spawn-target one-of other turtles-here with [
xcor = [xcor] of myself
]
]
end
to create-casas
create-hous 5 [ set color red ]
ask hous [
set shape "house"
set size 1.5
setxy random-xcor random-ycor
]
end
现在,不再依赖who
数字来索引房屋,直接使用您的房屋列表carr
routes:
to make-routes
; Just use the car-order
let car-order [ 0 1 2 0 1 ]
; Create a list of hous directly by sorting them
let houses sort hous
; Your same foreach procedure, but in this case the carr
; are storing the house in the list directly to avoid
; indexing problems
ask carr [ ]
(foreach car-order houses
[ [the-car the-house] ->
ask carr with [who = the-car] [ set route lput the-house route ]
]
)
end
然后,carr
可以迭代他们的路线来根据索引值选择一个新目标route-counter
(他们有一个小休息时间spawn-target
).
to go
ask carr [
; If a car has no target, set the target to the
; item indexed by 'route-counter'
if target = nobody [
set target item route-counter route
]
; Movement chunk
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
; Only advance the route counter if the current target
; was not the original spawn point
if target != spawn-target [
set route-counter route-counter + 1
]
set target nobody
]
; If the route counter would index outside of the
; list boundaries, reset it to 0
if route-counter > length route - 1 [
set route-counter 0
set target spawn-target
]
]
tick
end
这仍然不是超级程序化,因为您依赖于您的汽车订单与您的房屋数量相同的长度,但我不确定您实际上想要做什么,所以也许它会起作用。
EDIT
As per 你的评论 https://stackoverflow.com/questions/47730424/netlogo-how-using-the-route-variable-to-actually-move-along-the-path/47735178?noredirect=1#comment82473779_47735178- 如果你must使用who
数字,您仍然可以使用“生成目标”示例让海龟返回到其起始位置 - 只需让它在carr
and hous
已经产生。再说一遍,这绝对不理想,因为如果您不小心生成顺序、数量,您的模型可能会“崩溃”carr
/ house
, etc.
如此基础setup
and create-casas
程序如上所述,以此作为您的新程序create-carros
程序:
to create-carros
create-carr 3 [ set color green ]
ask carr [
set size 1.5
setxy random-xcor random-ycor
; set up a 'route-counter' to act as an index for a car's route
set route-counter 0
set target nobody
set route []
pd
]
end
现在,你的make-routes
可以包含spawn
目标海龟(此示例具有您评论中的乱序房屋):
to make-routes
set route-vector [4 7 6 3 5 0 1 2 0 1]
let houses sublist route-vector 0 (length route-vector / 2 )
let carlist sublist route-vector (length route-vector / 2 ) (length route-vector)
(foreach carlist houses
[ [the-car the-house] ->
ask car the-car [
set route lput ( housess the-house ) route
]
]
)
; hatch a 'spawn-target' turtle that can be used to return
; the carr back to their starting position
ask carr [
hatch 1 [
set breed spawns
ht
]
set spawn-target one-of other turtles-here with [
xcor = [xcor] of myself
]
]
end
然后,go
上面的过程应该可以正常工作,无需任何更改。
EDIT 2
让你的 carr 停止的一个简单方法是设置一个逻辑标志,以便只有满足特定条件的 carr 才会移动。考虑这个修改car-own
and create-carros
setup:
carr-own [ route route-counter spawn-target target route-complete? ]
to create-carros
create-carr 3 [ set color green ]
ask carr [
set size 1.5
setxy random-xcor random-ycor
; set up a 'route-counter' to act as an index for a car's route
set route-counter 0
set target nobody
set route []
set route-complete? false
pd
]
end
在这里,我们现在有一个名为的布尔(逻辑)变量route-complete?
,设置为false
对于所有新车。现在,您可以将一行添加到go
程序说“只有具有route-complete?
设置为 false,执行这些操作。”
to go
; ask carr with route-complete set to false
ask carr with [ not route-complete? ] [
; If a car has no target, set the target to the
; item indexed by 'route-counter'
if target = nobody [
set target item route-counter route
]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
; Only advance the route counter if the current target
; was not the original spawn point. ADDITIONALLY,
; if the target is the starting target, set route-complete?
; to true for that carr
ifelse target != spawn-target [
set route-counter route-counter + 1
] [
set route-complete? true
]
set target nobody
]
; If the route counter would index outside of the
; list boundaries, reset it to 0
if route-counter > length route - 1 [
set route-counter 0
set target spawn-target
]
]
tick
end
你会注意到有一个修改过的位move-to
块如果carr
正在移回到其起始位置,它也设置了它的route-complete?
to true, so that the next time
gois called, that
卡尔不会动。
请注意,您可以更改它route-complete?
如果你愿意的话,可以到计数器而不是真/假carr
跑过他们的路线一定次数。