-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_agent.py
88 lines (69 loc) · 3 KB
/
test_agent.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import agentframework
import unittest
#create data for test : 4x4 environment and 3 agents. neighborhood that includes all agents.
env_data = [[20,20,20,20],[20,20,20,20],[20,20,20,20],[20,20,20,20]]
agent_data=[]
agent_data.append(agentframework.Agent(env_data,agent_data,0,0))
agent_data.append(agentframework.Agent(env_data,agent_data,0,2))
agent_data.append(agentframework.Agent(env_data,agent_data,0,1))
neighborhood=10
baby_energy=20
class TestAgent(unittest.TestCase):
"""
def __init__(self):
'''Create data for tests.'''
self.env_data = [[20,20,20,20],[20,20,20,20],[20,20,20,20],[20,20,20,20]]
self.agent_data = [agentframework.Agent(self.env_data,self.agent_data,0,0),agentframework.Agent(self.env_data,self.agent_data,0,2),agentframework.Agent(self.env_data,self.agent_data,0,1)]
self.neighborhood=10
"""
def test_attributes(self):
'''
Test existance and relevance of attributes
'''
self.assertEqual(agent_data[0].x,0)
self.assertEqual(agent_data[0].y,0)
self.assertEqual(agent_data[0].environment,env_data)
self.assertEqual(agent_data[0].others,agent_data)
self.assertEqual(agent_data[0].store,0)
self.assertTrue(agent_data[0].hunger<=1)
self.assertTrue(agent_data[0].share_will<=0.5)
def test_move(self):
'''
Test agent has effectively moved according to given rules.
'''
agent_data[0].move()
self.assertTrue(agent_data[0].x==1 or agent_data[0].x==3 )
def test_eat(self):
'''
Test if agent has effectively eaten.
'''
agent_data[0].eat(quantity=5)
self.assertEqual(agent_data[0].store,agent_data[0].hunger*5)
self.assertEqual(env_data[agent_data[0].y][agent_data[0].x],20-agent_data[0].store)
def test_distance(self):
'''
Test the distance_between() method.
'''
#test distance with agents that have not moved yet.
self.assertEqual(agent_data[1].distance_between(agent_data[2]),1)
def test_share(self):
'''
Test sharing method.
'''
agent_data[0].share_with_neighbors(neighborhood)
self.assertEqual(len(agent_data[0].neighbors),len(agent_data))
self.assertTrue((agent_data[1].store > 0) & (agent_data[2].store > 0))
def test_reproduce(self):
'''
Test reproduce() method.
'''
agent_data[0].store=50
agent_data[0].reproduce(20)
self.assertEqual(agent_data[0].store,30)
self.assertEqual(len(agent_data),4)
self.assertEqual(agent_data[3].x,agent_data[0].x)
self.assertEqual(agent_data[3].y,agent_data[0].y)
def test_print(self):
self.assertEqual(agent_data[0].__str__(),'y:'+str(agent_data[0].y)+'\nx:'+str(agent_data[0].x)+'\nstore:'+str(agent_data[0].store)+'\n'+str(len(agent_data[0].others))+' other agents share the same environment'+'\nneighbors: '+str(len(agent_data[0].neighbors)))
if __name__ == '__main__':
unittest.main()