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 ,則允許刊登車輛。
- 如果將
avoidUTurns 設為 true,則必須為相關造訪設定 injectedSolutionLocationToken
如果注入的解決方案不可行,系統不一定會傳回驗證錯誤,而是可能傳回表示不可行性的錯誤。
|
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
截斷傳回的驗證錯誤數量。這些錯誤通常會以 BadRequest 錯誤詳細資料的形式附加至 INVALID_src 錯誤酬載 (https://cloud.google.com/apis/design/errors#error_details),除非 ResolutionMode=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-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[[["\u003cp\u003eThis service optimizes routes for shipments, assigning them to vehicles to minimize overall costs, based on a \u003ccode\u003eShipmentModel\u003c/code\u003e that defines shipments, vehicles, and various cost components.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eOptimizeToursRequest\u003c/code\u003e includes parameters like \u003ccode\u003etimeout\u003c/code\u003e, \u003ccode\u003emodel\u003c/code\u003e, and \u003ccode\u003esolvingMode\u003c/code\u003e, which influence the optimization process, and the \u003ccode\u003emodel\u003c/code\u003e parameter specifies the \u003ccode\u003eShipmentModel\u003c/code\u003e to use for the routes.\u003c/p\u003e\n"],["\u003cp\u003eThe HTTP request uses a \u003ccode\u003ePOST\u003c/code\u003e method with a URL structure \u003ccode\u003ehttps://routeoptimization.googleapis.com/v1/{parent=projects/*/locations/*}:optimizeTours\u003c/code\u003e, accepting a JSON request body that contains the \u003ccode\u003eShipmentModel\u003c/code\u003e and other settings.\u003c/p\u003e\n"],["\u003cp\u003eThe service offers options to refine solutions with previous data, via \u003ccode\u003einjectedFirstSolutionRoutes\u003c/code\u003e or \u003ccode\u003einjectedSolutionConstraint\u003c/code\u003e, and can refresh existing routes without changing their sequence with \u003ccode\u003erefreshDetailsRoutes\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe request can specify details like considering road traffic, populating polylines, using geodesic distances, and setting time limits, and returns an \u003ccode\u003eOptimizeToursResponse\u003c/code\u003e upon success.\u003c/p\u003e\n"]]],[],null,[]]