Part 1 in a series of 9 practicals with instructions to develop a simple spatial Agent Based Model (ABM) - a model for simulating the actions and interactions of entities (agents) located within a spatial environment which may change and be changed by the agents.
Agents will be located at coordinates in the environment and move around randomly interacting with the environment and each other. How the agents interact will be based on their characteristics, the characteristics of their environment, and the characteristics of other agents in their environment.
The model will iterate with a regular time step in that all agents will have a turn at interacting in each iteration.
You will learn about:
The final version of will:
Initially two agents are going to be represented in an abstract two dimensional plane. The location of these agents will be given by the coordinates: (x0, y0) and (x1, y1). They will be initialised at the same location and will move randomly one step. Code will be written to calculate the distance between coordinates.
The code will subsequently be refactored (rewritten) so it is more concise and easier to understand, maintain and reuse.
The algorithm to implement for this practical can be written as follows:
# Initialise variable x0 # Initialise variable y0 # Change x0 and y0 randomly # Initialise variable x1 # Initialise variable y1 # Change x1 and y1 randomly # Calculate the Euclidean distance between (x0, y0) and (x1, y1)
In your local code repository create a new directory 'src' and within this create a new directory 'abm1'.
Create a file 'model.py' in the 'abm1' directory.
Copy the algorithm above and paste it in.
Add a line after the line "# Initialise a variable x0", to initialise a variable 'x0' with the value '0', then a line to print 'x0':
x0 = 0
print("x0", x0)
Save and run the program. The output from running the program should be:
x0 0
Note that two parameters are passed into the printfunction. These are printed out sequentially with a space delimiter by default.
Do similarly to initialise the variable 'y0'.
The Python standard library random module implements pseudo-random number generators for various distributions and provides a way to change variable values randomly. To use the module it has to be imported. Towards the top of 'model.py' add the following import statement:
import random
It is good practice to organise import statements at the top of files as this makes them easy to find and makes it less likely that modules are imported multiple times. Whilst importing modules multiple time is not likely to result in errors, it is usually not necessary and generally inefficent.
Use an if statement to determine whether to increase or decrease 'x0' based on the value obtained from a call to the functionnull: First obtain and print a pseudo-random number in the range [0, 1) as follows:
rn = random.random()
print(rn)
Run your program a few times to observe that the value of 'rn' varies somewhat randomly.
What is happening, is that the start (seed) of a pseudo-random sequence used to generate numbers randomly is being set from the computer clock time (which updates many times a second). So, each time the code is run, a different seed is set and the movement of the agents might be different. The seed set in this way would nearly always be different and so results will often be different and the more so, the more changes there were in the model. We will reconsider how to make results more reprocuible in due course.
Declare an 'if statement' so that if the value 'rn' is less than '0.5' increase 'x0' by '1', otherwise decrease "'x0' by '1':
if rn < 0.5:
x0 = x0 + 1
else:
x0 = x0 - 1
print("x0", x0)
Run your program a few times to observe that sometimes the value of 'x0' increases and sometimes it decreases and this corresponds to the value of 'rn'.
When testing code, it can be difficult if results vary from one run to the next, so it can be helpful to set the random seed to ensure that the same results are produced each time. At the top of your code, after the import statements add the following:
# Set the pseudo-random seed for reproducibility
random.seed(0)
When run, the program should generate the following output:
x0 0 y0 0 rn 0.8444218515250481 b False x0 - 1
To generate different results you pass in a different parameter value into the 'seed' function.
Similarly modify the value of 'y0'. Then similarly initialise the variables 'x1' and 'y1' and then similarly modify these randomly. Test that the movements vary for different seeds and that changes in each coordinate are independent of how the other coordinates change.
Use the Pythogorean theorem to calculate the distance between the coordinates (x0, y0) and (x1, y1). The algorithm is:
# Calculate the difference in the x coordinates. # Calculate the difference in the y coordinates. # Square the differences and add the squares # Calculate the square root
At the end of your source code set: 'x0' and 'y0' to equal '0'; 'x1' to equal '3', and 'y1' to equal '4'. Then copy and paste the Pythogorean theorem algorithm into your source code. Try to implement the algorithm, but spend no more than 5 minutes trying to do this. Hints: the distance between coordinates (0, 0) and (3, 4) is 5; the symbol '**' can be used to raise a number to the power of another number; raising a number to the power of '0.5' calculates the square root; testing can simply be done by printing out the result.
Hopefully, you managed to develop code that produces the correct answer. If not, do not worry, the important thing is to have tried and not spent too long trying.
There is a lot of repetition in the code.
An alternative, to using '**0.5' to calculate a square root is to import the standard library math module and use the sqrt function.
Add and commit to your local git repository and assuming you are using GitHub - push your changes to GitHub.
In the next ABM practical: The code will be modified so the coordinates for each agent are stored together in a container; for loopswill be used to create and move more agents, and the model will be visualised.