如何避免netlogo中没有人出现运行时错误?

时间:2019-03-28 07:29:19

标签: runtime netlogo

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

我尝试编写一个Nobody命令,它不正确。感谢您的建议。

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

1 个答案:

答案 0 :(得分:1)

我猜测抛出错误的代码部分是(供以后参考,如果您尝试着重于引起问题的代码部分会很有帮助):

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 grey neighbors with [pcolor = grey]
let fc 0.1
if any? greys
[ set fc [c] of one-of greys ]

或者

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

请注意,这些代码段均未经过测试。