スタートガイド

この例は、MathOpt を使用して単純な線形プログラム(LP)を構築、解く、結果を調べる方法を示しています。OR-Tools のインストールについては、インストール ガイドをご覧ください。ソースからビルドして実行する方法についての追加情報は、最後に続きます。

MathOpt モデルを構築する

ソースでは通常、MathOpt 依存関係を 1 つのみ追加します。

Python

from ortools.math_opt.python import mathopt

C++

#include <iostream>
#include <ostream>

#include "absl/log/check.h"
#include "absl/status/statusor.h"
#include "ortools/base/init_google.h"
#include "ortools/math_opt/cpp/math_opt.h"

このガイドでは、次の線形プログラミングの問題を使用しており、この問題は GLOP で解決します。

$$\begin{aligned} &\max &x + 2 \cdot y\\ &\text{subject to} &x + y &\leq 1.5 \\ &&-1 \leq x &\leq 1.5 \\ &&0 \leq y &\leq 1 \end{aligned}$$

まず、モデルを構築します。

Python

# Build the model.
model = mathopt.Model(name="getting_started_lp")
x = model.add_variable(lb=-1.0, ub=1.5, name="x")
y = model.add_variable(lb=0.0, ub=1.0, name="y")
model.add_linear_constraint(x + y <= 1.5)
model.maximize(x + 2 * y)

C++

// Build the model.
namespace math_opt = ::operations_research::math_opt;
math_opt::Model lp_model("getting_started_lp");
const math_opt::Variable x = lp_model.AddContinuousVariable(-1.0, 1.5, "x");
const math_opt::Variable y = lp_model.AddContinuousVariable(0.0, 1.0, "y");
lp_model.AddLinearConstraint(x + y <= 1.5, "c");
lp_model.Maximize(x + 2 * y);

解を解いて調べる

次に、解法のパラメータを設定します。MathOpt による最適化モデルは 高度に構成できますソルバーに依存しないパラメータ(出力の有効化など)、ソルバー固有のパラメータ(GlopParameters.optimization_rule など)、モデルのプロパティに依存するパラメータ(分岐の優先度など)、ソルバーログのコールバック、最適化をモニタリングして制御するコールバックがあります。次のコードは、ソルバーのログを有効にします。

Python

# Set parameters, e.g. turn on logging.
params = mathopt.SolveParameters(enable_output=True)

C++

// Set parameters, e.g. turn on logging.
math_opt::SolveArguments args;
args.parameters.enable_output = true;

Google のシンプレックス ベースの LP 解法である GLOP を使用して問題を解くには、Solve() 関数を使用します。

Python

# Solve and ensure an optimal solution was found with no errors.
# (mathopt.solve may raise a RuntimeError on invalid input or internal solver
# errors.)
result = mathopt.solve(model, mathopt.SolverType.GLOP, params=params)
if result.termination.reason != mathopt.TerminationReason.OPTIMAL:
    raise RuntimeError(f"model failed to solve: {result.termination}")

C++

// Solve and ensure an optimal solution was found with no errors.
const absl::StatusOr<math_opt::SolveResult> result =
    math_opt::Solve(lp_model, math_opt::SolverType::kGlop, args);
CHECK_OK(result.status());
CHECK_OK(result->termination.EnsureIsOptimal());

最後に、最適な解と最適な変数値の目標値を調べます。終了理由が最適であったため、これらの値が存在すると仮定しても問題ありませんが、その他の終了理由(実行不能または無限大など)では、これらのメソッドを呼び出す CHECK fail(C++ の場合)または raise an exception(Python の場合)が可能です。

Python

# Print some information from the result.
print("MathOpt solve succeeded")
print("Objective value:", result.objective_value())
print("x:", result.variable_values()[x])
print("y:", result.variable_values()[y])

C++

// Print some information from the result.
std::cout << "MathOpt solve succeeded" << std::endl;
std::cout << "Objective value: " << result->objective_value() << std::endl;
std::cout << "x: " << result->variable_values().at(x) << std::endl;
std::cout << "y: " << result->variable_values().at(y) << std::endl;

Bazel を使用したコードのビルドと実行に関する注意事項

bazel を使用してソースから MathOpt をビルドする場合、この例ではビルド ターゲットに次の依存関係が必要です。

Python

"//util/operations_research/math_opt/python:mathopt"

C++

"//util/operations_research/math_opt/cpp:math_opt"
"//util/operations_research/math_opt/solvers:glop_solver"

コードを実行するには、次の bazel コマンドでターゲットをビルドして実行します。

Python

bazel run path/to/you:target --with_scip=false --with_cp_sat=false
--with_glpk=false --with_glop=true -- --your_flags

C++

bazel run path/to/you:target -- --your_flags