Class LinearOptimizationEngine

线性优化引擎

用于对线性规划进行建模和求解的引擎。以下示例会解析以下线性规划:

两个变量 xy
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(`Value of x: ${solution.getVariableValue('x')}`);
  Logger.log(`Value of y: ${solution.getVariableValue('y')}`);
}

方法

方法返回类型简介
addConstraint(lowerBound, upperBound)LinearOptimizationConstraint在模型中添加新的线性约束条件。
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)LinearOptimizationEngine向模型批量添加约束条件。
addVariable(name, lowerBound, upperBound)LinearOptimizationEngine向模型添加新的连续变量。
addVariable(name, lowerBound, upperBound, type)LinearOptimizationEngine向模型添加新变量。
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)LinearOptimizationEngine向模型添加新变量。
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)LinearOptimizationEngine将变量批量添加到模型。
setMaximization()LinearOptimizationEngine将优化方向设置为最大限度地提高线性目标函数的值。
setMinimization()LinearOptimizationEngine将优化方向设置为使线性目标函数最小化。
setObjectiveCoefficient(variableName, coefficient)LinearOptimizationEngine设置线性目标函数中变量的系数。
solve()LinearOptimizationSolution使用默认截止期限 30 秒求解当前线性规划问题。
solve(seconds)LinearOptimizationSolution求解当前线性规划。

详细文档

addConstraint(lowerBound, upperBound)

在模型中添加新的线性约束条件。约束条件的上限和下限是在创建时定义的。变量的系数是通过调用 LinearOptimizationConstraint.setCoefficient(variableName, coefficient) 定义的。

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);

参数

名称类型说明
lowerBoundNumber约束条件的下限
upperBoundNumber约束条件的上限

返回

LinearOptimizationConstraint - 创建的约束条件


addConstraints(lowerBounds, upperBounds, variableNames, coefficients)

向模型批量添加约束条件。

const engine = LinearOptimizationService.createEngine();

// Add a boolean variable 'x' (integer >= 0 and <= 1) and a real (continuous >=
// 0 and <= 100) variable 'y'.
engine.addVariables(
    ['x', 'y'],
    [0, 0],
    [1, 100],
    [
      LinearOptimizationService.VariableType.INTEGER,
      LinearOptimizationService.VariableType.CONTINUOUS,
    ],
);

// Adds two constraints:
//   0 <= x + y <= 3
//   1 <= 10 * x - y <= 5
engine.addConstraints(
    [0.0, 1.0],
    [3.0, 5.0],
    [
      ['x', 'y'],
      ['x', 'y'],
    ],
    [
      [1, 1],
      [10, -1],
    ],
);

参数

名称类型说明
lowerBoundsNumber[]约束条件的下限
upperBoundsNumber[]约束条件的上限
variableNamesString[][]要设置系数的变量的名称
coefficientsNumber[][]要设置的系数

返回

LinearOptimizationEngine - 线性优化引擎


addVariable(name, lowerBound, upperBound)

向模型添加新的连续变量。系统会通过变量名称引用该变量。类型设置为 VariableType.CONTINUOUS

const engine = LinearOptimizationService.createEngine();
const constraint = engine.addConstraint(0, 10);

// Add a boolean variable (integer >= 0 and <= 1)
engine.addVariable('x', 0, 1, LinearOptimizationService.VariableType.INTEGER);

// Add a real (continuous) variable. Notice the lack of type specification.
engine.addVariable('y', 0, 100);

参数

名称类型说明
nameString变量的唯一名称
lowerBoundNumber变量的下限
upperBoundNumber变量的上限

返回

LinearOptimizationEngine - 线性优化引擎


addVariable(name, lowerBound, upperBound, type)

向模型添加新变量。系统会通过变量名称引用该变量。

const engine = LinearOptimizationService.createEngine();
const constraint = engine.addConstraint(0, 10);

// Add a boolean variable (integer >= 0 and <= 1)
engine.addVariable('x', 0, 1, LinearOptimizationService.VariableType.INTEGER);

// Add a real (continuous) variable
engine.addVariable(
    'y',
    0,
    100,
    LinearOptimizationService.VariableType.CONTINUOUS,
);

