Method: projects.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/*}: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 (因應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
詳情請參閱身分與存取權管理說明文件。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Route Optimization API minimizes the total cost of routes by assigning \u003ccode\u003eShipmentRoute\u003c/code\u003es to \u003ccode\u003eVehicle\u003c/code\u003es.\u003c/p\u003e\n"],["\u003cp\u003eThe API takes an \u003ccode\u003eOptimizeToursRequest\u003c/code\u003e with a \u003ccode\u003eShipmentModel\u003c/code\u003e and returns an \u003ccode\u003eOptimizeToursResponse\u003c/code\u003e with \u003ccode\u003eShipmentRoute\u003c/code\u003es.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eShipmentModel\u003c/code\u003e defines the \u003ccode\u003eShipment\u003c/code\u003es to be transported, the \u003ccode\u003eVehicle\u003c/code\u003es available, and the cost components.\u003c/p\u003e\n"],["\u003cp\u003eThe API considers various factors, including traffic, time windows, and vehicle capacities, to optimize the routes.\u003c/p\u003e\n"],["\u003cp\u003eAuthorization is required using OAuth scope \u003ccode\u003ehttps://www.googleapis.com/auth/cloud-platform\u003c/code\u003e and IAM permission \u003ccode\u003erouteoptimization.locations.use\u003c/code\u003e.\u003c/p\u003e\n"]]],["This content details the `OptimizeTours` API, which optimizes routes for shipments using vehicles. The core action involves sending a `POST` request to `https://routeoptimization.googleapis.com/v1/{parent=projects/*}:optimizeTours`, specifying a `ShipmentModel` within the request body. This model defines shipments, vehicles, and cost parameters. The API returns an `OptimizeToursResponse` that details `ShipmentRoute`s, assigning visits to each vehicle to minimize costs. The request body allows for parameters like `timeout`, `solvingMode`, and `injectedSolutionConstraint` to further fine-tune the route optimization process.\n"],null,[]]