Dark theme

Communication


So, let's build our share_with_neighbours(neighbourhood) method in Agent.

Here's the algorithm for it. The method should work out the distance to each of the other agents, and if they fall within neighbourhood distance, it should set it, and its neighbours, stores equal to the average of their two stores. We've given you one line of code: the line to call the distance_between method (see below).

# Loop through the agents in self.agents .
    # Calculate the distance between self and the current other agent:
    distance = self.distance_between(agent)
    # If distance is less than or equal to the neighbourhood
        # Sum self.store and agent.store .
        # Divide sum by two to calculate average.
        # self.store = average
            # agent.store = average
    # End if
# End loop

By far the most complicated bit of this is calculating the distance. You have your Pythagoras code in model.py. Copy this code into Agent and adapt it to be a function that works with the above method call. Remember that whenever a method in an object is called, the self is invisibly passed into them method. To be clear, we won't need the distance_between function in model.py any more, nor the code that loops through comparing the distances between each agent (unless you want to keep it): the former will be put in Agent, and the latter can be deleted.

Give it a go getting this all working in Agent.


  1. Getting to know you...
  2. This page
  3. Where we're at <-- next
  4. Shuffle