def main():
    """
    Run the server
    """
    
    time1 = time.time()
    print("Initializing..")

    # Grab program arguments
    args = parse_args()

    # Load data file
    try:
        global V_coord
        (V, E, V_coord, E_name) = readgraph.readgraph(args.graphname)
    except FileNotFoundError:
        print("Data file {} not found!".format(args.graphname), file=sys.stderr)
        exit()

    # Initialize serial port
    try:
        if args.serialport:
            print("Opening serial port: {}".format(args.serialport))
            serial_out = serial_in = serial.Serial(args.serialport, 9600)
        else:
            print("No serial port. Supply one with the -s port option.")
            exit()
    except serial.SerialException:
        print("Could not open serial port: {}".format(args.serialport),
              file=sys.stderr)
        exit()

    # Set debug mode
    if args.verbose:
        debug = True
    else:
        debug = False
    debug = True

    # Generate a graph from the map and grab the needed values
    G = digraph.Digraph(E)
    # Add all orphan vertices. Not really useful, and in fact detrimental
    # in all cases, but do it for the sake of completeness
    for v in V:
        G.add_vertex(v)

    # Print some debug output
    if debug:
        print("Graph loaded with {} vertices and {} edges.".format(G.num_vertices(), G.num_edges()))

    # initialize storage value
    prev_end = 0
    time2 = time.time()
    delta_t = repr(time2 - time1)
    print("Done initializing, took " + delta_t + " seconds")

    # Parse input
    while True:
        msg = receive(serial_in)
        debug and print("GOT:{}:".format(msg), file=sys.stderr)

        fields = msg.split(" ")

        # Ignore malformed messages
        if len(fields) != 4:
            debug and print("Ignoring message: {}".format(msg))
            continue
        time1 = time.time()
        print("Processing..")
        # Get start and end vertices
        start_v = (int(fields[0])/10**5, int(fields[1])/10**5)
        end_v = (int(fields[2])/10**5, int(fields[3])/10**5)
        start = nearest_vertex( start_v )
        end = nearest_vertex( end_v )

        debug and print("Routing path from vertex {} to {}".format(start, end))

        if end is prev_end:
            # to speed things up, if the user wants to go to the same destination
            # as last time, find which point in the previous path
            # the user is close to, and return the shortest distance to
            # the next point in the previous path
            min_dist = float('infinity')
            for i in range(len(path)):
                print("i is: " + str(i) + " and path[i] is: " + str(path[i]))
                dist = distance(V_coord[start], V_coord[path[i]])
                if dist < min_dist:
                    closest_v = path[i]
                    min_dist = dist
                    next_dest = path[i + 1]
            
            if closest_v == prev_end:
                # we're there!
                prev_end = 0
                continue
            
            secondary_path = least_cost_path(G, start, next_dest, cost_distance)
            
            send(serial_out, str(len(secondary_path)))
            print("The secondary path is:")
            for v in secondary_path:
                print(str(v))
                print("lat: " + str(int(V_coord[v][0] * 10**5)) + " lon: " + str(int(V_coord[v][1] * 10**5)))
                send(serial_out, "{} {}".format(int(V_coord[v][0] * 10**5),
                                     int(V_coord[v][1] * 10**5)))
                
            print("Send path of length {}".format(len(secondary_path)))
            
            time2 = time.time()
            delta_t = repr(time2 - time1)
            print("Done processing, took " + delta_t + "seconds")
            continue

        path = least_cost_path(G, start, end, cost_distance)
        if path is None:
            send(serial_out, "0")
            debug and print("No path found!", file=sys.stderr)
        else:
            send(serial_out, str(len(path)))
            print("The path is:")
            for v in path:
                print(str(v))
                print("lat: " + str(int(V_coord[v][0] * 10**5)) + " lon: " + str(int(V_coord[v][1] * 10**5)))
                send(serial_out, "{} {}".format(int(V_coord[v][0] * 10**5),
                                     int(V_coord[v][1] * 10**5)))
            print("Send path of length {}".format(len(path)))
            
        
        # store for optimization
        prev_start = start
        prev_end = end
        
        time2 = time.time()
        delta_t = repr(time2 - time1)
        print("Done processing, took " + delta_t + "seconds")
Example #2
0
import os

import readgraph
from graphviz import Digraph

dot = Digraph()
nodes, edges = readgraph.readgraph(10)

for i in range(1, len(nodes) + 1):
    dot.node(str(i))

for edge in edges:
    dot.edge(str(edge[0]), str(edge[1]))

