Netlogo:当遇到条件(找到合适的补丁)时,如何让海龟停止移动?

时间:2016-12-17 22:41:35

标签: netlogo

我正在尝试构建一个模拟特定区域中住房选择模式的模型。

我要求海龟在半径-3中寻找一个提供最多资源的定居点补丁,如果找到了,他们应该在那里定居并停止移动;否则,他们应该移动到其他地方并停止移动。然而,无论我做什么,似乎没有乌龟停止;无论是否找到定居点,每只乌龟都在不断移动。

以下是我在“go”下的搜寻结算模块。如果条件得到满足(即找到合适的定居点),请告知如何让乌龟停止移动?我尝试在各个地方坚持“停止”,但没有区别。

to seek-settlement-1
  ask turtles with [wealth >= com] 
  [
    ifelse any? patches in-radius 3 with [pcolor = green and count      turtles-here = 0]
    [   ;;; if there are any neighboring empty green patches with resource larger than wealth of the turtle
      move-to max-one-of patches in-radius 3 with [pcolor = green and count turtles-here = 0] [resource] ;+ (0.5 * count turtles in-radius 3 with [color = blue])]
      set settlement patch-here   ;;; move to the one with the most utility (defined as an even combination of resource and number of blue turtles in radius 1) and settle there
      stop   ;;; this stop appears to make no difference
    ]

    [right random 360
     forward 3
     set wealth (wealth - com)
     stop]   ;;; this stop appears to make no difference

    if settlement != nobody
    [
     set wealth (wealth - com + (0.5 * [resource] of settlement))   ;;; update the turtle's wealth by subtracting the cost of movement and adding 20% of the resource of the patch here
     ask settlement
     [set resource (0.5 * resource)]   ;;; update the settlement patch's resource by subtracting 20%
stop   ;;; this stop appears to make no difference
    ]
  ]
end

1 个答案:

答案 0 :(得分:0)

请查看stop的文档: http://ccl.northwestern.edu/netlogo/docs/dict/stop.html stop只会退出ask

您的代码与您的描述不符。试着按照你的描述,我得到如下内容:

globals [com]
patches-own [resource]
turtles-own [wealth settlement]

to setup
  ca
  set com 10
  crt 50 [
    set wealth random 500
    set settlement nobody
    setxy random-xcor random-ycor
  ]
  ask patches [
    if random-float 1 < 0.2 [set resource 100 set pcolor green]
  ]
end

to go
  ;you can change the filter any way you wish
  ask turtles with [wealth >= com and settlement = nobody] [seek-settlement]
end

to seek-settlement
  let _candidates (patches in-radius 3 with [pcolor = green and count turtles-here = 0])
  if any? _candidates [ 
    set settlement max-one-of _candidates [resource]
    move-to settlement 
  ]  
  ifelse (settlement = nobody) [ ;this conditional shd probably be in a separate proc
    right random 360
    forward 3
    set wealth (wealth - com)
  ][
    set wealth (wealth - com + (0.5 * [resource] of settlement))
    ask settlement [set resource (0.5 * resource)] 
  ]
end