線性規劃的解。以下範例會解決下列線性程式:
兩個變數,x 和 y:
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(`Objective value: ${solution.getObjectiveValue()}`); Logger.log(`Value of x: ${solution.getVariableValue('x')}`); Logger.log(`Value of y: ${solution.getVariableValue('y')}`); }
方法
| 方法 | 傳回類型 | 簡短說明 |
|---|---|---|
get | Number | 取得目前解決方案中目標函式的值。 |
get | Status | 取得解決方案的狀態。 |
get | Number | 取得上次呼叫 Linear 所建立解決方案中的變數值。 |
is | Boolean | 判斷解決方案是否可行或最佳。 |
內容詳盡的說明文件
getObjectiveValue()
取得目前解決方案中目標函式的值。
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(); Logger.log(`ObjectiveValue: ${solution.getObjectiveValue()}`);
回攻員
Number:目標函式的值
getStatus()
取得解決方案的狀態。解決問題前,狀態會顯示為「NOT_SOLVED」。
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(); const status = solution.getStatus(); if (status !== LinearOptimizationService.Status.FEASIBLE && status !== LinearOptimizationService.Status.OPTIMAL) { throw `No solution ${status}`; } Logger.log(`Status: ${status}`);
回攻員
Status:求解器的狀態
getVariableValue(variableName)
取得上次呼叫 LinearOptimizationEngine.solve() 所建立解決方案中的變數值。
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(); Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
variable | String | 變數名稱 |
回攻員
Number:解決方案中的變數值
isValid()
判斷解決方案是否可行或最佳。
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()}`; }
回攻員
Boolean:解決方案有效 (Status.FEASIBLE 或 Status.OPTIMAL);true:解決方案無效;false:解決方案未知