Esempio n. 1
0
def edge_attachment_test(seed=None):
    import math
    if seed==None:
        seed = npr.randint(1E6)
    print('rnd seed: %d'%seed)
    npr.seed(seed)
    random.seed(seed)

    nn = 30
    G = nx.watts_strogatz_graph(n=nn, k=4, p=0.0)
    print('All new edges should lie close to the cycle')

    pos = {node:(math.cos(float(node)/nn * math.pi * 2),math.sin(float(node)/nn * math.pi * 2)) for node in G}

    def visualize_rewiring(G, added_edges_set, deled_edges_set, tpl_data):
        old_G = G.copy()
        old_G.remove_edges_from(added_edges_set)
        old_G.add_edges_from(deled_edges_set)
        print('added edges: ')
        print(added_edges_set)
        print('deled edges: ')
        print(deled_edges_set)
        benchmarks.editing_demo_draw(G=old_G, new_G=G, seed=1, pos=pos)
        print(tpl_data)
        pylab.show()

    params = {}
    params['edit_edges_tester'] = visualize_rewiring
    params['edge_edit_rate']    = [0.10]
    params['node_edit_rate']    = [0.]
    params['node_growth_rate']  = [0.]
    params['verbose'] = True

    algorithms.generate_graph(G, params=params)
Esempio n. 2
0
def coarsening_test():
#visualizes coarsening
    import matplotlib as mpl
    def build_block_G(pin=0.1, pout=0.01, block_size=32, num_blocks=4):
        G = nx.Graph()
        nn = block_size*num_blocks
        G.add_nodes_from([i for i in range(nn)])

        for nodeA in G:
            for nodeB in G:
                if nodeA / block_size == nodeB / block_size:
                    if random.random() < pin:
                        G.add_edge(nodeA, nodeB)
                else:
                    if random.random() < pout:
                        G.add_edge(nodeA, nodeB)
        G.remove_edges_from(G.selfloop_edges())
        return G

    G = build_block_G(pin=0.1, pout=0.01)
    def visualize_coarsening(G, G_coarse, c_data):
        npr.seed(10)
        random.seed(10)
        pos = nx.fruchterman_reingold_layout(G)

        seeds = list(c_data['aggregates'].keys())
        for seed in seeds:
            trapped_nodes = c_data['aggregates'][seed][:]
            trapped_nodes.remove(seed)
            #rnd_color     = random.choice(['r', 'b', 'g', 'c', 'm', 'y', 'w']) #[npr.rand(), npr.rand(), npr.rand(), npr.rand()]
            #rnd_color     = mpl.colors.rgb2hex((npr.rand(), npr.rand(), npr.rand()))
            #rnd_color     = random.random()
            #rnd_color      = random.choice(mpl.colors.cnames.keys())
            rnd_color     = (npr.rand(), npr.rand(), npr.rand(), 1.)
            color_seed = np.ones((1,4))
            color_rest = np.ones((len(trapped_nodes),4))
            for i,val in enumerate(rnd_color):
                color_seed[:,i] *= val 
                color_rest[:,i] *= val 
            nx.draw_networkx_nodes(G, pos=pos, nodelist=[seed],        node_color=color_seed, cmap=pylab.hot, node_size=500, with_labels=True, node_shape='s')
            nx.draw_networkx_nodes(G, pos=pos, nodelist=trapped_nodes, node_color=color_rest, cmap=pylab.hot, node_size=200, with_labels=True, node_shape='o')
        nx.draw_networkx_edges(G, pos=pos, alpha=1.0)
        nx.draw_networkx_labels(G, pos=pos)
        pylab.show()

    params = {}
    params['do_coarsen_tester'] = visualize_coarsening
    params['edge_edit_rate']    = [0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001]
    params['node_edit_rate']    = [0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001]
    params['node_growth_rate']    = [0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001]

    algorithms.generate_graph(G, params=params)
