Netlogo - 根据曼哈顿距离找到最近的代理
Netlogo - find the closest agent based on Manhattan distance
我正在为大型仓库操作建模(见下图)。
我在每个补丁(绿点)和 link 上实现了一个顶点,这样我就可以计算从每个顶点到所有其他顶点的最短路径(使用 dijkstra 算法),存储在 table(字典)每个顶点。此过程在设置阶段完成,非常耗时。然后黄色库存单元格(矩形)将发出任务完成的需求请求。
叉车(路径中的一些)假定在不忙时停留在顶点(节点)之一上。当得到需求请求时,它会要求它当前的站立顶点(节点)得到它要去的库存单元格的starting_node和end_node。然后叉车要求starting_node从它的table得到最短路径得到最短路径(节点组合)并开到end_node(黄色单元格)。
目前,黄色格子只是在仓库中随机挑选免费的叉车。作为进一步的开发,我想添加允许黄色单元格根据实际距离(而不是 Netlogo 中内置的欧氏距离)识别离它最近的免费卡车的功能。由于有很多卡车,所以使用 "foreach" 遍历所有可用卡车并使用上述方法计算实际距离并不好。您可以使用原始 "distance myself" 来快速定位卡车,但这并不准确。有没有一种计算速度更快的识别最近卡车的好方法?
我不知道这是否适用于您当前的设置,但您可能还会发现 nw
扩展有帮助。例如,试试这个设置(我为长度道歉,只是近似你的世界):
extensions [ nw ]
breed [ nodes node ]
breed [ trucks truck ]
breed [ cells cell ]
to setup
ca
resize-world -29 29 -14 14
ask patches with [ ( pxcor mod 5 = 0 or pycor = 0 ) ] [
set pcolor grey
sprout-nodes 1 [
set size 0.5
set shape "circle"
set color green
]
]
ask nodes [
create-links-with turtles-on neighbors4 [
set color green
]
]
ask n-of 5 nodes [
hatch 1 [
set size 1
set color red
set breed trucks
set shape "car"
]
]
ask n-of 2 trucks [
set color blue
]
ask one-of nodes [
ask one-of neighbors with [ pcolor = black ] [
sprout-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
]
reset-ticks
end
给出一个像这样的简单世界:
您可以使用 nw
扩展来即时计算距离,而不是将它们存储在每个单元格中 table,并让叉车(此处为卡车)跟随最短距离小路。更多详情见评论:
to summon-nearest-truck
; Treat blue trucks as 'empty' and ready to go to a cell
let ready-trucks trucks with [ color = blue ]
; Get the nodes associated with those ready trucks
let truck-proxies turtle-set map [ i -> [nodes-on patch-here] of i ] sort ready-trucks
; Randomly ask one of the cells to
ask one-of cells [
; Choose one of the neighboring road nodes as a proxy for distance calculation
let node-proxy one-of nodes-on neighbors4
; Get the node proxy to:
ask node-proxy [
; Find the nearest (by network distance) trucks, and select the node
; located on the same patch as that truck
let my-truck-proxy min-one-of truck-proxies [length ( nw:turtles-on-path-to myself) ]
; Get that truck-proxy node to generate the path to the original asking node
; and have the appropriate truck follow that path
ask my-truck-proxy [
; Generate the path to follow
let path-to-follow nw:turtles-on-path-to myself
; Highlight that path for visualization
ask turtle-set path-to-follow [
set color orange
]
; Get the truck ready to move
let ready-truck one-of trucks-here with [ color = blue ]
ask ready-truck[
set color yellow
]
; Move that truck along the path-to-follow
ask ready-truck [
foreach path-to-follow [
n ->
face n
move-to n
; For visualization only
wait 0.1
]
]
]
]
]
end
根据返回的 nw:turtles-on-path-to
的长度,黄色框会召唤最近的卡车。那辆卡车沿着通往召唤节点的路径:
我正在为大型仓库操作建模(见下图)。
我在每个补丁(绿点)和 link 上实现了一个顶点,这样我就可以计算从每个顶点到所有其他顶点的最短路径(使用 dijkstra 算法),存储在 table(字典)每个顶点。此过程在设置阶段完成,非常耗时。然后黄色库存单元格(矩形)将发出任务完成的需求请求。
叉车(路径中的一些)假定在不忙时停留在顶点(节点)之一上。当得到需求请求时,它会要求它当前的站立顶点(节点)得到它要去的库存单元格的starting_node和end_node。然后叉车要求starting_node从它的table得到最短路径得到最短路径(节点组合)并开到end_node(黄色单元格)。
目前,黄色格子只是在仓库中随机挑选免费的叉车。作为进一步的开发,我想添加允许黄色单元格根据实际距离(而不是 Netlogo 中内置的欧氏距离)识别离它最近的免费卡车的功能。由于有很多卡车,所以使用 "foreach" 遍历所有可用卡车并使用上述方法计算实际距离并不好。您可以使用原始 "distance myself" 来快速定位卡车,但这并不准确。有没有一种计算速度更快的识别最近卡车的好方法?
我不知道这是否适用于您当前的设置,但您可能还会发现 nw
扩展有帮助。例如,试试这个设置(我为长度道歉,只是近似你的世界):
extensions [ nw ]
breed [ nodes node ]
breed [ trucks truck ]
breed [ cells cell ]
to setup
ca
resize-world -29 29 -14 14
ask patches with [ ( pxcor mod 5 = 0 or pycor = 0 ) ] [
set pcolor grey
sprout-nodes 1 [
set size 0.5
set shape "circle"
set color green
]
]
ask nodes [
create-links-with turtles-on neighbors4 [
set color green
]
]
ask n-of 5 nodes [
hatch 1 [
set size 1
set color red
set breed trucks
set shape "car"
]
]
ask n-of 2 trucks [
set color blue
]
ask one-of nodes [
ask one-of neighbors with [ pcolor = black ] [
sprout-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
]
reset-ticks
end
给出一个像这样的简单世界:
您可以使用 nw
扩展来即时计算距离,而不是将它们存储在每个单元格中 table,并让叉车(此处为卡车)跟随最短距离小路。更多详情见评论:
to summon-nearest-truck
; Treat blue trucks as 'empty' and ready to go to a cell
let ready-trucks trucks with [ color = blue ]
; Get the nodes associated with those ready trucks
let truck-proxies turtle-set map [ i -> [nodes-on patch-here] of i ] sort ready-trucks
; Randomly ask one of the cells to
ask one-of cells [
; Choose one of the neighboring road nodes as a proxy for distance calculation
let node-proxy one-of nodes-on neighbors4
; Get the node proxy to:
ask node-proxy [
; Find the nearest (by network distance) trucks, and select the node
; located on the same patch as that truck
let my-truck-proxy min-one-of truck-proxies [length ( nw:turtles-on-path-to myself) ]
; Get that truck-proxy node to generate the path to the original asking node
; and have the appropriate truck follow that path
ask my-truck-proxy [
; Generate the path to follow
let path-to-follow nw:turtles-on-path-to myself
; Highlight that path for visualization
ask turtle-set path-to-follow [
set color orange
]
; Get the truck ready to move
let ready-truck one-of trucks-here with [ color = blue ]
ask ready-truck[
set color yellow
]
; Move that truck along the path-to-follow
ask ready-truck [
foreach path-to-follow [
n ->
face n
move-to n
; For visualization only
wait 0.1
]
]
]
]
]
end
根据返回的 nw:turtles-on-path-to
的长度,黄色框会召唤最近的卡车。那辆卡车沿着通往召唤节点的路径: