선형 프로그램의 해법입니다. 아래 예는 다음과 같은 선형 프로그램을 해결합니다.
두 개의 변수, x
및 y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
제약조건:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
목표:
x + y
최대화
var 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 var constraint = engine.addConstraint(0, 10); constraint.setCoefficient('x', 2); constraint.setCoefficient('y', 5); // Create the constraint: 0 <= 10 * x + 3 * y <= 20 var 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 var 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')); }
방법
메서드 | 반환 유형 | 간략한 설명 |
---|---|---|
getObjectiveValue() | Number | 현재 해의 목표 함수의 값을 가져옵니다. |
getStatus() | Status | 솔루션의 상태를 가져옵니다. |
getVariableValue(variableName) | Number | 솔루션의 마지막 LinearOptimizationEngine.solve() 호출로 생성된 변수 값을 가져옵니다. |
isValid() | Boolean | 솔루션이 실행 가능한지, 아니면 최적의 솔루션인지 판단합니다. |
자세한 문서
getObjectiveValue()
현재 해의 목표 함수의 값을 가져옵니다.
var engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program var solution = engine.solve(); Logger.log('ObjectiveValue: ' + solution.getObjectiveValue());
리턴
Number
: 목표 함수의 값
getStatus()
솔루션의 상태를 가져옵니다. 문제를 풀기 전에는 상태가 NOT_SOLVED
입니다.
var engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program var solution = engine.solve(); if (solution.getStatus() != LinearOptimizationService.Status.FEASIBLE && solution.getStatus() != LinearOptimizationService.Status.OPTIMAL) { throw 'No solution ' + status; } Logger.log('Status: ' + solution.getStatus());
리턴
Status
: 솔버의 상태입니다.
getVariableValue(variableName)
솔루션의 마지막 LinearOptimizationEngine.solve()
호출로 생성된 변수 값을 가져옵니다.
var engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program var solution = engine.solve(); Logger.log('Value of x: ' + solution.getVariableValue('x'));
매개변수
이름 | 유형 | 설명 |
---|---|---|
variableName | String | 변수 이름 |
리턴
Number
- 해의 변수 값입니다.
isValid()
솔루션이 실행 가능한지, 아니면 최적의 솔루션인지 판단합니다.
var engine = LinearOptimizationService.createEngine(); // Add variables, constraints and define the objective with addVariable(), addConstraint(), etc engine.addVariable('x', 0, 10); // ... // Solve the linear program var solution = engine.solve(); if (!solution.isValid()) { throw 'No solution ' + status; }
리턴
Boolean
- 솔루션이 유효하면 true
(Status.FEASIBLE
또는 Status.OPTIMAL
), 유효하지 않으면 false
입니다.