让多只乌龟同时移动

时间:2016-05-26 15:15:21

标签: parallel-processing netlogo

该计划的想法是什么:我们有看门狗,绵羊,狼。我的羊在福特,狼从外面来,当进入福特时,看门狗开始移动。狼吃羊,羊吃草,看门狗应该吓唬狼(尚未实施)。

我的问题:所有海龟一次移动一个。

我想要的是什么:我希望他们每次都能一次性移动。

代码

    breed [sheeps sheep]
breed [wolves wolf]
breed [watchdogs watchdog]

turtles-own [attacked?]

globals [counter
  attack
  sheep-energy
  wolf-energy
  grass-counter
  lifespan
  age
  num-attacks
  death-countdown]

to set-variables-shapes
  set lifespan 365 * 10
    set-default-shape sheeps "sheep"
    set-default-shape watchdogs "dog"
    set-default-shape wolves "wolf"
     set sheep-energy 200
     set wolf-energy 400
     set death-countdown 60
end

to setup
  clear-all
 set-variables-shapes
 create-fold
 spawn-animals
  reset-ticks

end

to go

   ask turtles [
     move
   ]
    respawn-grass

  ask sheeps [
    if ( pcolor != green or pcolor != brown) [
      move-to one-of patches with [ pcolor = green or pcolor = brown] ;; If i change this with turn-around (procedure), all sheeps die at once.
    ]
    eat-grass
    reproduce
    sheep-status
    death
 show sheep-energy
  ]

  ask wolves [
    attack-sheep
    if (xcor < 5 and xcor > -14 and ycor < 15 and ycor > -5 ) [

      ask watchdogs [
    move
    if ( pcolor != green or pcolor != brown ) [
      move-to one-of patches with [ pcolor = green]
    ]
      ]
    ]
  ]

tick
end

to create-fold
  ask patches [
    if (pxcor < 6 and pxcor > -15 and pycor < 16 and pycor > -6)[
      set pcolor grey]
    if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5) [
      set pcolor green]
   ]

end


to sheep-status
  if (pcolor = brown) [
    set sheep-energy sheep-energy - (life-from-food / 2)]
  if attacked?
  [set color red]

 end


to attack-sheep
    ask other sheeps in-radius (size)[
      set attacked? true
        set color red
    ]

end
to reproduce
  if (age = lifespan / 10) [
    hatch-sheeps 40 [
      hatch-options
      ]
  ]
end

to eat-grass
  if (pcolor = green)[
    set pcolor brown
    set sheep-energy sheep-energy + life-from-food
  ]
end

to respawn-grass
  ask patches [
  if pcolor = brown [
    ifelse grass-counter = 0 [
      set pcolor green
      set grass-counter grass-respawn-timer
    ]
    [ set grass-counter grass-counter - 1]
  ]
  ]
  end
to death
  sheep-death-timer
  if (sheep-energy <= 0)
  [die]
  ;;if (age >= lifespan) [die]
  sheep-explode

end

to sheep-death-timer
  if (attacked?)[
  set death-countdown death-countdown - 1

  if (death-countdown  <= 30)
  [set color black]
  if (death-countdown <= 0)
  [die]
  ]
end

to sheep-explode
  if (sheep-energy >= 10000)[
  hatch-sheeps 20 [
    hatch-options]
  die
  ]
end

to hatch-options
  set sheep-energy 200
      set age 1
      set attacked? false
  end

to move
  fd 1
  rt random 90
  lt random 90
end

to spawn-animals
   create-wolves number-of-wolves [
      set color red
      setxy 10 -10
      set size 1.3
      ask patch 10 -10[
        Set plabel "Wolves"]
  ]
  create-sheeps number-of-sheeps [
     if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5 )[
        setxy -4.5 5]
     ask patch -4.5 15 [
       Set plabel "Sheeps"]
  set color white
  set attacked? false
   ]
 create-watchdogs number-of-dogs [
    if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5 )[
    setxy  -4.5 5
    set size 1.3]
  set color blue
  ]
end
to turn-around
  fd -1
  rt 180
end

2 个答案:

答案 0 :(得分:0)

在NetLogo中没有“同时”这样的东西。 NetLogo是单线程的,不支持并行处理。

如果所有代理人在相同的时间段内移动,您可以将其视为“同时”,因为这一切都发生在勾选过程中。

但是在一个单一的刻度线中,代理商根本无法一次移动而不是一个。

答案 1 :(得分:-1)

您可以使用

ask-concurrent sheeps [move ]

让每个绵羊代理人同时移动,但这不重要,他们实际上并不建议你使用它。