線性程式的解。以下範例會解決下列線性程式:
兩個變數 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 | 判斷解決方案是否可行或最佳化。 |
內容詳盡的說明文件
get Objective Value()
取得目前解決方案中的目標函式值。
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
:目標函式的值
get Status()
取得解決方案的狀態。在解決問題之前,狀態會是 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
:解算器的狀態
get Variable Value(variableName)
取得上次呼叫 Linear
時所建立的解決方案中變數的值。
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
:解決方案中的變數值
is Valid()
判斷解決方案是否可行或最佳化。
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