Dark theme

Functions


How did you get on? Here's one version:

def distance_between(agents_row_a, agents_row_b):
    return (((agents_row_a[0] - agents_row_b[0])**2) + ((agents_row_a[1] - agents_row_b[1])**2))**0.5

Why do we use agents_row_a[0] rather than agents_row_a[0][0]?

Once you've got that working, try and build a for-loop structure that tests every agent against ever other agent. Place it at the bottom of the script, where you placed the previous function call.

Now, in theory, we should only need to test half the combinations (because the distance between two agents doesn't change based on their order). Indeed, we need to test less than half, as we don't need to test agents against themselves. Nevertheless, start by getting it to work with every combination of agents.


Now, this is quite a computationally aggressive bit of code, and it is likely to get worse as we increase the number of agents. One thing we might like to consider is timing the code so we can see how quickly it runs, and therefore whether it is worth optimising the combination process.

Timing code is an important part of working out how it will scale and how long a job will take to run. Note that just because code runs at one speed with a certain about of data, does not mean it will run the same speed with more, or that it will scale linearly: as computers run out of memory, they can slow down dramatically.

If you want to time how long a section of code takes to run in seconds, up to Python 3.8 you could do this:

import time

start = time.clock()

# The code to run, here.

end = time.clock()

print("time = " + str(end - start))

However since Python 3.3, there has been a deprecation warning which explains that from Python 3.8 time.clock will be removed. The warning suggests an alternative to use. The text of the warning is as follows: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead It is useful to know about deprecation for several reasons. We don't teach you how to deprecate your code in this introductory course, but if in the future you want to support users of your code and want to change the way your code works, then this is something that is important to learn as it help keep users happy. Essentially, deprecation helps users of code understand when the language is changing and gives them time to adapt. So, some timing code that works from Python 3.9 is: import time

start = time.process_time()

# The code to run, here.

end = time.process_time()

print("time = " + str(end - start))

A few notes:

Note that you may have to raise the number of agents to 100 to see anything in the way of processing longer than a microsecond.

Note also that there are more involved ways of timing things that try to make timing more consistent (for example, using timeit). You may get different timings for running the same code as both the Python enviornment and other processes running on your machine can vary the speed at which your program executes.

Try changing the number of agents and see how the timing scales when running through all the agents.


That's it for this practical, but if you've got some time, here's a few challenges you might like to try:

1) Can you find the maximum and minimum distances between your agents?

2) Can you optimise the search so it doesn't repeat pairs of agents or test agents against themselves?

3) Can you try a variety of different orders of magnitude of agent numbers, record the times, and plot them as a scattergraph?


  1. The code
  2. Building a function
  3. This page