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 - 最佳化解決方案