Netlogo:如何使用路由变量实际沿路径移动
Netlogo: How using the route variable to actually move along the path
我用两种乌龟,汽车和房子。两者都是随机放置的。
我的目标是从组合的路线矢量开始为每辆车获取路线,并让每辆车移动并访问分配给它的每个家庭。
首先,我根据组合路线矢量为每辆车创建一条路线。
我在下面展示我的代码。
但是现在,我正在尝试让汽车按照各自的路线行驶...
globals [ route-vector ]
breed [carr car]
breed [hous housess]
carr-own [ route ]
to setup
clear-all
create-carros
create-casas
make-routes
end
to create-carros
create-carr cars [ set color green ]
ask carr [
set shape "car"
set size 1.5
setxy random-xcor random-ycor
]
end
to create-casas
create-hous house [ set color red ]
ask hous [
set shape "house"
set size 1.5
setxy random-xcor random-ycor
]
end
to make-routes
set route-vector [ 3 4 5 6 7 0 1 2 0 1 ] ;5 10 15 20 25
let houses sublist route-vector 0 (length route-vector / 2 )
let carlist sublist route-vector (length route-vector / 2 ) (length route-
vector)
ask carr [ set route [] ]
(foreach carlist houses
[ [the-car the-house] ->
ask carr with [who = the-car] [ set route lput the-house route ]
]
)
end
to go
ask carr [
;; if at target, choose a new random target
; if distance route = 0
; [ set route one-of route-vector
; face route ]
; ;; move towards target. once the distance is less than 1,
; ;; use move-to to land exactly on the target.
; ifelse distance route < 1
;let mylist [1 2 3]
;foreach route
face route
fd 1
;print map last filter first route
; face housess 3
; fd 1
; move-to one-of route
; fd 1
]
; move-to housess 3
;fd 1
;tick
end
我想使用路线变量实际沿着路径移动。
但我不知道如何通知每辆车各自的路线并让他们移动到他们的家。
我尝试只在一辆车上使用开始按钮
做:
问车1
[
面对路线
1
但总是会出现错误(“FACE 期望输入是一个代理但得到了
改为列出 [4 7]。”)
]
结束
在这种情况下,我想让汽车 1 先移动到 4 号房,然后再移动到 7 号房,然后回到原来的位置 ...
我尝试了几种方法,但找不到解决方案。我试着分开做,我为每辆车选择了 "route" 列表中的第一项,但我仍然不能..
如果有人能帮助我,我将不胜感激。谢谢
使用 who
数字索引海龟可能会导致问题 - 在这种情况下,您将 运行 陷入无法真正动态更新列表的问题,因为 hous
和 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
路线中使用房屋列表:
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
的索引值迭代他们的路线到 select 一个新目标(他们的 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
这仍然不是超级程序化的,因为你依赖于你的汽车订单与你的房屋数量的长度相同,但我不确定你到底想做什么,所以也许它会工作。
编辑
根据 - 如果您 必须 使用 who
号码,您仍然可以使用 "spawn target" 示例来获得海龟return 到他们的起始位置 - 在 carr
和 hous
生成之后就可以了。同样,这绝对不是理想的,因为如果您不注意生成顺序、carr
/ house
的数量等,您的模型可以 'break'
所以,基本 setup
和 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
过程应该可以正常工作,无需任何更改。
编辑 2
让你的车停下来的一个简单方法是设置一个逻辑标志,这样只有满足特定条件的车才会移动。考虑这个修改后的 car-own
和 create-carros
设置:
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
过程中添加一行 "only cars that have route-complete?
set to false, do these actions."
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?
设置为 true, so that the next time
gois called, that
carr`不会移动。
请注意,如果您希望 carr
到 运行 通过他们的路线一定次数,您可以将 route-complete?
更改为计数器而不是 true/false。
我用两种乌龟,汽车和房子。两者都是随机放置的。 我的目标是从组合的路线矢量开始为每辆车获取路线,并让每辆车移动并访问分配给它的每个家庭。 首先,我根据组合路线矢量为每辆车创建一条路线。 我在下面展示我的代码。 但是现在,我正在尝试让汽车按照各自的路线行驶...
globals [ route-vector ]
breed [carr car]
breed [hous housess]
carr-own [ route ]
to setup
clear-all
create-carros
create-casas
make-routes
end
to create-carros
create-carr cars [ set color green ]
ask carr [
set shape "car"
set size 1.5
setxy random-xcor random-ycor
]
end
to create-casas
create-hous house [ set color red ]
ask hous [
set shape "house"
set size 1.5
setxy random-xcor random-ycor
]
end
to make-routes
set route-vector [ 3 4 5 6 7 0 1 2 0 1 ] ;5 10 15 20 25
let houses sublist route-vector 0 (length route-vector / 2 )
let carlist sublist route-vector (length route-vector / 2 ) (length route-
vector)
ask carr [ set route [] ]
(foreach carlist houses
[ [the-car the-house] ->
ask carr with [who = the-car] [ set route lput the-house route ]
]
)
end
to go
ask carr [
;; if at target, choose a new random target
; if distance route = 0
; [ set route one-of route-vector
; face route ]
; ;; move towards target. once the distance is less than 1,
; ;; use move-to to land exactly on the target.
; ifelse distance route < 1
;let mylist [1 2 3]
;foreach route
face route
fd 1
;print map last filter first route
; face housess 3
; fd 1
; move-to one-of route
; fd 1
]
; move-to housess 3
;fd 1
;tick
end
我想使用路线变量实际沿着路径移动。 但我不知道如何通知每辆车各自的路线并让他们移动到他们的家。
我尝试只在一辆车上使用开始按钮
做: 问车1 [ 面对路线 1 但总是会出现错误(“FACE 期望输入是一个代理但得到了 改为列出 [4 7]。”) ] 结束
在这种情况下,我想让汽车 1 先移动到 4 号房,然后再移动到 7 号房,然后回到原来的位置 ... 我尝试了几种方法,但找不到解决方案。我试着分开做,我为每辆车选择了 "route" 列表中的第一项,但我仍然不能..
如果有人能帮助我,我将不胜感激。谢谢
使用 who
数字索引海龟可能会导致问题 - 在这种情况下,您将 运行 陷入无法真正动态更新列表的问题,因为 hous
和 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
路线中使用房屋列表:
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
的索引值迭代他们的路线到 select 一个新目标(他们的 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
这仍然不是超级程序化的,因为你依赖于你的汽车订单与你的房屋数量的长度相同,但我不确定你到底想做什么,所以也许它会工作。
编辑
根据 who
号码,您仍然可以使用 "spawn target" 示例来获得海龟return 到他们的起始位置 - 在 carr
和 hous
生成之后就可以了。同样,这绝对不是理想的,因为如果您不注意生成顺序、carr
/ house
的数量等,您的模型可以 'break'
所以,基本 setup
和 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
过程应该可以正常工作,无需任何更改。
编辑 2
让你的车停下来的一个简单方法是设置一个逻辑标志,这样只有满足特定条件的车才会移动。考虑这个修改后的 car-own
和 create-carros
设置:
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
过程中添加一行 "only cars that have route-complete?
set to false, do these actions."
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?
设置为 true, so that the next time
gois called, that
carr`不会移动。
请注意,如果您希望 carr
到 运行 通过他们的路线一定次数,您可以将 route-complete?
更改为计数器而不是 true/false。