Class LinearOptimizationEngine

Công cụtối ưu hoátuyến tính

Công cụ dùng để lập mô hình và giải một chương trình tuyến tính. Ví dụ dưới đây giải quyết chương trình tuyến tính sau:

Hai biến, xy:
0 ≤ x ≤ 10
0 ≤ y ≤ 5

Quy tắc ràng buộc:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20

Mục tiêu:
maximized 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')}`);
}

Phương thức

Phương thứcLoại dữ liệu trả vềMô tả ngắn
addConstraint(lowerBound, upperBound)LinearOptimizationConstraintThêm một quy tắc ràng buộc tuyến tính mới trong mô hình.
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)LinearOptimizationEngineThêm các quy tắc ràng buộc theo lô vào mô hình.
addVariable(name, lowerBound, upperBound)LinearOptimizationEngineThêm một biến liên tục mới vào mô hình.
addVariable(name, lowerBound, upperBound, type)LinearOptimizationEngineThêm một biến mới vào mô hình.
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)LinearOptimizationEngineThêm một biến mới vào mô hình.
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)LinearOptimizationEngineThêm các biến theo lô vào mô hình.
setMaximization()LinearOptimizationEngineĐặt hướng tối ưu hoá để tối đa hoá hàm mục tiêu tuyến tính.
setMinimization()LinearOptimizationEngineĐặt hướng tối ưu hoá để giảm thiểu hàm mục tiêu tuyến tính.
setObjectiveCoefficient(variableName, coefficient)LinearOptimizationEngineĐặt hệ số của một biến trong hàm mục tiêu tuyến tính.
solve()LinearOptimizationSolutionGiải chương trình tuyến tính hiện tại với thời hạn mặc định là 30 giây.
solve(seconds)LinearOptimizationSolutionGiải chương trình tuyến tính hiện tại.

Tài liệu chi tiết

addConstraint(lowerBound, upperBound)

Thêm một quy tắc ràng buộc tuyến tính mới trong mô hình. Giới hạn trên và giới hạn dưới của quy tắc ràng buộc được xác định tại thời điểm tạo. Hệ số cho các biến được xác định thông qua các lệnh gọi đến 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);

Tham số

TênLoạiMô tả
lowerBoundNumbergiới hạn dưới của quy tắc ràng buộc
upperBoundNumbergiới hạn trên của quy tắc ràng buộc

Cầu thủ trả bóng

LinearOptimizationConstraint – quy tắc ràng buộc được tạo


addConstraints(lowerBounds, upperBounds, variableNames, coefficients)

Thêm các quy tắc ràng buộc theo lô vào mô hình.

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

Tham số

TênLoạiMô tả
lowerBoundsNumber[]giới hạn dưới của các quy tắc ràng buộc
upperBoundsNumber[]giới hạn trên của các quy tắc ràng buộc
variableNamesString[][]tên của các biến mà hệ số đang được đặt
coefficientsNumber[][]hệ số đang được đặt

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


addVariable(name, lowerBound, upperBound)

Thêm một biến liên tục mới vào mô hình. Biến được tham chiếu theo tên của biến. Loại được đặt thành 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);

Tham số

TênLoạiMô tả
nameStringtên duy nhất của biến
lowerBoundNumbergiới hạn dưới của biến
upperBoundNumbergiới hạn trên của biến

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


addVariable(name, lowerBound, upperBound, type)

Thêm một biến mới vào mô hình. Biến được tham chiếu theo tên của biến.

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

Tham số

TênLoạiMô tả
nameStringtên duy nhất của biến
lowerBoundNumbergiới hạn dưới của biến
upperBoundNumbergiới hạn trên của biến
typeVariableTypeloại biến, có thể là một trong các VariableType

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


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

Thêm một biến mới vào mô hình. Biến được tham chiếu theo tên của biến.

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.

Tham số

TênLoạiMô tả
nameStringtên duy nhất của biến
lowerBoundNumbergiới hạn dưới của biến
upperBoundNumbergiới hạn trên của biến
typeVariableTypeloại biến, có thể là một trong các VariableType
objectiveCoefficientNumberhệ số mục tiêu của biến

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


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

Thêm các biến theo lô vào mô hình. Các biến được tham chiếu theo tên.

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

Tham số

TênLoạiMô tả
namesString[]tên riêng biệt của các biến
lowerBoundsNumber[]giới hạn dưới của các biến
upperBoundsNumber[]giới hạn trên của các biến
typesVariableType[]loại của biến, có thể là một trong các VariableType
objectiveCoefficientsNumber[]hệ số khách quan của các biến

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


setMaximization()

Đặt hướng tối ưu hoá để tối đa hoá hàm mục tiêu tuyến tính.

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

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


setMinimization()

Đặt hướng tối ưu hoá để giảm thiểu hàm mục tiêu tuyến tính.

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

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


setObjectiveCoefficient(variableName, coefficient)

Đặt hệ số của một biến trong hàm mục tiêu tuyến tính.

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

Tham số

TênLoạiMô tả
variableNameStringtên của biến mà hệ số đang được đặt
coefficientNumberhệ số của biến trong hàm mục tiêu

Cầu thủ trả bóng

LinearOptimizationEngine – công cụ tối ưu hoá tuyến tính


solve()

Giải chương trình tuyến tính hiện tại với thời hạn mặc định là 30 giây. Trả về giải pháp đã tìm thấy.

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')}`);

Cầu thủ trả bóng

LinearOptimizationSolution – giải pháp của quá trình tối ưu hoá


solve(seconds)

Giải chương trình tuyến tính hiện tại. Trả về giải pháp đã tìm thấy và liệu đó có phải là giải pháp tối ưu hay không.

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')}`);

Tham số

TênLoạiMô tả
secondsNumberthời hạn giải quyết vấn đề, tính bằng giây; thời hạn tối đa là 300 giây

Cầu thủ trả bóng

LinearOptimizationSolution – giải pháp của quá trình tối ưu hoá