예제 #1
0
def __generate_expected_behavior():
    ''' Generate the state transition for the POMDP Model'''
    for old_state_id in POMDPSettings.state_transition_with_adversary:
        old_state = POMDPSettings.state_space[old_state_id]
        #################################### New States ###################################
        for new_state_id in POMDPSettings.state_transition_with_adversary[
                old_state_id]:
            new_state = POMDPSettings.state_space[new_state_id]
            #################################### Defense Action ###################################
            for defense_action_id in POMDPSettings.state_transition_with_adversary[
                    old_state_id][new_state_id]:
                ################################ Initialize such condition if does not exist #################
                if defense_action_id not in POMDPSettings.state_transition_pomdp:
                    POMDPSettings.state_transition_pomdp[
                        defense_action_id] = {}
                if old_state_id not in POMDPSettings.state_transition_pomdp[
                        defense_action_id]:
                    POMDPSettings.state_transition_pomdp[defense_action_id][
                        old_state_id] = {}
                if new_state_id not in POMDPSettings.state_transition_pomdp[
                        defense_action_id][old_state_id]:
                    POMDPSettings.state_transition_pomdp[defense_action_id][
                        old_state_id][new_state_id] = 0.0

                ################################ Adversary Actions #####################################
                for adversary_action_id in POMDPSettings.state_transition_with_adversary[
                        old_state_id][new_state_id][defense_action_id]:
                    adversary = POMDPSettings.adversary_action_objects[
                        adversary_action_id]
                    ######## Expected State Transition #################
                    POMDPSettings.state_transition_pomdp[defense_action_id][old_state_id][new_state_id] += \
                        POMDPSettings.state_transition_with_adversary[old_state_id][new_state_id][defense_action_id][adversary_action_id]\
                        *adversary.attack_probability
    DataStructureFunctions.normalize_probability_by_keys(
        POMDPSettings.state_transition_pomdp)
def irrelevant_prunning(action_space_objects):
    # print("****** Before Irrelevant Prunning ******")
    # PrintLibrary.number_action_available_each_node(action_space_objects)
    for node_index in range(len(action_space_objects)):
        dict_cost_vs_effectiveness = {}
        index = 0
        for action in action_space_objects[node_index]:
            dict_cost_vs_effectiveness[index] = [action.cost,action.weighted_effectiveness]
            index += 1
        # print("Irrelevant Prunning %s"%(dict_cost_vs_effectiveness))
        sorted_list = DataStructureFunctions.sort_dict_by_values(dict_cost_vs_effectiveness,0)
        # print(sorted_list)
        seen_effectiveness = []
        delete_element = []
        for action_property in sorted_list:
            if len(seen_effectiveness) == 0:
                seen_effectiveness.append(action_property[1][1])
                continue
            else:
                current_id = action_property[0]
                current_effectiveness = action_property[1][1]
                if current_effectiveness <= seen_effectiveness[-1]:
                    delete_element.append(current_id)
                else:
                    seen_effectiveness.append(current_effectiveness)
        # print(seen_effectiveness)
        # print('Delete %s'%(delete_element))
        DataStructureFunctions.delete_values_by_index_from_list(action_space_objects[node_index],delete_element)
def cost_based_prunning(action_space_objects):
    for node_type in range(len(action_space_objects)):
        delete_element = []
        index = 0
        for action in action_space_objects[node_type]:
            if action.cost > POMDPSettings.MAXIMUM_BUDGET:
                delete_element.append(index)
            index += 1
        DataStructureFunctions.delete_values_by_index_from_list(action_space_objects[node_type],delete_element)
def calculate_mean_cluster(k_means_cluster,weighted_effectiveness_action,weighted_cost_effectiveness_action,mean_cluster):
    delete_cluster = []
    for i in range(len(mean_cluster)):
        if len(k_means_cluster[i])==0:
            delete_cluster.append(i)
            continue
        mean_cluster[i][0] = sum([weighted_effectiveness_action[element] for element in k_means_cluster[i]])/len(mean_cluster[i])
        mean_cluster[i][1] = sum([weighted_cost_effectiveness_action[element] for element in k_means_cluster[i]])/len(mean_cluster[i])
    DataStructureFunctions.delete_values_by_index_from_list(mean_cluster,delete_cluster)
    DataStructureFunctions.delete_values_by_index_from_list(k_means_cluster,delete_cluster)
