Class LinearOptimizationService

LinearOptimizationService

線性最佳化服務,用於模擬及解線性與混合整數線性 計畫。以下範例解析以下線性程式:

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

屬性

屬性類型說明
StatusStatus解題工具狀態。
VariableTypeVariableType解題工具建立的變數類型。

方法

方法傳回類型簡短說明
createEngine()LinearOptimizationEngine建立引擎,解決線性程式 (可能為整數程式)。

內容詳盡的說明文件

createEngine()

建立引擎,解決線性程式 (可能為整數程式)。

// Creates a linear optimization engine.
var engine = LinearOptimizationService.createEngine();
engine.addVariable('x', 0, 10);

// ...

回攻員

LinearOptimizationEngine:線性最佳化引擎