Communication
How did you do? It's quite tricky to adapt the function: it requires thinking about the objects that are being passed in. Here's our take on the two new methods in Agent:
def share_with_neighbours(self, neighbourhood):
for agent in self.agents:
dist = self.distance_between(agent)
if dist <= neighbourhood:
sum = self.store + agent.store
ave = sum /2
self.store = ave
agent.store = ave
#print("sharing " + str(dist) + " " + str(ave))
def distance_between(self, agent):
return (((self.x - agent.x)**2) + ((self.y - agent.y)**2))**0.5
The commented out print
statement is just so we can check it is working. You may find you need to change
the parameters in model.py
to give agents enough overlap to get some interaction, without getting so many it takes ages to run.
If it does take ages, you can end code runs in Spyder early by pressing the red square icon above the console window. It is also worth
printing out the iteration number in model.py
so you can tell whether it is working or not and how long each iteration lasts.
Note that you may also like to filter out agents comparing with themselves (not that it matters much).
Once that's working, you've got basic agent communication set up. All agent communication is is agents either adjusting each
other's variables or (actually more usually and politely) calling functions within each other and passing information in, or getting information out
(not that it would be more usual with the above to call agent.getstore()
and agent.setstore(ave)
just because then
agent
can decide whether it wants to do this. This basic meet-and-trade-information or meet-and-call-functions model is at the centre of many of the best known
ABM, including, for example variations on the Axelrod tournaments.
This leaves us almost at the point of having a fully formed basic ABM. There's just one more thing we need to pay attention to: model artifacts.