예제 #5
0
def dfs_many_nodes(current_position,current_path):

    current_position_temp = []
    for node in current_position:
        current_position_temp.append(node)
    current_path.append(current_position_temp)
    # print('S --> P %s %s ' % (current_position, current_path))

    for node in current_position:
        if node==target:
            print("Path %s" % (current_path))
            create_files(current_path)
            del current_path[-1]
            return

    node_index = 0
    previously_explored = {}
    for node in current_position_temp:
        # print('Current Node %s'%(node))
        if adj_matrix[node][0] in previously_explored:
            node_index += 1
            continue
        path_index = 0
        for path_ind in path:
            if node in path_ind:
                next_node = adj_matrix[node][0]
                previously_explored[next_node] = 1
                other_node_index = 0
                other_node_delete = []
                for other_nodes in current_position:
                    if other_nodes==node:
                        other_node_index += 1
                        continue
                    for path_ind_other in path:
                        if other_nodes in path_ind_other:
                            if next_node in path_ind_other:
                                other_node_delete.append(other_node_index)
                                break
                    other_node_index += 1
                # print('%s %s --> %s of %s'%(current_position,other_node_delete,next_node,node_index))
                del current_position[node_index]
                current_position.insert(node_index, next_node)
                DataStructureFunctions.delete_values_by_index_from_list(current_position,other_node_delete)
                # print('%s %s' % (current_position, other_node_delete))
                dfs_many_nodes(sorted(current_position),current_path)
                del current_position[:]
                for past_node in current_position_temp:
                    current_position.append(past_node)
                break

            path_index += 1
        node_index += 1
    del current_path[-1]
def marginal_prunning(action_space_objects):
    # print("***** Before Marginal Prunning Number of Actions ******")
    # PrintLibrary.number_action_available_each_node(action_space_objects)
    for node_index in range(len(action_space_objects)):
        list_of_remove = []
        for index in range(len(action_space_objects[node_index])):
            action = action_space_objects[node_index][index]
            # action.printProperties()
            if action.effeciveness_with_scan < POMDPSettings.MINIMUM_EFFECTIVENESS_WITH_SCAN:
                list_of_remove.append(index)
                # print("Pruned Out : Action Effectiveness with Scan %s ID %s"%(action.effeciveness_with_scan,action.primary_key))
                continue
            if action.effeciveness_without_scan < POMDPSettings.MINIMUM_EFFECTIVENESS_WITHOUT_SCAN:
                list_of_remove.append(index)
                # print("Pruned Out : Action Effectiveness without Scan %s ID %s" % (action.effeciveness_without_scan,action.primary_key))
                continue
        DataStructureFunctions.delete_values_by_index_from_list(POMDPSettings.action_space_objects[node_index],list_of_remove)
