yy w
yy w

Reputation: 65

When trying to move to nearest breed, what causes the "Nobody" error?

I built a model of a tiger eating wolves. When the tiger's energy is greater than half of the sum of wolves' energy, the tiger would find the wolf closest to it, eat it and gain energy. So I wrote:

to tigers-feed
  ask tigers [
    let WE-sum sum [WEnergy] of wolves
    if TEnergy > WE-sum * 0.5 [
      move-to min-one-of wolves [distance myself] ;;it reports bugs here.
      ask one-of wolves-here [die]
      set TEnergy Wt * tiger-gain-from-food
    ]
  ]

But when running the to go command, the program warns me:

MOVE-TO expected input to be an agent but got NOBODY instead.

Here is my complete code:

globals[
  Ww
  Wt
  wolf-gain-from-food
  tiger-gain-from-food
]

breed [wolves wolf]
breed [tigers tiger]

wolves-own [WEnergy]
tigers-own [TEnergy]

;;probably nothing wrong with here, you can jump them.
to setup
  clear-all
  parameter-initialization
  setup-breed
  reset-ticks
end

to parameter-initialization
  set Ww random 2
  set Wt random 4
  set wolf-gain-from-food 100
  set tiger-gain-from-food 200
end

to setup-breed

  set-default-shape wolves "wolf"
  create-wolves 100 [setxy random-xcor random-ycor]
  ask wolves [
    set WEnergy Ww * wolf-gain-from-food
    set size 4
    set color grey
  ]

  set-default-shape tigers "x"
  create-tigers 1 [setxy random-xcor random-ycor]
  ask tigers [
    set TEnergy Wt * tiger-gain-from-food
    set size 6
    set color yellow
  ]

end



to go
  ask tigers [set TEnergy TEnergy + 2]
  tigers-feed
  tick
end

to tigers-feed
  ask tigers [
    let WE-sum sum [WEnergy] of wolves
    
    if TEnergy > WE-sum * 0.5 [
      move-to min-one-of wolves [distance myself]
      ask one-of wolves-here [die]
      set TEnergy Wt * tiger-gain-from-food
    ]
  ]

end

In fact, when I try to run go only once, the program doesn't report errors. So I guess it's not the command move-to min-one-of wolves [distance myself] that causes the error. Then what exactly caused the error?

Upvotes: 1

Views: 38

Answers (1)

Matteo
Matteo

Reputation: 2926

The line giving you the error is in fact move-to min-one-of wolves [distance myself].

As you can see, the error only happens when there are no foxes left in the model. This is because you have not included a stop condition or anything else that prevents the model to search for wolves where there are none.

Assuming that you are happy to have the model stop when the tiger has eaten all the wolves, you can just do:

to go
  if (not any? wolves) [stop]
  ask tigers [set TEnergy TEnergy + 2]
  tigers-feed
  tick
end

Note that writing any? <agentset> is equivalent to writing count <agentset> > 0 but, as rightly suggested in the NetLogo Dictionary, it is more efficient and readable for this purpose (in our case we have to add not before it).

Upvotes: 1

Related Questions