如何在Netlogo中使用海龟在半径指令中寻找食物以及如何随着时间的推移再生食物?

时间:2018-11-29 18:04:25

标签: netlogo

我正在NetLogo中尝试执行以下操作:

  1. 让海龟(大象)觅食
  2. 让植物随着时间的推移缓慢地繁殖,一面先于另一面
  3. 让乌龟(大象)留在世界边界内
  4. 保持乌龟(大象)直立

我基本上想做的是让我们的海龟(大象)一侧吃食物,然后寻找另一侧的食物。如果他们被汽车撞到,他们将会死亡。我们希望他们在双方之间来回穿越,以便他们随着时间的流逝而死亡。我们尝试使用“寻找食物”原语,但不适用于我们的仿真。我们还使用反弹原语让海龟呆在了世界上,但是使用当前的代码,它们倾向于再次移动到任何地方。至于食物的再生,我们尝试使用填充功能,但这也行不通。

非常感谢您的帮助。

这是我们的模拟代码:

breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]

turtles-own [
  speed
  speed-limit
  speed-min
]

to setup
  clear-all
  setup-patches
  setup-elephants
  setup-cars
  setup-plants
  reset-ticks
end

to setup-patches
  ask patches [
    ifelse (pycor > -2) and (pycor < 2)
    [ set pcolor black ]
    [ set pcolor green ]
  ]
end

to setup-elephants
  ask n-of number-of-elephants (patches with [ pycor < -4 ])
  [ sprout-elephants 1
    [ set shape "elephant"
      set color 4
      set size 4
    ]
  ]

end

to setup-cars
  ask n-of number-of-cars (patches with [ pcolor = black ])
  [ sprout-cars 1
    [ set shape "car"
      set color 105
      set size 2
      set heading 90
    ]
  ]
end

to setup-plants
  ask n-of number-of-plants (patches with [ pcolor = green ])
  [ sprout-plants 1
    [ set shape "plant"
      set color 62
      set size 1
    ]
  ]
end

to go
  ask elephants [
    bounce forward 1
  ]
  ask cars [
    set xcor random-xcor
    set heading 90
    forward 1
    move-elephants
    move-cars
    eat-plants
    kill-elephants
  ]
end

to bounce
  if abs pxcor = max-pxcor
    [ set heading ( - heading ) ]
  if abs pycor = max-pycor
    [ set heading ( 180 - heading ) ]
end

to move-elephants
  ask elephants [
    right random 360
    forward 1
  ]
end

to move-cars
  set speed 0.1
  set speed-limit 0.1
end

to eat-plants
  ask elephants
  [ let prey one-of plants-here
    if prey != nobody [ask prey [die]]
  ]
end

to kill-elephants
  ask cars
  [ let prey one-of elephants-here 
    if prey != nobody [ask prey [die]]
  ]
end

1 个答案:

答案 0 :(得分:3)

此代码存在多个问题,因此,我将尝试摆脱更明显的逻辑问题,并查看是否可以使您专注于特定问题。请注意,您实际上应该逐步构建代码-添加一种行为(例如,移动大象,移动汽车,吃东西或其他),并确保它能在添加下一个行为之前起作用。

  1. 您的执行程序没有tick用于时间流逝
  2. 您的行驶程序中,每辆车随机移动所有大象,因此它们多次移动
  3. 您的车速和速度限制将被设置为每次刻度相同且永不改变
  4. 您已经筑巢ask cars [ ask elephants [ <do stuff> ] ]来食用植物和杀死大象,这会使它们在每一个滴答声中多次发生。

解决这些问题,就可以解决这个问题(请注意,我用数字替换了滑块输入,因此您必须将其改回)。这应该可以解决您在评论中提到的问题。您将要询问有关您要解决的其他问题的具体问题。

breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]

turtles-own
[ speed
  speed-limit
  speed-min
]

to setup
  clear-all
  setup-patches
  setup-elephants
  setup-cars
  setup-plants
  reset-ticks
end

to go
  ask elephants
  [ bounce
    forward 1
  ]
  ask cars [ forward 1 ]
  move-elephants
  eat-plants
  kill-elephants
  tick
end

to bounce
  if abs pxcor = max-pxcor
    [ set heading ( - heading ) ]
  if abs pycor = max-pycor
    [ set heading ( 180 - heading ) ]
end

to move-elephants
  ask elephants
  [ right random 360
    forward 1
  ]
end

to eat-plants
  ask elephants
  [ let prey one-of plants-here
    if prey != nobody [ask prey [die]]
  ]
end

to kill-elephants
  ask cars
  [ let prey one-of elephants-here 
    if prey != nobody [ask prey [die]]
  ]
end

to setup-patches
  ask patches [
    ifelse (pycor > -2) and (pycor < 2)
    [ set pcolor black ]
    [ set pcolor green ]
  ]
end

to setup-elephants
  ask n-of 20 (patches with [ pycor < -4 ])
  [ sprout-elephants 1
    [ set shape "wolf"
      set color 4
      set size 4
    ]
  ]

end

to setup-cars
  ask n-of 20 (patches with [ pcolor = black ])
  [ sprout-cars 1
    [ set shape "car"
      set color 105
      set size 2
      set heading 90
      set speed 0.1
      set speed-limit 0.1
    ]
  ]
end

to setup-plants
  ask n-of 50 (patches with [ pcolor = green ])
  [ sprout-plants 1
    [ set shape "plant"
      set color 62
      set size 1
    ]
  ]
end