Using the Command Line
This page shows you how to get started to solve a vehicle routing problem with the Cloud Optimization API using command line.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Optimization API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Optimization API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
Make an optimize tours request
The following script sends the request in a file request.json
to the
:optimizeTours
method of the Cloud Fleet Routing service.
The following shows an example request body:
{ "parent": "projects/${YOUR_GCP_PROJECT_ID}", "model": { "shipments": [ { "deliveries": [ { "arrivalLocation": { "latitude": 48.880942, "longitude": 2.323866 }, "duration": "250s", "timeWindows": [ { "endTime": "1970-01-01T01:06:40Z", "startTime": "1970-01-01T00:50:00Z" } ] } ], "loadDemands": { "weight": { "amount": "10" } }, "pickups": [ { "arrivalLocation": { "latitude": 48.874507, "longitude": 2.30361 }, "duration": "150s", "timeWindows": [ { "endTime": "1970-01-01T00:33:20Z", "startTime": "1970-01-01T00:16:40Z" } ] } ] }, { "deliveries": [ { "arrivalLocation": { "latitude": 48.88094, "longitude": 2.323844 }, "duration": "251s", "timeWindows": [ { "endTime": "1970-01-01T01:06:41Z", "startTime": "1970-01-01T00:50:01Z" } ] } ], "loadDemands": { "weight": { "amount": "20" } }, "pickups": [ { "arrivalLocation": { "latitude": 48.880943, "longitude": 2.323867 }, "duration": "151s", "timeWindows": [ { "endTime": "1970-01-01T00:33:21Z", "startTime": "1970-01-01T00:16:41Z" } ] } ] } ], "vehicles": [ { "loadLimits": { "weight": { "maxLoad": 50 } }, "endLocation": { "latitude": 48.86311, "longitude": 2.341205 }, "startLocation": { "latitude": 48.863102, "longitude": 2.341204 }, "costPerTraveledHour": 3600 }, { "loadLimits": { "weight": { "maxLoad": 60 } }, "endLocation": { "latitude": 48.86312, "longitude": 2.341215 }, "startLocation": { "latitude": 48.863112, "longitude": 2.341214 }, "costPerTraveledHour": 3600 } ] } }
Command-line
Usecurl
to make a POST
request to the optimizeTours
method and provide the appropriate request body as shown in the following
example.
Get your access token by entering gcloud auth print-access-token
in your terminal.
Save the JSON request file as request.json
in the same directory.
Replace ${YOUR_GCP_PROJECT_ID}
with your Google Cloud project ID in both the code
and the request.
In your terminal, run the Curl script. Note that the command includes the name of the request file as an argument.
curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${YOUR_ACCESS_TOKEN}" \ -H "x-goog-user-project: ${YOUR_GCP_PROJECT_ID}" \ -d @request.json \ https://cloudoptimization.googleapis.com/v1/projects/${YOUR_GCP_PROJECT_ID}:optimizeTours
You should see a response 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" } ] }
Clean up
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What's next
- Get started with the Cloud Optimization API in your language of choice by using a Cloud Optimization Client Libraries.