线性规划的解。以下示例会解题以下线性规划:
两个变量 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