0

I'm trying to complete the Mesa tutorial (https://media.readthedocs.org/pdf/mesa/latest/mesa.pdf) and am running into repeated attribute errors.

I just tried this code (exactly as it's printed in the tutorial):

import random
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid

class MyAgent(Agent):
    def __init__(self, name, model):
        super().__init__(name, model)
    def step(self):
        print("{} activated".format(self.name))

class MyModel(Model):
    def __init__(self, n_agents):
        self.schedule = RandomActivation(self)
        self.grid = MultiGrid(10, 10, torus=True)
        for i in range(n_agents):
            a = MyAgent(i, self)
            self.schedule.add(a)
            coords = (random.randrange(0, 10), random.randrange(0, 10))
            self.grid.place_agent(a, coords)
    def step(self):
        self.schedule.step()

I ran it like so:

model = MyModel(5)
model.step()

And ended up getting the following error: Traceback (most recent call last):

File "", line 2, in model.step()

File "", line 11, in step self.schedule.step()

File "/anaconda3/lib/python3.6/site-packages/mesa/time.py", line 113, in step agent.step()

File "", line 5, in step print("{} activated".format(self.name))

AttributeError: 'MyAgent' object has no attribute 'name'

I've looked at a number of postings dealing with attribute errors and none of them seem to be applicable to whatever is going on here. Any suggestions?

Thanks!

0 Answers0