Dark theme

Data variables


In this practical we're going to build a main 'Model' program, containing variables representing two agents' locations (y and x coordinates for each), and we're going to start to build code to move them around. We're going to move them on a "random walk", that is, they'll start from some position, and then move one step in based on a random number. Once we've moved them a couple of steps, we'll work out the distance between them.

These are two key elements of ABM: agents need to move, and they need to work out whether they are close enough to interact.

Here's the algorithm for this practical:

Model:
# Make a y variable.
# Make a x variable.
# Change y and x based on random numbers.
# Make a second set of y and xs, and make these change randomly as well.
# Work out the distance between the two sets of y and xs.

   

 


Start by making a file called model.py in a new directory: \python\src\unpackaged\abm.

We'd suggest doing this with Spyder. Eventually we'll see how to store copies of code so you can roll back changes. For the moment, though, you may want to copy the code at the start of each practical into an backup directory so you know you have something that works. Put in a print ("hello world") (if Spyder hasn't made one for you) and run the code to check it works.

Why have we done something so simple? As we said in the last lecture, the best way to code is to build up a few lines at a time, and run regularly. We've built and tested something so simple so we can now have confidence to build on it.

Note that it is always worth thinking about how you would test any code you write to make sure it is working ok.

 


Delete the "hello world" code; make two variable labels, "y0" and "x0" and assign them the values 50 and 50.

[You may wonder why we're saying "y and x" rather than the more usual "x and y" – it doesn't actually especially matter for these coordinates which way round we treat them, as long as we're consistent, so the materials assume y,x, but the reason for putting them that way round is that when we deal with reading files, the y-dimension is usually first, so we want to get you thinking along those lines ready for later.]

Ultimately we'll put these two variables into some kind of agent code and then make lots of agents. For the moment though we already now have a representation of a point in space – and we're only two lines in!

You may wonder why we've called the y0 and x0 – the answer is that we're going to make several, and numbering in computing, as we'll see in the course, generally begins with zero.

Add in code to print the variables out and test they've been assigned properly

Again, run the file. Note that while the file just running would have been an indication it worked, we've gone the extra step of checking it worked how we expected. Let's now give our model some behaviour.

 


We'll now build a chunk of code which will alter y randomly.

Here's the algorithm:

# import random
# if random_number < 0.5:
#     y0 = y0 + 1
# else:
#     y0 = y0 - 1

You'll see that it uses some control flow: a set of if-else statements. These take the form:

if (some condition):
    # Do something.
else:
    # Do something else.

See if you can build it. Here's some stuff you'll need to know.

  • import statements are, by tradition, at the very top of files, just because it makes them easy to find.
  • The if and else statements (or "clause headers") are as printed above.
  • To generate a random number between 0 and 1, use the function random.random() – this is imported from the random module.
  • The statements to set y to a new value that happen under the if and else should be indented with four spaces. Spyder should do this if your push the TAB key. This isn't just to look nice   Python demands it.
  • You may also like to look up in your notes the "augmented" assignment form for y = y + 1 and get used to using it; we'll be using it alot.

If you manage to build it and get it running, great   think about how you'd check it is working. If you struggle, no problem, go to the next page and we'll look at the solution...


  1. Agent-based models
  2. Components of a model
  3. This page
  4. Moving y and x <-- next
  5. Distance between two points
  6. Final ideas