Example #1
0
def separate_path(all_orders, path, condition, params):
    """
    将一个序列分离为满足条件的车辆路径
    :type all_orders: list
    :type path: list
    :type condition: dict
    :type params: dict
    """
    assert len(all_orders) - 1 == len(path)
    separate_number_list = []

    start = all_orders[0]  # 起点
    v = params['velocity']  # 速度
    stay_time = params['stay_time']  # 停留时间

    path_time = 0  # 记录每车的总时间
    path_len = len(path)  # 路径长度,不包括起点
    i = 0  # 路径中 node 的指针
    pre = start
    total_distance = 0
    while i < path_len:
        curr_node = all_orders[path[i]]
        distance = Distance.get_distance(pre['lat'], pre['lng'], curr_node['lat'], curr_node['lng'])
        time = distance / v
        if path_time + time > condition["time"]:
            # 重置迭代变量
            if pre == start:
                raise RuntimeError("经纬度{}离原点{}太远".format(curr_node, pre))
            path_time = 0
            pre = start
            separate_number_list.append(i)
        else:
            pre = curr_node
            path_time += time + stay_time
            total_distance += distance
            i += 1

    separate_number_list.append(i)
    return {"separate_number_list": separate_number_list, "distance": total_distance}