-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.py
More file actions
265 lines (230 loc) · 10.9 KB
/
testing.py
File metadata and controls
265 lines (230 loc) · 10.9 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import unittest
import run_sim
import models.staghunt_htn
# stag tests
# sim tests
# sim state
## setup start state
## sim
## get back x number states
## states[0] = start state
## final state is different?
class PassTest(unittest.TestCase):
def get_start_state(self):
state = models.staghunt_htn.get_start_state()
state.loc = {('h1', 'hunter'):(4,4), ('s1', 'stag'):(3,4), ('s2', 'stag'):(4,2), ('s3', 'stag'):(1,2), ('h2', 'hunter'):(2,1), ('h3', 'hunter'):(4,3), ('r1', 'rabbit'):(5,3), ('r2', 'rabbit'):(4,1)}
state.map = run_sim.map5x5x3
state.goal[('h2', 'hunter')] = {'cooperateWith': (('h2', 'hunter'), ('h3', 'hunter'))}
state.goal[('h3', 'hunter')] = {'cooperateWith': (('h3', 'hunter'), ('h2', 'hunter'))}
return state
def setUp(self):
pass
def testSimulate_State(self):
state = self.get_start_state()
run_sim.assignGoals(state, 4)
print('**** goals ****', state.goal)
states,_ = run_sim.simulate_state(state, 3)
print('**** scores ****', states[-1].score)
self.assertEqual(len(states), 4)
self.assertEqual(states[0], state)
self.assertNotEqual(states[-1], state)
def testSimulate_State_Goals0(self):
state = self.get_start_state()
run_sim.assignGoals(state, 0)
print('**** goals ****', state.goal)
states,_ = run_sim.simulate_state(state, 3)
print('**** scores ****', states[-1].score)
self.assertEqual(len(states), 4)
self.assertEqual(states[0], state)
self.assertNotEqual(states[-1], state)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 0, ('h2', 'hunter'): 1, ('h3', 'hunter'): 1})
def testSimulate_State_Goals1(self):
state = self.get_start_state()
run_sim.assignGoals(state, 1)
print('**** goals ****', state.goal)
states,_ = run_sim.simulate_state(state, 3)
print('**** scores ****', states[-1].score)
self.assertEqual(len(states), 4)
self.assertEqual(states[0], state)
self.assertNotEqual(states[-1], state)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 3, ('h2', 'hunter'): 4, ('h3', 'hunter'): 1})
def testSimulate_State_Goals2(self):
state = self.get_start_state()
run_sim.assignGoals(state, 2)
print('**** goals ****', state.goal)
states,_ = run_sim.simulate_state(state, 3)
print('**** scores ****', states[-1].score)
self.assertEqual(len(states), 4)
self.assertEqual(states[0], state)
self.assertNotEqual(states[-1], state)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 0, ('h2', 'hunter'): 1, ('h3', 'hunter'): 0})
def testSimulate_State_Goals3(self):
state = self.get_start_state()
run_sim.assignGoals(state, 3)
print('**** goals ****', state.goal)
states,_ = run_sim.simulate_state(state, 3)
print('**** scores ****', states[-1].score)
self.assertEqual(len(states), 4)
self.assertEqual(states[0], state)
self.assertNotEqual(states[-1], state)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 1, ('h2', 'hunter'): 3, ('h3', 'hunter'): 3})
def testSimulate_State_Goals4(self):
state = self.get_start_state()
run_sim.assignGoals(state, 4)
state.loc[('r2', 'rabbit')] = (1,4)
state.loc[('h2', 'hunter')] = (1,1)
state.loc[('h3', 'hunter')] = (5,3)
print('**** goals ****', state.goal)
states,_ = run_sim.simulate_state(state, 3)
print('**** scores ****', states[-1].score)
self.assertEqual(len(states), 4)
self.assertEqual(states[0], state)
self.assertNotEqual(states[-1], state)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 2, ('h2', 'hunter'): 2, ('h3', 'hunter'): 2})
def testStag_noEvasion1(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h2', 'hunter')] = (3,1)
state.loc[('h3', 'hunter')] = (4,2)
run_sim.assignGoals(state, 3)
states,_ = run_sim.simulate_state(state, 1)
# stag can't evade, is cornered, thus captured, thus no loc any more
self.assertNotIn(('s2', 'stag'), states[-1].loc)
#self.assertEqual(states[-1].loc[('s2', 'stag')], (4,1))
def testStag_noEvasion2(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (1,5)
state.loc[('h2', 'hunter')] = (1,4)
state.loc[('h3', 'hunter')] = (2,5)
run_sim.assignGoals(state, 3)
states,_ = run_sim.simulate_state(state, 1)
# stag can't evade, is cornered, thus captured, thus no loc any more
self.assertNotIn(('s2', 'stag'), states[-1].loc)
#self.assertEqual(states[-1].loc[('s2', 'stag')], (1,5))
def testStag_moveAway1(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h2', 'hunter')] = (2,1)
state.loc[('h3', 'hunter')] = (4,2)
run_sim.assignGoals(state, 3)
states,_ = run_sim.simulate_state(state, 1)
self.assertEqual(states[-1].loc[('s2', 'stag')], (3,1))
def testStag_moveAway2(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h2', 'hunter')] = (3,1)
state.loc[('h3', 'hunter')] = (4,3)
run_sim.assignGoals(state, 3)
states,_ = run_sim.simulate_state(state, 1)
self.assertEqual(states[-1].loc[('s2', 'stag')], (4,2))
def testCapture2Hunters(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h2', 'hunter')] = (3,1)
state.loc[('h3', 'hunter')] = (4,2)
run_sim.assignGoals(state, 3)
states,_ = run_sim.simulate_state(state, 1)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 0, ('h2', 'hunter'): 3, ('h3', 'hunter'): 3})
def testCapture3Hunters1(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h1', 'hunter')] = (4,1)
state.loc[('h2', 'hunter')] = (3,1)
state.loc[('h3', 'hunter')] = (4,2)
run_sim.assignGoals(state, 4)
states,_ = run_sim.simulate_state(state, 1)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 2, ('h2', 'hunter'): 2, ('h3', 'hunter'): 2})
def testCapture3Hunters2(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h2', 'hunter')] = (4,1)
state.loc[('h3', 'hunter')] = (3,1)
state.loc[('h1', 'hunter')] = (4,2)
run_sim.assignGoals(state, 4)
states,_ = run_sim.simulate_state(state, 1)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 2, ('h2', 'hunter'): 2, ('h3', 'hunter'): 2})
def testCapture3Hunters3(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('h3', 'hunter')] = (4,1)
state.loc[('h1', 'hunter')] = (3,1)
state.loc[('h2', 'hunter')] = (4,2)
run_sim.assignGoals(state, 4)
states,_ = run_sim.simulate_state(state, 1)
print(states[1].score)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 2, ('h2', 'hunter'): 2, ('h3', 'hunter'): 2})
def testCapture3Hunters1Missing(self):
pass
def testDecideEveryManForThemselves(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (1,5)
run_sim.decide(state)
print('**** goals ****', state.goal)
self.assertFalse(state.goal)
def testDecideAllWorkTogether(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('r2', 'rabbit')] = (5,5)
state.loc[('h3', 'hunter')] = (4,1)
state.loc[('h1', 'hunter')] = (3,1)
state.loc[('h2', 'hunter')] = (4,2)
run_sim.decide(state)
print('**** goals ****', state.goal)
self.assertTrue(state.goal)
# if they all have goals, they are effectively working together, even if they don't know it
self.assertIn(('h1', 'hunter'), state.goal)
self.assertIn(('h2', 'hunter'), state.goal)
self.assertIn(('h3', 'hunter'), state.goal)
def testSimulateStateDecide(self):
state = self.get_start_state()
states,_ = run_sim.simulate_state(state, 1, run_sim.decide)
print('**** scores ****', states[-1].score)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 0, ('h2', 'hunter'): 0, ('h3', 'hunter'): 0})
###
state = states[-1]
states,_ = run_sim.simulate_state(state, 1, run_sim.decide)
print('**** scores ****', states[-1].score)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 1, ('h2', 'hunter'): 3, ('h3', 'hunter'): 3})
###
state = states[-1]
states,_ = run_sim.simulate_state(state, 1, run_sim.decide)
print('**** scores ****', states[-1].score)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 1, ('h2', 'hunter'): 4, ('h3', 'hunter'): 3})
last = states[-1]
###
state = self.get_start_state()
states,_ = run_sim.simulate_state(state, 3, run_sim.decide)
print('**** scores ****', states[-1].score)
self.assertEqual(states[-1].score, {('h1', 'hunter'): 1, ('h2', 'hunter'): 4, ('h3', 'hunter'): 3})
self.assertEqual(states[-1].loc, last.loc)
self.assertEqual(states[-1].goal, last.goal)
def testAssumesForDecideEveryManForThemselves(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (1,5)
run_sim.decide(state)
print('**** assumes ****', state.assumes)
self.assertNotIn(('h1', 'hunter'), state.assumes[('h1', 'hunter')])
self.assertNotIn(('h2', 'hunter'), state.assumes[('h2', 'hunter')])
self.assertNotIn(('h3', 'hunter'), state.assumes[('h3', 'hunter')])
def testAssumesForDecideAllWorkTogether(self):
state = self.get_start_state()
state.loc[('s2', 'stag')] = (4,1)
state.loc[('r2', 'rabbit')] = (5,5)
state.loc[('h3', 'hunter')] = (4,1)
state.loc[('h1', 'hunter')] = (3,1)
state.loc[('h2', 'hunter')] = (4,2)
run_sim.decide(state)
# test above verifies that all hunters have a goal
print('**** assumes ****', state.assumes)
self.assertEqual(state.goal[('h1', 'hunter')], state.assumes[('h1', 'hunter')][('h1', 'hunter')])
self.assertEqual(state.goal[('h2', 'hunter')], state.assumes[('h2', 'hunter')][('h2', 'hunter')])
self.assertEqual(state.goal[('h3', 'hunter')], state.assumes[('h3', 'hunter')][('h3', 'hunter')])
def testRunOneSim(self):
states,_ = run_sim.run_one((0,0,0))
print(states[-1].score)
for i in range(len(states)):
print('loc', i, states[i].loc)
print('goals', i, states[i].goal)
print('score', i, states[i].score)
if __name__ == '__main__':
unittest.main()