用于存储形式为 lower 的线性约束条件的对象,其中 lower 和 upper 是常量,a(i) 是常量系数,x(i) 是变量(未知数)。
以下示例创建一个值介于 0 和 5 之间的变量 x,并创建约束条件 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 | 设置约束条件中变量的系数。 |
详细文档
set Coefficient(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 | 要设置的系数 |
返回
Linear - 此线性优化约束条件