MIP 解題範例
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
下例示範如何建立數學最佳化問題
,並使用 OR API 建立遠端解題。如要取得 API 金鑰,請先按照設定指南操作。MathOpt 隨附於 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)
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-09-12 (世界標準時間)。
[null,null,["上次更新時間: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)"]]