선형 프로그램의 해. 아래 예에서는 다음 선형 프로그램을 해결합니다.
두 변수 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)
Linear
를 마지막으로 호출하여 만든 솔루션의 변수 값을 가져옵니다.
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
— 해결 방법이 유효한 경우 (Status.FEASIBLE
또는 Status.OPTIMAL
) true
, 그렇지 않은 경우 false