2

I'm creating a fish-tank style simulation in NetLogo. There are "prey", "predators", and "hidingspots".

The idea is that when a predator appears on the map, the prey will individually run the "hide" behavior and head to the nearest "hidingspot" - provided there are no predators between it and the "hidingspot".

to move-turtles
  ask prey  [
    if (any? predators)
    [
      hide
      stop
    ]

The relevant code to run the hide command.

to hide
  face min-one-of hidingspot [distance myself]  
  set d distance min-one-of hidingspot [distance myself]
  ask patches in-cone d 80
  [ set pcolor yellow
    if (any? predators-here)
    [ ask prey
      [ forward 1
        set color red 
        output-print "DANGER"]]]
    forward 1
end

The problem is that I do not know how to use the if statement in the "ask patches" properly. And therefore when one prey spots a threat all prey are running the else part of the statement rather than evaluating it individually.

How would I fix this?

Any help is appreciated.

JenB
  • 17,620
  • 2
  • 17
  • 45
JT93
  • 461
  • 1
  • 3
  • 13

2 Answers2

2

You need to separate what you are asking the prey to do from what you are asking the patches to do. As King-Ink said, you are asking the patches to ask all the prey to do things.

The easiest way is to create a patchset for the 'danger' patches and then check if there's a predator on those patches. To do this, you want something like the following (note that this is a complete model, so you can copy this whole code into a new model and run it).

A couple of other things in your code that I cleaned up. I used let for the local variable d so that it doesn't have to appear in your globals. I asked for min-one-of only once and reused because otherwise a different hidingspot could be selected each time (if multiple at same distance). While this would not have caused an error this time (because the second selection is just to find the distance which is, by definition, the same), it is good practice.

breed [prey a-prey]
breed [predators predator]
breed [hidingspots hidingspot]

to setup
  clear-all
  create-predators 1 [setxy random-xcor random-ycor set color red]
  create-prey 5 [setxy random-xcor random-ycor set color brown]
  create-hidingspots 20
  [ setxy random-xcor random-ycor
    hide-turtle
    ask patch-here [set pcolor green]
  ]
  reset-ticks
end

to go
  ifelse any? predators
    [ ask prey [hide] ]
    [ ask prey [swim] ]
end

to hide                                      ; turtle procedure
  let target min-one-of hidingspots [distance self]
  let path patches in-cone distance target 80
  ask path [ set pcolor yellow ]
  if any? predators-on path
  [ set color red 
    output-print "DANGER"
    face target 
  ]
  forward 1
end

to swim
end
JenB
  • 17,620
  • 2
  • 17
  • 45
1

you are asking each prey to ask all prey to hide. If you drop the ask prey from the command all prey are running it should work fine and be a bit faster

to hide
   face min-one-of hidingspot [distance myself]  
   set d distance min-one-of hidingspot [distance myself]
   ask patches in-cone d 80
     [set pcolor yellow]
   if (any? predators-here)
    [ 
      forward 1
      set color red 
      output-print "DANGER"
    ] 
end
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
King-Ink
  • 2,096
  • 12
  • 18