首页 > 解决方案 > 如何避免 netlogo 中没有人运行时错误?

问题描述

我正在使用“一个邻居”命令检查三种类型补丁的属性之间的交互。在某些情况下,当两个补丁彼此不相邻并且实体 NOBODY 出现时,就会出现问题。有什么办法可以避免这种情况。我有必要使用“邻居之一”命令。我能否给出一个命令,说明如果检测到没人,那么该补丁的属性值为 0.1。在我的代码下面给出的问题发生在交互黑白黄色补丁和红色补丁中,因为在某些地方红色不是黄色的邻居。

我试着写了一个没有人命令它不正确。感谢您的建议。

globals [ k ] ; interaction constant
patches-own [ a b c d' e' eeep deep ] ; state variables of properties
                       ; a is the Proportion and variety of Blend of land 
use
                       ; b is the Land uses with supportiveness for 
complimentary  activities
                       ; c is the Vehicular and Pedestrian Intensity
                       ; d is the Intensity of Nodes in urban web
                       ; e' is the Frequency of Enforced Vigilance
to setup
  clear-all
  set k initial-k
  setup-patches
  reset-ticks
end
to setup-patches
  ask patches [ set pcolor yellow ] ; defines the patches as built up in an area

  ; to define yellow patches without a and b
  ask patches [ if (pycor > 1) and (pycor < 8) and (pxcor = -3 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor > 1) and (pycor < 8) and (pxcor = -2 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor < -1) and (pxcor = -10 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor < -1) and (pxcor = -9 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor < -1) and (pxcor = -5 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor < -1) and (pxcor = 2 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor < -2) and (pxcor = 6 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor > 1) and (pycor < 8) and (pxcor = 2 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = 2) and (pxcor > 8 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = -2) and (pxcor > 2 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = -3) and (pxcor > 6 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = -4) and (pxcor > 6 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = -8) and (pxcor > 6 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = -9) and (pxcor > 6 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = 7) and (pxcor > 8 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = 6) and (pxcor > 8 ) [ set pcolor 46 ] ]
  ask patches [ if (pycor = 2) and (pxcor > 2 ) and (pxcor < 6) [ set pcolor 46 ] ]

  ; to define road patches (horizontal)
 ask patches [ if pycor = 0 [ set pcolor grey ] ]
 ask patches [ if pxcor = 0 [ set pcolor grey ] ]
 ask patches [if pycor = 9 [ set pcolor grey ] ]
 ask patches [ if (pycor = 6) and (pxcor < -4 )[ set pcolor grey ] ]
  ask patches [ if (pycor = 3) and (pxcor < -4 ) [ set pcolor grey ] ]
  ask patches [ if (pycor = 4) and (pxcor > 3 ) [ set pcolor grey ] ]
  ask patches [ if (pycor = -6) and (pxcor > 7 ) [ set pcolor grey ] ]

  ; to define road patches (vertical)
  ask patches [ if (pycor > 0) and (pxcor = -10 ) [ set pcolor grey ] ]
  ask patches [ if (pycor > 0) and (pxcor = -5 ) [ set pcolor grey ] ]
  ask patches [ if (pycor < 0) and (pxcor = -7 ) [ set pcolor grey ] ]
  ask patches [ if (pycor < 0) and (pxcor = -3 ) [ set pcolor grey ] ]
  ask patches [ if (pycor < -3) and (pxcor = 4 ) [ set pcolor grey ] ]
  ask patches [ if (pycor > 3) and (pxcor = 4 ) [ set pcolor grey ] ]
  ask patches [ if (pycor > 0) and (pxcor = 7 ) [ set pcolor grey ] ]

  ; to define nodes as patches
  ask patches [ if pxcor = 0 and pycor = 0 [ set pcolor red ] ]
  ask patches [ if pxcor = 7 and pycor = 0 [ set pcolor red ] ]
  ask patches [ if pxcor = -3 and pycor = 0 [ set pcolor red ] ]
  ask patches [ if pxcor = -5 and pycor = 0 [ set pcolor red ] ]
  ask patches [ if pxcor = -7 and pycor = 0 [ set pcolor red ] ]
  ask patches [ if pxcor = -10 and pycor = 0 [ set pcolor red ] ]
  ask patches [ if pxcor = 4 and pycor = 4 [ set pcolor red ] ]
  ask patches [ if pxcor = 7 and pycor = 4 [ set pcolor red ] ]
  ask patches [ if pxcor = 7 and pycor = 9 [ set pcolor red ] ]
  ask patches [ if pxcor = 4 and pycor = 9 [ set pcolor red ] ]
  ask patches [ if pxcor = 0 and pycor = 9 [ set pcolor red ] ]
  ask patches [ if pxcor = -5 and pycor = 9 [ set pcolor red ] ]
  ask patches [ if pxcor = -10 and pycor = 9 [ set pcolor red ] ]
  ask patches [ if pxcor = -10 and pycor = 6 [ set pcolor red ] ]
  ask patches [ if pxcor = -10 and pycor = 3 [ set pcolor red ] ]
  ask patches [ if pxcor = -5 and pycor = 6 [ set pcolor red ] ]
  ask patches [ if pxcor = -5 and pycor = 3 [ set pcolor red ] ]

  ; to set intial values of properties for patches
  ask patches with [pcolor = yellow] [ set a random-float 0.9] ; initial a
  ask patches with [pcolor = yellow] [ set b random-float 0.9] ; initial b
  ask patches with [pcolor = grey] [ set c random-float 0.9] ; initial c
  ask patches with [pcolor = red] [ set d' random-float 0.9] ; initial d'
  ask patches with [pcolor = grey] [ set e' random-float 0.9] ; initial e'
end
to go
  tick
  if ticks >= 52 [ stop ]
  ask patches with [pcolor = yellow]
  [
    let fc [c] of one-of neighbors with [pcolor = grey] ; reports c of any 
 one grey patch of neighbours
    let fe' [e'] of one-of neighbors with [pcolor = grey] ; reports e' of any one grey patch of neighbours
    let fd' [d'] of one-of neighbors with [pcolor = red]; reports d' of any one red patch of neighbours
    if a < 0.1 [ set a 0.1
    if a > 0.9 [ set a 0.9 ] ]
    if b < 0.9 [ set b b + (k * a) + (k * fc) + (k * fd')
    if b > 0.9 [ set b 0.9 ] ]
    if b > 0.1 [ set b b - (k * fe')
    if b < 0.1 [ set b 0.1 ] ]
  ]
 end

标签: runtimenetlogo

解决方案


我猜测抛出错误的代码部分是(为了将来参考,如果您尝试专注于导致问题的代码部分,这将很有帮助):

ask patches with [pcolor = yellow]
  [ let fc [c] of one-of neighbors with [pcolor = grey]
    let fe' [e'] of one-of neighbors with [pcolor = grey]
    let fd' [d'] of one-of neighbors with [pcolor = red]
    ...
  ]
 end

你想要的原语是any?. 所以你可以重写:

let fc [c] of one-of neighbors with [pcolor = grey] 

作为

let greys neighbors with [pcolor = grey]
let fc 0.1
if any? greys
[ set fc [c] of one-of greys ]

或者

let greys neighbors with [pcolor = grey]
let fc ifelse-value any? greys
[ [c] of one-of greys ]
[ 0.1 ]

请注意,这些代码都没有经过测试。


推荐阅读