-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentframework.py
More file actions
52 lines (39 loc) · 1.58 KB
/
agentframework.py
File metadata and controls
52 lines (39 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 17 11:37:12 2019
@author: gynjkm
"""
import random
import math
class Agent():
def __init__ (self, environment, agents):
self.environment = environment
self.environment_height = len(environment)
self.environment_width = len(environment)
self.x = random.randint(0, self.environment_width-1)
self.y = random.randint(0, self.environment_height-1)
self.store = 0
self.agents = agents
def move(self):
if random.random() < 0.5:
self.y = (self.y + 1) % 300
else:
self.y = (self.y - 1) % 300
if random.random() < 0.5:
self.x = (self.x + 1) % 300
else:
self.x = (self.x - 1) % 300
def distance_between(self,a):
return math.sqrt( ((self.x - a.x)**2) + ((self.y-a.y)**2))
def eat(self): # can you make it eat what is left?
if self.environment[self.y][self.x] > 10:
self.environment[self.y][self.x] -= 10
self.store += 10
def share_with_neighbours(self, neighbourhood):
for agent in self.agents:
distance = self.distance_between(agent)
if distance <= neighbourhood:
average_store = (self.store + agent.store)/2
self.store = average_store
agent.store = average_store
# print("Shared with agent " + str(distance) + " units away, now they both have" + str(average_store))