dot.render(os.path.join(os.getcwd(), 'graph'), format='png')
Example #3
0
def main():
    """
    Run the server
    """

    time1 = time.time()
    print("Initializing..")

    # Grab program arguments
    args = parse_args()

    # Load data file
    try:
        global V_coord
        (V, E, V_coord, E_name) = readgraph.readgraph(args.graphname)
    except FileNotFoundError:
        print("Data file {} not found!".format(args.graphname),
              file=sys.stderr)
        exit()

    # Initialize serial port
    try:
        if args.serialport:
            print("Opening serial port: {}".format(args.serialport))
            serial_out = serial_in = serial.Serial(args.serialport, 9600)
        else:
            print("No serial port. Supply one with the -s port option.")
            exit()
    except serial.SerialException:
        print("Could not open serial port: {}".format(args.serialport),
              file=sys.stderr)
        exit()

    # Set debug mode
    if args.verbose:
        debug = True
    else:
        debug = False

    # Generate a graph from the map and grab the needed values
    G = digraph.Digraph(E)
    # Add all orphan vertices. Not really useful, and in fact detrimental
    # in all cases, but do it for the sake of completeness
    for v in V:
        G.add_vertex(v)

    # Print some debug output
    if debug:
        print("Graph loaded with {} vertices and {} edges.".format(
            G.num_vertices(), G.num_edges()))

    time2 = time.time()
    delta_t = repr(time2 - time1)
    print("Done initializing, took " + delta_t + " seconds")

    # Parse input
    while True:

        msg = receive(serial_in)
        debug and print("GOT:{}:".format(msg), file=sys.stderr)

        fields = msg.split(" ")

        # Ignore malformed messages
        if len(fields) != 4:
            debug and print("Ignoring message: {}".format(msg))
            continue
        time1 = time.time()
        print("Processing..")
        # Get start and end vertices
        start_v = (int(fields[0]) / 10**5, int(fields[1]) / 10**5)
        end_v = (int(fields[2]) / 10**5, int(fields[3]) / 10**5)
        start = nearest_vertex(start_v)
        end = nearest_vertex(end_v)

        debug and print("Routing path from vertex {} to {}".format(start, end))

        path = least_cost_path(G, start, end, cost_distance)
        if path is None:
            send(serial_out, "0")
            debug and print("No path found!", file=sys.stderr)
        else:
            send(serial_out, str(len(path)))
            for v in path:
                send(
                    serial_out, "{} {}".format(int(V_coord[v][0] * 10**5),
                                               int(V_coord[v][1] * 10**5)))
            print("Send path of length {}".format(len(path)))
        time2 = time.time()
        delta_t = repr(time2 - time1)
        print("Done processing, took " + delta_t + "seconds")
Example #4
0
def main():
    """
    Run the server
    """

    time1 = time.time()
    print("Initializing..")

    # Grab program arguments
    args = parse_args()

    # Load data file
    try:
        global V_coord
        (V, E, V_coord, E_name) = readgraph.readgraph(args.graphname)
    except FileNotFoundError:
        print("Data file {} not found!".format(args.graphname),
              file=sys.stderr)
        exit()

    # Initialize serial port
    try:
        if args.serialport:
            print("Opening serial port: {}".format(args.serialport))
            serial_out = serial_in = serial.Serial(args.serialport, 9600)
        else:
            print("No serial port. Supply one with the -s port option.")
            exit()
    except serial.SerialException:
        print("Could not open serial port: {}".format(args.serialport),
              file=sys.stderr)
        exit()

    # Set debug mode
    if args.verbose:
        debug = True
    else:
        debug = False
    debug = True

    # Generate a graph from the map and grab the needed values
    G = digraph.Digraph(E)
    # Add all orphan vertices. Not really useful, and in fact detrimental
    # in all cases, but do it for the sake of completeness
    for v in V:
        G.add_vertex(v)

    # Print some debug output
    if debug:
        print("Graph loaded with {} vertices and {} edges.".format(
            G.num_vertices(), G.num_edges()))

    # initialize storage value
    prev_end = 0
    time2 = time.time()
    delta_t = repr(time2 - time1)
    print("Done initializing, took " + delta_t + " seconds")

    # Parse input
    while True:
        msg = receive(serial_in)
        debug and print("GOT:{}:".format(msg), file=sys.stderr)

        fields = msg.split(" ")

        # Ignore malformed messages
        if len(fields) != 4:
            debug and print("Ignoring message: {}".format(msg))
            continue
        time1 = time.time()
        print("Processing..")
        # Get start and end vertices
        start_v = (int(fields[0]) / 10**5, int(fields[1]) / 10**5)
        end_v = (int(fields[2]) / 10**5, int(fields[3]) / 10**5)
        start = nearest_vertex(start_v)
        end = nearest_vertex(end_v)

        debug and print("Routing path from vertex {} to {}".format(start, end))

        if end is prev_end:
            # to speed things up, if the user wants to go to the same destination
            # as last time, find which point in the previous path
            # the user is close to, and return the shortest distance to
            # the next point in the previous path
            min_dist = float('infinity')
            for i in range(len(path)):
                print("i is: " + str(i) + " and path[i] is: " + str(path[i]))
                dist = distance(V_coord[start], V_coord[path[i]])
                if dist < min_dist:
                    closest_v = path[i]
                    min_dist = dist
                    next_dest = path[i + 1]

            if closest_v == prev_end:
                # we're there!
                prev_end = 0
                continue

            secondary_path = least_cost_path(G, start, next_dest,
                                             cost_distance)

            send(serial_out, str(len(secondary_path)))
            print("The secondary path is:")
            for v in secondary_path:
                print(str(v))
                print("lat: " + str(int(V_coord[v][0] * 10**5)) + " lon: " +
                      str(int(V_coord[v][1] * 10**5)))
                send(
                    serial_out, "{} {}".format(int(V_coord[v][0] * 10**5),
                                               int(V_coord[v][1] * 10**5)))

            print("Send path of length {}".format(len(secondary_path)))

            time2 = time.time()
            delta_t = repr(time2 - time1)
            print("Done processing, took " + delta_t + "seconds")
            continue

        path = least_cost_path(G, start, end, cost_distance)
        if path is None:
            send(serial_out, "0")
            debug and print("No path found!", file=sys.stderr)
        else:
            send(serial_out, str(len(path)))
            print("The path is:")
            for v in path:
                print(str(v))
                print("lat: " + str(int(V_coord[v][0] * 10**5)) + " lon: " +
                      str(int(V_coord[v][1] * 10**5)))
                send(
                    serial_out, "{} {}".format(int(V_coord[v][0] * 10**5),
                                               int(V_coord[v][1] * 10**5)))
            print("Send path of length {}".format(len(path)))

        # store for optimization
        prev_start = start
        prev_end = end

        time2 = time.time()
        delta_t = repr(time2 - time1)
        print("Done processing, took " + delta_t + "seconds")
