示例#1
0
def test_altgraph():

    # these are the edges
    edges = [ (1,2), (2,4), (1,3), (2,4), (3,4), (4,5), (6,5), (6,14), (14,15), (6, 15),
    (5,7), (7, 8), (7,13), (12,8), (8,13), (11,12), (11,9), (13,11), (9,13), (13,10) ]

    store = {}
    g = Graph.Graph()
    for head, tail in edges:
        store[head] = store[tail] = None
        g.add_edge(head, tail)

    # check the parameters
    assert g.number_of_nodes() == len(store)
    assert g.number_of_edges() == len(edges)


    # do a forward bfs
    assert g.forw_bfs(1) == [1, 2, 3, 4, 5, 7, 8, 13, 11, 10, 12, 9]


    # diplay the hops and hop numbers between nodes
    assert g.get_hops(1, 8) == [(1, 0), (2, 1), (3, 1), (4, 2), (5, 3), (7, 4), (8, 5)]

    assert GraphAlgo.shortest_path(g, 1, 12) == [1, 2, 4, 5, 7, 13, 11, 12]
示例#2
0
 def test_shortest_path(self):
     self.assertEqual(GraphAlgo.shortest_path(self.g, 1, 12),
                      [1, 2, 4, 5, 7, 13, 11, 12])
 def test_shortest_path(self):
     self.assertEqual(GraphAlgo.shortest_path(self.g, 1, 12), 
             [1, 2, 4, 5, 7, 13, 11, 12])
示例#4
0
def Dijkstra(G, start, end):
    return ga.shortest_path(G, start, end)
