diff --git a/examples/service/BUILD.bazel b/examples/service/BUILD.bazel new file mode 100644 index 0000000000..b85c0ca2f3 --- /dev/null +++ b/examples/service/BUILD.bazel @@ -0,0 +1,30 @@ +# Copyright 2010-2024 Google LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@pip_deps//:requirements.bzl", "requirement") +load("@rules_python//python:defs.bzl", "py_binary") + +package(default_visibility = [ + "//ortools/math_opt:__subpackages__", + "//ortools/service:__subpackages__", +]) + +py_binary( + name = "solve_math_opt_model_via_http", + srcs = ["solve_math_opt_model_via_http.py"], + deps = [ + requirement("absl-py"), + "//ortools/math_opt/python:mathopt", + "//ortools/math_opt/python/ipc:remote_http_solve", + ], +) diff --git a/examples/service/solve_math_opt_model_via_http.py b/examples/service/solve_math_opt_model_via_http.py new file mode 100644 index 0000000000..39a4a874fe --- /dev/null +++ b/examples/service/solve_math_opt_model_via_http.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# Copyright 2010-2024 Google LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Example of solving a MathOpt model through the OR API. + +The model is built using the Python API, and the corresponding proto is +serialize to JSON to make the HTTP request. +""" + +from collections.abc import Sequence + +from absl import app +from absl import flags + +from ortools.math_opt.python import mathopt +from ortools.math_opt.python.ipc import remote_http_solve + +_API_KEY = flags.DEFINE_string("api_key", None, "API key for the OR API") + + +def request_example() -> None: + """Endpoint for the Operations Research API.""" + + # Set up the API key and endpoint. + api_key = _API_KEY.value + if not api_key: + print( + "API key is required. See" + " https://developers.google.com/optimization/service/setup for" + " instructions." + ) + return + + # Build a MathOpt model + model = mathopt.Model(name="my_model") + x = model.add_binary_variable(name="x") + y = model.add_variable(lb=0.0, ub=2.5, name="y") + model.add_linear_constraint(x + y <= 1.5, name="c") + model.maximize(2 * x + y) + try: + result, logs = remote_http_solve.remote_http_solve( + model, mathopt.SolverType.GSCIP, api_key=api_key + ) + print(result) + print(logs) + except remote_http_solve.OptimizationServiceError as err: + print(err) + + +def main(argv: Sequence[str]) -> None: + del argv # Unused. + request_example() + + +if __name__ == "__main__": + app.run(main)