参数

名称类型说明
nameString变量的唯一名称
lowerBoundNumber变量的下限
upperBoundNumber变量的上限
typeVariableType变量类型,可以是 VariableType 之一

返回

LinearOptimizationEngine - 线性优化引擎


addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)

向模型添加新变量。系统会通过变量名称引用该变量。

const engine = LinearOptimizationService.createEngine();
const constraint = engine.addConstraint(0, 10);

// Add a boolean variable (integer >= 0 and <= 1)
engine.addVariable(
    'x',
    0,
    1,
    LinearOptimizationService.VariableType.INTEGER,
    2,
);
// The objective is now 2 * x.

// Add a real (continuous) variable
engine.addVariable(
    'y',
    0,
    100,
    LinearOptimizationService.VariableType.CONTINUOUS,
    -5,
);
// The objective is now 2 * x - 5 * y.

参数

名称类型说明
nameString变量的唯一名称
lowerBoundNumber变量的下限
upperBoundNumber变量的上限
typeVariableType变量类型,可以是 VariableType 之一
objectiveCoefficientNumber变量的目标系数

返回

LinearOptimizationEngine - 线性优化引擎


addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)

将变量批量添加到模型。系统会按名称引用变量。

const engine = LinearOptimizationService.createEngine();

// Add a boolean variable 'x' (integer >= 0 and <= 1) and a real (continuous >=0
// and <= 100) variable 'y'.
engine.addVariables(
    ['x', 'y'],
    [0, 0],
    [1, 100],
    [
      LinearOptimizationService.VariableType.INTEGER,
      LinearOptimizationService.VariableType.CONTINUOUS,
    ],
);

参数

名称类型说明
namesString[]变量的唯一名称
lowerBoundsNumber[]变量的下限
upperBoundsNumber[]变量的上限
typesVariableType[]变量的类型,可以是 VariableType 之一
objectiveCoefficientsNumber[]变量的目标系数

返回

LinearOptimizationEngine - 线性优化引擎


setMaximization()

将优化方向设置为最大限度地提高线性目标函数的值。

const engine = LinearOptimizationService.createEngine();

// Add a real (continuous) variable. Notice the lack of type specification.
engine.addVariable('y', 0, 100);

// Set the coefficient of 'y' in the objective.
// The objective is now 5 * y
engine.setObjectiveCoefficient('y', 5);

// We want to maximize.
engine.setMaximization();

返回

LinearOptimizationEngine - 线性优化引擎


setMinimization()

将优化方向设置为使线性目标函数最小化。

const engine = LinearOptimizationService.createEngine();

// Add a real (continuous) variable. Notice the lack of type specification.
engine.addVariable('y', 0, 100);

// Set the coefficient of 'y' in the objective.
// The objective is now 5 * y
engine.setObjectiveCoefficient('y', 5);

// We want to minimize
engine.setMinimization();

返回

LinearOptimizationEngine - 线性优化引擎


setObjectiveCoefficient(variableName, coefficient)

设置线性目标函数中变量的系数。

const engine = LinearOptimizationService.createEngine();

// Add a real (continuous) variable. Notice the lack of type specification.
engine.addVariable('y', 0, 100);

// Set the coefficient of 'y' in the objective.
// The objective is now 5 * y
engine.setObjectiveCoefficient('y', 5);

参数

名称类型说明
variableNameString要设置系数的变量的名称
coefficientNumber目标函数中变量的系数

返回

LinearOptimizationEngine - 线性优化引擎


solve()

求解当前线性规划问题,默认截止期限为 30 秒。返回找到的解决方案。

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()}`;
}
Logger.log(`Value of x: ${solution.getVariableValue('x')}`);

返回

LinearOptimizationSolution - 优化解


solve(seconds)

求解当前线性规划。返回找到的解决方案,以及它是否为最佳解决方案。

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(300);
if (!solution.isValid()) {
  throw `No solution ${solution.getStatus()}`;
}
Logger.log(`Value of x: ${solution.getVariableValue('x')}`);

参数

名称类型说明
secondsNumber解决问题的截止时间(以秒为单位);截止时间上限为 300 秒

返回

LinearOptimizationSolution - 优化解