مثال على حل MIP
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يوضّح المثال التالي كيفية إنشاء مشكلة تحسين رياضي باستخدام MathOpt وإجراء حلّ عن بُعد باستخدام واجهة برمجة التطبيقات OR 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)
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 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)"]]