This directory contains an idiomatic C++ client library for theCloud Optimization API, a service that provides a portfolio of solvers to address common optimization use cases.
While this library isGA, please note Google Cloud C++ client libraries donotfollowSemantic Versioning.
Quickstart
The following shows the code that you'll run in thegoogle/cloud/optimization/quickstart/directory, which should give you a taste of the Cloud Optimization API C++ client library API.
#include "google/cloud/optimization/v1/fleet_routing_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/project.h"
#include <iostream>
int main(int argc, char* argv[]) try {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " project-id destination\n"
<< " destination is a GCS bucket\n";
return 1;
}
// This quickstart loads an example model from a known GCS bucket. The service
// solves the model, and writes the solution to the destination GCS bucket.
std::string const source =
"gs://cloud-samples-data/optimization-ai/async_request_model.json";
auto const destination =
std::string{"gs://"} + argv[2] + "/optimization_quickstart_solution.json";
namespace gc = ::google::cloud;
namespace optimization = ::google::cloud::optimization_v1;
auto options = gc::Options{}.set<gc::UserProjectOption>(argv[1]);
auto client = optimization::FleetRoutingClient(
optimization::MakeFleetRoutingConnection(options));
google::cloud::optimization::v1::BatchOptimizeToursRequest req;
req.set_parent(gc::Project(argv[1]).FullName());
auto& config = *req.add_model_configs();
auto& input = *config.mutable_input_config();
input.mutable_gcs_source()->set_uri(source);
input.set_data_format(google::cloud::optimization::v1::JSON);
auto& output = *config.mutable_output_config();
output.mutable_gcs_destination()->set_uri(destination);
output.set_data_format(google::cloud::optimization::v1::JSON);
auto fut = client.BatchOptimizeTours(req);
std::cout << "Request sent to the service...\n";
auto resp = fut.get();
if (!resp) throw std::move(resp).status();
std::cout << "Solution written to: " << destination << "\n";
return 0;
} catch (google::cloud::Status const& status) {
std::cerr << "google::cloud::Status thrown: " << status << "\n";
return 1;
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis page details the C++ client library for the Cloud Optimization API, which offers solvers for various optimization scenarios.\u003c/p\u003e\n"],["\u003cp\u003eThe latest release candidate version is 2.37.0-rc, with several prior versions, including 2.23.0, listed for reference.\u003c/p\u003e\n"],["\u003cp\u003eThe primary class in this library is \u003ccode\u003eoptimization_v1::FleetRoutingClient\u003c/code\u003e, which contains all the RPC functionalities.\u003c/p\u003e\n"],["\u003cp\u003eThe library uses asynchronous request that sends the request to the service, which solves the model and writes the solution to the specified destination, all of which can be observed in the quickstart example.\u003c/p\u003e\n"],["\u003cp\u003eUsers can customize aspects of the library, such as error handling, endpoint overrides, authentication, and retry policies, as detailed in the "More Information" section.\u003c/p\u003e\n"]]],[],null,["Version 2.23.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/optimization/latest)\n- [2.41.0](/cpp/docs/reference/optimization/2.41.0)\n- [2.40.0](/cpp/docs/reference/optimization/2.40.0)\n- [2.39.0](/cpp/docs/reference/optimization/2.39.0)\n- [2.38.0](/cpp/docs/reference/optimization/2.38.0)\n- [2.37.0](/cpp/docs/reference/optimization/2.37.0)\n- [2.36.0](/cpp/docs/reference/optimization/2.36.0)\n- [2.35.0](/cpp/docs/reference/optimization/2.35.0)\n- [2.34.0](/cpp/docs/reference/optimization/2.34.0)\n- [2.33.0](/cpp/docs/reference/optimization/2.33.0)\n- [2.32.0](/cpp/docs/reference/optimization/2.32.0)\n- [2.31.0](/cpp/docs/reference/optimization/2.31.0)\n- [2.30.0](/cpp/docs/reference/optimization/2.30.0)\n- [2.29.0](/cpp/docs/reference/optimization/2.29.0)\n- [2.28.0](/cpp/docs/reference/optimization/2.28.0)\n- [2.27.0](/cpp/docs/reference/optimization/2.27.0)\n- [2.26.0](/cpp/docs/reference/optimization/2.26.0)\n- [2.25.1](/cpp/docs/reference/optimization/2.25.1)\n- [2.24.0](/cpp/docs/reference/optimization/2.24.0)\n- [2.23.0](/cpp/docs/reference/optimization/2.23.0)\n- [2.22.1](/cpp/docs/reference/optimization/2.22.1)\n- [2.21.0](/cpp/docs/reference/optimization/2.21.0)\n- [2.20.0](/cpp/docs/reference/optimization/2.20.0)\n- [2.19.0](/cpp/docs/reference/optimization/2.19.0)\n- [2.18.0](/cpp/docs/reference/optimization/2.18.0)\n- [2.17.0](/cpp/docs/reference/optimization/2.17.0)\n- [2.16.0](/cpp/docs/reference/optimization/2.16.0)\n- [2.15.1](/cpp/docs/reference/optimization/2.15.1)\n- [2.14.0](/cpp/docs/reference/optimization/2.14.0)\n- [2.13.0](/cpp/docs/reference/optimization/2.13.0)\n- [2.12.0](/cpp/docs/reference/optimization/2.12.0)\n- [2.11.0](/cpp/docs/reference/optimization/2.11.0) \n\nCloud Optimization API C++ Client Library\n=========================================\n\nThis directory contains an idiomatic C++ client library for the [Cloud Optimization API](https://cloud.google.com/optimization), a service that provides a portfolio of solvers to address common optimization use cases.\n\nWhile this library is **GA** , please note Google Cloud C++ client libraries do **not** follow [Semantic Versioning](https://semver.org/).\n\n### Quickstart\n\nThe following shows the code that you'll run in the `google/cloud/optimization/quickstart/` directory, which should give you a taste of the Cloud Optimization API C++ client library API. \n\n #include \"google/cloud/optimization/v1/fleet_routing_client.h\"\n #include \"google/cloud/common_options.h\"\n #include \"google/cloud/project.h\"\n #include \u003ciostream\u003e\n\n int main(int argc, char* argv[]) try {\n if (argc != 3) {\n std::cerr \u003c\u003c \"Usage: \" \u003c\u003c argv[0] \u003c\u003c \" project-id destination\\n\"\n \u003c\u003c \" destination is a GCS bucket\\n\";\n return 1;\n }\n // This quickstart loads an example model from a known GCS bucket. The service\n // solves the model, and writes the solution to the destination GCS bucket.\n std::string const source =\n \"gs://cloud-samples-data/optimization-ai/async_request_model.json\";\n auto const destination =\n std::string{\"gs://\"} + argv[2] + \"/optimization_quickstart_solution.json\";\n\n namespace gc = ::google::cloud;\n namespace optimization = ::google::cloud::optimization_v1;\n\n auto options = gc::Options{}.set\u003cgc::UserProjectOption\u003e(argv[1]);\n auto client = optimization::FleetRoutingClient(\n optimization::MakeFleetRoutingConnection(options));\n\n google::cloud::optimization::v1::BatchOptimizeToursRequest req;\n req.set_parent(gc::Project(argv[1]).FullName());\n auto& config = *req.add_model_configs();\n auto& input = *config.mutable_input_config();\n input.mutable_gcs_source()-\u003eset_uri(source);\n input.set_data_format(google::cloud::optimization::v1::JSON);\n auto& output = *config.mutable_output_config();\n output.mutable_gcs_destination()-\u003eset_uri(destination);\n output.set_data_format(google::cloud::optimization::v1::JSON);\n\n auto fut = client.BatchOptimizeTours(req);\n std::cout \u003c\u003c \"Request sent to the service...\\n\";\n auto resp = fut.get();\n if (!resp) throw std::move(resp).status();\n std::cout \u003c\u003c \"Solution written to: \" \u003c\u003c destination \u003c\u003c \"\\n\";\n\n return 0;\n } catch (google::cloud::Status const& status) {\n std::cerr \u003c\u003c \"google::cloud::Status thrown: \" \u003c\u003c status \u003c\u003c \"\\n\";\n return 1;\n }\n\n### Main classes\n\nThe main class in this library is [`optimization_v1::FleetRoutingClient`](/cpp/docs/reference/optimization/2.23.0/classgoogle_1_1cloud_1_1optimization__v1_1_1FleetRoutingClient). All RPCs are exposed as member functions of this class. Other classes provide helpers, configuration parameters, and infrastructure to mock [`optimization_v1::FleetRoutingClient`](/cpp/docs/reference/optimization/2.23.0/classgoogle_1_1cloud_1_1optimization__v1_1_1FleetRoutingClient) when testing your application.\n\n### More Information\n\n- [Error Handling](https://cloud.google.com/cpp/docs/reference/common/latest/common-error-handling.html) - describes how the library reports errors.\n- [How to Override the Default Endpoint](/cpp/docs/reference/optimization/2.23.0/optimization-override-endpoint) - describes how to override the default endpoint.\n- [How to Override the Authentication Credentials](/cpp/docs/reference/optimization/2.23.0/optimization-override-authentication) - describes how to change the authentication credentials used by the library.\n- [Override Retry, Backoff, and Idempotency Policies](/cpp/docs/reference/optimization/2.23.0/optimization-override-retry) - describes how to change the default retry policies.\n- [Environment Variables](/cpp/docs/reference/optimization/2.23.0/optimization-env) - describes environment variables that can configure the behavior of the library."]]