Reference 〉 Class

MultiLevel( str breed, int id, Helipad model )

A class allowing multi-level agent-based models to be constructed, where the agents at one level are themselves full models with sub-agents. This class inherits all the methods and properties from both Agent and Helipad, which will not be reproduced here. See the Deme Selection sample model for an example.

To use a multi-level model, import the class and register it with model.addPrimitive().

Initialization Parameters

  • breed str, required

    The breed of the newly created agent.

  • id int, required

    A unique (at the top level) ID for the agent. Unexpected results may occur if non-unique ids are used.

  • model Helipad, required

    The model object of which this agent is a child.


  • dontStepAgents bool

    By default, all sub-agents are stepped each stage of the main model. This property can be set on a per-stage basis to prevent the sub-agents from being stepped. It is reset to False at the beginning of each stage.

    Initial value: False

Notes and Examples

  1. charwick

    May 09, 2020 at 18:02

    To set up a multi-level model, register a primitive with MultiLevel as the class. The default 'agent' primitive can also optionally be removed.

    from helipad import Helipad, MultiLevel
    heli = Helipad()
    heli.addPrimitive('deme', MultiLevel, dflt=20, priority=1)
  2. charwick

    May 09, 2020 at 18:15

    Note that any setup that would have been done in the main loop for the main model will have to be done individually for each sub-model, including parameters, hooks, shocks, goods, and breeds. This can be done in the init hook of the MultiLevel primitive in the main model. With MultiLevel used for a 'deme' primitive,

    def agentStep(agent, deme, stage):
    	# Write sub-agent behavior here
    # This takes advantage of the [primitive]Init hook pattern
    def demeInit(deme, model):
    	deme.param('agents_agent', 20)
    	deme.addBreed('altruist', '009900')
    	deme.addBreed('selfish', '990000')
    	deme.addGood('payoff', '000099', 1)
    	deme.addHook('agentStep', agentStep)

    Notice in particular that sub-agent behavior has to be hooked with deme.addHook rather than heli.addHook, and the model argument of the hooked function has been named deme rather than model to avoid ambiguity between levels.

  3. charwick

    May 09, 2020 at 18:44

    An example of dontStepAgents to make sure that each MultiLevel agent only steps its sub-agents in stage 1 of the top-level model.

    heli.stages = 3
    heli.order = ['linear', 'linear', 'match']
    # This takes advantage of the [primitive]Step hook pattern
    def demeStep(deme, model, stage):
    	if stage > 1: deme.dontStepAgents = True
  4. Contribute a Note

    Your email address will not be published.

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