Search code examples
timernetlogoscheduling

NetLogo scheduling of different actions


I am very new to NetLogo and now I try to understand the logic behind NetLogo. I am building my first model and can not figure out how the procedures have to be ordered to do what I would like them to. What my model should do:

  • In the “morning” (beginning of the simulation) spread out my agents randomly
  • Then let them move “uphill” with a little bit of randomness to go to the places with the highest insolation in their surroundings.
  • Stay there for a number of ticks (between 25 and 34)
  • After my timer has expired they should start foraging – so far just moving around randomly – the timer should not start again and also the “uphill” behaviour should not be executed again

What the model does:

  • The agents spread randomly
  • They move to the place with the highest insolation in their surrounding (uphill)
  • The timer starts and counts down
  • The agents start moving again– but the I am trapped in the uphill – stay put till the timer expires “loop” again

I guess that it is a real beginner-mistake… Could you please explain to me how to get this right?

to go
  move-turtles
  tick
end

to move-turtles
  ask turtles
  [ifelse insolation <= max [insolation] of neighbors
  [continue]
  [bask]
  pen-down
  ]
end

to continue   ;; a turtle procedure
  ifelse random-float 1.0 < 0.7
  [uphill insolation]
  [move-to one-of neighbors]
end

to bask  ;; a turtle procedure
  set count-down count-down - 1
  set label count-down
  if count-down = 0
  [
   forage
   set label ""
  ]
end


to forage  ;; turtle procedure 
  set heading (random-normal 180 30)
  fd random-normal 3 2
end

Solution

  • If I understand your problem correctly, you only want them to do the uphill / bask once per day but they do it repeatedly. Imagine a turtle has done their basking, the forage procedure sends them in a random direction once. Now, the next tick, they are faced with:

    ifelse insolation <= max [insolation] of neighbors
    [ continue ]
    [ bask ]
    

    So they look around and compare the insolation values and then select the 'continue' or 'bask' procedure depending on the comparison. If they get the continue procedure, they move to one of the neighbouring patches, either to the one with the largest insolation value or randomly. If they get the bask procedure, nothing happens except that the label gets turned on. This is because the value of count-down is 0 from the last time they basked, so it is -1 by the time the code checks whether the value is 0.

    Once nothing happens, they are faced with the same choice next tick and will have the same outcome. So once the turtle is at the insolation peak, it will stay there indefinitely.

    If you want something to only occur once, the easiest thing to do is to add a variable that remembers whether it has happened yet. NetLogo convention has true/false (yes/no) variables with a question mark at the end. So you can do something like (assuming you have added 'bask-over?' to the turtles-own and initialised it as false):

    to bask  ;; a turtle procedure
      set count-down count-down - 1
      set label count-down
      if count-down = 0
      [ forage
        set label ""
        set bask-over? true
      ]
    end
    

    And you then bring that variable into the test for whether to continue or bask:

    to move-turtles
      ask turtles
      [ ifelse bask-over? or insolation <= max [insolation] of neighbors
        [ continue ]
        [ bask ]
        pen-down
      ]
    end
    

    Note that if bask-over? is the same as if bask-over? = true. Similarly you can use if not bask-over? instead of if bask-over? = false.