[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis content explains how to use transition attributes to encourage grouping nearby pickups and deliveries within a single vehicle's time block.\u003c/p\u003e\n"],["\u003cp\u003eBy setting a high cost for the initial segment (e.g., first 100 meters) of each transition and a lower cost for subsequent distance, you can incentivize the optimizer to group nearby visits together.\u003c/p\u003e\n"],["\u003cp\u003eA threshold distance (e.g., 100 meters) is defined to determine what constitutes "nearby" for grouping, and the cost structure is implemented using \u003ccode\u003eTransitionAttributes\u003c/code\u003e in the \u003ccode\u003eShipmentModel\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eSetting a low cost for short transitions and high cost for long transitions can have the opposite of the desired effect, making the optimizer prefer spread-out transitions instead of grouped ones.\u003c/p\u003e\n"],["\u003cp\u003eUsing a high cost per kilometer below the defined threshold and a low cost per kilometer above the defined threshold ensures the optimizer will prefer solutions where nearby visits are performed together, minimizing cost.\u003c/p\u003e\n"]]],["To prioritize routes with grouped deliveries, use transition attributes with distance limits. Set a threshold (e.g., 100 meters) for \"nearby\" visits and assign a high cost to the first 100 meters of each transition, with a lower cost for additional distance. This encourages the optimizer to group nearby deliveries. For instance, set `cost_per_kilometer_below_soft_max` to 50 and `cost_per_kilometer_above_soft_max` to 1, within the defined threshold. Using a low cost below the threshold will have an inverse effect.\n"],null,["# Prioritize nearby visits with transition attributes\n\nThis example shows how to use transition attributes to prioritize routes where\nnearby pickups and deliveries are performed by the same vehicle in one time\nblock. To learn more about transition attributes, see [Model Business Logic with\nTransition\nAttributes](/maps/documentation/route-optimization/transition-attributes).\n\nIn this example:\n\n- Deliveries of shipments A, B, and C are close to each other on the same road.\n- Additional deliveries are further down the road.\n- Deliveries have no specified delivery times.\n- Regardless of the visit schedule, the vehicle needs to drive this road twice: once in the morning on the way from the depot, and once in the evening on the way back.\n- The overall travel distance and duration of the route is always the same, regardless of when A, B, and C are performed.\n\nIn this situation, and for a request that uses only cost per hour and cost per\nkilometer, the optimized route could have A and B handled in the morning and C\nhandled in the evening and the cost of the solution would be the same as if all\nthree are handled at the same time.\n| **Key Point:** To get a route where all three deliveries are performed together, you add constraints that increase the cost of routes where the shipments are not delivered together.\n\nCost per kilometers with a threshold\n------------------------------------\n\nTo group nearby visits, you first need to select a threshold distance. This is\nthe maximal distance between two visits that you consider to be nearby. This\nexample uses a threshold of 100 meters that roughly corresponds to one block in\nan urban area. You can increase or decrease the threshold to match your business\nneeds and the preferences of your drivers.\n\nTo group nearby visits within 100 meters of each other, you set a high cost on\nthe first 100 meters of each transition and a lower cost for any additional\nmeters of the transition. Since the first 100 meters are the most expensive, the\noptimizer makes the biggest savings by using transitions that are shorter than\nthe 100-meter threshold, even if it means extending the overall length of the\nroute.\n\nTo set up the costs, you add a new entry to\n[`ShipmentModel.transition_attributes`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.ShipmentModel.FIELDS.repeated.google.maps.routeoptimization.v1.TransitionAttributes.google.maps.routeoptimization.v1.ShipmentModel.transition_attributes)\nwith the following properties:\n\n- To match all possible transitions, pick a tag that is not used anywhere in the model, for example \u003cvar translate=\"no\"\u003eUNUSED_TAG\u003c/var\u003e. Set [`TransitionAttributes.excluded_src_tag`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.TransitionAttributes.FIELDS.string.google.maps.routeoptimization.v1.TransitionAttributes.excluded_src_tag) and [`TransitionAttributes.excluded_dst_tag`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.TransitionAttributes.FIELDS.string.google.maps.routeoptimization.v1.TransitionAttributes.excluded_dst_tag) to this tag.\n- Set up [`TransitionAttributes.distance_limit`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.TransitionAttributes.FIELDS.google.maps.routeoptimization.v1.DistanceLimit.google.maps.routeoptimization.v1.TransitionAttributes.distance_limit) with the threshold distance and costs:\n - Set [`DistanceLimit.soft_max_meters`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.DistanceLimit.FIELDS.int64.google.maps.routeoptimization.v1.DistanceLimit.soft_max_meters) to the selected threshold.\n - Set [`DistanceLimit.cost_per_kilometer_below_soft_max`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.DistanceLimit.FIELDS.double.google.maps.routeoptimization.v1.DistanceLimit.cost_per_kilometer_below_soft_max) to the cost per kilometer below threshold.\n - Set [`DistanceLimit.cost_per_kilometer_above_soft_max`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.DistanceLimit.FIELDS.double.google.maps.routeoptimization.v1.DistanceLimit.cost_per_kilometer_above_soft_max) to the cost per kilometer above threshold.\n\n {\n \"model\": {\n \"transitionAttributes\": [\n {\n \"excluded_dst_tag\": \"\u003cvar translate=\"no\"\u003eUNUSED_TAG\u003c/var\u003e\",\n \"excluded_src_tag\": \"\u003cvar translate=\"no\"\u003eUNUSED_TAG\u003c/var\u003e\",\n \"distanceLimit\": {\n \"softMaxMeters\": 100,\n \"costPerKilometerBelowSoftMax\": 50,\n \"costPerKilometerAboveSoftMax\": 1,\n }\n }\n ]\n }\n }\n\nThe tag `#unused_tag#` must not be used by any shipments or vehicles to match\nall possible transitions. For more information, see [How to match all visit\nrequests](/maps/documentation/route-optimization/transition-attributes#how_to_match_all_visit_requests).\n\nHow a high cost below threshold works\n-------------------------------------\n\nThis section shows how the cost below and above threshold affect the overall\ncost of different solutions of the example scenario.\n\n### Solution 1: Perform A, B on the way there, C on the way back\n\nIn this solution, the shipments are split into the two traversals of this road.\nTwo of them are delivered on the first traversal, and the remaining one is\ndelivered on the second. There are 5 transitions:\n\n| Transition | Distance | Below threshold || Above threshold ||\n| || Distance | Cost | Distance | Cost |\n|------------|----------|----------|------|----------|------|\n| depot →A | 1000 m | 100 m | 5 | 900 m | 0.9 |\n| A→B | 50 m | 50 m | 2.5 | 0 m | 0 |\n| B→other | 1030 m | 100 m | 5 | 930 m | 0.93 |\n| other→C | 1000 m | 100 m | 5 | 900 m | 0.9 |\n| C→depot | 1080 m | 100 m | 5 | 980 m | 0.98 |\n| Total || 450 m | 22.5 | 3710 m | 3.71 |\n\n\u003cbr /\u003e\n\nThe overall cost is computed the sum of the two costs per kilometer:\n\n- the cost per kilometer below threshold (50) times the total traveled distance below threshold (450 m = 0.45 km),\n- the cost per kilometer above threshold (1) times the total traveled distance above threshold (3710 m = 3.71 km).\n\nThe overall cost is thus 0.45 \\* 50 + 3.71 \\* 1 = 22.5 + 3.71 = 26.21.\n\n### Solution 2: Perform A, B, C on the way there, nothing on the way back\n\nIn this solution, unlike solution 1, all three shipments are delivered \"as a\ngroup\" during one traversal of the road. On the other traversal, the vehicle\ndoes not stop at all. Again, there are 5 transitions, but their lengths and\ncompositions are different:\n\n| Transition | Distance | Below threshold || Above threshold ||\n| || Distance | Cost | Distance | Cost |\n|-------------|----------|----------|------|----------|------|\n| depot →A | 1000 m | 100 m | 5 | 900 m | 0.9 |\n| A→B | 50 m | 50 m | 2.5 | 0 m | 0 |\n| B→C | 30 m | 30 m | 1.5 | 0 m | 0 |\n| C→other | 1000 m | 100 m | 5 | 900 m | 0.9 |\n| other→depot | 2080 m | 100 m | 5 | 1980 m | 1.98 |\n| Total | | 380 m | 19 | 3780 m | 3.78 |\n\nUsing the same computation as in solution 1, the overall cost is 0.38 \\* 50 +\n3.78 \\* 1 = 19 + 3.78 = 22.78, and performing all visits in one time block has a\nlower cost than performing them in two groups. You can reinforce this effect by\nincreasing\n[`DistanceLimit.cost_per_kilometer_below_soft_max`](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.DistanceLimit.FIELDS.double.google.maps.routeoptimization.v1.DistanceLimit.cost_per_kilometer_below_soft_max).\n\n### Why a low cost per kilometer below threshold doesn't work\n\nSince you want to prefer short transitions over long transitions, you may be\ntempted to give a high cost per kilometer to long transitions, and keep the low\ncost per kilometer for short transitions. But this has in fact an inverse\neffect: since the first 100 meters of the transition are the cheapest, the\noptimizer uses these \"cheap\" meters to the greatest effect by preferring\ntransitions that have close to or over 100 meters.\n\nYou can see this effect on the two example solutions. If you swap the cost per\nkilometer below and above the threshold, the route costs change:\n\n| | High cost above threshold || High cost below threshold ||\n| | Solution 1 | Solution 2 | Solution 1 | Solution 2 |\n|-----------------------------|------------|------------|------------|------------|\n| KMs below threshold | 0.45 | 0.38 | 0.45 | 0.38 |\n| Cost per KM below threshold | 1.00 | 1.00 | 50.00 | 50.00 |\n| KMs above threshold | 3.71 | 3.78 | 3.71 | 3.78 |\n| Cost per KM above threshold | 50.00 | 50.00 | 1.00 | 1.00 |\n| Total cost | 185.95 | 189.38 | 26.21 | 22.78 |\n\nFor each version, the lower of the total costs of the two solutions is\nhighlighted in bold. You can see that when you use a high cost above threshold,\nthe total cost of the route is now higher for the route where the visits are\ngrouped, which is the opposite of what you wanted to achieve."]]