def redundant_prunning(action_space_objects):
    # print("****** Before Redundant Prunning %s******"%(len(action_space_objects)))
    weighted_effectiveness_action = [] ###### Index of a value in this list is also representing the index in the action space
    weighted_cost_effectiveness_action = []
    effectiveness_action_scan = []
    effectiveness_action_without_scan = []
    for action in action_space_objects:
        weighted_effectiveness_action.append(action.weighted_effectiveness)
        effectiveness_action_scan.append(action.effeciveness_with_scan)
        effectiveness_action_without_scan.append(action.effeciveness_without_scan)
        if POMDPSettings.Y_AXIS_COST_EFFECTIVENESS:
            weighted_cost_effectiveness_action.append(action.weighted_cost_effectiveness)
        else:
            weighted_cost_effectiveness_action.append(action.cost)
    # print(weighted_effectiveness_action)
    # print(weighted_cost_effectiveness_action)
    if len(weighted_cost_effectiveness_action)==0:
        number_of_cluster = 0
    else:
        number_of_cluster = (max(weighted_effectiveness_action)-min(weighted_effectiveness_action))/POMDPSettings.CLUSTER_DIFFERENCE
    # print('Number Cluster %s'%(number_of_cluster))
    if number_of_cluster != int(number_of_cluster):
        number_of_cluster = int(number_of_cluster)+1
    elif number_of_cluster == 0:
        number_of_cluster = 1
    else:
        number_of_cluster = int(number_of_cluster)

    min_cluster = number_of_cluster
    max_cluster = min_cluster + POMDPSettings.MAX_CLUSTER_ITERATION + 1
    best_selection = None
    best_selection_distance = None
    best_cluster_size = None
    for cluster_size in range(min_cluster,max_cluster):
        if POMDPSettings.THREE_DIMENSIONAL_CLUSTER:
            selected_action, distance = Utilities.create_clusters_three_dimensional(effectiveness_action_scan,effectiveness_action_without_scan,
                                                                                    weighted_cost_effectiveness_action,
                                                                                    weighted_effectiveness_action,cluster_size)
        else:
            selected_action,distance = Utilities.create_clusters(weighted_effectiveness_action,weighted_cost_effectiveness_action,cluster_size)
        if best_selection is None \
                or best_selection_distance > distance:
            best_selection = selected_action
            best_selection_distance = distance
            best_cluster_size = cluster_size

    # print('Cluster Size %s\n\tSelected Action : %s with distance %s'%(best_cluster_size,best_selection,best_selection_distance))

    action_space_objects = DataStructureFunctions.keep_value_by_index_in_list(action_space_objects,best_selection)
    # print("****** After Redundant Prunning %s******" % (len(action_space_objects)))
    return action_space_objects
예제 #8
0
def calculate_precision():
    ########################### Find the action with maximum reward ################################
    max_reward = DataStructureFunctions.find_max_or_min_of_dictionary(
        POMDPSettings.rewards_pomdp, max_flag=True)
    print('Max Number of Steps %s' % (POMDPSettings.MAX_STEPS_TOPOLOGY))
    POMDPSettings.POMDP_PRECISION = max_reward
    current_value = max_reward
    while True:
        current_value *= POMDPSettings.DISCOUNT_FACTOR
        if current_value <= 0.001:
            break
        # print(current_value)
        POMDPSettings.POMDP_PRECISION += current_value
    POMDPSettings.POMDP_PRECISION *= POMDPSettings.REGRET_PERCENTAGE
    print('********* Precision %s' % (POMDPSettings.POMDP_PRECISION))
    Utilities.write_log_files('********* Precision %s' %
                              (POMDPSettings.POMDP_PRECISION))
