@@ -20,9 +20,7 @@ def test__state_space(self) -> None:
20
20
21
21
:return: None
22
22
"""
23
- assert (
24
- isinstance (item , int ) for item in IntrusionRecoveryPomdpUtil .state_space ()
25
- )
23
+ assert (isinstance (item , int ) for item in IntrusionRecoveryPomdpUtil .state_space ())
26
24
assert IntrusionRecoveryPomdpUtil .state_space () is not None
27
25
assert IntrusionRecoveryPomdpUtil .state_space () == [0 , 1 , 2 ]
28
26
@@ -40,9 +38,7 @@ def test_action_space(self) -> None:
40
38
41
39
:return: None
42
40
"""
43
- assert (
44
- isinstance (item , int ) for item in IntrusionRecoveryPomdpUtil .action_space ()
45
- )
41
+ assert (isinstance (item , int ) for item in IntrusionRecoveryPomdpUtil .action_space ())
46
42
assert IntrusionRecoveryPomdpUtil .action_space () is not None
47
43
assert IntrusionRecoveryPomdpUtil .action_space () == [0 , 1 ]
48
44
@@ -79,10 +75,7 @@ def test_cost_tensor(self) -> None:
79
75
actions = [0 ]
80
76
negate = False
81
77
expected = [[0 , 0.5 ]]
82
- assert (
83
- IntrusionRecoveryPomdpUtil .cost_tensor (eta , states , actions , negate )
84
- == expected
85
- )
78
+ assert IntrusionRecoveryPomdpUtil .cost_tensor (eta , states , actions , negate ) == expected
86
79
87
80
def test_observation_function (self ) -> None :
88
81
"""
@@ -93,9 +86,7 @@ def test_observation_function(self) -> None:
93
86
s = 1
94
87
o = 1
95
88
num_observations = 2
96
- assert round (
97
- IntrusionRecoveryPomdpUtil .observation_function (s , o , num_observations ), 1
98
- )
89
+ assert round (IntrusionRecoveryPomdpUtil .observation_function (s , o , num_observations ), 1 )
99
90
100
91
def test_observation_tensor (self ) -> None :
101
92
"""
@@ -126,15 +117,7 @@ def test_transition_function(self) -> None:
126
117
p_c_1 = 0.1
127
118
p_c_2 = 0.2
128
119
p_u = 0.5
129
- assert (
130
- round (
131
- IntrusionRecoveryPomdpUtil .transition_function (
132
- s , s_prime , a , p_a , p_c_1 , p_c_2 , p_u
133
- ),
134
- 1 ,
135
- )
136
- == 0.2
137
- )
120
+ assert (round (IntrusionRecoveryPomdpUtil .transition_function (s , s_prime , a , p_a , p_c_1 , p_c_2 , p_u ), 1 ) == 0.2 )
138
121
139
122
def test_transition_function_game (self ) -> None :
140
123
"""
@@ -148,15 +131,7 @@ def test_transition_function_game(self) -> None:
148
131
a2 = 1
149
132
p_a = 0.2
150
133
p_c_1 = 0.1
151
- assert (
152
- round (
153
- IntrusionRecoveryPomdpUtil .transition_function_game (
154
- s , s_prime , a1 , a2 , p_a , p_c_1
155
- ),
156
- 2 ,
157
- )
158
- == 0.18
159
- )
134
+ assert (round (IntrusionRecoveryPomdpUtil .transition_function_game (s , s_prime , a1 , a2 , p_a , p_c_1 ), 2 ) == 0.18 )
160
135
161
136
def test_transition_tensor (self ) -> None :
162
137
"""
@@ -171,19 +146,15 @@ def test_transition_tensor(self) -> None:
171
146
p_c_2 = 0.2
172
147
p_u = 0.5
173
148
expected = [[[0.7 , 0.2 , 0.1 ], [0.4 , 0.4 , 0.2 ], [0 , 0 , 1.0 ]]]
174
- transition_tensor = IntrusionRecoveryPomdpUtil .transition_tensor (
175
- states , actions , p_a , p_c_1 , p_c_2 , p_u
176
- )
149
+ transition_tensor = IntrusionRecoveryPomdpUtil .transition_tensor (states , actions , p_a , p_c_1 , p_c_2 , p_u )
177
150
for i in range (len (transition_tensor )):
178
151
for j in range (len (transition_tensor [i ])):
179
152
for k in range (len (transition_tensor [i ][j ])):
180
153
transition_tensor [i ][j ][k ] = round (transition_tensor [i ][j ][k ], 1 )
181
154
assert transition_tensor == expected
182
155
states = [0 , 1 ]
183
156
with pytest .raises (AssertionError ):
184
- transition_tensor = IntrusionRecoveryPomdpUtil .transition_tensor (
185
- states , actions , p_a , p_c_1 , p_c_2 , p_u
186
- )
157
+ IntrusionRecoveryPomdpUtil .transition_tensor (states , actions , p_a , p_c_1 , p_c_2 , p_u )
187
158
188
159
def test_transition_tensor_game (self ) -> None :
189
160
"""
@@ -196,14 +167,12 @@ def test_transition_tensor_game(self) -> None:
196
167
attacker_actions = [0 , 1 ]
197
168
p_a = 0.5
198
169
p_c_1 = 0.3
199
- result = IntrusionRecoveryPomdpUtil .transition_tensor_game (
200
- states , defender_actions , attacker_actions , p_a , p_c_1
201
- )
170
+ result = IntrusionRecoveryPomdpUtil .transition_tensor_game (states , defender_actions , attacker_actions , p_a ,
171
+ p_c_1 )
202
172
assert len (result ) == len (defender_actions )
203
173
assert all (len (a1 ) == len (attacker_actions ) for a1 in result )
204
174
assert all (len (a2 ) == len (states ) for a1 in result for a2 in a1 )
205
175
assert all (len (s ) == len (states ) for a1 in result for a2 in a1 for s in a2 )
206
-
207
176
assert result [0 ][1 ][0 ][0 ] == (1 - p_a ) * (1 - p_c_1 )
208
177
assert result [1 ][0 ][1 ][1 ] == 0
209
178
assert result [1 ][1 ][2 ][2 ] == 1.0
@@ -234,12 +203,8 @@ def test_sampe_next_observation(self) -> None:
234
203
observation_tensor = [[0.8 , 0.2 ], [0.4 , 0.6 ]]
235
204
s_prime = 1
236
205
observations = [0 , 1 ]
237
- assert isinstance (
238
- IntrusionRecoveryPomdpUtil .sample_next_observation (
239
- observation_tensor , s_prime , observations
240
- ),
241
- int ,
242
- )
206
+ assert isinstance (IntrusionRecoveryPomdpUtil .sample_next_observation (observation_tensor , s_prime , observations ),
207
+ int )
243
208
244
209
def test_bayes_filter (self ) -> None :
245
210
"""
@@ -256,22 +221,9 @@ def test_bayes_filter(self) -> None:
256
221
observation_tensor = [[0.8 , 0.2 ], [0.4 , 0.6 ]]
257
222
transition_tensor = [[[0.6 , 0.4 ], [0.1 , 0.9 ]]]
258
223
b_prime_s_prime = 0.7
259
- assert (
260
- round (
261
- IntrusionRecoveryPomdpUtil .bayes_filter (
262
- s_prime ,
263
- o ,
264
- a ,
265
- b ,
266
- states ,
267
- observations ,
268
- observation_tensor ,
269
- transition_tensor ,
270
- ),
271
- 1 ,
272
- )
273
- == b_prime_s_prime
274
- )
224
+ assert (round (IntrusionRecoveryPomdpUtil .bayes_filter (s_prime , o , a , b , states , observations ,
225
+ observation_tensor , transition_tensor ), 1 )
226
+ == b_prime_s_prime )
275
227
276
228
def test_p_o_given_b_a1_a2 (self ) -> None :
277
229
"""
@@ -286,15 +238,8 @@ def test_p_o_given_b_a1_a2(self) -> None:
286
238
observation_tensor = [[0.8 , 0.2 ], [0.4 , 0.6 ]]
287
239
transition_tensor = [[[0.6 , 0.4 ], [0.1 , 0.9 ]]]
288
240
expected = 0.5
289
- assert (
290
- round (
291
- IntrusionRecoveryPomdpUtil .p_o_given_b_a1_a2 (
292
- o , b , a , states , transition_tensor , observation_tensor
293
- ),
294
- 1 ,
295
- )
296
- == expected
297
- )
241
+ assert (round (IntrusionRecoveryPomdpUtil .p_o_given_b_a1_a2 (o , b , a , states , transition_tensor ,
242
+ observation_tensor ), 1 ) == expected )
298
243
299
244
def test_next_belief (self ) -> None :
300
245
"""
@@ -309,23 +254,8 @@ def test_next_belief(self) -> None:
309
254
observations = [0 , 1 ]
310
255
observation_tensor = [[0.8 , 0.2 ], [0.4 , 0.6 ]]
311
256
transition_tensor = [[[0.3 , 0.7 ], [0.6 , 0.4 ]]]
312
- assert (
313
- round (
314
- sum (
315
- IntrusionRecoveryPomdpUtil .next_belief (
316
- o ,
317
- a ,
318
- b ,
319
- states ,
320
- observations ,
321
- observation_tensor ,
322
- transition_tensor ,
323
- )
324
- ),
325
- 1 ,
326
- )
327
- == 1
328
- )
257
+ assert (round (sum (IntrusionRecoveryPomdpUtil .next_belief (o , a , b , states , observations , observation_tensor ,
258
+ transition_tensor )), 1 ) == 1 )
329
259
330
260
def test_pomdp_solver_file (self ) -> None :
331
261
"""
@@ -334,33 +264,14 @@ def test_pomdp_solver_file(self) -> None:
334
264
:return: None
335
265
"""
336
266
337
- assert (
338
- IntrusionRecoveryPomdpUtil .pomdp_solver_file (
339
- IntrusionRecoveryPomdpConfig (
340
- eta = 0.1 ,
341
- p_a = 0.2 ,
342
- p_c_1 = 0.2 ,
343
- p_c_2 = 0.3 ,
344
- p_u = 0.3 ,
345
- BTR = 1 ,
346
- negate_costs = True ,
347
- seed = 1 ,
348
- discount_factor = 0.5 ,
349
- states = [0 , 1 ],
350
- actions = [0 ],
351
- observations = [0 , 1 ],
352
- cost_tensor = [[0.1 , 0.5 ], [0.5 , 0.6 ]],
353
- observation_tensor = [[0.8 , 0.2 ], [0.4 , 0.6 ]],
354
- transition_tensor = [[[0.8 , 0.2 ], [0.6 , 0.4 ]]],
355
- b1 = [0.3 , 0.7 ],
356
- T = 3 ,
357
- simulation_env_name = "env" ,
358
- gym_env_name = "gym" ,
359
- max_horizon = np .inf ,
360
- )
361
- )
362
- is not None
363
- )
267
+ assert (IntrusionRecoveryPomdpUtil .pomdp_solver_file (
268
+ IntrusionRecoveryPomdpConfig (eta = 0.1 , p_a = 0.2 , p_c_1 = 0.2 , p_c_2 = 0.3 , p_u = 0.3 , BTR = 1 , negate_costs = True ,
269
+ seed = 1 , discount_factor = 0.5 , states = [0 , 1 ], actions = [0 ], observations = [0 , 1 ],
270
+ cost_tensor = [[0.1 , 0.5 ], [0.5 , 0.6 ]],
271
+ observation_tensor = [[0.8 , 0.2 ], [0.4 , 0.6 ]],
272
+ transition_tensor = [[[0.8 , 0.2 ], [0.6 , 0.4 ]]], b1 = [0.3 , 0.7 ], T = 3 ,
273
+ simulation_env_name = "env" , gym_env_name = "gym" , max_horizon = np .inf ))
274
+ is not None )
364
275
365
276
def test_sample_next_state_game (self ) -> None :
366
277
"""
@@ -444,9 +355,7 @@ def test_generate_transitions(self) -> None:
444
355
gym_env_name = "gym_env" ,
445
356
max_horizon = 1000 ,
446
357
)
447
- assert (
448
- IntrusionRecoveryPomdpUtil .generate_transitions (dto )[0 ] == "0 0 0 0 0 0.06"
449
- )
358
+ assert IntrusionRecoveryPomdpUtil .generate_transitions (dto )[0 ] == "0 0 0 0 0 0.06"
450
359
451
360
def test_generate_rewards (self ) -> None :
452
361
"""
@@ -502,7 +411,11 @@ def test_generate_rewards(self) -> None:
502
411
assert IntrusionRecoveryPomdpUtil .generate_rewards (dto )[0 ] == "0 0 0 -1"
503
412
504
413
def test_generate_os_posg_game_file (self ) -> None :
505
- """ """
414
+ """
415
+ Tests the generate_os_posg_game function
416
+
417
+ :return: None
418
+ """
506
419
507
420
states = [0 , 1 , 2 ]
508
421
actions = [0 , 1 ]
@@ -580,24 +493,13 @@ def test_generate_os_posg_game_file(self) -> None:
580
493
581
494
output_lines = game_file_str .split ("\n " )
582
495
583
- assert (
584
- output_lines [0 ] == expected_game_description
585
- ), f"Game description mismatch: { output_lines [0 ]} "
586
- assert (
587
- output_lines [1 :4 ] == expected_state_descriptions
588
- ), f"State descriptions mismatch: { output_lines [1 :4 ]} "
589
- assert (
590
- output_lines [4 :6 ] == expected_player_1_actions
591
- ), f"Player 1 actions mismatch: { output_lines [4 :6 ]} "
592
- assert (
593
- output_lines [6 :8 ] == expected_player_2_actions
594
- ), f"Player 2 actions mismatch: { output_lines [6 :8 ]} "
595
- assert (
596
- output_lines [8 :10 ] == expected_obs_descriptions
597
- ), f"Observation descriptions mismatch: { output_lines [8 :10 ]} "
598
- assert (
599
- output_lines [10 :13 ] == expected_player_2_legal_actions
600
- ), f"Player 2 legal actions mismatch: { output_lines [10 :13 ]} "
601
- assert (
602
- output_lines [13 :14 ] == expected_player_1_legal_actions
603
- ), f"Player 1 legal actions mismatch: { output_lines [13 :14 ]} "
496
+ assert (output_lines [0 ] == expected_game_description ), f"Game description mismatch: { output_lines [0 ]} "
497
+ assert (output_lines [1 :4 ] == expected_state_descriptions ), f"State descriptions mismatch: { output_lines [1 :4 ]} "
498
+ assert (output_lines [4 :6 ] == expected_player_1_actions ), f"Player 1 actions mismatch: { output_lines [4 :6 ]} "
499
+ assert (output_lines [6 :8 ] == expected_player_2_actions ), f"Player 2 actions mismatch: { output_lines [6 :8 ]} "
500
+ assert (output_lines [8 :10 ] == expected_obs_descriptions ), \
501
+ f"Observation descriptions mismatch: { output_lines [8 :10 ]} "
502
+ assert (output_lines [10 :13 ] == expected_player_2_legal_actions ), \
503
+ f"Player 2 legal actions mismatch: { output_lines [10 :13 ]} "
504
+ assert (output_lines [13 :14 ] == expected_player_1_legal_actions ), \
505
+ f"Player 1 legal actions mismatch: { output_lines [13 :14 ]} "
0 commit comments