linda
linda

Reputation: 117

How to make agent stay at certain patch in netlogo within certain ticks?

I'm doing code for making agents roaming around the world to forage.

After they find it, they should stay at the patch where they find the food for a certain time. But I have a problem to make them stay on the food patch for a certain time.

Certain time: each turtle has its own variable "s", and "s" is a random number generated by NetLogo and the number shouldn't change within each tick, and I have a problem to make "s" doesn't change.

For example, turtle 1 has "s"=60, after turtle 1 find the food, it will stay on the food for 60 ticks. After turtle 1 has stayed for 60 ticks there, it will leave the food and the value of "s" will be reset again to another random value. And each turtle has a different value of "s"

I tried to use timer coding, but the timer only decreases each time the agent come across the food (like they just walk on the food instead of staying there) I can't use bk 1 or fd -1 command because the turtle just keeps on moving forward after they walk backwards for 1 tick.

Any help is appreciated.

here is the code:

turtles-own
[ 
  food
  location 
  s
]

patches-own
[
  food-quality
]

to go
  move
  forage
end

to move
 ask turtles
  [fd 1]
end


to forage

 set s random 100

   ask turtles
     [ [ if food < food-quality  ;meaning that turtle will only keep the best food's value each time they found food
        [set food food-quality  ;food-quality is patch-own, the food's value
          set location patch-here]
          if s >= 50  
           [stay] ]

    ]
    ]

end

to stay   
  set s s - 1   ;decrement-timer  
  if s = 0 
    [move
      set label ""
      reset-s
    ]    

end

to reset-count-down   
  set s random 100
end

Edit: making the question clearer and update some code.

Upvotes: 0

Views: 711

Answers (1)

JenB
JenB

Reputation: 17678

I think you have a logic problem rather than a coding problem, but it's not particularly clear. If your variable 's' is supposed to be something like a stay-timer (much more descriptive name), then don't set it at the beginning of the forage procedure. Instead, set it when the turtle finds food and then only move/forage if it's not being held by the stay-timer. You also don't have tick anywhere in your code, so you are not advancing the clock. I think you want something like this:

to forage
  ask turtles
  [ if food < food-quality  ; has found food that is more than currently owned
    [ set stay-timer random 5 + 20    ; sets number of ticks to stay between 5 and 24
      set food food-quality  ; food-quality is patch-own, the food's value
      set location patch-here
    ]
    set stay-timer stay-timer - 1 
  ]
end

to go
  ask turtles with [stay-timer = 0] [ move ]
  forage
  tick
end

Upvotes: 1

Related Questions