Method: projects.locations.optimizeTours
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
傳送包含 ShipmentModel
的 OptimizeToursRequest
,並傳回內含 ShipmentRoute
的 OptimizeToursResponse
,也就是車輛將總成本降至最低的一組路線。
ShipmentModel
模型主要包含需要執行的 Shipment
,以及用於傳輸 Shipment
的 Vehicle
。ShipmentRoute
會將 Shipment
指派給 Vehicle
。具體來說,系統會為每輛車輛指派一系列 Visit
,其中 Visit
對應至 VisitRequest
,也就是 Shipment
的接送或送達地點。
目標是將 ShipmentRoute
指派給 Vehicle
,這樣就能盡可能降低 ShipmentModel
中定義多個元件的總費用。
HTTP 要求
POST https://routeoptimization.googleapis.com/v1/{parent=projects/*/locations/*}:optimizeTours
這個網址使用 gRPC 轉碼語法。
路徑參數
參數 |
parent |
string
必要欄位。指定專案或位置即可撥打電話。 格式:* projects/{project-id} * projects/{project-id}/locations/{location-id} 如未指定位置,系統會自動選擇區域。
|
要求主體
要求主體的資料會採用以下結構:
JSON 表示法 |
{
"timeout": string,
"model": {
object (ShipmentModel )
},
"solvingMode": enum (SolvingMode ),
"searchMode": enum (SearchMode ),
"injectedFirstSolutionRoutes": [
{
object (ShipmentRoute )
}
],
"injectedSolutionConstraint": {
object (InjectedSolutionConstraint )
},
"refreshDetailsRoutes": [
{
object (ShipmentRoute )
}
],
"interpretInjectedSolutionsUsingLabels": boolean,
"considerRoadTraffic": boolean,
"populatePolylines": boolean,
"populateTransitionPolylines": boolean,
"allowLargeDeadlineDespiteInterruptionRisk": boolean,
"useGeodesicDistances": boolean,
"label": string,
"geodesicMetersPerSecond": number,
"maxValidationErrors": integer
} |
欄位 |
timeout |
string (Duration format)
如果設定了逾時,伺服器會在逾時期限或同步要求的伺服器期限已過 (以較早者為準) 之前傳回回應。 對於非同步要求,伺服器會在逾時前 (如果可行) 產生解決方案。 持續時間以秒為單位,最多 9 個小數位數,結尾為「s 」。範例:"3.5s" 。
|
model |
object (ShipmentModel )
要解決的運送模型。
|
solvingMode |
enum (SolvingMode )
根據預設,解決模式為 DEFAULT_SOLVE (0)。
|
searchMode |
enum (SearchMode )
用於解決要求的搜尋模式。
|
injectedFirstSolutionRoutes[] |
object (ShipmentRoute )
引導最佳化演算法尋找與先前解決方案相似的第一個解決方案。 建立第一個解決方案時,模型會受到限制。在第一個解決方案中,凡是未在路線上執行的運送作業都會隱含略過,但可能會在後續解決方案中執行。 解決方案必須滿足一些基本有效性假設:
- 對於所有路線,
vehicleIndex 必須在範圍內,且不得重複。
- 對於所有造訪,
shipmentIndex 和 visitRequestIndex 都必須在範圍內。
- 一項運送資訊只能納入一條路線。
- 取貨服務配送完成後,請務必安排取貨服務配送訂單。
- 每筆訂單只能使用一種取貨或送貨替代方案。
- 而且時間都會增加 (例如
vehicleStartTime
<= visits[0].start_time <= visits[1].start_time ...
<= vehicleEndTime )。
- 運送作業只能在允許的車輛上執行。如果
Shipment.allowed_vehicle_indices 為空,或 Shipment.allowed_vehicle_indices 包含車輛的 vehicleIndex ,則允許刊登車輛。
如果插入的解決方案不可行,系統不一定會傳回驗證錯誤,而是可能傳回表示不可行性的錯誤。
|
injectedSolutionConstraint |
object (InjectedSolutionConstraint )
限制最佳化演算法,以找出類似於先前解決方案的最終解決方案。舉例來說,您可以使用此方法,將已完成或將要完成但不得修改的路線部分凍結。 如果插入的解決方案不可行,則不一定會傳回驗證錯誤,系統可能會改為傳回不可行的錯誤。
|
refreshDetailsRoutes[] |
object (ShipmentRoute )
如果不為空白,系統會重新整理指定路線,但不會修改其基礎的造訪序列或行程時間:只會更新其他詳細資料。這麼做無法解決模型問題, 自 2020 年 11 月起,這只會填入非空白路線的折線,且 populatePolylines 為 true。 傳入路徑的 routePolyline 欄位可能與路徑 transitions 不一致。 這個欄位不得與 injectedFirstSolutionRoutes 或 injectedSolutionConstraint 搭配使用。 Shipment.ignore 和 Vehicle.ignore 不會對行為造成任何影響。無論是否忽略相關的貨運或車輛,所有非空白路線的造訪點之間,仍會填入折線。
|
interpretInjectedSolutionsUsingLabels |
boolean
如果是 true:
這項解讀方式適用於 injectedFirstSolutionRoutes 、injectedSolutionConstraint 和 refreshDetailsRoutes 欄位。如果要求中的運送或車輛索引在解決方案建立後有所變更,可以使用這項屬性,可能是因為運輸或車輛從要求中移除或新增至要求中。 如果為 true,則下列類別中的標籤在該類別中最多只能出現一次:
如果插入的解決方案中的 vehicleLabel 不對應至要求的車輛,系統會從解決方案中移除對應的路線,並一併移除其造訪次數。如果插入解決方案中的 shipmentLabel 不符合要求運送要求,系統會將相應的造訪動作從解決方案中移除。如果插入解決方案中的 SkippedShipment.label 不符合要求運送要求,系統會將 SkippedShipment 從解決方案中移除。 從插入解決方案中移除路徑造訪或整條路線,可能會影響隱含的限制,進而影響解決方案、驗證錯誤或無法運作。 注意:呼叫端必須確保每個 Vehicle.label (resp. Shipment.label ) 可明確識別兩個相關要求中使用的車輛 (彈性運送) 實體:過去產生插入解決方案所用 OptimizeToursResponse 的要求,以及目前包含插入解決方案的要求。上述獨特性檢查不足以保證這項規定。
|
considerRoadTraffic |
boolean
在計算 ShipmentRoute 欄位 Transition.travel_duration 、Visit.start_time 和 vehicleEndTime 、設定 ShipmentRoute.has_traffic_infeasibilities 欄位,以及計算 OptimizeToursResponse.total_cost 欄位時,請考慮使用流量預估功能。
|
populatePolylines |
boolean
如果為 true,則會在回應 ShipmentRoute 中填入折線。
|
populateTransitionPolylines |
boolean
如果為 true,系統會在回應 ShipmentRoute.transitions 中填入多邊形和路線符記。
|
allowLargeDeadlineDespiteInterruptionRisk |
boolean
如果設定了這個項目,該要求的期限 (請參閱 https://grpc.io/blog/deadlines) 最多可以有 60 分鐘。否則,最長期限為 30 分鐘。請注意,長時間的要求具有極大 (但仍然很小) 的干擾風險。
|
useGeodesicDistances |
boolean
如果為 true,系統會使用大地測量距離 (而非 Google 地圖距離) 計算行程距離,並使用大地測量距離和 geodesicMetersPerSecond 定義的速度計算行程時間。
|
label |
string
可能會用來識別這項要求的標籤,並回報於 OptimizeToursResponse.request_label 中。
|
geodesicMetersPerSecond |
number
如果 useGeodesicDistances 為 true,就必須設定這個欄位,並定義計算交通時間時要套用的速度。這個值必須至少為 1.0 公尺/秒。
|
maxValidationErrors |
integer
截斷傳回的驗證錯誤數量。這些錯誤通常會附加至 INVALID_ARGUMENT 錯誤酬載,做為 BadRequest 錯誤詳細資料 (https://cloud.google.com/apis/design/errors#error_details),除非 solvingMode=VALIDATE_ONLY:請參閱 OptimizeToursResponse.validation_errors 欄位。預設值為 100,上限是 10,000。
|
回應主體
如果成功,回應主體會包含 OptimizeToursResponse
的執行例項。
授權範圍
需要下列 OAuth 範圍:
https://www.googleapis.com/auth/cloud-platform
身分與存取權管理權限
需要在 parent
資源上具備下列 IAM 權限:
routeoptimization.locations.use
詳情請參閱 IAM 說明文件。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eSends an \u003ccode\u003eOptimizeToursRequest\u003c/code\u003e and returns an \u003ccode\u003eOptimizeToursResponse\u003c/code\u003e containing optimized \u003ccode\u003eShipmentRoute\u003c/code\u003es for a given \u003ccode\u003eShipmentModel\u003c/code\u003e and \u003ccode\u003eVehicle\u003c/code\u003es.\u003c/p\u003e\n"],["\u003cp\u003eThe service aims to minimize the total cost of routes by assigning \u003ccode\u003eShipment\u003c/code\u003es to \u003ccode\u003eVehicle\u003c/code\u003es and optimizing their \u003ccode\u003eVisit\u003c/code\u003e sequences.\u003c/p\u003e\n"],["\u003cp\u003eRequest body includes parameters to guide the optimization process such as timeout, model, solving mode, search mode, and constraints.\u003c/p\u003e\n"],["\u003cp\u003eThe response contains the optimized routes, including details like polylines, travel durations, and total cost.\u003c/p\u003e\n"],["\u003cp\u003eRequires the \u003ccode\u003ehttps://www.googleapis.com/auth/cloud-platform\u003c/code\u003e OAuth scope and \u003ccode\u003erouteoptimization.locations.use\u003c/code\u003e IAM permission.\u003c/p\u003e\n"]]],["This content describes the `OptimizeTours` API, which optimizes routes for shipments using vehicles. A `POST` request is sent to `https://routeoptimization.googleapis.com/v1/{parent=projects/*/locations/*}:optimizeTours`. The request body includes a `ShipmentModel` specifying shipments and vehicles. The API then generates an `OptimizeToursResponse` with `ShipmentRoute`s, assigning `Visit`s (pickups/deliveries) to each vehicle to minimize total cost. Optional parameters allow traffic consideration, polyline population, and injected solution constraints. Authorization requires cloud platform scope and IAM permissions.\n"],null,[]]