Netlogo,为邻居分配变量

时间:2018-06-16 10:33:06

标签: netlogo neighbours

我的代码有问题, 我正在制作一个土地利用模型,我想给出一块有足够土地用途的补丁。确定补丁应该改变的土地利用是基于其邻居的吸引力。

补丁拥有=土地使用意愿改变和吸引力

我想要的: 愿意改变的补丁应该将他们的土地用途改为具有最高吸引力的邻居的土地使用(越接近0越有吸引力)

我尝试使用以下语句执行此操作:

CloseHandle()

然而,Netlogo在运行时报告:"预期输入为海龟代理集或补丁代理集或乌龟或补丁,但得到数字0。"

有人建议如何编写它的工作原理吗?

我的整个代码:

to askforchange
  ask patches [
    if Willingstochange = true  [change]   ;; this ask the patches that are willing to change (have a surpuls) to go and change
  ]
end


to change
  ask neighbors with [Willingstochange = false ]  [         ;; this asks if the patch had neigbors with a shortage
  set Atractiveneighbor min-one-of patches [atractiveness]  ;; this asks to give the neigbor with the lowest patchcount/senario ratio
  ]


  ask patches with [Willingstochange = true]  [
    set Land-use ([Land-use] of Atractiveneighbor)   ;; this asks the patches to change their land-use to the land-use of neigbor with the lowest patchcount/senario ratio
  ]     

end

1 个答案:

答案 0 :(得分:1)

您的问题是您要求neighbors with [Willingstochange = false ] set全局变量Attractiveneighbor。因此,如果没有任何邻居不愿意改变,那么该变量处于其默认值(即0),因为没有人设置它。此外,你实际上想要最少的邻居,但是要求所有补丁。

这可以解决您的问题(未经测试)。请注意,该过程仅针对patches with [Willingstochange = true]运行,因此您无需在过程中检查该过程。

to change
  set Atractiveneighbor min-one-of neighbors [atractiveness]
  set Land-use ([Land-use] of Atractiveneighbor)
end

但是,我怀疑这是你的代码中唯一使用全局变量Atractiveneighbor的地方,在这种情况下根本不需要有这样的变量。从变量列表中删除它,然后使用let代替set

to change
  let Atractiveneighbor min-one-of neighbors [atractiveness]
  set Land-use ([Land-use] of Atractiveneighbor) 
end

即使更干净(虽然可能更难以阅读新的NetLogo编码器),您可以这样做:

to change
  set Land-use [Land-use] of min-one-of neighbors [atractiveness]
end

更好的是,为什么要在单独的程序中进行检查?您可以完全删除更改过程并执行:

to askforchange
  ask patches with [Willingstochange]
  [ set Land-use [Land-use] of min-one-of neighbors [atractiveness]
  ]
end

除了合并这两个过程之外,还会将ask patches [ if Willingstochange = true] []替换为ask patches with [ Willingstochange = true] [](使用with)并利用更简单的truefalse假设true的编码(您可以使用not Willingstochange代替Willingstochange = false