Reference 〉 Decorator

@Helipad.event( str name = None, bool repeat = False,  **kwargs )

A function decorator used to register an Event. When triggered, an event stores the data output at that time and registers on the visualizer. TimeSeries, for example, draws a vertical line on the plots (see the image to the right), and Charts will flash the background. In the image to the right there are two events, marking the increase and leveling off of population.

This decorator can be used bare (i.e. without parentheses) on a function whose name will be used as the event name. It can also take arguments, which are passed to model.addEvent().

The decorated function will run at the end of each period, immediately after data collection but before the modelPostStep hook.

Parameters

  • name str, optional

    An optional reference name, to override the function name.

    Default value: None

  • repeat bool, optional

    Whether the event should trigger once, or every time the criterion is satisfied.

    Default value: False

  • **kwargs optional

    Further options to be passed to the visualizer's event() method. TimeSeries.event(), for example, draws a vertical line on the plot area, and takes color, linestyle, and linewidth arguments to customize its appearance.

    Default value: {}

Notes and Examples

  1. charwick

    Dec 14, 2020 at 22:05

    The image above is generated by the following two events, the first of which fires when the increase in population over the last 20 periods is greater than 3, and the second when the first has triggered and the increase in population over the last period.

    @heli.event
    def exp1(model): return sum(diff(model.data.getLast('ruralPop', 20))) > 3
    
    @heli.event
    def taper2(model): return model.events['exp1'].triggered and sum(diff(model.data.getLast('ruralPop', 20))) < -1

    The second is an example of ordering events: the second will only fire after the first.

  2. charwick

    Dec 14, 2020 at 22:08

    The decorated function can be written so as to take an action when the event is triggered, as in the following:

    @heli.event
    def stab4(model):
    	go = model.events['cull3'].triggered and sum(diff(model.data.getLast('ruralPop', 400))) > 0
    	if go:
    		#Do some task when the event fires
    		pass
    	return go
  3. charwick

    Dec 14, 2020 at 22:12

    Models can be set to stop at an event, by setting the stopafter parameter to the name of an event, as in this example from the walkthrough, which terminates the model once the volume of trade for each good is under 20.

    #Stop the model when we're basically equilibrated
    @heli.event
    def stopCondition(model):
    	return model.t > 1 and model.data.getLast('demand-shmoo') < 20 and model.data.getLast('demand-soma') < 20
    heli.param('stopafter', 'stopCondition')
  4. Contribute a Note

    Your email address will not be published. Required fields are marked *

    You may use limited HTML for formatting. Please embed blocks of code in <pre><code> </code></pre> tags.