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 表示無效