선형 프로그램의 솔루션입니다. 아래 예에서는 다음 선형 프로그램을 해결합니다.
두 변수 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(`Objective value: ${solution.getObjectiveValue()}`); Logger.log(`Value of x: ${solution.getVariableValue('x')}`); Logger.log(`Value of y: ${solution.getVariableValue('y')}`); }
메서드
| 메서드 | 반환 유형 | 간략한 설명 |
|---|---|---|
get | Number | 현재 솔루션에서 목표 함수의 값을 가져옵니다. |
get | Status | 솔루션의 상태를 가져옵니다. |
get | Number | Linear에 대한 마지막 호출로 생성된 솔루션에서 변수의 값을 가져옵니다. |
is | Boolean | 솔루션이 실행 가능하거나 최적인지 확인합니다. |
자세한 문서
getObjectiveValue()
현재 솔루션에서 목표 함수의 값을 가져옵니다.
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(); Logger.log(`ObjectiveValue: ${solution.getObjectiveValue()}`);
리턴
Number — 목표 함수의 값
getStatus()
솔루션의 상태를 가져옵니다. 문제를 해결하기 전에는 상태가 NOT_SOLVED입니다.
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(); const status = solution.getStatus(); if (status !== LinearOptimizationService.Status.FEASIBLE && status !== LinearOptimizationService.Status.OPTIMAL) { throw `No solution ${status}`; } Logger.log(`Status: ${status}`);
리턴
Status — 솔버의 상태
getVariableValue(variableName)
LinearOptimizationEngine.solve()에 대한 마지막 호출로 생성된 솔루션에서 변수의 값을 가져옵니다.
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(); Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
매개변수
| 이름 | 유형 | 설명 |
|---|---|---|
variable | String | 변수의 이름 |
리턴
Number — 솔루션에서 변수의 값
isValid()
솔루션이 실행 가능하거나 최적인지 확인합니다.
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()}`; }
리턴
Boolean — 솔루션이 유효한 경우 true (Status.FEASIBLE 또는
Status.OPTIMAL), 그렇지 않은 경우 false