examples: add service example

This commit is contained in:
Corentin Le Molgat
2024-07-12 16:22:07 +02:00
parent 7ad3909af3
commit d4d86d36c4
2 changed files with 97 additions and 0 deletions

View File

@@ -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",
],
)

View File

@@ -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)