Esempio n. 3
0
def coarsening_test2(seed=None):
#visualizes coarsening: stores the coarsening of the nodes, and then labels the original nodes based on their aggregates in the final level
    import matplotlib as mpl
    if seed==None:
        seed = npr.randint(1E6)
    print('rnd seed: %d'%seed)
    npr.seed(seed)
    random.seed(seed)

    G = graphutils.load_graph('data/mesh33.gml')
    #G = graphutils.load_graph('data-engineering/watts_strogatz98_power.elist')
    c_tree = []
    def store_aggregation_chain(G, G_coarse, c_data):
        store_aggregation_chain.static_c_tree.append(c_data['home_nodes'].copy())
        #print c_data['home_nodes']
        #print store_aggregation_chain.static_c_tree

    store_aggregation_chain.static_c_tree = c_tree

    params = {}
    params['do_coarsen_tester'] = store_aggregation_chain
    params['node_edit_rate']    = [0, 0, 0, 0]  #change to force coarsening

    dummy_replica = algorithms.generate_graph(G, params=params)

    node_colors = {}
    aggregate_colors = {seed:(npr.rand(), npr.rand(), npr.rand(), 1.) for seed in list(c_tree[-1].values())}
    for node in G:
        my_final_agg = node
        for c_set in c_tree:
            my_final_agg = c_set[my_final_agg]  #this could be faster with union-find structure
        node_colors[node] = aggregate_colors[my_final_agg]
        clr = aggregate_colors[my_final_agg]
        G.node[node]['color'] = '%.3f %.3f %.3f'%(clr[0],clr[1],clr[2])
        G.node[node]['label'] = ''

    all_nodes = G.nodes()
    color_array = np.ones((len(all_nodes),4))
    for i,node in enumerate(all_nodes):
        color_array[i,:] *= node_colors[node] 

    #pos = nx.fruchterman_reingold_layout(G)
    #nx.draw_networkx_nodes(G, pos=pos, nodelist=G.nodes(), node_color=color_array, cmap=pylab.hot, node_size=500, with_labels=True, node_shape='s')
    #nx.draw_networkx_edges(G, pos=pos, alpha=1.0)
    #nx.draw_networkx_labels(G, pos=pos)
    #pylab.show()
    
    gpath     = 'output/coarsening_test_'+timeNow()+'.dot'
    gpath_fig = gpath+'.pdf'
    graphutils.write_graph(G=G, path=gpath)
    print('Writing graph image: %s ..'%gpath_fig)
    visualizer_cmdl = 'sfdp -Nwidth=0.10 -Nheight=0.10 -Nfixedsize=true -Nstyle=filled -Tpdf %s > %s &'%(gpath,gpath_fig)
    #visualizer_cmdl = 'sfdp -Nwidth=0.03 -Nheight=0.03 -Nfixedsize=true -Nstyle=solid  -Tpdf %s > %s &'%(gpath,gpath_fig)
    retCode = os.system(visualizer_cmdl)
    time.sleep(1)
    subprocess.call(['xdg-open', gpath_fig])
Esempio n. 4
0
def integrity_test():
    import algorithms
    print 'Integrity testing ...'
    graphs = {'karate': nx.generators.karate_club_graph(),
              'er200_025': nx.erdos_renyi_graph(n=200, p=0.25, seed=17),
              'er200_0001': nx.erdos_renyi_graph(n=200, p=0.001, seed=42)}

    params = {'verbose':True,
              'node_edit_rate': [0],
              'edge_edit_rate': [0],
              'node_growth_rate': [0],
              'verbose':False}
    for name,G in graphs.items():
        print name
        replica = algorithms.generate_graph(original=G, params=params)

        diff    = graphutils.graph_graph_delta(G, replica)
        assert diff['new_nodes'] == []
        assert diff['del_edges'] == []
        assert diff['new_nodes'] == []
        assert diff['del_edges'] == []
        
    print 'Integrity test: PASSED'
Esempio n. 5
0
def smoke_test():
    import algorithms
    print 'Smoke testing ...'
    graphs = {'karate': nx.generators.karate_club_graph(),
              'er200_025': nx.erdos_renyi_graph(n=200, p=0.25, seed=42),
              'er200_0001': nx.erdos_renyi_graph(n=200, p=0.001, seed=42)}

    params = {'verbose':False,
              'node_edit_rate': [0.1/(1.+i) for i in xrange(100)],
              'edge_edit_rate': [0.1/(1.+i) for i in xrange(100)],
              'node_growth_rate': [0.1/(1.+i) for i in xrange(100)]}
    for name,G in graphs.items():
        print name
        #print '  nn=%d,ne=%d'%(G.number_of_nodes(), G.number_of_edges())
        replica = algorithms.generate_graph(original=G, params=params)
        #print '  nn=%d,ne=%d'%(replica.number_of_nodes(), replica.number_of_edges())
        assert G.selfloop_edges() == []

    assert 0 == os.system(graphutils.MUSKETEER_EXAMPLE_CMD)
        

    print 'Smoke test: PASSED'
    print
    return
Esempio n. 6
0
    output_path = init_options['output_path']
    visualizer = init_options['visualizer']
    verbose = init_options['verbose']
    write_graph = init_options['write_graph']

    if verbose:
        print 'Loading: %s' % input_path
    G = graphutils.load_graph(path=input_path,
                              params={
                                  'graph_type': graph_type,
                                  'verbose': verbose
                              })

    if verbose:
        print 'Generating ...'
    new_G = algorithms.generate_graph(G, params=params)

    #optional
    #print graphutils.graph_graph_delta(G=G, new_G=new_G)
    #new_G = nx.convert_node_labels_to_integers(new_G, 1, 'default', True)

    #TODO: too many reports
    if params.get('stats_report_on_all_levels', False):
        model_Gs = [new_G.model_graph]
        Gs = [new_G]
        current_G = new_G.coarser_graph
        while current_G.coarser_graph != None:
            Gs.append(current_G)
            model_Gs.append(current_G.model_graph)
            current_G = current_G.coarser_graph
        model_Gs.reverse()
def coarsening_test():
    #visualizes coarsening
    import matplotlib as mpl

    def build_block_G(pin=0.1, pout=0.01, block_size=32, num_blocks=4):
        G = nx.Graph()
        nn = block_size * num_blocks
        G.add_nodes_from([i for i in range(nn)])

        for nodeA in G:
            for nodeB in G:
                if nodeA / block_size == nodeB / block_size:
                    if random.random() < pin:
                        G.add_edge(nodeA, nodeB)
                else:
                    if random.random() < pout:
                        G.add_edge(nodeA, nodeB)
        G.remove_edges_from(G.selfloop_edges())
        return G

    G = build_block_G(pin=0.1, pout=0.01)

    def visualize_coarsening(G, G_coarse, c_data):
        npr.seed(10)
        random.seed(10)
        pos = nx.fruchterman_reingold_layout(G)

        seeds = list(c_data['aggregates'].keys())
        for seed in seeds:
            trapped_nodes = c_data['aggregates'][seed][:]
            trapped_nodes.remove(seed)
            #rnd_color     = random.choice(['r', 'b', 'g', 'c', 'm', 'y', 'w']) #[npr.rand(), npr.rand(), npr.rand(), npr.rand()]
            #rnd_color     = mpl.colors.rgb2hex((npr.rand(), npr.rand(), npr.rand()))
            #rnd_color     = random.random()
            #rnd_color      = random.choice(mpl.colors.cnames.keys())
            rnd_color = (npr.rand(), npr.rand(), npr.rand(), 1.)
            color_seed = np.ones((1, 4))
            color_rest = np.ones((len(trapped_nodes), 4))
            for i, val in enumerate(rnd_color):
                color_seed[:, i] *= val
                color_rest[:, i] *= val
            nx.draw_networkx_nodes(G,
                                   pos=pos,
                                   nodelist=[seed],
                                   node_color=color_seed,
                                   cmap=pylab.hot,
                                   node_size=500,
                                   with_labels=True,
                                   node_shape='s')
            nx.draw_networkx_nodes(G,
                                   pos=pos,
                                   nodelist=trapped_nodes,
                                   node_color=color_rest,
                                   cmap=pylab.hot,
                                   node_size=200,
                                   with_labels=True,
                                   node_shape='o')
        nx.draw_networkx_edges(G, pos=pos, alpha=1.0)
        nx.draw_networkx_labels(G, pos=pos)
        pylab.show()

    params = {}
    params['do_coarsen_tester'] = visualize_coarsening
    params['edge_edit_rate'] = [
        0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
        0.001
    ]
    params['node_edit_rate'] = [
        0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
        0.001
    ]
    params['node_growth_rate'] = [
        0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
        0.001
    ]

    algorithms.generate_graph(G, params=params)
def coarsening_test2(seed=None):
    #visualizes coarsening: stores the coarsening of the nodes, and then labels the original nodes based on their aggregates in the final level
    import matplotlib as mpl
    if seed == None:
        seed = npr.randint(1E6)
    print('rnd seed: %d' % seed)
    npr.seed(seed)
    random.seed(seed)

    G = graphutils.load_graph('data/mesh33.gml')
    #G = graphutils.load_graph('data-engineering/watts_strogatz98_power.elist')
    c_tree = []

    def store_aggregation_chain(G, G_coarse, c_data):
        store_aggregation_chain.static_c_tree.append(
            c_data['home_nodes'].copy())
        #print c_data['home_nodes']
        #print store_aggregation_chain.static_c_tree

    store_aggregation_chain.static_c_tree = c_tree

    params = {}
    params['do_coarsen_tester'] = store_aggregation_chain
    params['node_edit_rate'] = [0, 0, 0, 0]  #change to force coarsening

    dummy_replica = algorithms.generate_graph(G, params=params)

    node_colors = {}
    aggregate_colors = {
        seed: (npr.rand(), npr.rand(), npr.rand(), 1.)
        for seed in list(c_tree[-1].values())
    }
    for node in G:
        my_final_agg = node
        for c_set in c_tree:
            my_final_agg = c_set[
                my_final_agg]  #this could be faster with union-find structure
        node_colors[node] = aggregate_colors[my_final_agg]
        clr = aggregate_colors[my_final_agg]
        G.node[node]['color'] = '%.3f %.3f %.3f' % (clr[0], clr[1], clr[2])
        G.node[node]['label'] = ''

    all_nodes = G.nodes()
    color_array = np.ones((len(all_nodes), 4))
    for i, node in enumerate(all_nodes):
        color_array[i, :] *= node_colors[node]

    #pos = nx.fruchterman_reingold_layout(G)
    #nx.draw_networkx_nodes(G, pos=pos, nodelist=G.nodes(), node_color=color_array, cmap=pylab.hot, node_size=500, with_labels=True, node_shape='s')
    #nx.draw_networkx_edges(G, pos=pos, alpha=1.0)
    #nx.draw_networkx_labels(G, pos=pos)
    #pylab.show()

    gpath = 'output/coarsening_test_' + timeNow() + '.dot'
    gpath_fig = gpath + '.pdf'
    graphutils.write_graph(G=G, path=gpath)
    print('Writing graph image: %s ..' % gpath_fig)
    visualizer_cmdl = 'sfdp -Nwidth=0.10 -Nheight=0.10 -Nfixedsize=true -Nstyle=filled -Tpdf %s > %s &' % (
        gpath, gpath_fig)
    #visualizer_cmdl = 'sfdp -Nwidth=0.03 -Nheight=0.03 -Nfixedsize=true -Nstyle=solid  -Tpdf %s > %s &'%(gpath,gpath_fig)
    retCode = os.system(visualizer_cmdl)
    time.sleep(1)
    subprocess.call(['xdg-open', gpath_fig])
Esempio n. 9
0
                                              id)
    WDS_services.has_solution(output_path, id)


if __name__ == "__main__":
    init_options = initialize()
    input_path = init_options['input_path']
    params = init_options['params']
    output_path = init_options['output_path']
    job_id = init_options['job_id']
    if input_path == None:
        print("No input network given")
        sys.exit(2)

    G, network_data = WDS_services.read_inp_file(input_path)
    #WDS_services.plot_graph(G, network_data)
    new_G = algorithms.generate_graph(G, params=params, planar=True)
    nx.write_edgelist(new_G, "Generated_network.edges", data=False)
    #print (new_G)

    if output_path == None:
        t_str = timeNow()
        if not os.path.exists('output'):
            os.mkdir('output')
        if not os.path.isdir('output'):
            raise ValueError('Cannot write to directory "output"')
        output_path = 'output/' + os.path.splitext(
            os.path.basename(input_path))[0] + timeNow()

    generateAndValidate(job_id, G, new_G, network_data, output_path)
Esempio n. 10
0
    init_options = initialize()
    input_path = init_options["input_path"]
    params = init_options["params"]
    graph_type = init_options["graph_type"]
    output_path = init_options["output_path"]
    visualizer = init_options["visualizer"]
    verbose = init_options["verbose"]
    write_graph = init_options["write_graph"]

    if verbose:
        print "Loading: %s" % input_path
    G = graphutils.load_graph(path=input_path, params={"graph_type": graph_type, "verbose": verbose})

    if verbose:
        print "Generating ..."
    new_G = algorithms.generate_graph(G, params=params)

    # optional
    # print graphutils.graph_graph_delta(G=G, new_G=new_G)
    # new_G = nx.convert_node_labels_to_integers(new_G, 1, 'default', True)

    # TODO: too many reports
    if params.get("stats_report_on_all_levels", False):
        model_Gs = [new_G.model_graph]
        Gs = [new_G]
        current_G = new_G.coarser_graph
        while current_G.coarser_graph != None:
            Gs.append(current_G)
            model_Gs.append(current_G.model_graph)
            current_G = current_G.coarser_graph
        model_Gs.reverse()
Esempio n. 11
0
    def generate(self, debug=False):
        ##prepare field
        #fill all field with walls
        self.field = [self.__char_wall * self.size[0]] * self.size[1]

        #make empty holes in each 2nd coord from (1,1): (3,1), (1,3), (3,3) and etc.
        for i in range(1, self.size[0] - 1, 2):
            for j in range(1, self.size[1] - 1, 2):
                self.set_cell(self.__char_empty, (i, j))

        #generate maze (maybe recursion?.. no)
        current = (random.randrange(3, self.size[0] - 4) | 1,
                   random.randrange(3, self.size[1] - 4) | 1
                   )  #self.player_coords
        stack = [current]
        while True:
            #current cell is visited, check nearest
            if debug:
                self.draw()
                time.sleep(0.015)
            self.set_cell(self.__char_visited, current)
            unvis = self.__get_unvisited_neighbours(current)

            #if no unvisited -- remove from stack, go back
            if len(unvis) == 0:
                stack = stack[:-1]
                if len(stack) == 0:
                    break
                current = stack[-1]
            #else choose new way randomly
            else:
                r = random.randrange(len(unvis))
                stack.append(unvis[r])
                next_c = stack[-1]
                #draw way #TODO: more beautiful
                if current[0] == next_c[0]:
                    for i in range(min(current[1], next_c[1]),
                                   max(current[1], next_c[1]) + 1):
                        self.set_cell(self.__char_visited, (current[0], i))
                else:
                    for i in range(min(current[0], next_c[0]),
                                   max(current[0], next_c[0]) + 1):
                        self.set_cell(self.__char_visited, (i, current[1]))
                current = next_c

        #generate rooms
        if self.conf.rooms():
            self.rooms = []
            for _ in range(random.randrange(2, 8)):
                for __ in range(5):  #try to place a room 5 times
                    room_size = [(3, 5), (5, 3)][random.randrange(2)]
                    room_pos = (
                        random.randrange(3, self.size[0] - 3 - room_size[0])
                        | 1,
                        random.randrange(3, self.size[1] - 3 - room_size[1])
                        | 1)
                    ok = True
                    for r in self.rooms:  #check collision
                        if self.__rooms_overlap(r, Room(room_pos, room_size)):
                            ok = False
                            break
                    if ok:
                        self.__place_room(room_pos, room_size)
                        break

        ##TODO: generate loops
        #if self.conf.loops():
        #    pass

        #place exit
        self.exit_coords = ((self.size[0] & (~1)) - 1,
                            (self.size[1] & (~1)) - 1)
        self.set_cell(self.__char_visited, self.exit_coords[0] - 1,
                      self.exit_coords[1])
        self.set_cell(self.__char_visited, self.exit_coords[0],
                      self.exit_coords[1] - 1)
        if self.conf.rooms():
            #self.__place_room( ((self.size[0]-1-3) | 1, (self.size[1]-1-3) | 1), (3,3) )
            self.__place_room(
                (self.exit_coords[0] - 2, self.exit_coords[1] - 2), (3, 3))
            self.exit_coords = (self.exit_coords[0] - 1,
                                self.exit_coords[1] - 1)
        #if self.size[0] & 1 == 1: #TODO: wut?? if field has right side 1 cell wider
        #    self.exit_coords = (self.size[0]-3, self.size[1]-3)
        #else:
        #    self.exit_coords = (self.size[0]-4, self.size[1]-3)
        self.set_cell(self.__char_exit, self.exit_coords)

        #TODO: generate keys
        if self.conf.keys():
            self.__generate_doors()

        #replace all visited cells with empty
        for i in range(len(self.field)):
            self.field[i] = self.field[i].replace(self.__char_visited,
                                                  self.__char_empty)
        g = algorithms.generate_graph(self, self.player_coords,
                                      self.exit_coords)  #TODO: temp!
        #print(g.verts)
        e = g.edges.items()