Kilian Murphy
Kilian Murphy

Reputation: 335

Hunt success netlogo

I have coded my turtles to go out and hunt, however when they find prey they simply eat it, is there anyway to add a mathematical factor for their chances of success instead of it always being 100%

essentially when they find prey, roll the dice and see if they can eat it.

to search ;when wolf is hungry 
  set energy  energy  - 1
    fd v-wolf
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 15 ] ;; could add some variation to this with random-normal 45 5
    [ lt 15 ]] ;; so that it samples from a dist with mean 45 SD 5

  ;; check if it can see a prey/food item
  ;; here i think we probably pick one of several possible prey 
  ;; that are detectable randomly using the one-of command.
  ;; We should probably select the nearest one instead, but 
  ;; i cant code that off the top of my head
  if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
  if energy < 0 [die]

end


To eat ;to kill prey and eat it
  let kill one-of prey-here in-radius smell
  ;move-to (need to code it so they move toward prey in a radius
  ;need to code in a variable for success too
  if kill != nobody
    [ask kill [ die ]
      set energy energy + 10000]
end

Upvotes: 1

Views: 98

Answers (1)

JenB
JenB

Reputation: 17678

Yes, you can generate a random number and then only do the kill commands if that random number meets certain conditions. The usual way is to generate a random number between 0 and 1 (which is random-float 1 in NetLogo) and then do something like if random-float 1 < 0.4 [ <what happens> ] if you want 40% probability for example.

In response to comment:

to eat
  let kill one-of prey-here in-radius smell
  if kill != nobody and random-float 1 < 0.4
  [ ask kill [ die ]
    set energy energy + 10000 ]
end

Please make an attempt to understand what this is doing and think about the answer yourself first. If you don't understand what any command means, or what the logic is of any sequence of commands, do not move on until you do. If you don't learn while the code is easy, you will never be able to work out the more difficult things you need to do in the model you are building.

Upvotes: 3

Related Questions