線形プログラムの解。次の例では、次の線形プログラムを解きます。
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)
Linear
の最後の呼び出しによって作成されたソリューション内の変数の値を取得します。
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
- ソリューションが有効な場合(Status.FEASIBLE
または Status.OPTIMAL
)は true
、無効な場合は false