Công cụ dùng để mô hình hoá 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, x và y:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Các ràng buộc:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Mục tiêu:
Tối đa hoá 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
Tài liệu chi tiết
addConstraint(lowerBound, upperBound)
Thêm một ràng buộc tuyến tính mới vào mô hình. Giới hạn trên và giới hạn dưới của điều kiện ràng buộc được xác định tại thời điểm tạo. Các 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ên | Loại | Mô tả |
|---|---|---|
lower | Number | giới hạn dưới của ràng buộc |
upper | Number | giới hạn trên của điều kiện ràng buộc |
Cầu thủ trả bóng
LinearOptimizationConstraint – ràng buộ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ên | Loại | Mô tả |
|---|---|---|
lower | Number[] | giới hạn dưới của các ràng buộc |
upper | Number[] | cận trên của các ràng buộc |
variable | String[][] | tên của các biến mà hệ số đang được đặt |
coefficients | Number[][] | các hệ số đang được đặt |
Cầu thủ trả bóng
LinearOptimizationEngine – một 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. 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ên | Loại | Mô tả |
|---|---|---|
name | String | tên duy nhất của biến |
lower | Number | giới hạn dưới của biến |
upper | Number | cận trên của biến |
Cầu thủ trả bóng
LinearOptimizationEngine – một 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.
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ên | Loại | Mô tả |
|---|---|---|
name | String | tên duy nhất của biến |
lower | Number | giới hạn dưới của biến |
upper | Number | cận trên của biến |
type | Variable | loại biến, có thể là một trong các Variable |
Cầu thủ trả bóng
LinearOptimizationEngine – một 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.
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ên | Loại | Mô tả |
|---|---|---|
name | String | tên duy nhất của biến |
lower | Number | giới hạn dưới của biến |
upper | Number | cận trên của biến |
type | Variable | loại biến, có thể là một trong các Variable |
objective | Number | hệ số mục tiêu của biến |
Cầu thủ trả bóng
LinearOptimizationEngine – một 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ên | Loại | Mô tả |
|---|---|---|
names | String[] | tên riêng biệt của các biến |
lower | Number[] | giới hạn dưới của các biến |
upper | Number[] | cận trên của các biến |
types | Variable | các loại biến, có thể là một trong Variable |
objective | Number[] | hệ số mục tiêu của các biến |
Cầu thủ trả bóng
LinearOptimizationEngine – một công cụ tối ưu hoá tuyến tính
setMaximization()
Đặt hướng tối ưu hoá thành 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 – một 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 – một 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ên | Loại | Mô tả |
|---|---|---|
variable | String | tên của biến mà hệ số đang được đặt |
coefficient | Number | hệ số của biến trong hàm mục tiêu |
Cầu thủ trả bóng
LinearOptimizationEngine – một 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 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 được 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ên | Loại | Mô tả |
|---|---|---|
seconds | Number | thờ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 tối ưu hoá