agent.py
is the base class of all agents, environments, and objects contained in an environment.
Its basic character is that it is a vector, and supports basic
vector and matrix operations.
In [1]:
cd ..
In [2]:
from indra2.agent import Agent
Agent class constructor accepts 5 parameters:
Lets create an agent called newton.
In [3]:
def newt_action(agent):
print("I'm " + agent.name + " and I'm inventing modern mechanics!")
newton = Agent("Newton",
attrs={"place": 0.0, "time": 1658.0, "achieve": 43.9},
action=newt_action,
duration=30)
Now we will explore all the magic methods of agent class.
In [4]:
len(newton)
Out[4]:
In [5]:
str(newton)
Out[5]:
In [6]:
newton['time']
Out[6]:
In [7]:
newton['place'] = 2.5
newton['place']
Out[7]:
In [8]:
"time" in newton
Out[8]:
In [9]:
for attr in newton:
print(attr)
In [10]:
for attr in reversed(newton):
print(attr)
In [11]:
LEIBBYEAR = 1646
LEIBDYEAR = 1716
def leib_action(agent):
print("I'm " + agent.name + " and I'm inventing calculus!")
leibniz = Agent("Leibniz",
attrs={"place": 0.0, "time": LEIBBYEAR},
action=leib_action,
duration=20)
other_Leibniz = Agent("Leibniz",
attrs={"place": 1.0, "time": LEIBBYEAR},
action=leib_action,
duration=20)
print("Leibniz & othere_Leibniz:", leibniz == other_Leibniz)
print("Leibniz & Leibniz:", leibniz == leibniz)
print("Leibniz & Newton:", leibniz == newton)
In [12]:
repr(leibniz)
Out[12]:
act()
function, do nothing. Agents should return True if they did, in fact,'do something,' or False if they did not.
In [13]:
newton()
leibniz()
Out[13]:
In [14]:
newton
Out[14]:
In [15]:
newton += 2
In [16]:
newton
Out[16]:
In [17]:
newton += 2
newton
Out[17]:
In [18]:
newton -= 2
newton
Out[18]:
In [19]:
newton *= 2
newton
Out[19]:
In [21]:
import composite
In [45]:
comp = newton + leibniz
In [46]:
comp
Out[46]:
Now we will explore general class methods
In [47]:
newton.to_json()
Out[47]:
In [48]:
ModernNewton = Agent("ModerNewton",
attrs={"place": 0.0, "time": 1658.0, "achieve": 43.9},
action=newt_action,
duration=30)
In [49]:
ModernNewton.join_group(comp)
In [50]:
ModernNewton
Out[50]:
In [51]:
comp
Out[51]:
In [59]:
newton.same_type(leibniz)
Out[59]:
In [60]:
newton.same_type(newton)
Out[60]:
In [63]:
newton.attrs_to_dict()
Out[63]:
In [66]:
newton.sum()
Out[66]:
In [75]:
newton.magnitude()
Out[75]:
In [76]:
# newton.die()
In [78]:
newton
Out[78]:
In [79]:
newton.set_pos(100,100)
In [80]:
newton
Out[80]:
In [ ]: