線形計画法の解。次の例では、次の線形計画法を解きます。
2 つの変数 x と y:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
制約:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
目的:
最大化 x + y
const engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), // addConstraint(), etc. Add two variables, 0 <= x <= 10 and 0 <= y <= 5 engine.addVariable('x', 0, 10); engine.addVariable('y', 0, 5); // Create the constraint: 0 <= 2 * x + 5 * y <= 10 let constraint = engine.addConstraint(0, 10); constraint.setCoefficient('x', 2); constraint.setCoefficient('y', 5); // Create the constraint: 0 <= 10 * x + 3 * y <= 20 constraint = engine.addConstraint(0, 20); constraint.setCoefficient('x', 10); constraint.setCoefficient('y', 3); // Set the objective to be x + y engine.setObjectiveCoefficient('x', 1); engine.setObjectiveCoefficient('y', 1); // Engine should maximize the objective engine.setMaximization(); // Solve the linear program const solution = engine.solve(); if (!solution.isValid()) { Logger.log(`No solution ${solution.getStatus()}`); } else { Logger.log(`Objective value: ${solution.getObjectiveValue()}`); Logger.log(`Value of x: ${solution.getVariableValue('x')}`); Logger.log(`Value of y: ${solution.getVariableValue('y')}`); }
メソッド
| メソッド | 戻り値の型 | 概要 |
|---|---|---|
get | Number | 現在の解における目的関数の値を取得します。 |
get | Status | ソリューションのステータスを取得します。 |
get | Number | Linear の最後の呼び出しで作成されたソリューションの変数の値を取得します。 |
is | Boolean | 解が実行可能か最適かを判断します。 |
詳細なドキュメント
getObjectiveValue()
現在の解における目的関数の値を取得します。
const engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), // addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program const solution = engine.solve(); Logger.log(`ObjectiveValue: ${solution.getObjectiveValue()}`);
戻る
Number - 目的関数の値
getStatus()
ソリューションのステータスを取得します。問題を解決する前は、ステータスは NOT_SOLVED になります。
const engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), // addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program const solution = engine.solve(); const status = solution.getStatus(); if (status !== LinearOptimizationService.Status.FEASIBLE && status !== LinearOptimizationService.Status.OPTIMAL) { throw `No solution ${status}`; } Logger.log(`Status: ${status}`);
戻る
Status - ソルバーのステータス
getVariableValue(variableName)
LinearOptimizationEngine.solve() の最後の呼び出しで作成されたソリューションの変数の値を取得します。
const engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), // addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program const solution = engine.solve(); Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
variable | String | 変数の名前 |
戻る
Number - 解の変数の値
isValid()
解が実行可能か最適かを判断します。
const engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), // addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program const solution = engine.solve(); if (!solution.isValid()) { throw `No solution ${solution.getStatus()}`; }
戻る
Boolean - 解が有効な場合は true(Status.FEASIBLE または
Status.OPTIMAL)、無効な場合は false