Beispiel #1
0
    def fill_list(self):
        def check_small_worlds_conditions(graph):
            """
            Checks that n > k > ln(n) > 1

            See the paper "Collective dynamics of 'small-world' networks", 
            by Duncan J. Watts & Steven H. Strogatz
            """
            n = graph.number_of_nodes()
            k = topology.average_degree(graph)
            return n > k > math.log(n) > 1

        statistics = [("Number of nodes (n)", lambda graph: graph.number_of_nodes()),
                      ("Number of edges", lambda graph: graph.number_of_edges()),
                      ("Average degree (k)", topology.average_degree),
                      ("n > k > ln(n) > 1", check_small_worlds_conditions),
                      ("Directed?", lambda graph: graph.is_directed()),
                      ("Clustering coefficient", NX.average_clustering),
                      ("Clustering coefficient (random)", lambda graph: \
                           topology.average_clustering_random_graph(graph.number_of_nodes(), graph.number_of_edges())),
                      ("Average shortest path", topology.average_shortest_path),
                      ("Average shortest path (random)", lambda graph: \
                           topology.average_shortest_path_random_graph(graph.number_of_nodes(), graph.number_of_edges()))
                      ]  

        for label, func in statistics:
            self.list.Append([label, "%s" % func(self.graph)])

        self.list.SetColumnWidth(0, 230)
        self.list.SetColumnWidth(1, 120)
 def greed(self, g):  #greed method, factor=log(2,n)/2+1/2
     graph = g.graph
     n = graph.number_of_nodes()
     result = [0]
     for i in range(n - 1):
         k = g.nearest(result[-1], exclude=result)
         result.append(k[1])
     g.route = result
     return result
Beispiel #3
0
        def check_small_worlds_conditions(graph):
            """
            Checks that n > k > ln(n) > 1

            See the paper "Collective dynamics of 'small-world' networks", 
            by Duncan J. Watts & Steven H. Strogatz
            """
            n = graph.number_of_nodes()
            k = topology.average_degree(graph)
            return n > k > math.log(n) > 1
 def cheapest(self, g):  #cheapest method, factor=2
     graph = g.graph
     nlist = [0]
     nlist.append(g.nearest(nlist[0], exclude=nlist)[1])
     nodes = graph.number_of_nodes()
     while len(nlist) < nodes:
         cost_min = float("inf")
         nelem = len(nlist)
         for n2 in range(nodes):
             if n2 in nlist:
                 continue
             for i in range(nelem):
                 cost = g.savings(nlist[i], nlist[(i + 1) % nelem], n2)
                 if cost < cost_min:
                     cost_min = cost
                     indices = [i, n2]
         nlist.insert((indices[0] + 1) % nelem, indices[1])
     g.route = nlist
     return nlist
 def farthest(self, g):  #farthest method, factor=log(2,n)+0.16
     graph = g.graph
     nlist = [0]
     nlist.append(g.farthest(nlist[0], exclude=nlist)[1])
     nodes = graph.number_of_nodes()
     while len(nlist) < nodes:
         x0 = float("-inf")
         for r in nlist:
             (x, index) = g.farthest(r, exclude=nlist)
             if x > x0:
                 x0 = x
                 index0 = index
         s0 = float("inf")
         nelem = len(nlist)
         for t in range(nelem):
             s = g.savings(nlist[t], nlist[(t + 1) % nelem], index0)
             if s < s0:
                 s0 = s
                 ind_s = t
         nlist.insert((ind_s + 1) % nelem, index0)
     g.route = nlist
     return nlist
 def nearest(self, g):  #nearest method, factor=2
     graph = g.graph
     nlist = [0]
     nlist.append(g.nearest(nlist[0], exclude=nlist)[1])
     nodes = graph.number_of_nodes()
     while len(nlist) < nodes:
         x0 = float("inf")
         for r in nlist:
             (x, index) = g.nearest(r, exclude=nlist)
             if x < x0:
                 x0 = x
                 index0 = index
         s0 = float("inf")
         nelem = len(nlist)
         for t in range(nelem):
             s = g.savings(nlist[t], nlist[(t + 1) % nelem], index0)
             if s < s0:
                 s0 = s
                 ind_s = t
         nlist.insert(
             (ind_s + 1) % nelem, index0
         )  # default inserts BEFORE the elem, here modified to AFTER the elem
     g.route = nlist
     return nlist
