Exemplo n.º 1
0
 def test_copy(self):
     state = GameState()
     num_attack_types = 10
     rows = 4
     cols = 4
     network_config = NetworkConfig(rows, cols)
     num_nodes = len(network_config.node_list)
     state.default_state(list(range(num_nodes)), (3, 1), num_attack_types,
                         network_config)
     copy = state.copy()
     assert copy.num_hacks == state.num_hacks
     assert np.array_equal(copy.attack_values, state.attack_values)
     assert np.array_equal(copy.defense_det, state.defense_det)
     assert np.array_equal(copy.defense_values, state.defense_values)
Exemplo n.º 2
0
 def test_default_state(self):
     state = GameState()
     rows = 4
     cols = 4
     network_config = NetworkConfig(rows, cols)
     num_nodes = len(network_config.node_list)
     num_attack_types = 10
     state.default_state(list(range(num_nodes)), (3, 1), num_attack_types,
                         network_config)
     assert state.attack_values.shape == (num_nodes, 10)
     assert state.defense_values.shape == (num_nodes, 10)
     assert state.defense_det.shape == (num_nodes, )
     assert state.attacker_pos == (3, 1)
     assert state.done == False
     assert state.hacked == False
     assert state.num_hacks == 0
     assert state.detected == False
     assert state.num_games == 0
Exemplo n.º 3
0
 def test_simulate_attack(self):
     rows = 4
     cols = 4
     network_config = NetworkConfig(rows, cols)
     num_nodes = len(network_config.node_list)
     state = GameState()
     num_attack_types = 10
     state.default_state(list(range(num_nodes)), (3, 1), num_attack_types,
                         network_config)
     attack_node_id = 3
     attack_type = 4
     state.defense_values[attack_node_id][attack_type] = 5
     state.attack_values[attack_node_id][attack_type] = 5
     assert not state.simulate_attack(attack_node_id, attack_type,
                                      network_config)
     state.defense_values[attack_node_id][attack_type] = 5
     state.attack_values[attack_node_id][attack_type] = 6
     assert state.simulate_attack(attack_node_id, attack_type,
                                  network_config)
Exemplo n.º 4
0
 def test_defend(self):
     state = GameState()
     rows = 4
     cols = 4
     network_config = NetworkConfig(rows, cols)
     num_nodes = len(network_config.node_list)
     num_attack_types = 10
     state.default_state(list(range(num_nodes)), (3, 1), num_attack_types,
                         network_config)
     defend_node_id = 3
     defense_type = 4
     max_value = 10
     old_count = state.defense_values[defend_node_id][defense_type]
     state.defend(defend_node_id, defense_type, max_value, network_config)
     assert state.defense_values[defend_node_id][defense_type] < max_value
     assert state.defense_values[defend_node_id][
         defense_type] == old_count + 1
     state.defense_values[defend_node_id][defense_type] = 10
     state.defend(defend_node_id, defense_type, max_value, network_config)
     assert state.defense_values[defend_node_id][defense_type] == max_value
Exemplo n.º 5
0
 def test_new_game(self):
     rows = 4
     cols = 4
     network_config = NetworkConfig(rows, cols)
     state = GameState()
     num_nodes = len(network_config.node_list)
     num_attack_types = 10
     state.default_state(list(range(num_nodes)), (3, 1), num_attack_types,
                         network_config)
     init_state = state.copy()
     old_game_count = state.num_games
     state.new_game(init_state)
     assert state.num_games == old_game_count + 1
     assert state.done == False
     assert state.detected == False
     state.default_state(list(range(num_nodes)), (3, 1), num_attack_types,
                         network_config)
     init_state = state.copy()
     state.hacked = True
     old_hacked_count = 0
     state.new_game(init_state)
     assert state.num_hacks == old_hacked_count + 1
Exemplo n.º 6
0
 def test_initialization(self):
     GameState()
Exemplo n.º 7
0
    def __init__(self,
                 network_config: NetworkConfig = None,
                 manual_attacker: bool = True,
                 num_layers: int = 1,
                 num_servers_per_layer: int = 2,
                 num_attack_types: int = 10,
                 max_value: int = 9,
                 initial_state: GameState = None,
                 manual_defender: bool = False,
                 initial_state_path: str = None,
                 dense_rewards=False,
                 min_random_a_val: int = 0,
                 min_random_d_val: int = 0,
                 min_random_det_val: int = 0,
                 dense_rewards_v2=False,
                 reconnaissance_actions: bool = False,
                 max_random_v_val: int = 1,
                 dense_rewards_v3=False):
        """
        Class constructor, initializes the DTO

        :param network_config: the network configuration of the game (e.g. number of nodes and their connectivity)
        :param manual_attacker: whether the attacker is controlled manually or by an agent
        :param manual_attacker: whether the defender is controlled manually or by an agent
        :param num_layers: the number of layers in the network
        :param num_servers_per_layer: the number of servers per layer in the network
        :param num_attack_types: the number of attack types
        :param max_value: max value for a defense/attack attribute
        :param initial_state: the initial state
        :param initial_state_path: path to the initial state saved on disk
        :param dense_rewards: if true, give hacker dense rewards (reward for each intermediate server hacked)
        :param dense_rewards_v2: if true, give defender reward only when blocking
        :param min_random_a_val: minimum attack value when randomizing the state
        :param min_random_d_val: minimum defense value when randomizing the state
        :param min_random_det_val: minimum detection value when randomizing the state
        :param reconnaissance_actions: a boolean flag that indicates whether reconnaissance activities are enabled for
                                       the attacker
        :param max_random_v_val: maximum random vulnerability value when usign randomized environment
        """
        self.reconnaissance_actions = reconnaissance_actions
        self.manual_attacker = manual_attacker
        self.manual_defender = manual_defender
        self.num_layers = num_layers
        self.num_servers_per_layer = num_servers_per_layer
        self.num_attack_types = num_attack_types
        self.max_value = max_value
        self.min_random_a_val = min_random_a_val
        self.min_random_d_val = min_random_d_val
        self.min_random_det_val = min_random_det_val
        self.num_rows = self.num_layers + 2
        self.num_nodes = self.num_layers * self.num_servers_per_layer + 2  # +2 for Start and Data Nodes
        self.num_cols = self.num_servers_per_layer
        self.set_attack_actions()
        self.num_defense_actions = (self.num_attack_types + 1) * self.num_nodes
        self.num_states = self.num_nodes
        self.network_config = network_config
        self.initial_state_path = initial_state_path
        self.defense_val = 2
        self.attack_val = 0
        self.num_vulnerabilities_per_node = 1
        self.det_val = 2
        self.dense_rewards_v2 = dense_rewards_v2
        self.dense_rewards_v3 = dense_rewards_v3
        self.vulnerabilitiy_val = 0
        self.max_random_v_val = max_random_v_val
        self.num_vulnerabilities_per_layer = None
        if network_config is None:
            self.network_config = NetworkConfig(self.num_rows,
                                                self.num_cols,
                                                connected_layers=False)
        self.initial_state = initial_state
        if self.initial_state is None and self.initial_state_path is not None:
            self.initial_state = GameState.load(self.initial_state)
        if self.initial_state is None and self.initial_state_path is None:
            self.initial_state = GameState(
                min_random_a_val=min_random_a_val,
                min_random_det_val=min_random_det_val,
                min_random_d_val=min_random_d_val,
                max_value=self.max_value,
                max_random_v_val=self.max_random_v_val)
            self.initial_state.default_state(
                self.network_config.node_list,
                self.network_config.start_pos,
                self.num_attack_types,
                network_config=self.network_config,
            )
        self.dense_rewards = dense_rewards