当前位置: 首页 > 知识库问答 >
问题:

运行时错误:在azure DataRicks中,SWIG std::函数调用失败

丁宏盛
2023-03-14

在使用google或tools的路由解算器时,会引发运行时错误。在收到此错误之前和之后,代码段中没有任何更改。以前,它是有效的。但最近在修改了数据库连接后,我发现了这个错误。(不过,我怀疑dB连接修改会如何影响路由解算器)

我正在使用Azure Databricks笔记本。由于我对运筹学还不熟悉,所以我以https://developers.google.com/optimization/routing/pickup_delivery#complete_programs佩奇,作为我的参考。

这是带有提货和交货问题的车辆路线。

from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp

def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = dist
    data['pickups_deliveries'] = nodes_pickup_delivery
    data['num_vehicles'] = 2
    data['depot'] = 0
    return data

solution_list = []
def print_solution(data, manager, routing, assignment):
    """Prints assignment on console."""
    total_distance = 0 
    for vehicle_id in range(data['num_vehicles']):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        i = []
        while not routing.IsEnd(index):
            i.append(manager.IndexToNode(index))
            plan_output += ' {} -> '.format(str(cityList[manager.IndexToNode(index)]))
            previous_index = index
            index = assignment.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id)
        solution_list.append(i)
        plan_output += '{}\n'.format(str(cityList[manager.IndexToNode(index)]))
        plan_output += 'Distance of the route: {} miles\n'.format(route_distance)
        #print(plan_output)
        total_distance += route_distance 
    #print('Total Distance of all routes: {} miles'.format(total_distance))

def main():
    """Entry point of the program."""
    # Instantiate the data problem.
    data = create_data_model()

    # Create the routing index manager.
    manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), data['num_vehicles'], data['depot'])

    # Create Routing Model.
    routing = pywrapcp.RoutingModel(manager)

    # Define cost of each arc.
    def distance_callback(from_index, to_index):
        """Returns the manhattan distance between the two nodes."""
        # Convert from routing variable Index to distance matrix NodeIndex.
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return data['distance_matrix'][from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    # Add Distance constraint.
    dimension_name = 'Distance'
    routing.AddDimension(
        transit_callback_index,
        0,  # no slack
        40,  # vehicle maximum travel distance
        True,  # start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionOrDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100)

    # Define Transportation Requests.
    for request in data['pickups_deliveries']:
        pickup_index = manager.NodeToIndex(request[0])
        delivery_index = manager.NodeToIndex(request[1])
        routing.AddPickupAndDelivery(pickup_index, delivery_index)
        routing.solver().Add(routing.VehicleVar(pickup_index) == routing.VehicleVar(delivery_index))
        routing.solver().Add(distance_dimension.CumulVar(pickup_index) <= distance_dimension.CumulVar(delivery_index))

    # Setting first solution heuristic.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    #search_parameters.time_limit.seconds = 90
    search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PARALLEL_CHEAPEST_INSERTION)

    # Solve the problem.
    assignment = routing.SolveWithParameters(search_parameters)

    # Print solution on console.
    if assignment:
        print_solution(data, manager, routing, assignment)


if __name__ == '__main__':
    main()

我得到的错误指向以下代码段:“plan_output=”车辆{}的路线:\n”。格式(车辆id)'引发的错误是:

RuntimeError: SWIG std::function invocation failed.

RuntimeErrorTraceback (most recent call last)
<command-2714173895177597> in <module>()
     89 
     90 if __name__ == '__main__':
---> 91     main()

<command-2714173895177597> in main()
     85     # Print solution on console.
     86     if assignment:
---> 87         print_solution(data, manager, routing, assignment)
     88 
     89 

<command-2714173895177597> in print_solution(data, manager, routing, assignment)
     18     for vehicle_id in range(data['num_vehicles']):
     19         index = routing.Start(vehicle_id)
---> 20         plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
     21         route_distance = 0
     22         i = []

RuntimeError: SWIG std::function invocation failed.

请帮忙。

共有1个答案

夏侯林
2023-03-14
def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = dist
    data['pickups_deliveries'] = nodes_pickup_delivery
    data['num_vehicles'] = 2
    data['depot'] = 0 #Dummy location
    return data

solution_list = []
def print_solution(data, manager, routing, assignment):
    """Prints assignment on console."""
    total_distance = 0 
    for vehicle_id in range(data['num_vehicles']):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        i = []
        while not routing.IsEnd(index):
            i.append(manager.IndexToNode(index))
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            previous_index = index
            index = assignment.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id)  
        solution_list.append(i)
        plan_output += '{}({})\n'.format(str(cityList[manager.IndexToNode(index)]))
        plan_output += 'Distance of the route: {} miles\n'.format(route_distance)
        print(plan_output)
        total_distance += route_distance
    print('Total Distance of all routes: {} miles'.format(total_distance))
 类似资料:
  • 运行下面的函数时,出现以下错误: Invoke-Command:无法验证参数“ComputerName”上的参数。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令。位于C:\ Users \ username one \ Desktop \ script . PS1:16 char:29 Invoke-Command-computer name 你知道我做错了什么吗?

  • 我面临这个运行时错误。它说: 错误:任务“:app:dexDebug”的执行失败。 com.android.ide.common.process.ProcessExcture:org.gradle.process.internal.ExecExc0019:进程'命令'/usr/lib/jvm/java-8-oracle/bin/java"完成非零退出值2 这是我的身材。渐变脚本: 请帮帮我。谢谢!

  • 当我在代码中进行扫描后到达此部分时,我收到此错误: Project2_JoshuaLucas[,0,0,0x0,无效,布局=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=350,hei

  • 在部署到vercel后,我的一个页面上出现了这个错误,在开发模式下一切正常。 我认为问题可能是我的fetch/API之一,因为它使用第一个fetch请求中的数据作为第二个fetch请求的URL。。。 我的所有其他页面都有不同的API/获取请求,可以正常工作。。。

  • 在我的应用程序中,我以以下方式加载缓存。 加载用户缓存loadUserCache() 加载帐户缓存loadAccountCache() 加载客户缓存loadCustomerCache() 上述每个调用都涉及一个数据库调用。像怀斯一样,有6-7个电话。 当我的应用程序加载时,我必须等待缓存加载。 那些都是一个接一个的顺序调用。 如果我能找到一种方法并行地进行这些调用,那么应用程序加载期间的等待时间将

  • 我有一个VS 2017 C#开发的Azure功能应用程序,使用VS 2017发布机制部署在消费计划上。所有功能均由定时器或服务总线触发。我看到Azure门户中运行的状态,并且功能似乎正在工作,例如,将行写入SQL Azure数据库,但是,我在尝试检查或监视功能时,在Azure门户中经常遇到错误,例如,弹出一个红色错误框,显示错误: 我查看了日志,在日志文件中看不到任何明显的东西,我尝试部署到一个新