Beispiel #7
0
def symmetric_gamma_node(graph, node, formulas_in):
    value_list = graph.node[node]
    size = len(value_list)
    index = 0
    for i in range(index,size):
        value = value_list[i]
        if value[0] == 'box':
            formula = value[1]
            if value not in formulas_in[node]:
                formulas_in[node].append(value)
                try:
                    graph.successors(node)
                except:
                    break
                next_node = graph.successors(node)
                for single_node in next_node:
                    if single_node < node:
                        #take the initial size of list to check whether it expanded
                        initial_size = len(graph.node[single_node])
                        if formula not in graph.node[single_node]:
                            graph.node[single_node].append(formula)

                        alpha_node_solve(graph,single_node)
                        beta_node_solve(graph,single_node, formulas_in)

                        final_size = len(graph.node[single_node])
                        #take diff to scan for these new entries
                        diff_size = final_size-initial_size
                        if diff_size > 0:
                            value_list_single_node_initial= graph.node[single_node]
                            value_list_single_node = value_list_single_node_initial[-diff_size:]
                            for value in value_list_single_node:
                                if isinstance(value,tuple) and value[0] == 'box':
                                    part = value[1]
                                    if part not in graph.node[node]:
                                        graph.node[node].append(part)

                                elif isinstance(value,tuple) and value[0] == 'not' and value[1][0] == 'diamond':
                                    part = ('not',value[1][1])
                                    if part not in graph.node[node]:
                                        graph.node[node].append(part)

                                elif isinstance(value, tuple) and value[0] == 'diamond':
                                    part = value[1]
                                    new_node= graph.number_of_nodes()+1
                                    #adding new world
                                    graph.add_edge(single_node,(new_node))
                                    #adding symmetric edge
                                    graph.add_edge((new_node),single_node)

                                    formulas_in[new_node] = []
                                    graph.node[single_node] = value_list_single_node_initial
                                    graph.node[new_node] = [part]

                                    #expand new delta formulae
                                    previous = graph.predecessors(new_node)
                                    for num in previous:
                                        set = graph.node[num];
                                        for j in range(0,len(set)):
                                            if set[j][0] == 'not' and set[j][1][0] == 'diamond':
                                                formula = ('not',set[j][1][1])
                                                if formula not in graph.node[new_node]:
                                                    graph.node[new_node].append(formula)
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)
                                            elif set[j][0] == 'box':
                                                if set[j][1] not in graph.node[new_node]:
                                                    graph.node[new_node].append(set[j][1])
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)
                                elif isinstance(value,tuple) and value[0] == 'not' and value[1][0] == 'box':
                                    part = ('not', value[1][1])
                                    new_node= graph.number_of_nodes()+1
                                    #adding new world
                                    graph.add_edge(single_node,(new_node))
                                    #adding symmetric edge
                                    graph.add_edge((new_node),single_node)

                                    formulas_in[new_node] = []
                                    graph.node[single_node] = value_list_single_node_initial
                                    graph.node[new_node] = [part]

                                    previous = graph.predecessors(new_node)
                                    for num in previous:
                                        set = graph.node[num];
                                        for j in range(0,len(set)):
                                            if set[j][0] == 'not' and set[j][1][0] == 'diamond':
                                                formula = ('not',set[j][1][1])
                                                if formula not in graph.node[new_node]:
                                                    graph.node[new_node].append(formula)
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)
                                            elif set[j][0] == 'box':
                                                if set[j][1] not in graph.node[new_node]:
                                                    graph.node[new_node].append(set[j][1])
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)

                alpha_node_solve(graph, node)
                beta_node_solve(graph, node, formulas_in)
                delta_node_solve(graph, node, formulas_in)

        elif value[0] == 'not' and value[1][0] == 'diamond':
            formula = ('not', value[1][1])
            if value not in formulas_in[node]:
                formulas_in[node].append(value)
                try:
                    graph.successors(node)
                except:
                    break
                next_node = graph.successors(node)

                for single_node in next_node:
                    if single_node < node:
                        #take the initial size of list to check whether it expanded
                        initial_size = len(graph.node[single_node])
                        if formula not in graph.node[single_node]:
                            graph.node[single_node].append(formula)
                        alpha_node_solve(graph, single_node)
                        beta_node_solve(graph, single_node, formulas_in)
                        final_size = len(graph.node[single_node])

                        #take diff to scan for these new entries
                        diff_size = final_size-initial_size
                        if diff_size > 0:
                            value_list_single_node_initial= graph.node[single_node]
                            value_list_single_node = value_list_single_node_initial[-diff_size:]
                            for value in value_list_single_node:

                                if isinstance(value,tuple) and value[0] == 'box':
                                    part = value[1]
                                    if part not in graph.node[node]:
                                        graph.node[node].append(part)
                                elif isinstance(value,tuple) and value[0] == 'not' and value[1][0] == 'diamond':
                                    part = ('not',value[1][1])
                                    if part not in graph.node[node]:
                                        graph.node[node].append(part)
                                elif isinstance(value, tuple) and value[0] == 'diamond':
                                    part = value[1]
                                    new_node= graph.number_of_nodes()+1
                                    #adding new world
                                    graph.add_edge(single_node,(new_node))
                                    #adding symmetric edge
                                    graph.add_edge((new_node),single_node)

                                    formulas_in[new_node] = []
                                    graph.node[single_node] = value_list_single_node_initial
                                    graph.node[new_node] = [part]

                                    previous = graph.predecessors(new_node)
                                    for num in previous:
                                        set = graph.node[num];
                                        for j in range(0,len(set)):
                                            if set[j][0] == 'not' and set[j][1][0] == 'diamond':
                                                formula = ('not',set[j][1][1])
                                                if formula not in graph.node[new_node]:
                                                    graph.node[new_node].append(formula)
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)
                                            elif set[j][0] == 'box':
                                                if set[j][0] not in graph.node[new_node]:
                                                    graph.node[new_node].append(set[j][1])
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)

                                elif isinstance(value,tuple) and value[0] == 'not' and value[1][0] == 'box':
                                    part = ('not', value[1][1])
                                    new_node= graph.number_of_nodes()+1
                                    #adding new world
                                    graph.add_edge(single_node,(new_node))
                                    #adding symmetric edge
                                    graph.add_edge((new_node),single_node)

                                    formulas_in[new_node] = []
                                    graph.node[single_node] = value_list_single_node_initial
                                    graph.node[new_node] = [part]

                                    previous = graph.predecessors(new_node)
                                    for num in previous:
                                        set = graph.node[num];
                                        for j in range(0,len(set)):
                                            if set[j][0] == 'not' and set[j][1][0] == 'diamond':
                                                formula = ('not',set[j][1][1])
                                                if formula not in graph.node[new_node]:
                                                    graph.node[new_node].append(formula)
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)
                                            elif set[j][0] == 'box':
                                                if set[j][0] not in graph.node[new_node]:
                                                    graph.node[new_node].append(set[j][1])
                                                    alpha_node_solve(graph,new_node)
                                                    beta_node_solve(graph,new_node, formulas_in)
                alpha_node_solve(graph, node)
                beta_node_solve(graph, node, formulas_in)
                delta_node_solve(graph, node, formulas_in)
