Reference 〉 Class
baseAgent
Helipad

MultiLevel(breedstr, idint, modelHelipad)

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 baseAgent 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 Agents.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.

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.agents.addPrimitive('deme', MultiLevel, dflt=20, priority=1)
    heli.agents.removePrimitive('agent')
  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
    	pass
    
    # This takes advantage of the [primitive]Init hook pattern
    @heli.hook
    def demeInit(deme, model):
    	deme.param('agents_agent', 20)
    	deme.addBreed('altruist', '009900')
    	deme.addBreed('selfish', '990000')
    	deme.goods.add('payoff', '000099', 1)
    	deme.hooks.add('agentStep', agentStep)

    Notice in particular that sub-agent behavior has to be hooked with deme.hooks.add rather than heli.hooks.add, 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 using cutStep() to make sure that each MultiLevel agent only steps its sub-agents in stage 1 of the top-level model. Notice we call cutStep() on deme (the MultiLevel object) and not model.

    heli.stages = 3
    heli.order = ['linear', 'linear', 'match']
    
    # This takes advantage of the [primitive]Step hook pattern
    @heli.hook
    def demeStep(deme, model, stage):
    	if stage > 1: deme.cutStep()
  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.

History