微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Google OR-Tools 中为每个节点添加时间维度

如何解决如何在 Google OR-Tools 中为每个节点添加时间维度

我正在使用 OR-Tools 解决站点多车辆车辆路径问题,到目前为止,我已经成功建立了一个模型,其中一辆车从每个站点离开,并且它们在它们之间的最大时间允许内访问每个节点然后通过虚拟节点 0 退出。 然而,由于这是一个现实世界的问题,在每个节点,车辆都需要停下设定的时间来执行一项服务,每个节点都有一个设定的等待时间,例如在下表中:

节点 持续时间
4 9
38 8
... ...
8545 17

我不确定如何将这些添加到每个节点,尽管到目前为止节点之间的弧长表示每个节点之间的行驶时间(假设所有车辆都以相同的速度行驶)。所以我的问题是如何将这些等待时间添加到模型中,同时仍然最大限度地减少每辆车的行驶时间并将它们保持在 25200 秒的最大阈值以下(对应于 7 小时轮班)。我在下面包含了我的工作代码

from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

距离矩阵是从一个像这样布局的 csv 中输入的:

空白 4 38 71 90 94 ... 8545
4 0 1280 1762 1406 1589 ... 1017
38 1280 0 681 202 385 ... 1433
71 1762 681 0 503 0 ... 0
90 1406 202 503 0 0 ... 1559
94 1589 385 0 0 0 ... 1742
.. .. ... ... ... ... 0 ...
8545 1017 1433 0 1559 1742 ... 0
Caredist_Matrix = np.loadtxt(open("Caredistances-FULL.csv","rb"),dtype=int,delimiter=",",skiprows=1)
Caredist_Matrix

模型主体如下:

def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = Caredist_Matrix
    data['num_vehicles'] = 6
    data['starts'] = [3,7,14,104,185,220]
    data['ends'] = [0,0]
    return data

def print_solution(data,manager,routing,solution):
    """Prints solution on console."""
    max_route_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
        while not routing.IsEnd(index):
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            prevIoUs_index = index
            index = solution.Value(routing.Nextvar(index))
            route_distance += routing.GetArcCostForVehicle(
                prevIoUs_index,index,vehicle_id)
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        plan_output += 'distance of the route: {}m\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distance,max_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))
    """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['starts'],data['ends'])

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


    # Create and register a transit callback.
    def distance_callback(from_index,to_index):
        """Returns the 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)

    # Define cost of each arc.
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

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

    # Setting first solution heuristic.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEApest_ARC)

    # Solve the problem.
    solution = routing.solveWithParameters(search_parameters)

    # Print solution on console.
    if solution:
        print_solution(data,solution)
if __name__ == '__main__':
    main()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?