This example shows how to solve the VRP example by sending an asynchronous request to the Fleet Routing Service. If you encounter an IAM permission error, make sure to add proper IAM roles to your service account by following Before You Begin.
REST
The following example sends an optimization request to Cloud Fleet Routing API and creates a solution in Google Cloud Storage.
Before using any of the request data, make the following replacements:
- project-id: your GCP project ID.
- model-google-cloud-storage-uri: The Google Cloud Storage URI for the model to be solved, e.g. gs://foo-service/model.json.
- solution-google-cloud-storage-uri: The Google Cloud Storage URI for the solution generated, e.g. gs://foo-service/solution.json..
HTTP method and URL:
POST https://cloudoptimization.googleapis.com/v1/projects/project-id:batchOptimizeTours
Request JSON body:
{ "parent": "projects/project-id", "model_configs":[ { "input_config":{ "gcs_source":{ "uri":"model-google-cloud-storage-uri" }, "data_format":"JSON" }, "output_config":{ "gcs_destination":{ "uri":"solution-google-cloud-storage-uri" }, "data_format":"JSON" } } ] }
To send your request, choose one of these options:
curl
Save the request body in a file named request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://cloudoptimization.googleapis.com/v1/projects/project-id:batchOptimizeTours"
PowerShell
Save the request body in a file named request.json
,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://cloudoptimization.googleapis.com/v1/projects/project-id:batchOptimizeTours" | Select-Object -Expand Content
You should see output similar to the following. You can use the operation ID
(7781633313341553605
, in this case) to get the status of the task. For an
example, see Working with long-running operations:
{ "name": "projects/project-id/operations/7781633313341553605", }
After the long-running operation completes you can find the generated solution in the Google Cloud
Storage you specified in your request with solution-google-cloud-storage-uri
. In this
example, we only specified one model config so it only generates one solution and it is worthwhile to
mention that you can specify multiple model configs. The solution should
look similar to the following:
{ "routes": [ { "vehicleStartTime": "1970-01-01T00:02:11Z", "vehicleEndTime": "1970-01-01T01:15:34Z", "visits": [ { "isPickup": true, "startTime": "1970-01-01T00:16:40Z", "detour": "0s", "arrivalLoads": [ { "type": "weight" } ] }, { "shipmentIndex": 1, "isPickup": true, "startTime": "1970-01-01T00:27:35Z", "detour": "725s", "arrivalLoads": [ { "type": "weight", "value": "10" } ] }, { "startTime": "1970-01-01T00:50:00Z", "detour": "1345s", "arrivalLoads": [ { "type": "weight", "value": "30" } ] }, { "shipmentIndex": 1, "startTime": "1970-01-01T00:58:43Z", "detour": "1444s", "arrivalLoads": [ { "type": "weight", "value": "20" } ] } ], "travelSteps": [ { "duration": "869s", "distanceMeters": 4243 }, { "duration": "505s", "distanceMeters": 2480 }, { "duration": "0s" }, { "duration": "273s", "distanceMeters": 986 }, { "duration": "760s", "distanceMeters": 3099 } ], "vehicleDetour": "4403s", "endLoads": [ { "type": "weight" } ], "transitions": [ { "travelDuration": "869s", "travelDistanceMeters": 4243, "loads": [ { "type": "weight" } ] }, { "travelDuration": "505s", "travelDistanceMeters": 2480, "loads": [ { "type": "weight", "value": "10" } ] }, { "travelDuration": "0s", "loads": [ { "type": "weight", "value": "30" } ] }, { "travelDuration": "273s", "travelDistanceMeters": 986, "loads": [ { "type": "weight", "value": "20" } ] }, { "travelDuration": "760s", "travelDistanceMeters": 3099, "loads": [ { "type": "weight" } ] } ] }, { "vehicleIndex": 1, "vehicleStartTime": "1970-01-01T00:00:00Z", "vehicleEndTime": "1970-01-01T00:00:00Z", "vehicleDetour": "0s" } ] }
Java
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Python
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Working with long-running operations
You can get the status of a long-running operation by using the following code samples.
REST
Before using any of the request data, make the following replacements:
- project-id: your GCP project ID.
- operation-id: the ID of your operation. The ID is the last element of the name
of your operation. For example:
- operation name:
projects/project-id/operations/7781633313341553605
- operation id:
7781633313341553605
- operation name:
HTTP method and URL:
GET https://cloudoptimization.googleapis.com/v1/projects/project-id/operations/operation-id
To send your request, choose one of these options:
curl
Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
"https://cloudoptimization.googleapis.com/v1/projects/project-id/operations/operation-id"
PowerShell
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://cloudoptimization.googleapis.com/v1/projects/project-id/operations/operation-id" | Select-Object -Expand Content
{ "name": "projects/project-id/operations/operation-id", "metadata": { "@type": "type.googleapis.com/google.cloud.optimization.v1.AsyncModelMetadata", "state": "SUCCEEDED", "createTime": "2022-04-01T20:35:36.416835Z", "updateTime": "2022-04-01T20:35:37.906284Z" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.optimization.v1.BatchOptimizeToursResponse" } }
Java
Before trying this sample, follow the set up instructions for this language on the APIs & Reference > Client Libraries page.
Python
Before trying this sample, follow the set up instructions for this language on the APIs & Reference > Client Libraries page.