Example #5
0
def main():
    """
    Run the server
    """
    
    time1 = time.time()
    print("Initializing..")

    # Grab program arguments
    args = parse_args()

    # Load data file
    try:
        global V_coord
        (V, E, V_coord, E_name) = readgraph.readgraph(args.graphname)
    except FileNotFoundError:
        print("Data file {} not found!".format(args.graphname), file=sys.stderr)
        exit()

    # Initialize serial port
    try:
        if args.serialport:
            print("Opening serial port: {}".format(args.serialport))
            serial_out = serial_in = serial.Serial(args.serialport, 9600)
        else:
            print("No serial port. Supply one with the -s port option.")
            exit()
    except serial.SerialException:
        print("Could not open serial port: {}".format(args.serialport),
              file=sys.stderr)
        exit()

    # Set debug mode
    if args.verbose:
        debug = True
    else:
        debug = False

    # Generate a graph from the map and grab the needed values
    G = digraph.Digraph(E)
    # Add all orphan vertices. Not really useful, and in fact detrimental
    # in all cases, but do it for the sake of completeness
    for v in V:
        G.add_vertex(v)

    # Print some debug output
    if debug:
        print("Graph loaded with {} vertices and {} edges.".format(G.num_vertices(), G.num_edges()))

    time2 = time.time()
    delta_t = repr(time2 - time1)
    print("Done initializing, took " + delta_t + " seconds")

    # Parse input
    while True:
        
        msg = receive(serial_in)
        debug and print("GOT:{}:".format(msg), file=sys.stderr)

        fields = msg.split(" ")

        # Ignore malformed messages
        if len(fields) != 4:
            debug and print("Ignoring message: {}".format(msg))
            continue
        time1 = time.time()
        print("Processing..")
        # Get start and end vertices
        start_v = (int(fields[0])/10**5, int(fields[1])/10**5)
        end_v = (int(fields[2])/10**5, int(fields[3])/10**5)
        start = nearest_vertex( start_v )
        end = nearest_vertex( end_v )

        debug and print("Routing path from vertex {} to {}".format(start, end))

        path = least_cost_path(G, start, end, cost_distance)
        if path is None:
            send(serial_out, "0")
            debug and print("No path found!", file=sys.stderr)
        else:
            send(serial_out, str(len(path)))
            for v in path:
                send(serial_out, "{} {}".format(int(V_coord[v][0] * 10**5),
                                     int(V_coord[v][1] * 10**5)))
            print("Send path of length {}".format(len(path)))
        time2 = time.time()
        delta_t = repr(time2 - time1)
        print("Done processing, took " + delta_t + "seconds")