Skip to content

Commit 5bfeb40

Browse files
committed
refactor
1 parent 60c931f commit 5bfeb40

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

simulation-system/libs/csle-tolerance/src/csle_tolerance/util/intrusion_recovery_pomdp_util.py

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from typing import List
22
from scipy.stats import betabinom
33
import numpy as np
4-
from csle_tolerance.dao.intrusion_recovery_pomdp_config import (
5-
IntrusionRecoveryPomdpConfig,
6-
)
7-
from csle_tolerance.dao.intrusion_recovery_game_config import (
8-
IntrusionRecoveryGameConfig,
9-
)
4+
from csle_tolerance.dao.intrusion_recovery_pomdp_config import IntrusionRecoveryPomdpConfig
5+
from csle_tolerance.dao.intrusion_recovery_game_config import IntrusionRecoveryGameConfig
106

117

128
class IntrusionRecoveryPomdpUtil:
@@ -197,9 +193,9 @@ def transition_function_game(s: int, s_prime: int, a1: int, a2: int, p_a: float,
197193
elif s_prime == 0 and a1 == 0 and a2 == 1 and s == 0:
198194
return (1 - p_a) * (1 - p_c_1)
199195
elif (
200-
(s_prime == 0 and a2 == 0 and s == 0)
201-
or (s_prime == 0 and s == 1 and a1 == 1)
202-
or (s_prime == 1 and s == 1 and a1 == 0)
196+
(s_prime == 0 and a2 == 0 and s == 0)
197+
or (s_prime == 0 and s == 1 and a1 == 1)
198+
or (s_prime == 1 and s == 1 and a1 == 0)
203199
):
204200
return 1 - p_c_1
205201
elif s_prime == 1 and s == 0 and a2 == 1:
@@ -344,7 +340,7 @@ def bayes_filter(s_prime: int, o: int, a: int, b: List[float], states: List[int]
344340

345341
for s in states:
346342
temp += (
347-
observation_tensor[s_prime][o] * transition_tensor[a][s][s_prime] * b[s]
343+
observation_tensor[s_prime][o] * transition_tensor[a][s][s_prime] * b[s]
348344
)
349345
b_prime_s_prime = temp / norm
350346
if round(b_prime_s_prime, 2) > 1:
@@ -372,9 +368,9 @@ def p_o_given_b_a1_a2(o: int, b: List[float], a: int, states: List[int], transit
372368
for s in states:
373369
for s_prime in states:
374370
prob += (
375-
b[s]
376-
* transition_tensor[a][s][s_prime]
377-
* observation_tensor[s_prime][o]
371+
b[s]
372+
* transition_tensor[a][s][s_prime]
373+
* observation_tensor[s_prime][o]
378374
)
379375
assert prob < 1
380376
return prob
@@ -447,7 +443,7 @@ def pomdp_solver_file(config: IntrusionRecoveryPomdpConfig) -> str:
447443
for o in config.observations:
448444
c = config.cost_tensor[a][s]
449445
file_str = (
450-
file_str + f"R: {a} : {s} : {s_prime} : {o} {c:.80f}\n"
446+
file_str + f"R: {a} : {s} : {s_prime} : {o} {c:.80f}\n"
451447
)
452448
return file_str
453449

@@ -501,15 +497,13 @@ def generate_os_posg_game_file(game_config: IntrusionRecoveryGameConfig) -> str:
501497
"""
502498
num_partitions = 1
503499
transitions = IntrusionRecoveryPomdpUtil.generate_transitions(
504-
game_config=game_config
505-
)
500+
game_config=game_config)
506501
rewards = IntrusionRecoveryPomdpUtil.generate_rewards(game_config=game_config)
507502
game_description = (
508503
f"{len(game_config.states)} {num_partitions} {len(game_config.actions)} "
509504
f"{len(game_config.actions)} "
510505
f"{len(game_config.observations)} {len(transitions)} "
511-
f"{len(rewards)} {game_config.discount_factor}"
512-
)
506+
f"{len(rewards)} {game_config.discount_factor}")
513507
state_desriptions = []
514508
for s in game_config.states:
515509
state_desriptions.append(f"{s} {0}")
@@ -518,22 +512,16 @@ def generate_os_posg_game_file(game_config: IntrusionRecoveryGameConfig) -> str:
518512

519513
player_2_legal_actions = []
520514
for _ in game_config.states:
521-
player_2_legal_actions.append(
522-
" ".join(list(map(lambda x: str(x), game_config.actions)))
523-
)
515+
player_2_legal_actions.append(" ".join(list(map(lambda x: str(x), game_config.actions))))
524516

525517
player_1_legal_actions = []
526-
player_1_legal_actions.append(
527-
" ".join(list(map(lambda x: str(x), game_config.actions)))
528-
)
518+
player_1_legal_actions.append(" ".join(list(map(lambda x: str(x), game_config.actions))))
529519

530520
obs_desriptions = []
531521
for i, o in enumerate(game_config.observations):
532522
obs_desriptions.append(f"o_{o}")
533523

534-
initial_belief_str = (
535-
f"{0} {' '.join(list(map(lambda x: str(x), game_config.b1)))}"
536-
)
524+
initial_belief_str = f"{0} {' '.join(list(map(lambda x: str(x), game_config.b1)))}"
537525
game_file_str = ""
538526
game_file_str = game_file_str + game_description + "\n"
539527
game_file_str = game_file_str + "\n".join(state_desriptions) + "\n"

0 commit comments

Comments
 (0)