-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.py
223 lines (163 loc) · 6.21 KB
/
Tests.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from ProblemBuilder import *
from BayesianNetwork import *
from BayesianNode import *
from UtilityNode import *
from MapManager import *
# Class for testing
class TestClass(unittest.TestCase):
def setUp(self):
# Init core-objects
self.mapManager = MapManager([200, 200])
self.network = BayesianNetwork(self.mapManager)
self.nodes = [BayesianNode(self.network) for i in range(0,10)]
def test_baseFunctionality(self):
for i in range(len(self.nodes)):
self.nodes[i].addParent(self.nodes[i])
for node in self.nodes:
self.assertEqual(node.network, self.network)
def test_nodeParenting(self):
length = len(self.nodes)
self.nodes[0].addParent(self.nodes[length-1])
# Check if node 0 has a parent
self.assertNotEqual(len(self.nodes[0].getParents()), 0)
# Check if the parent is the correct one
self.assertIn(self.nodes[length-1], self.nodes[0].getParents())
self.nodes[0].addParent(self.nodes[length-2])
self.nodes[1].addParent(self.nodes[0])
self.nodes[1].addParent(self.nodes[length-1])
# Check if the node added as parent for node 1 is also a parent of node 0
self.assertIn(self.nodes[1].getParents()[1], self.nodes[0].getParents())
# Check if the node parent-lenghts are the same, i.e. all parents have been properly added
self.assertEqual(len(self.nodes[1].getParents()), len(self.nodes[0].getParents()))
def test_nodePositions(self):
for node in self.nodes:
node.setPosition([23, 25])
# Test position assignment
for node in self.nodes:
self.assertEqual(node.position[0], 23)
self.assertEqual(node.position[1], 25)
node1, node2 = self.nodes[0], self.nodes[1]
# Test setPosition and getPosition functions
node1.setPosition((0,3))
node2.setPosition((0,4))
self.assertEqual(node1.position[0], 0)
self.assertEqual(node1.position[1], 3)
pos = node1.getPosition()
self.assertEqual(node1.position, pos)
def test_nodeConnections(self):
node1, node2, node3 = self.nodes[0], self.nodes[1], self.nodes[2]
node1.addParent(node3)
node2.addParent(node3)
node1.addParent(node2)
# Check inheritance
connections1 = node1.getConnections()
connections2 = node2.getConnections()
connections3 = node3.getConnections()
# Check basic containment
self.assertNotEqual(len(connections1), 0)
self.assertNotEqual(connections1, connections2)
# Check if node1 is the child-part of the first connection in connections1
self.assertEqual(connections1[0].getChild(), node1)
parents1 = [connection.getParent() for connection in connections1]
parents2 = [connection.getParent() for connection in connections2]
self.assertIn(node2, parents1)
self.assertIn(node3, parents1)
for parent in parents2:
self.assertIn(parent, parents1)
# Test removal
self.assertIn(node3, parents1)
node1.removeConnectionTo(node3)
parents1 = [connection.getParent() for connection in connections1]
self.assertNotIn(node3, parents1)
connection1to2 = connections1[0]
self.assertEqual(node1, connection1to2.getChild())
self.assertEqual(node2, connection1to2.getParent())
node1.removeConnection(connection1to2)
self.assertEqual(len(connections1), 0)
def test_nodeValuesAndFinalize(self):
node1, node2 = self.nodes[0], self.nodes[1]
# One parent
node2.setName("Likes fruit")
node2.addValue("Yes")
node2.addValue("No")
node1.setName("Likes bananas")
node1.addValue("Yes")
node1.addValue("No")
node1.addParent(node2)
node1.finalize()
node1.definalize()
# Two parents
node3 = self.nodes[2]
node3.setName("Likes woman")
node3.addValue("Yes")
node3.addValue("No")
node3.addValue("Partially")
node1.addParent(node3)
node1.finalize()
def test_utilityNode(self):
node1, node2, node3 = self.nodes[0], self.nodes[1], self.nodes[2]
util = UtilityNode(self.network)
node1.setName("Has more than 1 million dollars")
node1.setObservable(True)
node1.addValue("Yes")
node1.addValue("No")
node2.setName("Winning a gamble of 2 million dollars")
node2.setObservable(False)
node2.addValue("Yes")
node2.addValue("No")
node3.setName("Has a keen eye for probability")
node3.setObservable(True)
node3.addValue("Yes")
node3.addValue("No")
node2.addParent(node3)
util.addParent(node1)
util.addParent(node2)
node1.setCurrentValue("Yes")
node2.setCurrentValue("No")
node3.setCurrentValue("Yes")
node3.finalize()
node2.finalize()
node1.finalize()
util.finalize()
print "Utility is: ", util.inferUtility()
# No explicit test result
assertEqual(0,0)
def test_utilityNode2(self):
nodes = self.nodes[0:5]
nodes[0].setName("Do I have time?")
nodes[0].setObservable(False)
nodes[0].addvalue("Yes")
nodes[0].addValue("No")
nodes[0].setName("Can I afford?")
nodes[0].setObservable(False)
nodes[0].addvalue("Yes")
nodes[0].addValue("No")
nodes[0].setName("Expected fun")
nodes[0].setObservable(False)
nodes[0].addvalue("High")
nodes[0].addValue("Medium")
nodes[0].addValue("Low")
nodes[0].setName("Do I have time?")
nodes[0].setObservable(False)
nodes[0].addvalue("Yes")
nodes[0].addValue("No")
nodes[0].setName("Do I have time?")
nodes[0].setObservable(False)
nodes[0].addvalue("Yes")
nodes[0].addValue("No")
nodes[0].setName("Do I have time?")
nodes[0].setObservable(False)
nodes[0].addvalue("Yes")
nodes[0].addValue("No")
util = UtilityNode()
util.addParent(nodes[1])
def test_networkValuesAndFinalize(self):
# Make semi-complex network
nodeNames = ["Likes hot weather", "Likes bananas", "Likes tropical climate", "Likes women", "Likes partying", "Likes donuts", "Wants to travel to: "]
nodeValues = 3
def test_nodeSizes(self):
self.lol = 4
def test_quadTreeInsertAndRetrieve(self):
self.lol = 3
def test_mapManagerSelectsCorrectNodes(self):
self.lol = 3