Search code examples
netlogo

Mousetrap time trigger and time counting


I am working on mousetrap model using the primitives in Netlogo, to simulate the trap triggered(pop) and pcolor changed with considering ticks (time). There is a similar model in Netlogo library, code examples--extensions -- time--discrete event mousetrap. However, my attempt did not use time series extension but all primitives. I got some problems and questions for this model:

1/ I want to use Behaviorspace to test the change of maximum distance with count the time when most balls are in the air, the time at the last trap triggers, and the fraction of all traps that get triggered. How should I set the reporters in experiment?

2/ I do not understand what is the difference that if I change the procedure in Pop 'if pcolor = yellow ' instead of 'if trigger-time < 0', I did not find any change between this change, the model still run. But, it seems like pcolor = yellow is not precise to count the trigger time. What is the effect to change?

3/ I want to update another procedure in order to visualize the path that patch trigger which other patch (pcolor changed), to view the relation of the start triggering patch and the patches got triggered from it. I was thinking to draw a line, but I am not sure about how to code it using primitives.

I attached my codes and the outcome what I have worked out now. Thanks for your help.

Outcome by now

BehaviorSpace by now

patches-own [trigger-time] ; The non-integer time at which trap triggers(ball in the air)

to setup
ca
ask patches
[
  set pcolor yellow         ; Yellow means trap has not triggered
  set trigger-time -1       ; Initialize trigger-time=-1 to before model starts
]
reset-ticks
end


to pop
show trigger-time         ; Test output- trigger times should always increase
set pcolor red            ; Show the snap
wait 0.05                 ; So we can see things happen on the View
set pcolor black          ; Black means the trap has triggered. 
                      
; Send 2 balls in air, determine where and when they land
ask n-of 2 patches in-radius 5
[
  if pcolor = yellow       ; what is the difference between the line right below?
   ;if trigger-time < 0     ; Patch only triggers if it has not already
   [ ;Set the time when the ball will land on and trigger trap
     ;set trigger-time ticks + (random-float 0.2 * distance myself) 
     ; random-float return greater than 0, less than 0.2
     ; for set up the behaviorspace experiment, add a new parameter max_dist instead of the 'distance myself' above, and max_dist is a slider on interface (global variable)

    set trigger-time ticks + (random-float 0.2 * max_dist)
    ]
  ]
 end

  to start
  ask one-of patches [pop]   ; Set off one trap to start the action
                       
   ; Now keep stepping time forward to next time a trap is triggered, as long as there are any balls in the air

   while [any? patches with [trigger-time > ticks] ]
    [ ; Find the triggered trap that has next trigger time
      let next-patch-to-trigger min-one-of patches with [trigger-time > ticks] [trigger-time]
      let time-til-next-trigger [trigger-time] of next-patch-to-trigger - ticks

      ; Move time forward to when next trap triggers and trigger it
      tick-advance time-til-next-trigger
      ask next-patch-to-trigger [pop]
      update-output

     ] ; while end
    end

 to update-output
 set-current-plot "balls in air"   ; plot on interface
 plotxy ticks count patches with [trigger-time > ticks]

 set-current-plot "untriggered traps"  ; plot on interface
 plotxy ticks count patches with [pcolor = yellow]

 end

Solution

  • First, I think your code is quite clever and close to the original. For your questions:

    1/ You could just define 2 global variables that contain the time when most balls are in the air and the time at which the last trap triggers. You can update the max number of balls in the air in start, perhaps just before update-output, using code like this:

    let balls-in-air-now count patches with [trigger-time > ticks]
    if balls-in-air-now > max-balls-in-air  [ set max-balls-in-air balls-in-air-now ]
    

    You could update the last trigger time in pop, using something like:

    set last-trigger-time trigger-time
    

    Then in BehaviorSpace, create output only at the end of each model run and have it output these reporters: max-balls-in-air, last-trigger-time, and count patches with [pcolor = black] / count patches.

    2/ The difference between using pcolor = yellow and trigger-time > 0 is subtle but important. Trigger-time is updated when the ball that will land on a new trap is launched, but pcolor is not updated until that ball lands. So, in pop, when you are testing whether another trap will pop when a ball lands on it, if you only look at that trap's pcolor then your model thinks that any patch currently un-popped will pop when a ball lands on it. That is not true because another ball that is already in the air could land on it first. In fact, your code is still not quite correct: to determine whether a new ball will pop a trap, you must determine whether that trap's trigger time is before the time when the ball will land. If you add the visualization in your question 3/, you will see the problem.

    The code in the Models Library's Time Extension code examples (which I wrote) fixes this in the snap procedure by not letting a trap pop if it has been triggered by an earlier ball (and therefore it's color is black).

    3/ To show where the balls start and where they end, you can create a NetLogo link from where the ball starts to where it lands. But links can only connect turtles, and you currently have no turtles in the model. So you would have to create hidden turtles in the two patches and then link them. This turns out to be rather tricky, but this seems to work: In pop, after set trigger-time... add:

      let land-patch self
      let trap-patch myself
      sprout 1 [ set hidden? true]
      ask trap-patch
      [ 
        sprout 1 [set hidden? true]
      ]
      ask turtles-on trap-patch [ create-link-to one-of turtles-on land-patch ]