예제 #9
0
def generate_observation_matrix():
    initialize_observation_space_data_structure()
    ''' Give the observation Matrix'''
    if POMDPSettings.OBSERVATION_ACTION_IRRESPECTIVE:
        POMDPSettings.observation_probability[
            POMDPSettings.WILDCARD_SYMBOL] = {}
        for old_state in POMDPSettings.state_space:
            if old_state.primary_key not in POMDPSettings.observation_probability[
                    POMDPSettings.WILDCARD_SYMBOL]:
                POMDPSettings.observation_probability[
                    POMDPSettings.WILDCARD_SYMBOL][old_state.primary_key] = {}
            for new_state in POMDPSettings.state_space:
                if new_state.primary_key == old_state.primary_key:
                    POMDPSettings.observation_probability[POMDPSettings.WILDCARD_SYMBOL][old_state.primary_key][new_state.primary_key] \
                        = new_state.get_observation_probability()[0]
                else:
                    POMDPSettings.observation_probability[POMDPSettings.WILDCARD_SYMBOL][old_state.primary_key][
                        new_state.primary_key] \
                        = new_state.get_observation_probability()[1]
                    difference = set(old_state.adversary_positions) & set(
                        new_state.adversary_positions)
                    for node in difference:
                        if node < 0:
                            POMDPSettings.observation_probability[
                                POMDPSettings.
                                WILDCARD_SYMBOL][old_state.primary_key][
                                    new_state.
                                    primary_key] /= POMDPSettings.MIRROR_NODE_FALSE_POSITIVE
                            POMDPSettings.observation_probability[
                                POMDPSettings.
                                WILDCARD_SYMBOL][old_state.primary_key][
                                    new_state.
                                    primary_key] *= POMDPSettings.MIRROR_NODE_TRUE_POSITIVE
                        else:
                            POMDPSettings.observation_probability[
                                POMDPSettings.
                                WILDCARD_SYMBOL][old_state.primary_key][
                                    new_state.
                                    primary_key] /= POMDPSettings.IDS_FALSE_POSITIVE
                            POMDPSettings.observation_probability[
                                POMDPSettings.
                                WILDCARD_SYMBOL][old_state.primary_key][
                                    new_state.
                                    primary_key] *= POMDPSettings.IDS_TRUE_POSITIVE_RATE
    else:
        for defense_type in POMDPSettings.action_space_objects:
            for defense in defense_type:
                if defense.primary_key not in POMDPSettings.observation_probability:
                    POMDPSettings.observation_probability[
                        defense.primary_key] = {}
                for old_state in POMDPSettings.state_space:
                    if old_state.primary_key not in POMDPSettings.observation_probability[
                            defense.primary_key]:
                        POMDPSettings.observation_probability[
                            defense.primary_key][old_state.primary_key] = {}
                    for new_state in POMDPSettings.state_space:  ###### This is actually for observation ################
                        if new_state.primary_key == old_state.primary_key:
                            POMDPSettings.observation_probability[defense.primary_key][old_state.primary_key][new_state.primary_key] \
                                = new_state.get_observation_probability()[0]
                        else:
                            POMDPSettings.observation_probability[defense.primary_key][old_state.primary_key][
                                new_state.primary_key] \
                                = new_state.get_observation_probability()[1]
                            difference = set(
                                old_state.adversary_positions) & set(
                                    new_state.adversary_positions)
                            for node in difference:
                                if node < 0:
                                    POMDPSettings.observation_probability[
                                        POMDPSettings.
                                        WILDCARD_SYMBOL][old_state.primary_key][
                                            new_state.
                                            primary_key] /= POMDPSettings.MIRROR_NODE_FALSE_POSITIVE
                                    POMDPSettings.observation_probability[
                                        POMDPSettings.
                                        WILDCARD_SYMBOL][old_state.primary_key][
                                            new_state.
                                            primary_key] *= POMDPSettings.MIRROR_NODE_TRUE_POSITIVE
                                else:
                                    POMDPSettings.observation_probability[
                                        POMDPSettings.
                                        WILDCARD_SYMBOL][old_state.primary_key][
                                            new_state.
                                            primary_key] /= POMDPSettings.IDS_FALSE_POSITIVE
                                    POMDPSettings.observation_probability[
                                        POMDPSettings.
                                        WILDCARD_SYMBOL][old_state.primary_key][
                                            new_state.
                                            primary_key] *= POMDPSettings.IDS_TRUE_POSITIVE_RATE
    DataStructureFunctions.normalize_probability_by_keys(
        POMDPSettings.observation_probability)
def create_clusters_three_dimensional(effectiveness_action_scan,effectiveness_action_without_scan,weighted_cost_effectiveness_action,
                                      weighted_effectiveness_action,number_of_cluster):
    DataStructureFunctions.normalization_by_min(effectiveness_action_scan)
    DataStructureFunctions.normalization_by_min(effectiveness_action_without_scan)
    DataStructureFunctions.normalization_by_min(weighted_cost_effectiveness_action)
    number_of_elements = len(effectiveness_action_scan)
    # print(weighted_effectiveness_action)
    # print(weighted_cost_effectiveness_action)
    # print('Number of cluster %s for %s elements'%(number_of_cluster,number_of_elements))

    ######################################### Pick 10 Random Poinrts for clustering #################################
    centroid_list = []
    mean_cluster = []
    number_taken = 0
    if number_of_cluster >= number_of_elements:
        for random_num in range(number_of_elements):
            mean_cluster.append([effectiveness_action_scan[random_num],effectiveness_action_without_scan[random_num],weighted_cost_effectiveness_action[random_num]])
    else:
        # print(" Start (*_*) (*_*) mean cluster %s %s" % (mean_cluster,number_of_cluster))
        while(True):
            random_num = random.randint(0,number_of_elements-1) ################# Return a random integer N such that a <= N <= b. ##################
            if random_num in centroid_list:
                continue
            centroid_list.append(random_num)
            mean_cluster.append([effectiveness_action_scan[random_num],effectiveness_action_without_scan[random_num],weighted_cost_effectiveness_action[random_num]])
            number_taken += 1
            if number_taken==number_of_cluster:
                break
    # print("mean cluster %s"%(mean_cluster))

    difference_between_two_iteration = 1
    iteration_index = 0
    cluster_index = [0 for i in range(number_of_elements)]
    distance_current_iteration = 0
    distance_previous_iteration = 0
    best_k_mean_cluster = None
    best_mean_cluster = None
    while difference_between_two_iteration > POMDPSettings.REDUNDANT_CLUSTERING_TOLERANCE_LEVEL:
        k_means_cluster = [[] for i in range(number_of_cluster)]
        for element_index in range(number_of_elements):
            min_distance = -1
            cluster_id = 0
            for centroid in mean_cluster:
                current_distance = pow((effectiveness_action_scan[element_index]-centroid[0]),2)\
                                   +pow(effectiveness_action_without_scan[element_index]-centroid[1],2)\
                                   +pow((weighted_cost_effectiveness_action[element_index]-centroid[2]),2)
                current_distance = pow(current_distance,0.5) #### Sqrt #######
                # print("Distance from Cluster %s is %s"%(cluster_id,current_distance))
                if min_distance == -1:
                    min_distance = current_distance
                    cluster_index[element_index] = cluster_id
                else:
                    if min_distance > current_distance:
                        min_distance = current_distance
                        cluster_index[element_index] = cluster_id
                cluster_id += 1
            k_means_cluster[cluster_index[element_index]].append(element_index)
            # print("Selected Cluster %s"%(cluster_index[element_index]))
        ############################ Calculate the mean of the centroid again ##############################
        calculate_mean_cluster_three_dimension(k_means_cluster, effectiveness_action_scan, effectiveness_action_without_scan,weighted_cost_effectiveness_action,
                               mean_cluster)
        # for j in range(len(k_means_cluster)):
        #     print('Cluster %s --> %s'%(j,k_means_cluster[j]))
        if iteration_index==0:
            iteration_index += 1
            distance_current_iteration = error_distance_three_dimensional(mean_cluster,k_means_cluster,
                                                                          effectiveness_action_scan,effectiveness_action_without_scan,
                                                                          weighted_cost_effectiveness_action)
            best_k_mean_cluster = k_means_cluster
            best_mean_cluster = mean_cluster
        else:
            ##################### Calculate the distance ######################################################
            distance_previous_iteration = distance_current_iteration
            distance_current_iteration = error_distance_three_dimensional(mean_cluster,k_means_cluster,effectiveness_action_scan,
                                                                          effectiveness_action_without_scan,weighted_cost_effectiveness_action)
            difference_between_two_iteration = distance_previous_iteration-distance_current_iteration
            if difference_between_two_iteration > 0:
                best_k_mean_cluster = k_means_cluster
                best_mean_cluster = mean_cluster
            # print("Difference between two iteration %s"%(difference_between_two_iteration))
            iteration_index += 1
        # print("Clustering : Current Iteration Distance %s"%(distance_current_iteration))
        if iteration_index == POMDPSettings.REDUNDANT_MAX_ITERATION:
            break
    distance = error_distance_three_dimensional(best_mean_cluster,best_k_mean_cluster,effectiveness_action_scan,
                                                effectiveness_action_without_scan,weighted_cost_effectiveness_action)
    best_action = trade_off_based_best_action_from_cluster(best_k_mean_cluster,best_mean_cluster,
                                        weighted_effectiveness_action,weighted_cost_effectiveness_action)
    # print(distance)
    DataStructureFunctions.delete_element_with_specific_value(best_action,-1)
    return best_action,distance