A First Model Using Indra2

The aim of re-writing the library code upon which Indra models relied was to reduce the complexity of the system, and achieve greater expressiveness with fewer lines of code.

So far, the results look promising. Let's take a look at the beginnings of the first model being written based on the indra2 library. This is a classic predator-prey model, often called "wolf-sheep" in the ABM world. The central idea is to model how populations of predators and their prey will cycle in a circumscribed environment.

The first thing to do is import the (few!) items we need to get going:


In [1]:
from indra.agent import Agent, AgentEncoder
from indra.composite import Composite
from indra.env import Env


/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scipy/__init__.py:115: UserWarning: Numpy 1.13.3 or above is required for this version of scipy (detected version 1.13.1)
  UserWarning)

Next define a few constants -- these will be params in the fully developed system:


In [2]:
NUM_WOLVES = 3
NUM_SHEEP = 12

WOLF_LIFESPAN = 5
WOLF_REPRO_PERIOD = 6

SHEEP_LIFESPAN = 8
SHEEP_REPRO_PERIOD = 6

Now we will define what wolves and sheep do when called upon to act -- the model is far from done at this point, and these actions are trivial, but they illustrate the use of the system:


In [ ]:
def sheep_action(agent):
    print("I'm " + agent.name + " and I eat grass.")
    
def wolf_action(agent):
    print("I'm " + agent.name + " and my remaining life is: "
          + str(agent.duration))

Next let's create to helper functions to create wolves and sheep, just to make the code following this a little simpler:


In [6]:
def create_wolf(i):
    return Agent("wolf" + str(i), duration=WOLF_LIFESPAN,
                     action=wolf_action,
                     attrs={"time_to_repr": WOLF_REPRO_PERIOD})


def create_sheep(i):
    return Agent("sheep" + str(i), duration=SHEEP_LIFESPAN,
                     action=sheep_action,
                     attrs={"time_to_repr": SHEEP_REPRO_PERIOD})

Now we have everything we need to create our populations and put them in a meadow to interact:


In [7]:
wolves = Composite("wolves")
for i in range(NUM_WOLVES):
    wolves += create_wolf(i)

sheep = Composite("sheep")
for i in range(NUM_SHEEP):
    sheep += create_sheep(i)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-90345ac69f9c> in <module>()
      1 wolves = Composite("wolves")
      2 for i in range(NUM_WOLVES):
----> 3     wolves += create_wolf(i)
      4 
      5 sheep = Composite("sheep")

<ipython-input-6-8785ab3c1afb> in create_wolf(i)
      1 def create_wolf(i):
      2     return Agent("wolf" + str(i), duration=WOLF_LIFESPAN,
----> 3                      action=wolf_action,
      4                      attrs={"time_to_repr": WOLF_REPRO_PERIOD})
      5 

NameError: name 'wolf_action' is not defined

Now we will create a "meadow" in which these critters will interact -- the meadow is an instance of Env:


In [4]:
meadow = Env("meadow")
meadow += wolves
meadow += sheep


Welcome to Indra, gcallah!
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-f4f9398b0894> in <module>()
      1 meadow = Env("meadow")
----> 2 meadow += wolves
      3 meadow += sheep

NameError: name 'wolves' is not defined

Lastly, we set things going in the meadow:


In [3]:
meadow()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-4a34772ce6ad> in <module>()
----> 1 meadow()

NameError: name 'meadow' is not defined

In [ ]:
3 + 7

In [ ]:
3 + + 7

In [ ]:
3 + * + 7

In [ ]: