MIP 求解示例
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
以下示例展示了如何构建数学优化问题
然后使用 MathOpt 进行远程求解,并使用 OR API 进行远程求解。要获取 API 密钥,请按以下步骤操作:
请先按照设置指南操作。MathOpt 是或工具的一部分提供
从 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)
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-09-12。
[null,null,["最后更新时间 (UTC):2024-09-12。"],[[["\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)"]]