Beispiel #1
1
    def allmax(self, num_nodes, edge_list):
        """Calculates allmax projection of STNU (see section 2.2).
        Returns two parameters:
            success - true if consistent
            potentials - potentials for use in Dijkstra algorithm
        """
        weights = {}
        neighbor_list = defaultdict(lambda: set())

        spfa_graph_to_distance_graph = {}

        for e in edge_list:
            if e.edge_type != EdgeType.LOWER_CASE:
                pair = (e.fro, e.to)
                if (pair not in weights) or (weights[pair] > e.value):
                    spfa_graph_to_distance_graph[pair] = e
                    neighbor_list[e.fro].add(e.to)
                    weights[pair] = e.value

        # Like in Johnson's algorithm we add node 0 artificially
        for node in range(1, num_nodes + 1):
            weights[(0,node)] = 0
            neighbor_list[0].add(node)

        # weights[(0,self.start_node)] = 0
        # neighbor_list[0].add(self.start_node)

        distances, negative_cycle = spfa(source = 0,
                                     num_nodes=num_nodes + 1,
                                     weights=weights,
                                     neighbor_list=neighbor_list)

        edges_on_cycle = None

        if negative_cycle is not None:
            assert(0 not in negative_cycle)
            edges_on_cycle = []
            nvalue = 0
            # print('---- to edge ')
            for fro, to in zip(negative_cycle, negative_cycle[1:] + negative_cycle[:1]):
                edge = spfa_graph_to_distance_graph[(fro, to)]
                edges_on_cycle.append(edge)
                nvalue += edge.value
                # print(str(fro) + '->' + str(to) + ':' + str(edge.value))
            # print('Cycle size: ' + str(len(edges_on_cycle)) + ' with value ' + str(nvalue))

            if nvalue > 0:
                raise Exception('Positive cycle value detected!');

        return (distances, edges_on_cycle)
Beispiel #2
1
    def consistent(self,num_nodes, edge_list):
        """Calculates consistency of reduced STNU.
        Returns one parameter:
            success - true if consistent
        """
        weights = {}
        neighbor_list = defaultdict(lambda: set())

        spfa_graph_to_distance_graph = {}

        for e in edge_list:
            pair = (e.fro, e.to)
            if (pair not in weights) or (weights[pair] > e.value):
                spfa_graph_to_distance_graph[pair] = e
                neighbor_list[e.fro].add(e.to)
                weights[pair] = e.value

        # Like in Johnson's algorithm we add node 0 artificially
        for node in range(1, num_nodes + 1):
            weights[(0,node)] = 0
            neighbor_list[0].add(node)

        distances, negative_cycle = spfa(source = 0,
                                     num_nodes=num_nodes + 1,
                                     weights=weights,
                                     neighbor_list=neighbor_list)

        edges_on_cycle = None

        if negative_cycle is not None:
            assert(0 not in negative_cycle)
            edges_on_cycle = []
            nvalue = 0
            # print('---- to edge ')
            for fro, to in zip(negative_cycle, negative_cycle[1:] + negative_cycle[:1]):
                edge = spfa_graph_to_distance_graph[(fro, to)]
                edges_on_cycle.append(edge)
                nvalue += edge.value
                # print(str(fro) + '->' + str(to) + ':' + str(edge.value))
            # print('Cycle size: ' + str(len(edges_on_cycle)) + ' with value ' + str(nvalue))

            if nvalue > 0:
                raise Exception('Positive cycle value detected!');

        return edges_on_cycle
Beispiel #3
0
    def allmax(self, num_nodes, edge_list):
        """Calculates allmax projection of STNU (see section 2.2).
        Returns two parameters:
            success - true if consistent
            potentials - potentials for use in Dijkstra algorithm
        """
        weights = {}
        neighbor_list = defaultdict(lambda: set())

        spfa_graph_to_distance_graph = {}

        for e in edge_list:
            if e.edge_type != EdgeType.LOWER_CASE:
                pair = (e.fro, e.to)
                if (pair not in weights) or (weights[pair] > e.value):
                    spfa_graph_to_distance_graph[pair] = e
                    neighbor_list[e.fro].add(e.to)
                    weights[pair] = e.value

        # Like in Johnson's algorithm we add node 0 artificially
        for node in range(1, num_nodes + 1):
            weights[(0, node)] = 0
            neighbor_list[0].add(node)

        # weights[(0,self.start_node)] = 0
        # neighbor_list[0].add(self.start_node)

        distances, negative_cycle = spfa(source=0,
                                         num_nodes=num_nodes + 1,
                                         weights=weights,
                                         neighbor_list=neighbor_list)

        edges_on_cycle = None

        if negative_cycle is not None:
            assert (0 not in negative_cycle)
            edges_on_cycle = []
            nvalue = 0
            # print('---- to edge ')
            for fro, to in zip(negative_cycle,
                               negative_cycle[1:] + negative_cycle[:1]):
                edge = spfa_graph_to_distance_graph[(fro, to)]
                edges_on_cycle.append(edge)
                nvalue += edge.value
                # print(str(fro) + '->' + str(to) + ':' + str(edge.value))
            # print('Cycle size: ' + str(len(edges_on_cycle)) + ' with value ' + str(nvalue))

            if nvalue > 0:
                raise Exception('Positive cycle value detected!')

        return (distances, edges_on_cycle)
Beispiel #4
0
    def consistent(self, num_nodes, edge_list):
        """Calculates consistency of reduced STNU.
        Returns one parameter:
            success - true if consistent
        """
        weights = {}
        neighbor_list = defaultdict(lambda: set())

        spfa_graph_to_distance_graph = {}

        for e in edge_list:
            pair = (e.fro, e.to)
            if (pair not in weights) or (weights[pair] > e.value):
                spfa_graph_to_distance_graph[pair] = e
                neighbor_list[e.fro].add(e.to)
                weights[pair] = e.value

        # Like in Johnson's algorithm we add node 0 artificially
        for node in range(1, num_nodes + 1):
            weights[(0, node)] = 0
            neighbor_list[0].add(node)

        distances, negative_cycle = spfa(source=0,
                                         num_nodes=num_nodes + 1,
                                         weights=weights,
                                         neighbor_list=neighbor_list)

        edges_on_cycle = None

        if negative_cycle is not None:
            assert (0 not in negative_cycle)
            edges_on_cycle = []
            nvalue = 0
            # print('---- to edge ')
            for fro, to in zip(negative_cycle,
                               negative_cycle[1:] + negative_cycle[:1]):
                edge = spfa_graph_to_distance_graph[(fro, to)]
                edges_on_cycle.append(edge)
                nvalue += edge.value
                # print(str(fro) + '->' + str(to) + ':' + str(edge.value))
            # print('Cycle size: ' + str(len(edges_on_cycle)) + ' with value ' + str(nvalue))

            if nvalue > 0:
                raise Exception('Positive cycle value detected!')

        return edges_on_cycle