Class LinearOptimizationSolution

LinearOptimizationSolution

線性程式的解決方案。以下範例解析以下線性程式:

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('Objective  value: ' + solution.getObjectiveValue());
  Logger.log('Value of x: ' + solution.getVariableValue('x'));
  Logger.log('Value of y: ' + solution.getVariableValue('y'));
}

方法

方法傳回類型簡短說明
getObjectiveValue()Number取得目前解決方案中目標函式的價值。
getStatus()Status取得解決方案的狀態。
getVariableValue(variableName)Number取得上次呼叫 LinearOptimizationEngine.solve() 所建立的解決方案中的變數值。
isValid()Boolean判斷解決方案是可行還是最佳。

內容詳盡的說明文件

getObjectiveValue()

取得目前解決方案中目標函式的價值。

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();
Logger.log('ObjectiveValue: ' + solution.getObjectiveValue());

回攻員

Number:目標函式的價值


getStatus()

取得解決方案的狀態。解決問題前,狀態會是 NOT_SOLVED

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.getStatus() != LinearOptimizationService.Status.FEASIBLE &&
    solution.getStatus() != LinearOptimizationService.Status.OPTIMAL) {
  throw 'No solution ' + status;
}
Logger.log('Status: ' + solution.getStatus());

回攻員

Status:解題工具的狀態


getVariableValue(variableName)

取得上次呼叫 LinearOptimizationEngine.solve() 所建立的解決方案中的變數值。

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();
Logger.log('Value of x: ' + solution.getVariableValue('x'));

參數

名稱類型說明
variableNameString變數名稱

回攻員

Number:解決方案中的變數值


isValid()

判斷解決方案是可行還是最佳。

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 ' + status;
}

回攻員

Boolean - 如果解決方案有效,則為 true (Status.FEASIBLEStatus.OPTIMAL);如果不需要,則設為 false