MIP ソルブの例
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
次の例は、数学的最適化の問題を構築する方法を示しています
OR API を使用して遠隔解法を作成しますAPI キーを取得するには、
まず、設定ガイドに沿って操作してください。MathOpt は、リリース 9.9 以降の OR-Tools の一部として利用できます。詳しくは、インストール ガイドをご覧ください。
# 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)
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は 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)"]]