用于存储形如 lowerBound ≤ Sum(a(i) x(i)) ≤ upperBound 的线性约束的对象,其中 lowerBound 和 upperBound 是常量,a(i) 是常数系数,x(i) 是变量(未知数)。
以下示例创建了一个变量 x,其值介于 0 和 5 之间,并创建了约束 0 ≤ 2 * x ≤ 5。为此,请先创建一个下限为 5、上限为 5 的约束。然后,此约束中变量 x 的系数设置为 2。
const engine = LinearOptimizationService.createEngine(); // Create a variable so we can add it to the constraint engine.addVariable('x', 0, 5); // Create a linear constraint with the bounds 0 and 10 const constraint = engine.addConstraint(0, 10); // Set the coefficient of the variable in the constraint. The constraint is now: // 0 <= 2 * x <= 5 constraint.setCoefficient('x', 2);
方法
| 方法 | 返回类型 | 简介 |
|---|---|---|
set | Linear | 设置约束中变量的系数。 |
详细文档
setCoefficient(variableName, coefficient)
设置约束中变量的系数。默认情况下,变量的系数为 0。
const engine = LinearOptimizationService.createEngine(); // Create a linear constraint with the bounds 0 and 10 const constraint = engine.addConstraint(0, 10); // Create a variable so we can add it to the constraint engine.addVariable('x', 0, 5); // Set the coefficient of the variable in the constraint. The constraint is now: // 0 <= 2 * x <= 5 constraint.setCoefficient('x', 2);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
variable | String | 要设置系数的变量的名称 |
coefficient | Number | 正在设置的系数 |
返回
LinearOptimizationConstraint - 这种线性优化约束