Dimensions
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
路线求解器使用一个称为维度的对象来跟踪
车辆沿途累积的数量,例如行程时间或
如果车辆在取车和送货,其总重量是多少
,了解所有最新动态。如果路由问题涉及这样的数量,无论是在约束条件中,还是在约束条件中,
您需要定义一个维度来指定它们。
本部分介绍了如何定义和使用维度。
维度示例
以下是前面部分中的几个维度示例。
以下示例使用
AddDimension
方法。
Python
routing.AddDimension(
callback_index,
slack_max,
capacity,
fix_start_cumul_to_zero,
dimension_name)
C++
routing.AddDimension(
callback_index,
slack_max,
capacity,
fix_start_cumul_to_zero,
dimension_name)
Java
routing.addDimension(
callbackIndex,
slackMax,
capacity,
fixStartCumulToZero,
dimensionName)
C#
routing.AddDimension(
callbackIndex,
slackMax,
capacity,
fixStartCumulToZero,
dimensionName)
AddDimension
方法具有以下输入:
callback_index
:返回数量的回调的索引。通过
索引,即求解器对回调的内部引用,由
方法,例如 RegisterTransitCallback
或 RegisterUnitaryTransitCallback
。
slack_max
:slack(用于表示等待的变量)的最大值
。请参阅下文的 slack 变量部分,了解
。
如果问题不涉及等待时间,您通常可以设置 slack_max
设置为 0。
capacity
:每条路线累计的总数量上限。
使用 capacity
创建约束条件,如
CVRP:如果您的问题
您可以将 capacity
设置为足够大的值,
不对路由施加任何限制,例如,所有请求的总和
矩阵或数组的条目,用于定义回调。
fix_start_cumulative_to_zero
:布尔值。如果为 true,则累积值
从 0 开始。在大多数情况下,应将其设置为 True
。
但是,对于 VRPTW 或
资源限制,部分车辆
由于时间窗口限制,可能必须在时间 0 之后开始,因此您应该
将 fix_start_cumulative_to_zero
设置为 False
。
dimension_name
:维度名称的字符串,例如 'Distance'
。
可用于访问程序中其他位置的变量。
CVRP 计划使用
AddDimensionWithVehicleCapacity
方法。此方法采用一系列容量,每辆车对应一个条目。
(相比之下,AddDimension
接受 capacity
的单个值,因此
并假设车辆具有相同的容量)。
请参阅 RoutingModel
参考页面了解创建维度的其他方法。
此部分
将解决方案时间范围保存到列表或数组
展示了将维度累计数据保存到列表或数组的函数。
Slack 变量
下面的示例展示了涉及
路途时间。假设
某辆车在其路线的一个路段中从位置 i 到位置 j,并且:
- 车辆在 i 的累计行程时间为 100 分钟。
- 车辆在 j 时的累计行程时间为 200 分钟。
- 从 i 到 j 的行程时间为 75 分钟。
车辆无法在到达后立即离开位置 i 或其累计值
地点 j 的时间为 175。车辆必须在
出发前的地点 i;也就是说,位置 i 的松厚度为 25。
您需要允许 VRPTW 使用 Slack,因为车辆可能需要等待
访问某个地理位置(由于时间范围限制)。在这类问题中,
slack_max
到您希望允许车辆等待的最长时间
然后才能前往下一个位置。这无关紧要
只需将 slack_max
设置为一个非常大的数字即可。
另一方面,对于 CVRP,累积负载从 i
变为
j
始终等于 i
的需求,因此没有空余。对于诸如
可以将 slack_max
设置为 0。
接下来,我们将给出 Slack 的正式定义。在内部,维度会存储
与路线沿途累积的数量相关的两种变量:
- 公交变量:
路径。
如果
i -> j
是路线中的一个路段,则公交变量为 i
、
公交矩阵的 j
条目(适用于公交回调),或者干脆是
位置 i 处的回调值(如果回调仅依赖于一个位置)。
- 累积变量:每个营业地点的累计数量。您
可以通过以下方式访问位置 i 的累积变量:
dimension_name.CumulVar(i)
。有关示例,请参见
时间范围限制
。
假设车辆从位置 i
前往位置 j
一步,
Slack 与这些变量的关系如下:
slack(i) = cumul(j) - cumul(i) - transit(i, j)
有关维度的详细信息,请参阅
RoutingDimension
部分。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-29。
[null,null,["最后更新时间 (UTC):2024-08-29。"],[[["\u003cp\u003eRouting solver uses \u003cem\u003edimensions\u003c/em\u003e to track quantities like travel time or load along a vehicle's route, essential for constraints and objective functions.\u003c/p\u003e\n"],["\u003cp\u003eDimensions are defined using the \u003ccode\u003eAddDimension\u003c/code\u003e method, specifying parameters like callback index, slack, capacity, and whether the cumulative value starts at zero.\u003c/p\u003e\n"],["\u003cp\u003eSlack variables represent waiting times at locations, crucial for problems with time window constraints like VRPTW.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eAddDimensionWithVehicleCapacity\u003c/code\u003e allows defining dimensions with varying capacities for different vehicles, unlike \u003ccode\u003eAddDimension\u003c/code\u003e which assumes uniform capacity.\u003c/p\u003e\n"],["\u003cp\u003eTwo key variable types within dimensions: \u003cem\u003etransit variables\u003c/em\u003e representing quantity changes at each step and \u003cem\u003ecumulative variables\u003c/em\u003e representing the total accumulated quantity at each location.\u003c/p\u003e\n"]]],["The routing solver utilizes *dimensions* to track accumulating quantities along vehicle routes, such as travel time or carried weight. To define a dimension, the `AddDimension` method is used, requiring inputs like `callback_index`, `slack_max`, `capacity`, `fix_start_cumul_to_zero`, and `dimension_name`. `AddDimensionWithVehicleCapacity` handles varying capacities per vehicle. Slack variables represent waiting times at locations and are computed using transit and cumulative variables. Dimensions are essential for enforcing constraints and defining the objective function in routing problems.\n"],null,["# Dimensions\n\nThe routing solver uses an object called a *dimension* to keep track of\nquantities that accumulate along a vehicle's route, such as the travel time or,\nif the vehicle is making pickups and deliveries, the total weight it is carrying\n. If a routing problem involves such a quantity, either in the constraints or\nthe objective function, you need to define a dimension to specify them.\n\nThis section explains how to define and use dimensions.\n\nExamples of dimensions\n----------------------\n\nHere are a couple of examples of dimensions from previous sections.\n\n- The [VRPTW example](/optimization/routing/cvrptw) creates a dimension to track\n each vehicle's cumulative travel time. The solver uses the dimension to enforce\n the constraint that a vehicle can only visit a location within the location's\n time window.\n\n- The [CVRP example](/optimization/routing/cvrp) creates a dimension for the\n *demands* (e.g., weights or volumes of packages to be picked up), which tracks\n the cumulative load the vehicle is carrying along its route.\n The solver uses the dimension to enforce the constraint that a vehicle's load\n can't exceed its capacity.\n\nThe examples below define a dimension for the VRPTW using the\n[`AddDimension`](/optimization/reference/constraint_solver/routing/RoutingModel#AddDimension)\nmethod. \n\n### Python\n\n```python\nrouting.AddDimension(\n callback_index,\n slack_max,\n capacity,\n fix_start_cumul_to_zero,\n dimension_name)\n```\n\n### C++\n\n```c++\nrouting.AddDimension(\n callback_index,\n slack_max,\n capacity,\n fix_start_cumul_to_zero,\n dimension_name)\n```\n\n### Java\n\n```java\nrouting.addDimension(\n callbackIndex,\n slackMax,\n capacity,\n fixStartCumulToZero,\n dimensionName)\n```\n\n### C#\n\n```c#\nrouting.AddDimension(\n callbackIndex,\n slackMax,\n capacity,\n fixStartCumulToZero,\n dimensionName)\n```\n\nThe `AddDimension` method has the following inputs:\n\n- `callback_index`: The index for the callback that returns the quantity. The index, which is the solver's internal reference to the callback, is created by methods such as `RegisterTransitCallback` or `RegisterUnitaryTransitCallback`.\n- `slack_max`: Maximum for the *slack* , a variable used to represent waiting times at the locations. See [slack variables](#slack_variables) below for details. If the problem doesn't involve waiting time, you can usually set `slack_max` to 0.\n- `capacity`: Maximum for the total quantity accumulated along each route. Use `capacity` to create constraints like those in the [CVRP](/optimization/routing/cvrp). If your problem doesn't have such a constraint, you can set `capacity` to a value that is sufficiently large to impose no restrictions on the routes ---for example, the sum of all entries of the matrix or array used to define the callback.\n- `fix_start_cumulative_to_zero`: Boolean value. If true, the cumulative value of the quantity starts at 0. In most cases, this should be set to `True`. However, for the [VRPTW](/optimization/routing/cvrptw) or problems with [resource constraints](/optimization/routing/cvrptw_resources), some vehicles may have to start after time 0 due to time window constraints, so you should set `fix_start_cumulative_to_zero` to `False` for these problems.\n- `dimension_name`: String for the name for the dimension, such as `'Distance'`, which you can use to access the variables elsewhere in the program.\n\nThe CVRP program creates a slightly different type of dimension using the\n[`AddDimensionWithVehicleCapacity`](/optimization/reference/constraint_solver/routing/RoutingModel#AddDimensionWithVehicleCapacity)\nmethod. This method takes an array of capacities, with one entry for each vehicle.\n(In contrast, `AddDimension` takes a single value for `capacity`, so all\nvehicles are assumed to have the same capacity.)\n\nSee the [`RoutingModel`](/optimization/reference/constraint_solver/routing/RoutingModel)\nreference page for other methods that create dimensions.\n\nThe section\n[Save the solution time windows to a list or array](/optimization/routing/vrptw#get-cumulative-data)\npresents functions that save the cumulative data in a dimension in a list or array.\n\nSlack variables\n---------------\n\nHere's an example that illustrates slack variables for a problem involving\ntravel time. Suppose that\na vehicle goes from location i to location j in one step of its route, and that:\n\n- The vehicle's cumulative travel time at i is 100 minutes.\n- The vehicle's cumulative travel time at j is 200 minutes.\n- The travel time from i to j is 75 minutes.\n\nThe vehicle can't leave location i immediately upon arrival, or its cumulative\ntime at location j would be 175. Instead, vehicle must wait for 25 minutes at\nlocation i before departing; in other words, the slack at location i is 25.\n\nYou need to allow slack in a VRPTW because vehicles may have to wait before\nvisiting a location, due to time window constraints. In a problem like this, set\n`slack_max` to the maximum amount of time you want to allow vehicles to wait at\na location before proceeding to the next location. If it doesn't matter how long\nthey wait, just set `slack_max` to a very large number.\n\nFor the CVRP, on the other hand, the change in the accumulated load from `i` to\n`j` always equals the demand at `i`, so there is no slack. For problems like\nthis, you can set `slack_max` to 0.\n\nNext, we'll give the formal definition of slack. Internally, a dimension stores\ntwo types of variables related to quantities that accumulate along routes:\n\n- *Transit variables* : The increase or decrease of the quantity at each step of a route. If `i -\u003e j` is one step in a route, the transit variable is either the `i`, `j` entry of the transit matrix (for a transit callback), or simply the callback value at location i (if the callback depends on just one location).\n- *Cumulative variables* : The total accumulated quantity at each location. You can access the cumulative variable at location i by `dimension_name.CumulVar(i)`. For an example, see the [time window constraints](/optimization/routing/vrptw#time_window_constraints) in the VRPTW example.\n\nAssuming that a vehicle goes from location `i` to location `j` in one step, the\nslack is related to these variables as follows: \n\n```\nslack(i) = cumul(j) - cumul(i) - transit(i, j)\n```\n\n\u003cbr /\u003e\n\nFor more details about dimensions, see\n[`RoutingDimension`](/optimization/reference/constraint_solver/routing/RoutingDimension)\nin the reference section."]]