Search code examples
counternetlogo

How can I properly implement a function at the end of a counter in Netlogo?


I am writing an assembly line model, and I would like to implement a counter to hold a turtle at a specific patch (in this case, patch 3 0) for 10 ticks. Once 10 ticks have passed, I would like the turtle to keep on moving at the rate of one patch per tick and for the next turtle in line to begin its own 10 tick timer once it arrives at the specified patch. So far, I can stop the turtles at the specified patch and run a 10 tick counter; however, I cannot seem to get the turtles to keep moving continuously after the timer is completed.

Here are the relevant parts of my code so far.

to go
  move-tubs
  move-drums
  machine-marriage
  move-machines
  stay
  keep-going
  tick
end



to move-machines
  ask wmachines [
    if not any? turtles-on patch-ahead 1 and xcor < 3
    [ forward 1]
    ]

end

to stay
  ask wmachines-on patch 3 0[
    ifelse counter = 0 [
    set counter 10
    ]
    [set counter counter - 1
      set label counter 
      if counter = 0 
      [forward 1]
    ]
    ]

end



to keep-going
  ask wmachines-on patch 4 0[
    if not any? turtles-on patch-ahead 1 and xcor < 12
    [ forward 1]
  ]
  end

Solution

  • If your problem is that turtles leave patch 3 0 but then they do not move forward continuously beyond patch 4 0, it is simply because your keep-going procedure is only addressing turtles that are exactly on patch 4 0 (and for this reason the xcor < 12 part is completely unused).

    In general, it looks very complicated and unnecessary that you are using three different procedures (i.e. one before patch 3 0, one for patch 3 0, and one for patch 4 0 but which should really be beyond patch 3 0) each of which is hard-coding some location in your model.

    The whole point of having a counter is that you can generalise a waiting condition across the whole model, so your go procedure can be simplified a lot by simply asking agents that have concluded their countdown to do one thing, and those who have not concluded their countdown to do another thing.

    Look at this minimal reproducible example where I have an unpredictable arrangement of stopping-patches but implement the waiting condition in a very general and simple way:

    turtles-own [
      counter
    ]
    
    to setup
      clear-all
      reset-ticks
      
      resize-world 0 30 0 4
      set-patch-size 25
      
      ask patches with [pxcor = min-pxcor] [
        sprout 1 [
          set heading 90
          set color lime
        ]
      ]
      
      ask n-of 15 patches with [pxcor > min-pxcor] [
        set pcolor brown
      ]
    end
    
    
    to go
      ask turtles [
        ifelse (counter = 0)
        ;; If the counter equals 0:
          [forward 1
           if (pcolor != black) [
             set counter 10
           ]
          ]
        ;; If the counter does not equal 0:
          [set counter counter - 1]
        
        
        ifelse (pcolor = black)
        ;; If the turtle is on a black patch:
          [set label ""]
        ;; If the turtle is not on a black patch:
          [set label counter]
      ]
      
      tick
    end