I tried implementing the tutorial from [mesa][1]. Here is the code that I have copied from there, split into two files, money_model.py, and server.py respectively:
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
class MoneyAgent(Agent):
""" An agent with fixed initial wealth."""
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.wealth = 1
def move(self):
possible_steps = self.model.grid.get_neighborhood(
self.pos,
moore=True,
include_center=False)
new_position = self.random.choice(possible_steps)
self.model.grid.move_agent(self, new_position)
def give_money(self):
cellmates = self.model.grid.get_cell_list_contents([self.pos])
if len(cellmates) > 1:
other_agent = self.random.choice(cellmates)
other_agent.wealth += 1
self.wealth -= 1
def step(self):
self.move()
if self.wealth > 0:
self.give_money()
class MoneyModel(Model):
"""A model with some number of agents."""
def __init__(self, N, width, height):
self.num_agents = N
self.grid = MultiGrid(width, height, True)
self.schedule = RandomActivation(self)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
self.schedule.add(a)
# Add the agent to a random grid cell
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))
def step(self):
self.schedule.step()
and
from money_model import *
from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
def agent_portrayal(agent):
portrayal = {"Shape": "circle",
"Filled": "true",
"Layer": 0,
"Color": "red",
"r": 0.5}
return portrayal
grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500)
server = ModularServer(MoneyModel,
[grid],
"Money Model",
{"N":100, "width":10, "height":10})
server.port = 8521 # The default
server.launch()
When I run the example, it gives me the following error:
Interface starting at http://127.0.0.1:8521
Traceback (most recent call last):
File "c:/Users/fisch/Desktop/mesa example/server.py", line 20, in <module>
server.launch()
File "C:\Program Files\villos\lib\site-packages\mesa\visualization\ModularVisualization.py", line 333, in launch
self.listen(self.port)
File "C:\Program Files\villos\lib\site-packages\tornado\web.py", line 2116, in listen
server.listen(port, address)
File "C:\Program Files\villos\lib\site-packages\tornado\tcpserver.py", line 152, in listen
self.add_sockets(sockets)
File "C:\Program Files\villos\lib\site-packages\tornado\tcpserver.py", line 165, in add_sockets
self._handlers[sock.fileno()] = add_accept_handler(
File "C:\Program Files\villos\lib\site-packages\tornado\netutil.py", line 279, in add_accept_handler
io_loop.add_handler(sock, accept_handler, IOLoop.READ)
File "C:\Program Files\villos\lib\site-packages\tornado\platform\asyncio.py", line 100, in add_handler
self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)
File "C:\Program Files\villos\lib\asyncio\events.py", line 501, in add_reader
raise NotImplementedError
NotImplementedError
This also happens when I run the project I am currently working on using "mesa runserver" when I am in the project directory that contains the run.py file. I have checked the environment variables and the path that contains mesa is present. Has anyone any idea where the problem may be? Thank you! [1]: https://mesa.readthedocs.io/en/master/tutorials/adv_tutorial.html
I got “NotImplementedError” because my I had an outdated version of mesa.
solved that.