3
的模擬通過使用網絡擴展,下面的代碼生成兩個多邊形之間的最小成本路徑(幾個補丁組成):我申請我怎樣才能提高加速我的最低成本路徑模型
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
這個代碼有兩個在圖中用黑色矩形表示的多邊形。通過使用profiler擴展,這段代碼運行了14分鐘。
對於每一個狼,我想建立一個多邊形,其中有狼和坐落在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 ] ] ]
的問題是,我的方法「計算-LCP」運行十分緩慢繞狼(我在我的模型百隻狼)緩衝區來定義最小代價路徑。我怎樣才能提高模型的速度?
非常感謝您的幫助。
謝謝布萊恩!最後,我使用新版本的NW擴展,我的代碼運行速度比我舊版本要快得多。我在代碼中刪除了nw:set-snapshot turtles鏈接。但是,調用nw:set-context turtles鏈接是否很重要?目前,我沒有將它添加到我的代碼中,我的程序「calculate-LCP」仍然有效。 – Marine
很高興新版本加快了速度!只要你使用'turtles'和'links'(或任何其他品種集)作爲你的上下文,你就不需要再次調用'set-context'。 –
我剛剛在 的「構建最低成本路徑」一節中添加了代碼行:'用[color = red]'查詢節點。我不明白爲什麼我又遇到了同樣的問題?我保留了NW-extension的新版本,並在我的代碼中刪除了nw:set-snapshot turtles鏈接。謝謝你的幫助。 – Marine