Agent Based Model Practical 9

1. Introduction and Preparation

This practical is about using some data in an HTML file Table to initialise part of the model.

In your local code repository 'src' directory duplicate your 'abm8' directory as 'abm9'.

2. Parsing an HTML File

The HTML to parse.

Have a look at the source code for the file by either opening it in a text editor or viewing the source in your Web browser.

Add the following import statements to 'model.py':

import requests
import bs4

Change the agent initialisation code block as follows:


# Initialise agents

url = 'https://agdturner.github.io/resources/abm9/data.html'
r = requests.get(url, 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)
agents = []
for i in range(n_agents):
    # Create an agent
    y = int(td_ys[i].text) + 99
    x = int(td_xs[i].text) + 99
    agents.append(af.Agent(agents, i, environment, n_rows, n_cols, x, y))
    print(agents[i].agents[i])

Change the '__init__' function in 'agentframework.py' to:

");
def __init__(self, agents, i, environment, n_rows, n_cols, x = None, y = None):
"""
The constructor method.

Parameters
----------
i : Integer
    To be unique to each instance.
environment : List
    A reference to a shared environment
n_rows : Integer
    The number of rows in environment.
n_cols : Integer
    The number of columns in environment.
x : Integer
    For initialising the x coordinate of the agent.
y : Integer
    For initialising the y coordinate of the agent.

Returns
-------
None.

"""
self.agents = agents
self.i = i
self.environment = environment
if x == None:
    tnc = int(n_cols / 3)
    self.x = random.randint(tnc - 1, (2 * tnc) - 1)
else:
    self.x = x
if y == None:
    tnr = int(n_rows / 3)
    self.y = random.randint(tnr - 1, (2 * tnr) - 1)
else:
    self.y = y
self.store = random.randint(0, 99)
self.store_shares = 0

3. Assignment 1 submission

Update the README file for your repository and submit your work.