示例#5
0
def main():
    if len(sys.argv) != 5:
        logging.info('please input args: car_path, road_path, cross_path, answerPath')
        exit(1)

    car_path = sys.argv[1]
    road_path = sys.argv[2]
    cross_path = sys.argv[3]
    answer_path = sys.argv[4]
    # car_path = '../config/car.txt'
    # road_path = '../config/road.txt'
    # cross_path = '../config/cross.txt'
    # answer_path = '../config/answer.txt'

    logging.info("car_path is %s" % (car_path))
    logging.info("road_path is %s" % (road_path))
    logging.info("cross_path is %s" % (cross_path))
    logging.info("answer_path is %s" % (answer_path))

    opts = {}
    opts['car_txt_path'] = car_path
    opts['cross_txt_path'] = cross_path
    opts['road_txt_path'] = road_path

    car_df = read_txt(car_path)
    # cross_df = read_txt(cross_path)
    road_df = read_txt(road_path)
    # 用于记录当前道路车辆数目
    road_df['car_num'] = 0
    # pdb.set_trace()
    car_df = car_df.sort_values(by=['speed', 'planTime'], ascending=False).sort_values(by=['planTime'])
    car_df = car_df.reset_index(drop=True)
    
    # 用于记录当前车辆所在道路及位置
    car_df['current_road'] = None
    car_df['current_loc'] = None
    


    total_path = []
    # 已发车车辆行车路线
    total_path_dict = {}
    count = 0  # 总发车数量
    num = 10  # 每次发车数量

    # for i in range(len(car_df.index)):
    # while not total_path_dict: # 车没有全部到站
    # pdb.set_trace()
    while len(car_df.index)-1 >= count:        # 没有排到最后一辆车
        # del total_path_dict['init']
        
        for i in range(count,min(count+num,len(car_df.index))):
            car_id = car_df.loc[i, 'id']
            
            car_df.loc[i, 'planTime'] = i // num + 1
    
            start = car_df.loc[i, 'from']
            stop = car_df.loc[i, 'to']
            
            graph = build_graph(road_df)
            dot_path = GraphAlgo.shortest_path(graph, start, stop)
            road_path = []
            
            for j in range(len(dot_path) - 1):
                road_id = road_df[((road_df.loc[:, 'from'] == dot_path[j]) & (road_df.to == dot_path[j + 1])) | (
                            (road_df.loc[:, 'from'] == dot_path[j + 1]) & (road_df.to == dot_path[j]))]['id'].values[0]
                road_path.append(road_id)
            # total_path_dict[str(car_id)] = road_path

            
            road_path.insert(0, i // num + 1)
            road_path.insert(0, car_id)
            total_path.append(road_path)
            # print(car_id)
            total_path_dict.update({str(car_id): road_path[2:]})  # 存入已经规划好行车路线的所有车的路径,若车到站,则删除
        update_car_num(road_df, car_df, total_path_dict)
        count += num
        

    # to write output file
    with open(answer_path, 'w') as f:
        for each_car in total_path:
            f.write('(')
            for each in each_car[:-1]:
                f.write(str(each))
                f.write(',')
            f.write(str(each_car[-1]))
            f.write(')')
            f.write('\n')
示例#6
0
def main():
    if len(sys.argv) != 5:
        logging.info(
            'please input args: car_path, road_path, cross_path, answerPath')
        exit(1)

    # pool = Pool(processes=4)
    # pool = mp.Pool(4)
    # jobs = []
    car_path = sys.argv[1]
    road_path = sys.argv[2]
    cross_path = sys.argv[3]
    answer_path = sys.argv[4]
    # car_path = '../config/car.txt'
    # road_path = '../config/road.txt'
    # cross_path = '../config/cross.txt'
    # answer_path = '../config/answer.txt'

    logging.info("car_path is %s" % (car_path))
    logging.info("road_path is %s" % (road_path))
    logging.info("cross_path is %s" % (cross_path))
    logging.info("answer_path is %s" % (answer_path))

    opts = {}
    opts['car_txt_path'] = car_path
    opts['cross_txt_path'] = cross_path
    opts['road_txt_path'] = road_path

    car_df = read_txt(car_path)
    # cross_df = read_txt(cross_path)
    road_df = read_txt(road_path)

    # pdb.set_trace()
    # car_df = car_df.sort_values(by=['speed', 'planTime'], ascending=False).sort_values(by=['planTime'])
    car_df = car_df.sort_values(by=['planTime']).reset_index(drop=True)
    num = 10

    # edges = [(head , tail , weight) , ... ,]
    edges = []
    for i in range(len(road_df.index)):
        head = road_df.loc[i, 'from']
        tail = road_df.loc[i, 'to']
        # graph weight
        time_as_weight = road_df.loc[i, 'length'] / (road_df.loc[i, 'speed'] *
                                                     road_df.loc[i, 'channel'])
        edges.append((head, tail, time_as_weight))

        if road_df.loc[i, 'isDuplex'] == 1:
            edges.append((tail, head, time_as_weight))
    graph = Graph.Graph()
    for head, tail, weight in edges:
        graph.add_edge(head, tail, weight)

    total_path = []
    for i in range(len(car_df.index)):
        car_id = car_df.loc[i, 'id']
        car_df.loc[i, 'planTime'] = i // num + 1

        start = car_df.loc[i, 'from']
        stop = car_df.loc[i, 'to']
        dot_path = GraphAlgo.shortest_path(graph, start, stop)
        road_path = []
        for j in range(len(dot_path) - 1):
            road_id = road_df[((road_df.loc[:, 'from'] == dot_path[j]) &
                               (road_df.to == dot_path[j + 1])) |
                              ((road_df.loc[:, 'from'] == dot_path[j + 1]) &
                               (road_df.to == dot_path[j]))]['id'].values[0]
            road_path.append(road_id)
        road_path.insert(0, i // num + 1)
        road_path.insert(0, car_id)
        total_path.append(road_path)

    # to write output file
    with open(answer_path, 'w') as f:
        for each_car in total_path:
            f.write('(')
            for each in each_car[:-1]:
                f.write(str(each) + ',')
            f.write(str(each_car[-1]) + ')' + '\n')