Class LinearOptimizationEngine

線性最佳化引擎

用於模擬及解決線性程式的引擎。以下範例會解決下列線性程式:

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

方法

方法傳回類型簡短說明
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) 的呼叫來定義。

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

參數

名稱類型說明
lowerBoundNumber限制的下限
upperBoundNumber限制的上限

回攻員

LinearOptimizationConstraint:已建立的限制


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

參數

名稱類型說明
lowerBoundsNumber[]限制的下限
upperBoundsNumber[]限制的上限
variableNamesString[][]要設定係數的變數名稱
coefficientsNumber[][]設定的係數

回攻員

LinearOptimizationEngine - 線性最佳化引擎


addVariable(name, lowerBound, upperBound)

在模型中新增連續變數。變數會透過其名稱參照。類型設為 VariableType.CONTINUOUS

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

參數

名稱類型說明
nameString變數的專屬名稱
lowerBoundNumber變數的下限
upperBoundNumber變數的上限

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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

參數

名稱類型說明
nameString變數的專屬名稱
lowerBoundNumber變數的下限
upperBoundNumber變數的上限
typeVariableType變數類型,可以是 VariableType 的其中一個

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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.

參數

名稱類型說明
nameString變數的專屬名稱
lowerBoundNumber變數的下限
upperBoundNumber變數的上限
typeVariableType變數類型,可以是 VariableType 的其中一個
objectiveCoefficientNumber變數的目標係數

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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

參數

名稱類型說明
namesString[]變數的專屬名稱
lowerBoundsNumber[]變數的下限
upperBoundsNumber[]變數的上限
typesVariableType[]變數類型,可為 VariableType 的其中一種
objectiveCoefficientsNumber[]變數的目標係數

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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

參數

名稱類型說明
variableNameString要設定係數的變數名稱
coefficientNumber目標函式中變數的係數

回攻員

LinearOptimizationEngine - 線性最佳化引擎


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')}`);

回攻員

LinearOptimizationSolution - 最佳化解決方案


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')}`);

參數

名稱類型說明
secondsNumber解決問題的期限 (以秒為單位);期限上限為 300 秒

回攻員

LinearOptimizationSolution - 最佳化解決方案