MIP 해답의 예
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
다음 예에서는 MathOpt를 사용하여 수학적 최적화 문제를 빌드하고 OR API를 사용하여 원격으로 해결하는 방법을 보여줍니다. API 키를 가져오려면
먼저 설정 가이드를 따르세요. MathOpt는 OR-Tools의 일부로 사용할 수 있습니다.
버전 9.9부터 지원됩니다. 자세한 내용은 설치 가이드를 참고하세요.
# solve_math_opt_model_via_http.py
"""Example of solving a MathOpt model through the OR API.
The model is built using the Python API, and the corresponding proto is
serialized 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:
"""Run example using MathOpt `remote_http_solve` function."""
# Set up the API key.
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,
mathopt.SolveParameters(enable_output=True),
api_key=api_key,
)
print("Objective value: ", result.objective_value())
print("x: ", result.variable_values(x))
print("y: ", result.variable_values(y))
print("\n".join(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)
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2024-09-12(UTC)
[null,null,["최종 업데이트: 2024-09-12(UTC)"],[[["\u003cp\u003eThis example demonstrates how to build a mathematical optimization problem with MathOpt and solve it remotely via the OR API.\u003c/p\u003e\n"],["\u003cp\u003eIt utilizes the \u003ccode\u003eremote_http_solve\u003c/code\u003e function for remote execution, requiring an API key for authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe example showcases the process of model creation, serialization to JSON for the HTTP request, and result retrieval, including objective value and variable values.\u003c/p\u003e\n"],["\u003cp\u003eIt's crucial to first obtain an API key through the provided setup guide and ensure you have MathOpt installed (available in OR-Tools since release 9.9).\u003c/p\u003e\n"]]],["This content demonstrates how to solve a mathematical optimization problem remotely using the OR API and MathOpt. First, an API key is required, obtainable via a setup guide. A MathOpt model is then built, including binary and bounded variables, a linear constraint, and an objective function to maximize. Finally, `remote_http_solve` function sends this model to OR API for solving, returning the solution's objective value, variable values, and logs. MathOpt is part of OR-Tools since version 9.9.\n"],null,["# MIP Solve Example\n\nThe following example showcases how to build a mathematical optimization problem\nusing MathOpt and make a remote solve using the OR API. To obtain an API Key,\nfollow the [setup guide](/optimization/service/setup) first. MathOpt is available as part of [OR-Tools\nsince release 9.9](https://github.com/google/or-tools/releases/tag/v9.9). Visit the [install guide](/optimization/install) for more information. \n\n\n # solve_math_opt_model_via_http.py\n\n \"\"\"Example of solving a MathOpt model through the OR API.\n\n The model is built using the Python API, and the corresponding proto is\n serialized to JSON to make the HTTP request.\n \"\"\"\n\n from collections.abc import Sequence\n\n from absl import app\n from absl import flags\n\n from ortools.math_opt.python import mathopt\n from ortools.math_opt.python.ipc import remote_http_solve\n\n _API_KEY = flags.DEFINE_string(\"api_key\", None, \"API key for the OR API\")\n\n def request_example() -\u003e None:\n \"\"\"Run example using MathOpt `remote_http_solve` function.\"\"\"\n # Set up the API key.\n api_key = _API_KEY.value\n if not api_key:\n print(\n \"API key is required. See\"\n \" https://developers.google.com/optimization/service/setup for\"\n \" instructions.\"\n )\n return\n\n # Build a MathOpt model\n model = mathopt.Model(name=\"my_model\")\n x = model.add_binary_variable(name=\"x\")\n y = model.add_variable(lb=0.0, ub=2.5, name=\"y\")\n model.add_linear_constraint(x + y \u003c= 1.5, name=\"c\")\n model.maximize(2 * x + y)\n try:\n result, logs = remote_http_solve.remote_http_solve(\n model,\n mathopt.SolverType.GSCIP,\n mathopt.SolveParameters(enable_output=True),\n api_key=api_key,\n )\n print(\"Objective value: \", result.objective_value())\n print(\"x: \", result.variable_values(x))\n print(\"y: \", result.variable_values(y))\n print(\"\\n\".join(logs))\n except remote_http_solve.OptimizationServiceError as err:\n print(err)\n\n def main(argv: Sequence[str]) -\u003e None:\n del argv # Unused.\n request_example()\n\n if __name__ == \"__main__\":\n app.run(main)"]]