我正在决策ABM工作,我的海龟必须根据适合做出最佳决策的斑块的特征和可用性来确定最佳的土地利用变化策略。然而,我的代码需要花费很多时间来处理代理的决定。
这是潜在土地用途变化的列表:
set landuselist [
;; in the list
;; +------------------------ landusechange
;; | +------------------ cost
;; | | +------------- expectedprofit
;; | | | +---------- maize
;; | | | | +----- expectedprofit(formodification)
;; | | | | |
[ "MEA1" 200 450 6 450 ] ;; maintenance of mechanized agriculture in patches with high suitability for mechanized agriculture
[ "TRA1" 50 350 3 350 ] ;; maintenance of traditional agriculture to traditional agriculture in patches with high suitability for traditional agriculture
[ "CAT1" 100 350 0 350 ] ;; maintenance of cattle ranching in any suitability except for null
[ "AGF1" 50 600 1 600 ] ;; Maintenance of agroforestry with an age over or equal to 6 year on suitability over null for agroforestry
[ "MEA2" 200 450 6 450 ] ;; conversion of forest to mechanized agriculture in patches with high suitability for mechanized agriculture
[ "TRA2" 100 300 3 300 ] ;; conversion of forest to traditional agriculture in patches with high suitability for traditional agriculture
[ "CAT2" 600 300 0 300 ] ;; change from forest to cattle ranching in any patch with suitability over null suitability
[ "AGF2" 50 50 1 50 ] ;; Maintenance of agroforestry with an age below 6 year on suitability over null for agroforestry
[ "MEA3" 200 450 6 450 ] ;; conversion of land uses different than forest and mechanized agriculture into mechanized agriculture in patches with high suitability for mechanized agriculture
[ "TRA3" 50 200 3 200 ] ;; conversion of land uses different to forest or traditional agriculture to traditional agriculture in patches with high suitability for traditional agriculture
[ "CAT3" 600 300 0 300 ] ;; change of land uses diferent to forest and cattle ranching to cattle ranching in any level of suitability
[ "AGF3" 350 50 1 50 ] ;; change from land uses different from forest and agroforestry to agroforestry in any suitability level exepct for null for agroforestry
[ "MEA4" 250 300 5 300 ] ;; maintenance of mechanized agriculture in patches with medium suitability for mechanized agriculture
[ "TRA4" 50 200 2 200 ] ;; maintenance of traditional agriculture to traditional agriculture in patches with medium suitability for traditional agriculture
[ "MEA5" 250 300 5 300 ] ;; conversion of forest to mechanized agriculture in patches with medium suitability for mechanized agriculture
[ "TRA5" 100 200 2 200 ] ;; conversion of forest to traditional agriculture in patches with medium suitability for traditional agriculture
[ "MEA6" 250 300 5 300 ] ;; conversion of land uses different than forest and mechanized agriculture into mechanized agriculture in patches with medium suitability for mechanized agriculture
[ "TRA6" 50 150 2 150 ] ;; conversion of land uses different to forest or traditional agriculture to traditional agriculture in patches with medium suitability for traditional agriculture
[ "MEA7" 250 250 4 250 ] ;; maintenance of mechanized agriculture in patches with low suitability for mechanized agriculture
[ "TRA7" 50 150 1 150 ] ;; maintenance of traditional agriculture on patches with traditional agriculture in patches with low suitability for traditional agriculture
[ "MEA8" 250 250 4 250 ] ;; conversion of forest to mechanized agriculture in patches with low suitability for mechanized agriculture
[ "TRA8" 100 100 1 100 ] ;; conversion of forest to traditional agriculture in patches with low suitability for traditional agriculture
[ "MEA9" 250 250 4 250 ] ;; conversion of land uses different than forest and mechanized agriculture into mechanized agriculture in patches with low suitability for mechanized agriculture
[ "TRA9" 50 100 1 100 ] ;; conversion of land uses different to forest or traditional agriculture to traditional agriculture in patches with low suitability for traditional agriculture
]
这是我的 while 循环:
to decideforprofit ;;;; procedure to create the decision model based.
;;;; It is a secondary process after maize self-consumption
set ownedpatcheslist [] ;; used to create a list of patches owned by each agent
while [investablemoney >= 50] [ ;; defined as the smallest cost to do a land use change
create-patch-types ;; create a list of available patches in its respective procedure to have only the available patches and their potential benefit in land use change
set alternatives filter [parches -> member? (item 0 parches) patch-types] landuselist ;;; to work with a list for each turtle that we can modify, it filters the global list based on the lo
create-patch-types list
let landuselist1 filter [lista -> item 1 lista <= investablemoney] alternatives;;; filter based on the budget of each turtle
let landuselist2 sort-with [ l -> item 2 l ] landuselist1 ;;; sort the alternatives of each turtle to find the most profitable one
ifelse empty? landuselist2 [ ;;; when there are not more patches then to continue the procedure as normal for the next turtle
;handle the case where there are no suitable patches
;show "NO AVAILABLE PATCHES"
] [;;; if it is not empty then
let highest-value first landuselist2 ;;; chose the most profitable land use change from the nested list
let value item 0 highest-value ;;; define the land use change "MEA1", "MEA2"...
set chosevalue value ;;; the turtle receive the order
let costs item 1 highest-value ;;; extract the cost of the decision from the list
let income item 2 highest-value ;;; extract the income of the decision form the list
set investablemoney (investablemoney - costs);; discount the cost to the variable investablemoney of turtles
set savings (savings + income) ;;; add the income of the decision to the savings variable
applydecisions;;; procedure that execute the order in chosevalue
]
if empty? landuselist2 or investablemoney < 50 [
;exit the while loop if there are no suitable alternatives or there is not enough investable money
;show "I am stopping#"
stop
]
]
;;; end of the while
end
;;;decideforprofit()
这是创建可用补丁和土地利用变化选项列表的过程:
to create-patch-types ;; this creates a list of patches
;; based on the patch suitability
;; current land use and ownership
set patch-types []
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; choices for traditional agriculture
if any? patches with [(land-use = "TRA") and (OwnerId = [who] of myself) and (TRAsuitability = 3) and (inuse = 0)] [set patch-types lput "TRA1" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (TRAsuitability = 3) and (inuse = 0)] [set patch-types lput "TRA2" patch-types]
if any? patches with [member? land-use ["MEA" "CAT" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (TRAsuitability = 3) and (inuse = 0)] [set patch-types lput "TRA3" patch-types]
if any? patches with [(land-use = "TRA") and (OwnerId = [who] of myself) and (TRAsuitability = 2) and (inuse = 0)] [set patch-types lput "TRA4" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (TRAsuitability = 2) and (inuse = 0)] [set patch-types lput "TRA5" patch-types]
if any? patches with [member? land-use ["MEA" "CAT" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (TRAsuitability = 2) and (inuse = 0)] [set patch-types lput "TRA6" patch-types]
if any? patches with [(land-use = "TRA") and (OwnerId = [who] of myself) and (TRAsuitability = 1) and (inuse = 0)] [set patch-types lput "TRA7" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (TRAsuitability = 1) and (inuse = 0)] [set patch-types lput "TRA8" patch-types]
if any? patches with [member? land-use ["MEA" "CAT" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (TRAsuitability = 1) and (inuse = 0)] [set patch-types lput "TR9" patch-types]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; choices for mechanized agriculture
if any? patches with [(land-use = "MEA") and (OwnerId = [who] of myself) and (MEAsuitability = 3) and (inuse = 0)] [set patch-types lput "MEA1" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (MEAsuitability = 3) and (inuse = 0)] [set patch-types lput "MEA2" patch-types]
if any? patches with [member? land-use ["TRA" "CAT" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (MEAsuitability = 3) and (inuse = 0)] [set patch-types lput "MEA3" patch-types]
if any? patches with [(land-use = "MEA") and (OwnerId = [who] of myself) and (MEAsuitability = 2) and (inuse = 0)] [set patch-types lput "MEA4" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (MEAsuitability = 2) and (inuse = 0)] [set patch-types lput "MEA5" patch-types]
if any? patches with [member? land-use ["TRA" "CAT" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (MEAsuitability = 2) and (inuse = 0)] [set patch-types lput "MEA6" patch-types]
if any? patches with [(land-use = "MEA") and (OwnerId = [who] of myself) and (MEAsuitability = 3) and (inuse = 0)] [set patch-types lput "MEA7" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (MEAsuitability = 1) and (inuse = 0)] [set patch-types lput "MEA8" patch-types]
if any? patches with [member? land-use ["TRA" "CAT" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (MEAsuitability = 1) and (inuse = 0)] [set patch-types lput "MEA9" patch-types]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; choices for cattle ranching
if any? patches with [(land-use = "CAT") and (OwnerId = [who] of myself) and (CATsuitability > 0) and (inuse = 0)] [set patch-types lput "CAT1" patch-types]
if any? patches with [(land-use = "TFM") and (OwnerId = 999) and (CATsuitability > 0) and (inuse = 0)] [set patch-types lput "CAT2" patch-types]
if any? patches with [member? land-use ["MEA" "TRA" "AGF"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (CATsuitability > 0) and (inuse = 0)] [set patch-types lput "CAT3" patch-types]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; choices for agroforestry
if any? patches with [(land-use = "AGF") and (OwnerId = [who] of myself) and (AGFsuitability > 0) and (inuse = 0) and (age >= 6)] and any? patches with [(land-use = "AGF") and (OwnerId = [who] of myself) and (AGFsuitability > 0) and (inuse = 0) and (age >= 6)] [set patch-types lput "AGF1" patch-types]
if any? patches with [(land-use = "AGF") and (OwnerId = [who] of myself) and (AGFsuitability > 0) and (inuse = 0) and (age < 6)] and any? patches with [(land-use = "AGF") and (OwnerId = [who] of myself) and (AGFsuitability > 0) and (inuse = 0) and (age < 6)] [set patch-types lput "AGF2" patch-types]
if any? patches with [member? land-use ["MEA" "CAT" "TRA"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (AGFsuitability > 0) and (inuse = 0)] and any? patches with [member? land-use ["MEA" "CAT" "TRA"] or (land-use = "FAL" and age > 4) and (OwnerId = [who] of myself) and (AGFsuitability > 0) and (inuse = 0)] [set patch-types lput "AGF3" patch-types]
end
;;;create-patch-types()
每个土地用途变更选项都会执行另一个程序。
这是一个例子:
to applydecisions
if (chosevalue = "TRA1") [move-to one-of patches with [land-use = "TRA" and (OwnerId = [who] of myself) and TRAsuitability = 3 and inuse = 0]
ask patch-here [set land-use "TRA" set OwnerId [who] of myself set breedowner [breed] of myself set inuse 1 ]
set ownedpatcheslist lput patch-here ownedpatcheslist ]
if (chosevalue = "TRA2") [move-to one-of patches with [land-use = "TFM" and OwnerId = 999 and TRAsuitability = 3 and inuse = 0]
ask patch-here [set land-use "TRA" set OwnerId [who] of myself set breedowner [breed] of myself set age 1 set inuse 1]
set ownedpatcheslist lput patch-here ownedpatcheslist]
end
;;;applydecisions()
如何让我的模型更快?我发现每只乌龟运行 while 过程需要很长时间,因为它们每次在循环中和每个时钟周期都必须构建不同的列表。
我想确定使
decisionforprofit
循环更快的最佳方法,以便我的海龟考虑他们拥有的资金和可用的补丁(不属于其他海龟或已经属于同一只海龟)来改变土地用途:目前我有 200 只海龟和 2600 个斑块,每种土地利用类型的适宜性分为三个级别。
考虑编程的 DRY 原则。
不要重复自己。
如果您可以在代码中找到重复模式,您将找到减少正在执行的指令数量的机会。
考虑“owner-id = [who] of my myself”
首先,“我自己”被运行了很多很多次。也许将其存储在本地。您可以删除一些说明并可能节省一些时间。
让我找谁
然后“owner-id = me”
它由所有补丁运行,可能运行很多次。
您可能想首先查找并存储该组拥有的补丁,例如:
让 mypatches 使用 [owner-id = me ] 进行修补
看看是否可以避免使用会员? 看看是否可以使用数字代码代替字符串。 看看是否可以避免重建不改变的东西 看看是否可以整理数据以便不需要过滤 看看能不能把搜索变成查找
这是一个开始。希望有人能添加更多想法。
我现在没有时间详细阐述这个建议,但您可以考虑使用其他海龟作为对象来保存和查询您的土地利用属性。