gpt4 book ai didi

netlogo - 我怎样才能加快我的最低成本路径模型的模拟速度

转载 作者:行者123 更新时间:2023-12-04 01:37:06 26 4
gpt4 key购买 nike

通过使用网络扩展,以下代码在两个多边形(由多个面片组成)之间构建成本最低的路径:

to calculate-LCP [ID-polygon-1 ID-polygon-2] 
let path []
let path-cost -1

;;;;;;;;;;;;;;;;;;;;;;;;
;; Define polygon edges
ask patches with [plabel != ID-polygon-1] [
ask neighbors with [plabel = ID-polygon-1] [
ask nodes-here [
set color red ] ] ]

ask patches with [plabel != ID-polygon-2] [
ask neighbors with [plabel = ID-polygon-2] [
ask nodes-here [
set color red ] ] ]

;;;;;;;;;;;;;;;;;;;;;;;;
;; Build least-cost path
ask nodes with [color = red] [
foreach sort nodes-on patches with [ID-polygon = ID-polygon-1] [
let node-on-polygon-1 ?
foreach sort nodes-on patches with [ID-polygon = ID-polygon-2] [
let node-on-polygon-2 ?

ask node-on-polygon-1 [
let cost nw:weighted-distance-to node-on-polygon-2 "link-cost"
if path-cost = -1 or cost < path-cost [
set path-cost cost
set path nw:weighted-path-to node-on-polygon-2 "link-cost" ] ] ] ] ]

;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Draw least-cost path
foreach path [ ;; trace le least-cost path
ask ? [ set color red
set thickness 0.2 ] ]
end

我将此代码应用于两个多边形,这两个多边形在图中由黑色矩形表示。通过使用探查器扩展,此代码运行了 14 分钟。

enter image description here

对于每只狼,我想在有狼的多边形和位于狼周围 3 公里半径范围内的所有多边形之间建立成本最低的路径。这是我的代码:

ask wolves [ 
set my-list-of-polygons-in-buffer ( [plabel] of patches in-radius 3 )
set my-list-of-polygons-in-buffer remove-duplicates my-list-of-polygons-in-buffer
set my-list-of-polygons-in-buffer remove [plabel] of patch-here my-list-of-polygons-in-buffer
set my-list-of-polygons-in-buffer remove "" my-list-of-polygons-in-buffer

foreach my-list-of-polygons-in-buffer [
let ID-polygon-in-buffer ?

ask patches with [plabel = ID-polygon-in-buffer] [

let LCP calculate-LCP [my-ID-polygon] of myself ID-polygon-in-buffer ] ] ]

问题是我的程序“calculate-LCP”运行速度太慢,无法在狼群周围的缓冲区中定义成本最低的路径(我的模型中有 100 只狼)。如何提高模型的仿真速度?

非常感谢您的帮助。

最佳答案

您只需在首次设置网络时调用一次nw:set-snapshot turtles links。如果任何权重发生变化,或者添加或删除任何链接或节点,您将需要再次调用它。这应该会极大地加快您的代码速度。

关于netlogo - 我怎样才能加快我的最低成本路径模型的模拟速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com