Matplotlib
In this section we'll initialise our model with the y and x data.
How did you get on? Here's one solution.
import requests
import bs4
r = requests.get('http://www.geog.leeds.ac.uk/courses/computing/practicals/python/agent-framework/part9/data.html', verify=False)
content = r.text
soup = bs4.BeautifulSoup(content, 'html.parser')
td_ys = soup.find_all(attrs={"class" : "y"})
td_xs = soup.find_all(attrs={"class" : "x"})
print(td_ys)
print(td_xs)
Given we've now got the y
and x
data from the site, let's use it to
initialize our model.
Remembering how we passed in the agents list and the environment, adjust your agent initialisation loop, thus:
for i in range(num_of_agents):
y = int(td_ys[i].text)
x = int(td_xs[i].text)
agents.append(agentframework.Agent(environment, agents, y, x))
Can you now adjust agentframework.py
to use these new values? Start by
getting the two ints into the constructor by adjusting the function header.
Note that it might be worth doing this kind of thing, so it will still run with
missing y
and x
values.
if (x == None):
self._x = random.randint(0,100)
else:
self._x = x
If you adjust the function header to give the default of the two variables as
None
this will then work if they are missing.
Once you've done that, your agent based model is done. We now have a model that:
- builds agents in a space;
- gets them to interact with each other;
- reads in environmental data;
- gets agents to interact with the environment;
- randomizes the order of agent actions;
- displays the model as an animation;
- is contained within a GUI;
- is initialised with data from the web.
In total, then, there's a lot going on. Hopefully you can see how the model would be a useful basis for a great many models of social, environmental or ecological systems. If you have some time, do revisit Part 8 and think about additions to your model.