Ví dụ về giải pháp MIP
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Ví dụ sau đây minh hoạ cách xây dựng một bài toán tối ưu hoá toán học
bằng MathOpt và giải quyết từ xa bằng API OR. Để lấy Khoá API,
hãy làm theo hướng dẫn thiết lập trước. MathOpt được cung cấp dưới dạng một phần của OR-Tools
kể từ bản phát hành 9.9. Hãy truy cập hướng dẫn cài đặt để biết thêm thông tin.
# 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)
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2024-09-12 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2024-09-12 UTC."],[[["This example demonstrates how to build a mathematical optimization problem with MathOpt and solve it remotely via the OR API."],["It utilizes the `remote_http_solve` function for remote execution, requiring an API key for authentication."],["The example showcases the process of model creation, serialization to JSON for the HTTP request, and result retrieval, including objective value and variable values."],["It'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)."]]],["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"]]