Beispiel #8
0
def delta_node_solve(graph, node, formulas_in):
    delta_list = graph.node[node]

    for i in range(len(delta_list)-1,-1,-1):
        part1 = delta_list[i][0]
        if part1 == 'diamond':
            sub = delta_list[i]
            if sub not in formulas_in[node]:
                formulas_in[node].append(sub)
                part2 = delta_list[i][1]
                new_node= graph.number_of_nodes()+1
                graph.add_edge(node,(new_node)) #adding new world and relation Rxx'
                graph.add_edge((new_node), node) #add symmetric edge
                graph.node[node] = delta_list

                graph.node[new_node] = [part2]
                formulas_in[new_node] = []
                alpha_node_solve(graph, node)
                beta_node_solve(graph, node, formulas_in)

                previous = graph.predecessors(new_node)
                for num in previous:
                    set = graph.node[num];
                    for j in range(0,len(set)):
                        if set[j][0] == 'not' and set[j][1][0] == 'diamond':
                            formula = ('not',set[j][1][1])
                            if formula not in graph.node[new_node]:
                                graph.node[new_node].append(formula)
                                alpha_node_solve(graph, new_node)
                                beta_node_solve(graph, new_node, formulas_in)
                        elif set[j][0] == 'box':
                            if set[j][1] not in graph.node[new_node]:
                                graph.node[new_node].append(set[j][1])
                                alpha_node_solve(graph, new_node)
                                beta_node_solve(graph, new_node, formulas_in)

        elif part1 == 'not' and delta_list[i][1][0] == 'box':
            sub = delta_list[i]
            if sub not in formulas_in[node]:
                formulas_in[node].append(sub)
                part2 = ('not', delta_list[i][1][1])
                new_node= graph.number_of_nodes()+1
                graph.add_edge(node,(new_node)) #adding new world and relation Rxx'
                graph.add_edge((new_node), node) #add symmetric edge

                graph.node[node] = delta_list
                graph.node[new_node] = [part2]
                formulas_in[new_node] = []
                alpha_node_solve(graph, node)
                beta_node_solve(graph, node, formulas_in)

                previous = graph.predecessors(new_node)
                for num in previous:
                    set = graph.node[num];
                    for j in range(0,len(set)):
                        if set[j][0] == 'not' and set[j][1][0] == 'diamond':
                            formula = ('not',set[j][1][1])
                            if formula not in graph.node[new_node]:
                                graph.node[new_node].append(formula)
                                alpha_node_solve(graph, new_node)
                                beta_node_solve(graph, new_node, formulas_in)
                        elif set[j][0] == 'box':
                            if set[j][1] not in graph.node[new_node]:
                                graph.node[new_node].append(set[j][1])
                                alpha_node_solve(graph, new_node)
                                beta_node_solve(graph, new_node, formulas_in)