NetLogo:在补丁自己的变量之后/重复乌龟移动

时间:2015-09-01 17:42:52

标签: debugging while-loop netlogo do-while

我想让我的乌龟在世界各地徘徊,并在其半径范围的补丁中查找具有最高totalattract值的补丁。龟应该徘徊到补丁的[energy] > [totalattract],然后乌龟必须留在补丁上并运行一个过程[infest]。我想在1个刻度内运行它,所以我想使用while或repeat或它们的组合,但我无法弄清楚确切的语法。与ifelse条件相同。

我的示例while条件只是永远运行结束并不会停止。

另外,我想测量乌龟运动开始的补丁和乌龟必须留下的补丁之间的欧氏距离(最短)。

谢谢!

patches-own [
  totalattract
]

turtles-own [
  energy
  efficiency
]

to setup
  clear-all
  ask patches [
    set totalattract random 4
  ]
  ask n-of 5 patches [set totalattract 4]
  ask patches with [totalattract = 4][ 
    set pcolor red
        ]
  ask patches [
    set plabel totalattract
  ]
  setup-turtles
  reset-ticks
end

to setup-turtles
  create-turtles 1 [ setxy random-xcor random-ycor 
    set color green
    set energy 10
    set efficiency 5
  ]
end

to go
  ask turtles [if color = green [move-turtles]
    if ([totalattract] of patch-here - energy) >= 0 [beetle_infest]
    ]
  tick
end

to move-turtles    
      set energy (energy - 1)
      while [energy < 4]
[
       move-to max-one-of patches in-radius 3 [ totalattract ] ]         
end

to beetle_infest
  ask patch-here [set pcolor pink]
end 

1 个答案:

答案 0 :(得分:2)

问题是它会移动到它应该的位置,但这不会导致能量增加,因此while [energy < 4]条件仍然满足。添加set energy 10如下所示将打破无限循环,但您需要将其更改为您真正希望乌龟做的任何事情以增加其能量。

to move-turtles    
      set energy (energy - 1)
      while [energy < 4]
[
       move-to max-one-of patches in-radius 3 [ totalattract ]
       set energy 10 ]           ; put stuff here to increase energy     
end