首页 > 解决方案 > 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

然而,Netlogo 在运行时报告:“OF 预期输入是海龟代理集或补丁代理集或海龟或补丁,但得到了数字 0。”

任何人建议如何以它的工作方式对其进行编码?

我的整个代码:

extensions [gis]
globals
[
  land-use-map
   Senario1N               ;; the count of patches senario 1 describes
   Senario1L
   Senario1A
   Senario1B
   Senario1I
   Senario1R
   Senario1W
  %landusetypeN           ;; the amount patches
  %landusetypeL
  %landusetypeA
  %landusetypeB
  %landusetypeI
  Atractiveneighbor

]

patches-own
  [   Land-use                ;; Wat kind og landusetype a patch has
      Willingstochange        ;; If true a patch would like to change (if true the count of patches has a surplus comparing to the sneario, if false they have a shortage)
      atractiveness           ;; if a patch type is attractive to change in <1 = yess
    ]



to setup
  clear-all
  load-gis                       ;;load the maps
  setup-constants
  update-global-variables
  update-display
  reset-ticks
end



to load-gis  ;;load the maps
  set land-use-map gis:load-dataset "a_LANDUSE_cellsize5.asc"     ;;loads the land use map
  gis:set-world-envelope-ds gis:envelope-of land-use-map          ;;sets the envelope of the world to match that of the GIS dataset
  gis:apply-raster land-use-map Land-use                          ;;patches in the land-use-map have a specific land-use now

  ask patches [
    if Land-use = 1 [ set pcolor Green ] ; Green = Nature         ;; patches have a certain color now
    if Land-use = 2 [ set pcolor red ] ; Dark red = Leisure
    if Land-use = 3 [ set pcolor Yellow ] ; Yellow = Agriculture
    if Land-use = 4 [ set pcolor brown ] ; brouwn = Buildup
    if Land-use = 5 [ set pcolor grey ] ; grey = roads
    if Land-use = 6 [ set pcolor pink ] ; pink = industry
    if Land-use = 7 [ set pcolor blue ] ; Blue = water
  ]
    resize-world 0 1633 0 780
    set-patch-size 1
end

to setup-constants
  set Senario1N 49174        ;; the count of patches senario 1 describes
  set Senario1L 17871
  set Senario1A 569970
  set Senario1B 34202
  set Senario1I 5540
  set Senario1R 34968
  set Senario1W 65594

end


to go ;; this asks the model to caculate certain variables defined below
  askforchange
  caculateWILLingtochange
  caculateAtrac
  tick
end


to update-display
  ask patches
  [ if Land-use = 1 [ set pcolor Green ]   ;; Green = Nature           
    if Land-use = 2 [ set pcolor red ]     ;; Dark red = Leisure
    if Land-use = 3 [ set pcolor yellow ]  ;; Yellow = Agriculture
    if Land-use = 4 [ set pcolor brown ]   ;; brouwn = Buildup
    if Land-use = 5 [ set pcolor grey ]    ;; grey = roads
    if Land-use = 6 [ set pcolor pink ]    ;; pink = industry
    if Land-use = 7 [ set pcolor blue ]    ;; Blue = water
    ] ;; patches have a certain color now
end

to update-global-variables
  if count patches > 0
    [ set %landusetypeN (count patches with [ Land-use = 1 ] / count patches) * 100
      set %landusetypeL (count patches with [ Land-use = 2 ] / count patches) * 100
      set %landusetypeA (count patches with [ Land-use = 3 ] / count patches) * 100
      set %landusetypeB (count patches with [ Land-use = 4 ] / count patches) * 100
      set %landusetypeI (count patches with [ Land-use = 6 ] / count patches) * 100
    ]
end



to caculateWILLingtochange
  Ask  patches [
    if count patches with [Land-use = 1] > Senario1N [ ask patches with [ Land-use = 1 ][
           set  Willingstochange True
         ] ]

    if count patches with [Land-use = 2] > Senario1L [ ask patches with [ Land-use = 2 ][
           set  Willingstochange True
         ] ]
    if count patches with [Land-use = 3] > Senario1A [ ask patches with [ Land-use = 3 ][
           set  Willingstochange True
         ] ]
    if count patches with [Land-use = 4] > Senario1B [ ask patches with [ Land-use = 4 ][
           set  Willingstochange True
         ] ]
    if count patches with [Land-use = 5] > Senario1R [ ask patches with [ Land-use = 5 ][
           set  Willingstochange True
         ]]
    if count patches with [Land-use = 6] > Senario1I [ ask patches with [ Land-use = 6 ][
           set  Willingstochange True
         ]]
    if count patches with [Land-use = 7] > Senario1W [ ask patches with [ Land-use = 7 ][
           set  Willingstochange True
         ] ]  ]
end

to caculateAtrac
  Ask  patches [
    if count patches with [Land-use = 1] > Senario1N [ ask patches with [ Land-use = 1 ][
           set  atractiveness (count patches with [Land-use = 1]/ Senario1N )
         ] ]

    if count patches with [Land-use = 2] > Senario1L [ ask patches with [ Land-use = 2 ][
           set  atractiveness (count patches with [Land-use = 2]/ Senario1L )
         ] ]
    if count patches with [Land-use = 3] > Senario1A [ ask patches with [ Land-use = 3 ][
           set  atractiveness (count patches with [Land-use = 3]/ Senario1A )
         ] ]
    if count patches with [Land-use = 4] > Senario1B [ ask patches with [ Land-use = 4 ][
            set  atractiveness (count patches with [Land-use = 4]/ Senario1B )
         ] ]
    if count patches with [Land-use = 5] > Senario1R [ ask patches with [ Land-use = 5 ][
            set  atractiveness (count patches with [Land-use = 5]/ Senario1R )
         ]]
    if count patches with [Land-use = 6] > Senario1I [ ask patches with [ Land-use = 6 ][
             set  atractiveness (count patches with [Land-use = 6]/ Senario1I )
         ]]
    if count patches with [Land-use = 7] > Senario1W [ ask patches with [ Land-use = 7 ][
             set  atractiveness (count patches with [Land-use = 7]/ Senario1W )
         ] ]  ]
end


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

标签: netlogoneighbours

解决方案


您的问题是您正在询问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] [](using with) 并且还利用了假设的更简单truefalse编码的优势(您可以使用而不是too.truenot WillingstochangeWillingstochange = false


推荐阅读