線形プログラムのモデル化と解析に使用されるエンジン。次の例では、次の線形プログラムを解きます。
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(`Value of x: ${solution.getVariableValue('x')}`); Logger.log(`Value of y: ${solution.getVariableValue('y')}`); }
メソッド
メソッド | 戻り値の型 | 概要 |
---|---|---|
add | Linear | モデルに新しい線形制約を追加します。 |
add | Linear | 制約を一括でモデルに追加します。 |
add | Linear | モデルに新しい連続変数を追加します。 |
add | Linear | モデルに新しい変数を追加します。 |
add | Linear | モデルに新しい変数を追加します。 |
add | Linear | 変数を一括でモデルに追加します。 |
set | Linear | 最適化の方向を、線形目的関数の最大化に設定します。 |
set | Linear | 最適化の方向を線形目的関数の最小化に設定します。 |
set | Linear | 線形目的関数の変数の係数を設定します。 |
solve() | Linear | デフォルトの期限 30 秒で現在の線形計画問題を解きます。 |
solve(seconds) | Linear | 現在の線形プログラムを解きます。 |
詳細なドキュメント
addConstraint(lowerBound, upperBound)
モデルに新しい線形制約を追加します。制約の上限と下限は、作成時に定義されます。変数の係数は、Linear
の呼び出しによって定義されます。
const engine = LinearOptimizationService.createEngine(); // Create a linear constraint with the bounds 0 and 10 const constraint = engine.addConstraint(0, 10); // Create a variable so we can add it to the constraint engine.addVariable('x', 0, 5); // Set the coefficient of the variable in the constraint. The constraint is now: // 0 <= 2 * x <= 5 constraint.setCoefficient('x', 2);
パラメータ
名前 | 型 | 説明 |
---|---|---|
lower | Number | 制約の下限 |
upper | Number | 制約の上限 |
戻る
Linear
- 作成された制約
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
制約を一括でモデルに追加します。
const engine = LinearOptimizationService.createEngine(); // Add a boolean variable 'x' (integer >= 0 and <= 1) and a real (continuous >= // 0 and <= 100) variable 'y'. engine.addVariables( ['x', 'y'], [0, 0], [1, 100], [ LinearOptimizationService.VariableType.INTEGER, LinearOptimizationService.VariableType.CONTINUOUS, ], ); // Adds two constraints: // 0 <= x + y <= 3 // 1 <= 10 * x - y <= 5 engine.addConstraints( [0.0, 1.0], [3.0, 5.0], [ ['x', 'y'], ['x', 'y'], ], [ [1, 1], [10, -1], ], );
パラメータ
名前 | 型 | 説明 |
---|---|---|
lower | Number[] | 制約の下限 |
upper | Number[] | 制約の上限 |
variable | String[][] | 係数が設定される変数の名前 |
coefficients | Number[][] | 設定される係数 |
戻る
Linear
- 線形最適化エンジン
addVariable(name, lowerBound, upperBound)
モデルに新しい連続変数を追加します。変数は名前で参照されます。タイプは Variable
に設定されています。
const engine = LinearOptimizationService.createEngine(); const constraint = engine.addConstraint(0, 10); // Add a boolean variable (integer >= 0 and <= 1) engine.addVariable('x', 0, 1, LinearOptimizationService.VariableType.INTEGER); // Add a real (continuous) variable. Notice the lack of type specification. engine.addVariable('y', 0, 100);
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 変数の一意の名前 |
lower | Number | 変数の下限 |
upper | Number | 変数の上限 |
戻る
Linear
- 線形最適化エンジン
addVariable(name, lowerBound, upperBound, type)
モデルに新しい変数を追加します。変数は名前で参照されます。
const engine = LinearOptimizationService.createEngine(); const constraint = engine.addConstraint(0, 10); // Add a boolean variable (integer >= 0 and <= 1) engine.addVariable('x', 0, 1, LinearOptimizationService.VariableType.INTEGER); // Add a real (continuous) variable engine.addVariable( 'y', 0, 100, LinearOptimizationService.VariableType.CONTINUOUS, );
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 変数の一意の名前 |
lower | Number | 変数の下限 |
upper | Number | 変数の上限 |
type | Variable | 変数の型。Variable のいずれか |
戻る
Linear
- 線形最適化エンジン
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
モデルに新しい変数を追加します。変数は名前で参照されます。
const engine = LinearOptimizationService.createEngine(); const constraint = engine.addConstraint(0, 10); // Add a boolean variable (integer >= 0 and <= 1) engine.addVariable( 'x', 0, 1, LinearOptimizationService.VariableType.INTEGER, 2, ); // The objective is now 2 * x. // Add a real (continuous) variable engine.addVariable( 'y', 0, 100, LinearOptimizationService.VariableType.CONTINUOUS, -5, ); // The objective is now 2 * x - 5 * y.
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 変数の一意の名前 |
lower | Number | 変数の下限 |
upper | Number | 変数の上限 |
type | Variable | 変数の型。Variable のいずれか |
objective | Number | 変数の目的係数 |
戻る
Linear
- 線形最適化エンジン
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
変数を一括でモデルに追加します。変数は名前で参照されます。
const engine = LinearOptimizationService.createEngine(); // Add a boolean variable 'x' (integer >= 0 and <= 1) and a real (continuous >=0 // and <= 100) variable 'y'. engine.addVariables( ['x', 'y'], [0, 0], [1, 100], [ LinearOptimizationService.VariableType.INTEGER, LinearOptimizationService.VariableType.CONTINUOUS, ], );
パラメータ
名前 | 型 | 説明 |
---|---|---|
names | String[] | 変数の一意の名前 |
lower | Number[] | 変数の下限 |
upper | Number[] | 変数の上限 |
types | Variable | 変数の型。Variable のいずれか |
objective | Number[] | 変数の目的係数 |
戻る
Linear
- 線形最適化エンジン
setMaximization()
最適化の方向を、線形目的関数を最大化するように設定します。
const engine = LinearOptimizationService.createEngine(); // Add a real (continuous) variable. Notice the lack of type specification. engine.addVariable('y', 0, 100); // Set the coefficient of 'y' in the objective. // The objective is now 5 * y engine.setObjectiveCoefficient('y', 5); // We want to maximize. engine.setMaximization();
戻る
Linear
- 線形最適化エンジン
setMinimization()
最適化の方向を線形目的関数の最小化に設定します。
const engine = LinearOptimizationService.createEngine(); // Add a real (continuous) variable. Notice the lack of type specification. engine.addVariable('y', 0, 100); // Set the coefficient of 'y' in the objective. // The objective is now 5 * y engine.setObjectiveCoefficient('y', 5); // We want to minimize engine.setMinimization();
戻る
Linear
- 線形最適化エンジン
setObjectiveCoefficient(variableName, coefficient)
線形目的関数の変数の係数を設定します。
const engine = LinearOptimizationService.createEngine(); // Add a real (continuous) variable. Notice the lack of type specification. engine.addVariable('y', 0, 100); // Set the coefficient of 'y' in the objective. // The objective is now 5 * y engine.setObjectiveCoefficient('y', 5);
パラメータ
名前 | 型 | 説明 |
---|---|---|
variable | String | 係数を設定する変数の名前 |
coefficient | Number | 目的関数の変数の係数 |
戻る
Linear
- 線形最適化エンジン
solve()
デフォルトの期限 30 秒で現在の線形計画問題を解きます。見つかったソリューションを返します。
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()}`; } Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
戻る
Linear
- 最適化の解
solve(seconds)
現在の線形プログラムを解きます。見つかったソリューションと、それが最適なソリューションかどうかを返します。
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(300); if (!solution.isValid()) { throw `No solution ${solution.getStatus()}`; } Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
パラメータ
名前 | 型 | 説明 |
---|---|---|
seconds | Number | 問題を解決する期限(秒単位)。期限は最大 300 秒です。 |
戻る
Linear
- 最適化の解