Data variables
So, how did you do with working out the distance?
Here's one solution:
answer = (((y0 - y1)**2) + ((x0 - x1)**2))**0.5
print(answer)
But sometimes it is more sensible to break complicated equations down into parts:
y_diff = (y0 - y1)
y_diffsq = y_diff * y_diff
x_diff = (x0 - x1)
x_diffsq = x_diff * x_diff
sum = y_diffsq + x_diffsq
answer = sum**0.5
print(answer)
Which you do depends entirely on how confident you are about stringing together the mathematics. The first is neater, but the second is easier to check by printing each stage. On the downside, the second creates more variables: if you were doing this a million times that would be a lot of unneeded variable creations, but a few times is fine. Quite often, in fact, you'll start out with the top code, and find there is a problem, and have to break it down into smaller chunks like the bottom code to find the exact point at which it is going wrong. That's fine; once you've found the error, you can then compress it all up into one line again.
The other thing that can help with finding errors is to hardwire in some values. At the moment it is quite hard to check the result because we don't know what y and x values will be generated. If we wait until these have been randomly generated, and then reset them to something we know (temporarily), we can check the code works ok. Once we're happy, we can remove the hardwired assignments. For example, we can stick the following just above our Pythagoras code:
y0 = 0
x0 = 0
y1 = 4
x1 = 3
...the answer should be 5.
So, our full code should look like this algorithm:
# import random
# Make y0 and x0 = 50
# Move y0 randomly
# Move x0 randomly
# Move y0 randomly
# Move x0 randomly
# Make y1 and x1 = 50
# Move y1 randomly
# Move x1 randomly
# Move y1 randomly
# Move x1 randomly
# answer = Pythagorian distance between y0,x0 and y1,x1
# print answer
Looking at this code, four things jump out.
Firstly there's a lot of repetion. We've got *almost* the same code running eight times. We'll look at how we can tidy that up in the next two practials.
Secondly, this doesn't feel very agent-based. It's like a pair of disembodied boots walking around: all we have is two pairs of coordinates. There's not much to them in the way of other attributes that move with the coordinates or behaviour. At the moment, the behaviour is quite simple: the coordinates update to pick out new points in space related to the old ones. We call this a Markovian process: one where the values at the next step rely only on the current values, not a longer history. Nevertheless, this is the core of a much more sophisticated model. If we could get these coordinates into some thing with a name and some more complicated behaviour, we'd feel much more like we had some agents. That'll build up as we go along. In actual fact, most agents are Markovian, though one of their strengths is that they don't need to be: we could, for example, make them learn about the environment as they explore the space, and work out optimal pathways next time they travel.
Thirdly, the area they are currently moving about is infinite; that may or may not be appropriate, depending on what you are modelling. However, for our purpose we could do with some data representing an environment, and limiting movement to within that data. We'll see how to do that in the next but one practical when we look at looping.
Lastly, it's clear that starting at the same point makes for very boring distances. The coordinates aren't likely to be very different by the end.
You actually have everything you need to know in order to randomly start the coordinates at different locations. See if you can randomise their starting points within a 100x100 grid, with the coordinates being integer values between and including 0 and 99 (if you look back over the practical you'll see we mention an appropriate function). Once you've managed that, comment your code so you'll understand it later, and you're done.