Indra Agent Class

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


/Users/monjurhasan/Desktop/DesktopFiles/CS/indras_net

In [2]:
from indra2.agent import Agent

Agent class constructor accepts 5 parameters:

  1. name
  2. attrs
  3. action
  4. duration
  5. groups

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.

  • len returns number of attributes of an agent

In [4]:
len(newton)


Out[4]:
3
  • str returns name of the agent

In [5]:
str(newton)


Out[5]:
'Newton'
  • getitem returns value of an attribute

In [6]:
newton['time']


Out[6]:
1658.0
  • setitem sets/changes value of an attribute. Returns keyerror if agent doesn't have the attribute.

In [7]:
newton['place'] = 2.5
newton['place']


Out[7]:
2.5
  • contains returns true if agent contains the attribute

In [8]:
"time" in newton


Out[8]:
True
  • iter loops over attributes of an agent

In [9]:
for attr in newton:
    print(attr)


place
time
achieve
  • reversed loops over attributes in reverse order

In [10]:
for attr in reversed(newton):
    print(attr)


achieve
time
place
  • eq checks if two agents are equivalent

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)


Leibniz & othere_Leibniz: False
Leibniz & Leibniz: True
Leibniz & Newton: False
  • repr

In [12]:
repr(leibniz)


Out[12]:
'{\n    "name": "Leibniz",\n    "duration": 20,\n    "pos": null,\n    "attrs": {\n        "place": 0.0,\n        "time": 1646.0\n    },\n    "groups": ""\n}'
  • call: Agents will 'act' by being called as a function. If the agent has no 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()


I'm Newton and I'm inventing modern mechanics!
I'm Leibniz and I'm inventing calculus!
Out[13]:
True
  • iadd increases value of each attributes

In [14]:
newton


Out[14]:
{
    "name": "Newton",
    "duration": 29,
    "pos": null,
    "attrs": {
        "place": 2.5,
        "time": 1658.0,
        "achieve": 43.9
    },
    "groups": ""
}

In [15]:
newton += 2

In [16]:
newton


Out[16]:
{
    "name": "Newton",
    "duration": 29,
    "pos": null,
    "attrs": {
        "place": 4.5,
        "time": 1660.0,
        "achieve": 45.9
    },
    "groups": ""
}

In [17]:
newton += 2 
newton


Out[17]:
{
    "name": "Newton",
    "duration": 29,
    "pos": null,
    "attrs": {
        "place": 6.5,
        "time": 1662.0,
        "achieve": 47.9
    },
    "groups": ""
}
  • isub this is opposite of iadd - substracts values of attributes.

In [18]:
newton -= 2
newton


Out[18]:
{
    "name": "Newton",
    "duration": 29,
    "pos": null,
    "attrs": {
        "place": 4.5,
        "time": 1660.0,
        "achieve": 45.9
    },
    "groups": ""
}
  • imul multiplies each attributes

In [19]:
newton *= 2
newton


Out[19]:
{
    "name": "Newton",
    "duration": 29,
    "pos": null,
    "attrs": {
        "place": 9.0,
        "time": 3320.0,
        "achieve": 91.8
    },
    "groups": ""
}
  • add adds two agents making it a composite/group. Name of the composite/group is concatinated names of all the agents. As shown below, agents become members of the composite.

In [21]:
import composite

In [45]:
comp = newton + leibniz

In [46]:
comp


Out[46]:
{
    "name": "NewtonLeibniz",
    "attrs": {},
    "members": {
        "Newton": {
            "name": "Newton",
            "duration": 29,
            "pos": null,
            "attrs": {
                "place": 9.0,
                "time": 3320.0,
                "achieve": 91.8
            },
            "groups": "NewtonLeibniz "
        },
        "Leibniz": {
            "name": "Leibniz",
            "duration": 19,
            "pos": null,
            "attrs": {
                "place": 0.0,
                "time": 1646.0
            },
            "groups": "NewtonLeibniz "
        }
    }
}

Now we will explore general class methods

  • to_json() - Returns json respresentation of the agent.

In [47]:
newton.to_json()


Out[47]:
{'name': 'Newton',
 'duration': 29,
 'pos': None,
 'attrs': OrderedDict([('place', 9.0), ('time', 3320.0), ('achieve', 91.8)]),
 'groups': 'NewtonLeibniz '}

In [48]:
ModernNewton =  Agent("ModerNewton",
                  attrs={"place": 0.0, "time": 1658.0, "achieve": 43.9},
                  action=newt_action,
                  duration=30)
  • join_group

In [49]:
ModernNewton.join_group(comp)

In [50]:
ModernNewton


Out[50]:
{
    "name": "ModerNewton",
    "duration": 30,
    "pos": null,
    "attrs": {
        "place": 0.0,
        "time": 1658.0,
        "achieve": 43.9
    },
    "groups": "NewtonLeibniz "
}

In [51]:
comp


Out[51]:
{
    "name": "NewtonLeibniz",
    "attrs": {},
    "members": {
        "Newton": {
            "name": "Newton",
            "duration": 29,
            "pos": null,
            "attrs": {
                "place": 9.0,
                "time": 3320.0,
                "achieve": 91.8
            },
            "groups": "NewtonLeibniz "
        },
        "Leibniz": {
            "name": "Leibniz",
            "duration": 19,
            "pos": null,
            "attrs": {
                "place": 0.0,
                "time": 1646.0
            },
            "groups": "NewtonLeibniz "
        }
    }
}
  • same_type - Returns true if agents are of same type.

In [59]:
newton.same_type(leibniz)


Out[59]:
False

In [60]:
newton.same_type(newton)


Out[60]:
True
  • attrs_to_dict - returns ordered dictionary representing attributes

In [63]:
newton.attrs_to_dict()


Out[63]:
OrderedDict([('place', 9.0), ('time', 3320.0), ('achieve', 91.8)])
  • sum - Returns sum of all the attributes

In [66]:
newton.sum()


Out[66]:
3420.8
  • magnitude -

In [75]:
newton.magnitude()


Out[75]:
3321.2811142690107
  • die - makes agent inactive

In [76]:
# newton.die()
  • is_active - returns true if agent is active
  • set_pos, get_pos, get_x, get_y - sets or returns x,y co-ordinates of the agent.

In [78]:
newton


Out[78]:
{
    "name": "Newton",
    "duration": -1,
    "pos": null,
    "attrs": {
        "place": 9.0,
        "time": 3320.0,
        "achieve": 91.8
    },
    "groups": "NewtonLeibniz "
}

In [79]:
newton.set_pos(100,100)

In [80]:
newton


Out[80]:
{
    "name": "Newton",
    "duration": -1,
    "pos": [
        100,
        100
    ],
    "attrs": {
        "place": 9.0,
        "time": 3320.0,
        "achieve": 91.8
    },
    "groups": "NewtonLeibniz "
}
  • is_located - Returns true if position of the agent is not None

In [ ]: