def test_scaling():

    vs = [Vertex(str(i)) for i in xrange(100)]
    # g = Graph(vs, [])
    g_scale = ScalingNetworkGraph(.1, 50, 400)
    print g_scale.avg_deg

    # g = [SmallWorldGraph(vs, int(round(g_scale.avg_deg)), 0) for i in xrange(20)]

    # gL = avg([ele.length for ele in g])
    # gC = avg([ele.clust for ele in g])

    # g = SmallWorldGraph(vs, int(round(g_scale.avg_deg)), .1)
    # # g.add_regular_edges(11)
    layout = CircleLayout(g_scale)

    # # draw the graph
    gw = GraphWorld()
    gw.show_graph(g_scale, layout)
    gw.mainloop()


    # layout = CircleLayout(gscale)
    # gw = GraphWorld()
    # gw.show_graph(g_norm, layout)
    # gw.mainloop()


    print gL, gC
Beispiel #2
0
def draw_graph(g):
    
    layout = CircleLayout(g)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
def main():
    g = SmallWorldGraph(1000, 10)
    g.rewire(0.10)
    print g.clustering_coefficient()
    print g.shortest_path_coeff()

    layout = GraphWorld.CircleLayout(g)

    # draw the graph
    gw = GraphWorld.GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
Beispiel #4
0
def main():
    read_write = TspRW()    # create a read_write object
    read_write.read_file(raw_input('Enter file to read from: '))    # reads all the coordinates of citties from an ASCII file
    l = read_write.labels   
    c = read_write.coordinates
    handle = Nearest_Neighbour(l, c)    # create a Nearest_neigbour class from the given vertices
    
    # for each vertex
    for i in handle.vertices:
        handle.mark_unvisited_cities()  # mark all the citites unvisited
        handle.empty_tour() # empties the tour
        handle.startcity = i    # set the city to start city
        handle.currentcity = handle.startcity   # sets the start city to current city
        handle.mark_visited()   # marks the city visited
        
        # while there is an unvisited city
        while len(handle.coordinates)!= 0:  
            handle.calculate_nearneighbor() # find the nearest neighbour for the current city
            handle.add_currcity_tour()  # adds the current city to the tour
            handle.add_tourCost()   # adds the cost to the tour
            handle.currentcity = handle.nearestcity # sets nearest city to current city
            handle.mark_visited()   # marks the current city visited
        e = Edge(handle.currentcity, handle.startcity)  # creates the last edge between the current and start city
        handle.best_tour.add_edge(e)    # add that last edge to best tour
        
        # updates best_tour_so_far if the last tour was lest costly
        if handle.cost_of_tour < handle.cost_of_best_tour_so_far :  
            handle.cost_of_best_tour_so_far = handle.cost_of_tour
            handle.best_tour_so_far = handle.best_tour
            handle.best_label_so_far = handle.best_label
            handle.best_coordinates_so_far = handle.best_coordinates

    # write the coordinates to an ASCII file with .tour extension
    read_write.coordinates = handle.best_coordinates_so_far
    read_write.labels = handle.best_label_so_far
    read_write.write_file()

    # print handle.best_tour_so_far
    # print handle.cost_of_best_tour_so_far
    # print handle.best_label_so_far
    # print handle.best_coordinates_so_far

    # for i in range(len(handle.best_label_so_far)):
    #     handle.best_label_so_far[i].pos = handle.best_coordinates_so_far

    # The following two lines allows to swtich the layout of the graph displayed
    layout = CartesianLayout(handle.best_tour_so_far)
    # layout = RandomLayout(handle.best_tour_so_far)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(handle.best_tour_so_far, layout)
    gw.mainloop()
Beispiel #5
0
    def generate_graph(self):
        """Generates the graph using GraphWorld.
        """
        grapher = BestPath(self.ichose) # create a BestPath object, using chosen cities
        grapher.set_map(self.graph) # set the map read from the database
        # generate a Tour by calling the generate_actualtour method of BetPath Class
        Tour = grapher.genrate_actualtour(Vertex(self.ichose[0]))
        
        layout = CircleLayout(Tour)

        gw = GraphWorld()
        gw.show_graph(Tour, layout)
        gw.mainloop()
Beispiel #6
0
def show_graph(g):
    import GraphWorld

    for v in g.vertices():
        if v.visited:
            v.color = 'white'
        else:
            v.color = 'red'

    layout = GraphWorld.CircleLayout(g)
    gw = GraphWorld.GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
Beispiel #7
0
def main():

    time.clock()
    object = TwoOpt()   # create a two-opt object

    read_write = TspRW()    # create a read_write object
    read_write.read_file(raw_input('Enter file to read from: '))    # reads all the coordinates of citties from an ASCII file
    object.labels = read_write.labels   # store the labels in a list
    object.coordinates = read_write.coordinates      # store the coordinates in this list

    numRound = int(raw_input('Enter the number of random graphs you want to generate: '))
    roundTimes = int(raw_input('Enter the number of times you want to swap edges: '))

    for i in range(numRound):
        object.generate_rand_graph()    # generate a random graph
        for i in range(roundTimes):
            object.generate_rand_vertices()    # randomly generate two indices which have no common connection
            object.find_random_edge()   # generate two random edges from the given indices
            object.find_new_edge()  # find the new edge if swap were to happen.

            dx_oldtour = object.cal_distance_of_edge(object.edge1) + object.cal_distance_of_edge(object.edge2)  # difference in cost before swapping using two-opt
            dx_newtour = object.cal_distance_of_edge(object.new_edge1) + object.cal_distance_of_edge(object.new_edge2)    # difference in cost after adding two edges.
            if dx_newtour < dx_oldtour:
                object.remove_edges()
                object.add_edge_reversely()
        if object.costOfBestTour > object.costOfTour:
            bestTour = object.tour
            bestTour_vertices = object.tour_vertices
            object.costOfBestTour = object.costOfTour

    # write the tour in .tour ASCII file
    read_write.coordinates = []
    read_write.labels = []
    for i in bestTour_vertices:
        read_write.coordinates.append(i.pos)
    for i in bestTour_vertices:
        read_write.labels.append(i.label)
    read_write.write_file()

    print 'Run time: ', time.clock()

    # The following two lines allows to switch the layout of the graph displayed
    # layout = CartesianLayout(bestTour)
    layout = RandomLayout(bestTour)
    # layout = CircleLayout(bestTour)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(bestTour, layout)
    gw.mainloop()
def test_regular_generator(n, k):
    # create n Vertices
    n = int(n)
    k = int(k)
    labels = string.ascii_lowercase + string.ascii_uppercase
    vs = [Vertex(c) for c in labels[:n]]

    # create a graph and a layout
    g = Graph(vs)
    # g.add_all_edges()
    g.add_regular_edges(k)
    layout = CircleLayout(g)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
def test_random_generator(n, p):
    # create n Vertices
    n = int(n)
    p = float(p)
    labels = string.ascii_lowercase + string.ascii_uppercase
    vs = [Vertex(c) for c in labels[:n]]

    # create a graph and a layout
    g = RandomGraph(vs)
    # g.add_all_edges()
    g.add_random_edges(p)
    layout = CircleLayout(g)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(g, layout)
    destroy_graph(gw, 2)
    gw.mainloop()
def main(script, n='252', k='5', p='0.1', *args):
    random.seed(17)

    # create n Vertices
    n = int(n)
    k = int(k)
    p = float(p)

    names = name_generator()
    vs = [Vertex(names.next()) for c in range(n)]

   
    g = SmallWorldGraph(vs, k, p)


    #from time import clock

    print 'number of edges = ', len(g.edges())
    print 'Is connected?', g.is_connected()
    print 'diameter = ', g.diameter()

    start = clock()
    print 'char_length = ', g.char_length()
    print clock()-start
    
    start = clock()
    print 'char_length2 = ', g.char_length2()
    print clock()-start

    start = clock()
    print 'char_length3 = ', g.char_length3()
    print clock()-start

    start = clock()
    print 'char_length4 = ', g.char_length4()
    print clock()-start

    print 'cluster_coef = ', g.cluster_coef()
   
    # draw the graph
    draw = False
    if draw:
        layout = CircleLayout(g)
        gw = GraphWorld()
        gw.show_graph(g, layout)
        gw.mainloop()
def main():
    read = ReadMap()
    read.read_file("test.map")
    print read.names
    grapher = BestPath(read.names[:5])
    grapher.set_map(read.graph)
    bestTour = grapher.genrate_actualtour(Vertex(read.names[4]))
    print bestTour
    newGraph = Graph(bestTour.items)
    for i in range(len(bestTour.items) - 1):
        newEdge = Edge(bestTour.items[i], bestTour.items[i + 1])
        newGraph.add_edge(newEdge)
    layout = RandomLayout(newGraph)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(newGraph, layout)
    gw.mainloop()
def something():
    n = 10
    p = 1

    # create n Vertices
    n = int(n)
    labels = string.ascii_lowercase + string.ascii_uppercase
    vs = [Vertex(c) for c in labels[:n]]

    # create a graph and a layout
    # g = RandomGraph(vs, p)
    g = Graph(vs, [])
    g.add_regular_edges(4)
    layout = CircleLayout(g)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
Beispiel #13
0
def main():
    
    read_write = TspRW()    # create a read_write object
    read_write.read_file(raw_input('Enter file to read from: '))    # reads all the coordinates of citties from an ASCII file

    # solver is an BNB object
    solver = BNB(read_write.coordinates)
    solver.explore()    # explores all the vertex using BNB algorithm

    # write the tour in .tour ASCII file
    read_write.coordinates = solver.tour
    read_write.write_file()
    
    # The following two lines allows to switch the layout of the graph displayed
    layout = CartesianLayout(solver.tourGraph)
    # layout = RandomLayout(solver.tourGraph)
    # layout = CircleLayout(solver.tourGraph)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(solver.tourGraph, layout)
    gw.mainloop()
def test_scaling():

    vs = [Vertex(str(i)) for i in xrange(100)]
    # g = Graph(vs, [])
    g_scale = ScalingNetworkGraph(.1, 50, 400)
    print g_scale.avg_deg

    # g = [SmallWorldGraph(vs, int(round(g_scale.avg_deg)), 0) for i in xrange(20)]

    # gL = avg([ele.length for ele in g])
    # gC = avg([ele.clust for ele in g])

    # g = SmallWorldGraph(vs, int(round(g_scale.avg_deg)), .1)
    # # g.add_regular_edges(11)
    layout = CircleLayout(g_scale)

    # # draw the graph
    gw = GraphWorld()
    gw.show_graph(g_scale, layout)
    gw.mainloop()

    # layout = CircleLayout(gscale)
    # gw = GraphWorld()
    # gw.show_graph(g_norm, layout)
    # gw.mainloop()

    print gL, gC
Beispiel #15
0
def main():
    read_write = TspRW()  # create a read_write object
    read_write.read_file(
        raw_input('Enter file to read from: '
                  ))  # reads all the coordinates of citties from an ASCII file
    l = read_write.labels
    c = read_write.coordinates
    handle = Nearest_Neighbour(
        l, c)  # create a Nearest_neigbour class from the given vertices

    # for each vertex
    for i in handle.vertices:
        handle.mark_unvisited_cities()  # mark all the citites unvisited
        handle.empty_tour()  # empties the tour
        handle.startcity = i  # set the city to start city
        handle.currentcity = handle.startcity  # sets the start city to current city
        handle.mark_visited()  # marks the city visited

        # while there is an unvisited city
        while len(handle.coordinates) != 0:
            handle.calculate_nearneighbor(
            )  # find the nearest neighbour for the current city
            handle.add_currcity_tour()  # adds the current city to the tour
            handle.add_tourCost()  # adds the cost to the tour
            handle.currentcity = handle.nearestcity  # sets nearest city to current city
            handle.mark_visited()  # marks the current city visited
        e = Edge(handle.currentcity, handle.startcity
                 )  # creates the last edge between the current and start city
        handle.best_tour.add_edge(e)  # add that last edge to best tour

        # updates best_tour_so_far if the last tour was lest costly
        if handle.cost_of_tour < handle.cost_of_best_tour_so_far:
            handle.cost_of_best_tour_so_far = handle.cost_of_tour
            handle.best_tour_so_far = handle.best_tour
            handle.best_label_so_far = handle.best_label
            handle.best_coordinates_so_far = handle.best_coordinates

    # write the coordinates to an ASCII file with .tour extension
    read_write.coordinates = handle.best_coordinates_so_far
    read_write.labels = handle.best_label_so_far
    read_write.write_file()

    # print handle.best_tour_so_far
    # print handle.cost_of_best_tour_so_far
    # print handle.best_label_so_far
    # print handle.best_coordinates_so_far

    # for i in range(len(handle.best_label_so_far)):
    #     handle.best_label_so_far[i].pos = handle.best_coordinates_so_far

    # The following two lines allows to swtich the layout of the graph displayed
    layout = CartesianLayout(handle.best_tour_so_far)
    # layout = RandomLayout(handle.best_tour_so_far)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(handle.best_tour_so_far, layout)
    gw.mainloop()
def something():
    n = 10
    p = 1

    # create n Vertices
    n = int(n)
    labels = string.ascii_lowercase + string.ascii_uppercase
    vs = [Vertex(c) for c in labels[:n]]

    # create a graph and a layout
    # g = RandomGraph(vs, p)
    g = Graph(vs, [])
    g.add_regular_edges(4)
    layout = CircleLayout(g)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
Beispiel #17
0
def main():

    read_write = TspRW()  # create a read_write object
    read_write.read_file(
        raw_input('Enter file to read from: '
                  ))  # reads all the coordinates of citties from an ASCII file

    # solver is an BNB object
    solver = BNB(read_write.coordinates)
    solver.explore()  # explores all the vertex using BNB algorithm

    # write the tour in .tour ASCII file
    read_write.coordinates = solver.tour
    read_write.write_file()

    # The following two lines allows to switch the layout of the graph displayed
    layout = CartesianLayout(solver.tourGraph)
    # layout = RandomLayout(solver.tourGraph)
    # layout = CircleLayout(solver.tourGraph)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(solver.tourGraph, layout)
    gw.mainloop()
Beispiel #18
0
from Graph import *
from GraphWorld import *

vs = [Vertex(c) for c in "abcdefgh"]
g = Graph(vs)
g.add_regular_edges(6)
layout = CircleLayout(g)

# draw the graph
gw = GraphWorld()
gw.show_graph(g, layout)
gw.mainloop()
Beispiel #19
0
 def show(self):
     # create a graph and a layout
     layout = GW.CircleLayout(self)
     gw = GW.GraphWorld()
     gw.show_graph(self, layout)
     gw.mainloop()
Beispiel #20
0
def main():

    time.clock()
    object = TwoOpt()  # create a two-opt object

    read_write = TspRW()  # create a read_write object
    read_write.read_file(
        raw_input('Enter file to read from: '
                  ))  # reads all the coordinates of citties from an ASCII file
    object.labels = read_write.labels  # store the labels in a list
    object.coordinates = read_write.coordinates  # store the coordinates in this list

    numRound = int(
        raw_input('Enter the number of random graphs you want to generate: '))
    roundTimes = int(
        raw_input('Enter the number of times you want to swap edges: '))

    for i in range(numRound):
        object.generate_rand_graph()  # generate a random graph
        for i in range(roundTimes):
            object.generate_rand_vertices(
            )  # randomly generate two indices which have no common connection
            object.find_random_edge(
            )  # generate two random edges from the given indices
            object.find_new_edge()  # find the new edge if swap were to happen.

            dx_oldtour = object.cal_distance_of_edge(
                object.edge1) + object.cal_distance_of_edge(
                    object.edge2
                )  # difference in cost before swapping using two-opt
            dx_newtour = object.cal_distance_of_edge(
                object.new_edge1) + object.cal_distance_of_edge(
                    object.new_edge2
                )  # difference in cost after adding two edges.
            if dx_newtour < dx_oldtour:
                object.remove_edges()
                object.add_edge_reversely()
        if object.costOfBestTour > object.costOfTour:
            bestTour = object.tour
            bestTour_vertices = object.tour_vertices
            object.costOfBestTour = object.costOfTour

    # write the tour in .tour ASCII file
    read_write.coordinates = []
    read_write.labels = []
    for i in bestTour_vertices:
        read_write.coordinates.append(i.pos)
    for i in bestTour_vertices:
        read_write.labels.append(i.label)
    read_write.write_file()

    print 'Run time: ', time.clock()

    # The following two lines allows to switch the layout of the graph displayed
    # layout = CartesianLayout(bestTour)
    layout = RandomLayout(bestTour)
    # layout = CircleLayout(bestTour)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(bestTour, layout)
    gw.mainloop()
Beispiel #21
0
def draw_graph(g):
    # draw the graph
    layout = CircleLayout(g)
    gw = GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
Beispiel #22
0
    def add_random_edges(self, prob):
        '''Starting with an edgeless graph, for every pair of nodes, add an edge with probability prob'''
        for edge in self.edges():
            self.remove_edge(edge)

        for pair in self._all_vert_pairs():
            if random.random() < prob:
                self.add_edge(Graph.Edge(*pair))


def proportion_connected(num_nodes, prob, num_trials=10):
    labels = string.ascii_lowercase + string.ascii_uppercase
    for trial in xrange(num_trials):
        g = RandomGraph([Vertex(c) for c in labels[:num_nodes]])
        g.add_random_edges


if __name__ == '__main__':
    import GraphWorld
    g = RandomGraph()
    for person in ('Brian', 'Morgan', 'David', 'Teresa', 'Corbin', 'Miranda',
                   'Remy', 'Jordan', 'Heidi'):
        g.add_vertex(Graph.Vertex(person))
    prob = float(input('connect edges with what probability? [0.0,1.0]'))
    g.add_random_edges(prob)
    print 'g {} connected!'.format('is' if g.is_connected() else 'is not')
    layout = GraphWorld.CircleLayout(g)
    gw = GraphWorld.GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()