Indra: Basic Capabilities of the System

Here we are not going to look at any actual model of anything, but rather at the basic capabilities the Indra system provides to any modeling effort. The system consists of a heirarchy of classes that give the modeler a suite of capabilities useful in any agent-based model. To demonstrate the system and to give people new to it basic files to clone and begin work from, Indra includes two scripts that run a simple agent system: basic_model.py basic_run.py

All of our models follow this convention: one file implements the model (X_model.py), and another controls the parameters to the model and runs it (X_run.py). Let's have a look at basic_run.py and see what is in it. The comments are fairly complete as to what is going on, we hope!


In [2]:
!cat basic_run.py


#!/usr/bin/env python3
import logging
import prop_args as props
import entity as ent
import basic_model as bm

# set up some file names:
MODEL_NM = "basic_model"
PROG_NM  = MODEL_NM + ".py"
LOG_FILE = MODEL_NM + ".txt"

# We store basic parameters in a "property" file; this allows us to save
#  multiple parameter sets, which is important in simulation work.
#  We can read these in from file or set them here.
read_props = False
if read_props:
    pa = props.PropArgs.read_props(MODEL_NM, "basic.props")
else:
    pa = props.PropArgs(MODEL_NM, logfile=LOG_FILE, props=None)
    pa.set("model", MODEL_NM)
    pa.set("num_agents", 10)
    pa.set("user_type", ent.User.IPYTHON_NB)

# Now we create a minimal environment for our agents to act within:
env = bm.BasicEnv(model_nm=MODEL_NM)
# And put the environment in the properties object:
pa.set("env", env)

# Now we loop creating multiple agents with numbered names based on the loop variable:
for i in range(pa.get("num_agents")):
    env.add_agent(
            bm.BasicAgent(name="agent" + str(i),
            goal="acting up!"))

# Logging is automatically set up for the modeler:
logging.info("Starting program " + PROG_NM)

# And now we set things running!
env.run()

OK, let us run the system and see what it does:


In [3]:
run basic_run.py


Welcome, gcallah
Running in a basic environment to get you going
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: q
Returning to runtime environment.

The first thing to notice in this aborted run is that the modeler gets an entire menuing system "for free": this is inherited from the base environment class. This menu is a simple text-based one, but it is organized to that it can be implemented in a GUI. Next, we look at what happens when we "step" through the simulation:


In [4]:
run basic_run.py


Welcome, gcallah
Running in a basic environment to get you going
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent1 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent3 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent4 with a goal of acting up!
Agent agent2 with a goal of acting up!
Agent agent0 with a goal of acting up!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent9 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent4 with a goal of acting up!
Agent agent3 with a goal of acting up!
Agent agent0 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent1 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent2 with a goal of acting up!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: q
Returning to runtime environment.

The basic environment operation is to loop through the contained agents and call each agent's act() function. In this case, the agents just announce their name and goal. One item to note: the agents appear in random order each time through the loop. This is built into the basic environment in an efficient fashion, as usually we don't want the fact an agent happens to be first in a list to affect the outcome. Of course, because this is an object-oriented system, this behavior can be over-ridden.

Let us look at a couple of other things the base environment automatically provides the modeler. For one thing, we can examine the state of any agent, and change its field values:


In [2]:
run basic_run.py


Welcome, gcallah
Running in a basic environment to get you going
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent0 with a goal of acting up!
Agent agent4 with a goal of acting up!
Agent agent2 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent1 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent3 with a goal of acting up!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: i
Type the name of the agent to inspect: agent0
goal = acting up!
name = agent0
env = a basic environment to get you going
prehensions = []
Change a field's value in agent0? (y/n) y
Which field? name
Enter new value for name: Eugene
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent4 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent1 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent2 with a goal of acting up!
Agent agent3 with a goal of acting up!
Agent Eugene with a goal of acting up!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: q
Returning to runtime environment.

While the program is running, we have changed the name of agent0 to Eugene. Obviously, that change is trivial, but the modeler could, instead, do something like change the utility function of a trader or the ethnic preferences of an agent in a segregation model. Now, let us add an agent on the fly: we will add an agent of a new type: Gozer. If you remember Ghostbusters, that would be Gozer the Destructor. Let's see what Gozer does.


In [3]:
run basic_run.py


Welcome, gcallah
Running in a basic environment to get you going
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent1 with a goal of acting up!
Agent agent0 with a goal of acting up!
Agent agent2 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent4 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent3 with a goal of acting up!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: a
Enter constructor for agent to add: Gozer()
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent8 with a goal of acting up!
Agent agent2 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent0 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent3 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Agent agent5 with a goal of acting up!
Agent agent1 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent4 with a goal of acting up!
Gozer has destroyed agent0!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent2 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent3 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Agent agent4 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent1 with a goal of acting up!
Gozer has destroyed agent1!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent Gozer with a goal of Destroy!
Agent agent4 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent2 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent3 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent7 with a goal of acting up!
Gozer has destroyed agent2!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent7 with a goal of acting up!
Agent agent5 with a goal of acting up!
Agent agent4 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Agent agent3 with a goal of acting up!
Gozer has destroyed agent3!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent4 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Agent agent9 with a goal of acting up!
Agent agent8 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent5 with a goal of acting up!
Gozer has destroyed agent4!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent9 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Agent agent8 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent agent5 with a goal of acting up!
Gozer has destroyed agent5!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent8 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent6 with a goal of acting up!
Agent agent7 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Gozer has destroyed agent6!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent Gozer with a goal of Destroy!
Agent agent7 with a goal of acting up!
Agent agent9 with a goal of acting up!
Agent agent8 with a goal of acting up!
Gozer has destroyed agent7!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent agent9 with a goal of acting up!
Agent Gozer with a goal of Destroy!
Agent agent8 with a goal of acting up!
Gozer has destroyed agent8!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent Gozer with a goal of Destroy!
Agent agent9 with a goal of acting up!
Gozer has destroyed agent9!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: s
Agent Gozer with a goal of Destroy!
Gozer the destructor has destroyed all!!
File: (w)rite properties | e(x)amine log file | (q)uit
Edit: (a)dd agent to env | (i)nspect agent | inspect (e)nvironment
View: (l)ist agents | (v)isualize
Tools: (s)tep (default) | (r)un | (d)ebug
Choose one of the above and press Enter: q
Returning to runtime environment.

Gozer has entered the environment and turn by turn destroyed all of the other agents. Other capabilities already provided in our basic framework are listing all agents, examining log files, write properties (parameter sets) to disk, and launching the Python debugger.


In [ ]: