예제 #1
0
def run(graph, measures, k):
    result = []

    for measure in measures:
        if '_approx' in measure:
            measure = measure.replace('_approx', '')
            r = run_measure(graph=graph, measure=measure, k=k)
        else:
            r = run_measure(graph=graph, measure=measure)

        result.append(r)

    return result
예제 #2
0
    def track_simulation(self, step):
        """
         Keeps track of important simulation information at each step of the simulation

         :param step: current simulation iteration
         """

        measure = run_measure(self.graph_, self.prm['robust_measure'])

        ccs = list(nx.connected_components(self.graph_))
        ccs.sort(key=len, reverse=True)

        m = interp1d([0, len(ccs)], [0.15, 1])

        status = {}
        for n in self.graph:
            for idx, cc in enumerate(ccs):
                if n in self.attacked[0:step]:
                    status[n] = 1
                    break
                elif n in cc:
                    status[n] = float(m(idx))
                    break
                else:
                    status[n] = 0

        self.sim_info[step] = {
            'status':  list(status.values()),
            'failed': len(self.graph_) - len(max(ccs)),
            'measure': measure,
            'protected': self.protected,
            'edges_added': self.protected['added'][0:step] if 'added' in self.protected else [],
            'edges_removed': self.protected['removed'][0:step] if 'removed' in self.protected else []
        }
예제 #3
0
def test_measures():

    measure_ground_truth = {  # graph order: o4, p4, c4, k4_1, c4_0, c4_1, c4_2, c4_3
        'node_connectivity': [0, 1, 2, 2, 3, 0, 1, 1, 1],
        'edge_connectivity': [0, 1, 2, 2, 3, 0, 1, 1, 1],
        'diameter': [None, 3, 2, 2, 1, None, 5, 5, 5],
        'average_distance':
        [None, 1.67, 1.33, 1.17, 1, None, 2.29, 2.29, 2.29],
        'average_inverse_distance':
        [0, 0.72, 0.83, 0.92, 1.0, 0.36, 0.58, 0.58, 0.58],
        'average_vertex_betweenness':
        [0, 4, 3.5, 3.25, 3, 3.5, 11.5, None, None],
        'average_edge_betweenness':
        [0, 3.33, 2.0, 1.4, 1, 2, 7.11, 7.11, 7.11],
        'average_clustering_coefficient': [0, 0, 0, 0.83, 1, 0, 0, None, None],
        'largest_connected_component': [1, 4, 4, 4, 4, 4, 8, 8, 8],
        'spectral_radius': [0, 1.62, 2, 2.56, 3, 2, 2.34, 2.9, 3.65],
        'spectral_gap': [0, 1, 2, 2.56, 4, 0, 0.53, 1.19, 2],
        'natural_connectivity':
        [0, 0.65, 0.87, 1.29, 1.67, 0.87, 0.97, 1.28, 1.81],
        'spectral_scaling':
        [None, 7.18, 7.28, 0.17, 0.09, None, None, 7.04, 6.93],
        'generalized_robustness_index':
        [None, 7.18, 7.28, 0.17, 0.09, None, None, 7.04, 6.93],
        'algebraic_connectivity': [0, 0.59, 2, 2, 4, 0, 0.29, 0.4, 0.45],
        'number_spanning_trees': [0, 1, 4, 8, 16, 0, 16, 32, 48],
        'effective_resistance': [np.inf, 10, 5, 4, 3, np.inf, 46, 38, 35.33]
    }

    graphs = [
        o4_graph(),
        p4_graph(),
        c4_graph(),
        k4_1_graph(),
        k4_2_graph(),
        two_c4_0_bridge(),
        two_c4_1_bridge(),
        two_c4_2_bridge(),
        two_c4_3_bridge()
    ]

    for measure_name, graph_values in measure_ground_truth.items():
        for idx, graph in enumerate(graphs):

            value = run_measure(graph, measure_name)
            if value is not None: value = round(value, 2)

            # print(idx, measure_name, value)
            assert (value, graph_values[idx])
예제 #4
0
    def track_simulation(self, step):
        """
        Keeps track of important simulation information at each step of the simulation

        :param step: current simulation iteration
        """

        nodes_functioning = set(self.graph.nodes).difference(self.failed)

        measure = 0
        if len(nodes_functioning) > 0:
            measure = run_measure(self.graph.subgraph(nodes_functioning),
                                  self.prm['robust_measure'])

        self.sim_info[step] = {
            'status': [self.load[n] for n in self.graph.nodes],
            'failed': len(self.failed),
            'measure': measure,
            'protected': self.protected
        }
예제 #5
0
def measure_time(graph, measure, timeout):
    start = time.time()

    if measure in spectral_approx:
        k = 30
        measure = measure.replace('_approx', '')
    elif measure in graph_approx:
        k = int(0.1 * len(graph))
        measure = measure.replace('_approx', '')
    else:
        k = np.inf

    result = run_measure(graph, measure, k, timeout=timeout)

    end = time.time()
    run_time = math.ceil(end - start)

    if result is None:
        run_time = None

    return len(graph), run_time
예제 #6
0
def test_measures():

    ground_truth = {  # graph order: o4, p4, c4, k4_1, K4_2, c4_0, c4_1, c4_2, c4_3
        'node_connectivity': [0, 1, 2, 2, 3, 0, 1, 1, 1],
        'edge_connectivity': [0, 1, 2, 2, 3, 0, 1, 1, 1],
        'diameter': [None, 3, 2, 2, 1, None, 5, 5, 5],
        'average_distance':
        [None, 1.67, 1.33, 1.17, 1, None, 2.29, 2.29, 2.29],
        'average_inverse_distance':
        [0, 0.72, 0.83, 0.92, 1.0, 0.36, 0.58, 0.58, 0.58],
        # different Python versions have different results
        'average_vertex_betweenness':
        ([0, 4, 3.5, 3.25, 3, 3.5, 11.5, 11.5,
          11.5], [0, 4, 3.5, 3.25, 3, 3.5, 11.5, None, None]),
        'average_edge_betweenness':
        ([0, 3.33, 2.0, 1.4, 1, 2, 7.11, 6.4,
          5.82], [0, 3.33, 2.0, 1.4, 1, 2, 7.11, 7.11, 7.11]),
        'average_clustering_coefficient': [0, 0, 0, 0.83, 1, 0, 0, None, None],
        'largest_connected_component': [1, 4, 4, 4, 4, 4, 8, 8, 8],
        'spectral_radius': [0, 1.62, 2, 2.56, 3, 2, 2.34, 2.9, 3.65],
        'spectral_gap': [0, 1, 2, 2.56, 4, 0, 0.53, 1.19, 2],
        'natural_connectivity':
        [0, 0.65, 0.87, 1.29, 1.67, 0.87, 0.97, 1.28, 1.81],
        # different Python versions have different results
        'spectral_scaling': (
            [None, 7.16, 7.26, 0.17, 0.09, None, None, 7.01, 6.9],
            [None, 7.18, 7.28, 0.17, 0.09, None, None, 7.04, 6.93],
        ),
        'generalized_robustness_index': (
            [None, 7.16, 7.26, 0.17, 0.09, None, None, 7.01, 6.9],
            [None, 7.18, 7.28, 0.17, 0.09, None, None, 7.04, 6.93],
        ),
        'algebraic_connectivity': [0, 0.59, 2, 2, 4, 0, 0.29, 0.4, 0.45],
        'number_spanning_trees': [0, 1, 4, 8, 16, 0, 16, 32, 48],
        'effective_resistance': [np.inf, 10, 5, 4, 3, np.inf, 46, 38, 35.33]
    }

    graphs = [
        o4_graph(),
        p4_graph(),
        c4_graph(),
        k4_1_graph(),
        k4_2_graph(),
        two_c4_0_bridge(),
        two_c4_1_bridge(),
        two_c4_2_bridge(),
        two_c4_3_bridge()
    ]

    for measure, gt in ground_truth.items():
        for idx, graph in enumerate(graphs):

            value = run_measure(graph, measure)
            if value is not None: value = round(value, 2)

            # print(idx, measure, value, gt[idx])
            # different results locally versus Github CI
            if measure == 'average_vertex_betweenness' or measure == 'average_edge_betweenness' \
                    or measure == 'spectral_scaling' or measure == 'generalized_robustness_index':
                if value is None:
                    assert gt[0][idx] == value or gt[1][idx] == value
                else:
                    assert gt[0][idx] - 0.1 <= value <= gt[0][idx] + 0.1 or gt[
                        1][idx] - 0.1 <= value <= gt[1][idx] + 0.1
            else:
                if value is None:
                    assert gt[idx] == value
                else:
                    assert gt[idx] - 0.1 <= value <= gt[idx] + 0.1