Class LinearOptimizationEngine

LinearOptimizationEngine

用來建立模型及解決線性程式模型的引擎。以下範例會解決以下線性程式:

兩個變數,xy
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'));
}

方法

方法傳回類型簡短說明
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:最佳化解決方案