선형 프로그램을 모델링하고 해결하는 데 사용되는 엔진입니다. 아래 예에서는 다음 선형 프로그램을 해결합니다.
두 변수 x
및 y
:
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')}`); }
메서드
메서드 | 반환 유형 | 간략한 설명 |
---|---|---|
add | Linear | 모델에 새 선형 제약조건을 추가합니다. |
add | Linear | 모델에 제약 조건을 일괄 추가합니다. |
add | Linear | 모델에 새 연속 변수를 추가합니다. |
add | Linear | 모델에 새 변수를 추가합니다. |
add | Linear | 모델에 새 변수를 추가합니다. |
add | Linear | 모델에 변수를 일괄 추가합니다. |
set | Linear | 선형 목표 함수를 극대화하도록 최적화 방향을 설정합니다. |
set | Linear | 선형 목적 함수를 최소화하도록 최적화 방향을 설정합니다. |
set | Linear | 선형 목적 함수에서 변수의 계수를 설정합니다. |
solve() | Linear | 기본 기한 30초로 현재 선형 프로그램을 풀고 |
solve(seconds) | Linear | 현재 선형 프로그램을 풀이합니다. |
자세한 문서
addConstraint(lowerBound, upperBound)
모델에 새 선형 제약조건을 추가합니다. 제약 조건의 상한과 하한은 생성 시 정의됩니다. 변수의 계수는 Linear
호출을 통해 정의됩니다.
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);
매개변수
이름 | 유형 | 설명 |
---|---|---|
lower | Number | 제약조건의 하한값 |
upper | Number | 제약조건의 상한값 |
리턴
Linear
: 생성된 제약조건
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], ], );
매개변수
이름 | 유형 | 설명 |
---|---|---|
lower | Number[] | 제약조건의 하한값 |
upper | Number[] | 제약조건의 상한값 |
variable | String[][] | 계수가 설정되는 변수의 이름 |
coefficients | Number[][] | 설정되는 계수 |
리턴
Linear
: 선형 최적화 엔진
addVariable(name, lowerBound, upperBound)
모델에 새 연속 변수를 추가합니다. 변수는 이름으로 참조됩니다. 유형은 Variable
로 설정됩니다.
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);
매개변수
이름 | 유형 | 설명 |
---|---|---|
name | String | 변수의 고유한 이름 |
lower | Number | 변수의 하한값 |
upper | Number | 변수의 상한값 |
리턴
Linear
: 선형 최적화 엔진
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, );
매개변수
이름 | 유형 | 설명 |
---|---|---|
name | String | 변수의 고유한 이름 |
lower | Number | 변수의 하한값 |
upper | Number | 변수의 상한값 |
type | Variable | 변수 유형이며 Variable 중 하나일 수 있습니다. |
리턴
Linear
: 선형 최적화 엔진
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.
매개변수
이름 | 유형 | 설명 |
---|---|---|
name | String | 변수의 고유한 이름 |
lower | Number | 변수의 하한값 |
upper | Number | 변수의 상한값 |
type | Variable | 변수 유형이며 Variable 중 하나일 수 있습니다. |
objective | Number | 변수의 목적 계수 |
리턴
Linear
: 선형 최적화 엔진
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, ], );
매개변수
이름 | 유형 | 설명 |
---|---|---|
names | String[] | 고유한 변수 이름 |
lower | Number[] | 변수의 하한 |
upper | Number[] | 변수의 상한값 |
types | Variable | 변수 유형이며 Variable 중 하나일 수 있습니다. |
objective | Number[] | 변수의 목적론적 계수 |
리턴
Linear
: 선형 최적화 엔진
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();
리턴
Linear
: 선형 최적화 엔진
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();
리턴
Linear
: 선형 최적화 엔진
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);
매개변수
이름 | 유형 | 설명 |
---|---|---|
variable | String | 계수가 설정되는 변수의 이름 |
coefficient | Number | 목적 함수의 변수 계수 |
리턴
Linear
: 선형 최적화 엔진
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')}`);
리턴
Linear
: 최적화의 솔루션
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')}`);
매개변수
이름 | 유형 | 설명 |
---|---|---|
seconds | Number | 문제 해결 기한(초)입니다. 최대 기한은 300초입니다. |
리턴
Linear
: 최적화의 솔루션