Class LinearOptimizationEngine

LinearOptimizationEngine

線形プログラムのモデル化と解法に使用されるエンジン。以下の例は、次の線形プログラムを解決します。

xy の 2 つの変数:
0 ≤ x ≤ 10
0 ≤ y ≤ 5

制約:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20

目標:
x + yの最大化

var 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
var constraint = engine.addConstraint(0, 10);
constraint.setCoefficient('x', 2);
constraint.setCoefficient('y', 5);

// Create the constraint: 0 <= 10 * x + 3 * y <= 20
var 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
var 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'));
}

Methods

メソッド戻り値の型概要
addConstraint(lowerBound, upperBound)LinearOptimizationConstraintモデルに新しい線形制約を追加します。
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)LinearOptimizationEngineモデルに制約を一括して追加します。
addVariable(name, lowerBound, upperBound)LinearOptimizationEngine新しい連続変数をモデルに追加します。
addVariable(name, lowerBound, upperBound, type)LinearOptimizationEngine新しい変数をモデルに追加します。
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)LinearOptimizationEngine新しい変数をモデルに追加します。
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)LinearOptimizationEngine変数をバッチでモデルに追加します。
setMaximization()LinearOptimizationEngine線形目的関数を最大化するように最適化の方向を設定します。
setMinimization()LinearOptimizationEngine線形目的関数を最小限に抑えるよう最適化の方向を設定します。
setObjectiveCoefficient(variableName, coefficient)LinearOptimizationEngine線形目的関数の変数の係数を設定します。
solve()LinearOptimizationSolutionデフォルトの期限が 30 秒である現在の線形プログラムを解きます。
solve(seconds)LinearOptimizationSolution現在の線形プログラムを解きます。

詳細なドキュメント

addConstraint(lowerBound, upperBound)

モデルに新しい線形制約を追加します。制約の上限と下限は、作成時に定義されます。変数の係数は、LinearOptimizationConstraint.setCoefficient(variableName, coefficient) の呼び出しで定義します。

var engine = LinearOptimizationService.createEngine();

// Create a linear constraint with the bounds 0 and 10
var 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);

パラメータ

名前説明
lowerBoundNumber制約の下限
upperBoundNumber制約の上限

リターン

LinearOptimizationConstraint - 作成された制約


addConstraints(lowerBounds, upperBounds, variableNames, coefficients)

モデルに制約を一括して追加します。

var 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]]);

パラメータ

名前説明
lowerBoundsNumber[]制約の下限
upperBoundsNumber[]制約の上限
variableNamesString[][]係数を設定する変数の名前
coefficientsNumber[][]係数を設定します。

リターン

LinearOptimizationEngine - 線形最適化エンジン


addVariable(name, lowerBound, upperBound)

新しい連続変数をモデルに追加します。変数は名前で参照されます。タイプは VariableType.CONTINUOUS に設定されます。

var engine = LinearOptimizationService.createEngine();
var 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);

パラメータ

名前説明
nameString一意の変数名
lowerBoundNumber変数の下限
upperBoundNumber変数の上限

リターン

LinearOptimizationEngine - 線形最適化エンジン


addVariable(name, lowerBound, upperBound, type)

新しい変数をモデルに追加します。変数は名前で参照されます。

var engine = LinearOptimizationService.createEngine();
var 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);

パラメータ

名前説明
nameString一意の変数名
lowerBoundNumber変数の下限
upperBoundNumber変数の上限
typeVariableType変数の型。VariableType のいずれかになります。

リターン

LinearOptimizationEngine - 線形最適化エンジン


addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)

新しい変数をモデルに追加します。変数は名前で参照されます。

var engine = LinearOptimizationService.createEngine();
var 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.

パラメータ

名前説明
nameString一意の変数名
lowerBoundNumber変数の下限
upperBoundNumber変数の上限
typeVariableType変数の型。VariableType のいずれかになります。
objectiveCoefficientNumber変数の客観的係数

リターン

LinearOptimizationEngine - 線形最適化エンジン


addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)

変数をバッチでモデルに追加します。変数は名前で参照されます。

var 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]);

パラメータ

名前説明
namesString[]固有の変数名
lowerBoundsNumber[]変数の下限
upperBoundsNumber[]変数の上限
typesVariableType[]変数の型。VariableType のいずれかです。
objectiveCoefficientsNumber[]変数の客観的係数

リターン

LinearOptimizationEngine - 線形最適化エンジン


setMaximization()

線形目的関数を最大化するように最適化の方向を設定します。

var 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();

リターン

LinearOptimizationEngine - 線形最適化エンジン


setMinimization()

線形目的関数を最小限に抑えるよう最適化の方向を設定します。

var 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();

リターン

LinearOptimizationEngine - 線形最適化エンジン


setObjectiveCoefficient(variableName, coefficient)

線形目的関数の変数の係数を設定します。

var 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);

パラメータ

名前説明
variableNameString係数を設定する変数の名前
coefficientNumber目的関数の変数の係数

リターン

LinearOptimizationEngine - 線形最適化エンジン


solve()

デフォルトの期限が 30 秒である現在の線形プログラムを解きます。見つかった解答を返します。

var engine = LinearOptimizationService.createEngine();

// Add variables, constraints and define the objective with addVariable(), addConstraint(), etc
engine.addVariable('x', 0, 10);

// ...

// Solve the linear program
var solution = engine.solve();
if (!solution.isValid()) {
  throw 'No solution ' + solution.getStatus();
}
Logger.log('Value of x: ' + solution.getVariableValue('x'));

リターン

LinearOptimizationSolution - 最適化の解答


solve(seconds)

現在の線形プログラムを解きます。見つかった解決策と、それが最適な解決策かどうかを返します。

var engine = LinearOptimizationService.createEngine();

// Add variables, constraints and define the objective with addVariable(), addConstraint(), etc
engine.addVariable('x', 0, 10);

// ...

// Solve the linear program
var solution = engine.solve(300);
if (!solution.isValid()) {
  throw 'No solution ' + solution.getStatus();
}
Logger.log('Value of x: ' + solution.getVariableValue('x'));

パラメータ

名前説明
secondsNumber問題解決の期限(秒単位)。最大期限は 300 秒です。

リターン

LinearOptimizationSolution - 最適化の解答