Class LinearOptimizationSolution

LinearOptimizationSolution

Es la solución de un programa lineal. El siguiente ejemplo resuelve el siguiente programa lineal:

Dos variables, x y y:
0 ≤ x ≤ 10
0 ≤ y ≤ 5

Restricciones:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20

Objetivo:
Maximizar 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'));
}

Métodos

MétodoTipo de datos que se muestraDescripción breve
getObjectiveValue()NumberObtiene el valor de la función objetivo en la solución actual.
getStatus()StatusObtiene el estado de la solución.
getVariableValue(variableName)NumberObtiene el valor de una variable en la solución que se creó en la última llamada a LinearOptimizationEngine.solve().
isValid()BooleanDetermina si la solución es óptima u factible.

Documentación detallada

getObjectiveValue()

Obtiene el valor de la función objetivo en la solución actual.

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());

Devolvedor

Number: Es el valor de la función objetivo.


getStatus()

Obtiene el estado de la solución. Antes de resolver un problema, el estado será 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());

Devolvedor

Status: Es el estado de la resolución.


getVariableValue(variableName)

Obtiene el valor de una variable en la solución que se creó en la última llamada a 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'));

Parámetros

NombreTipoDescripción
variableNameStringnombre de la variable

Devolvedor

Number: El valor de la variable en la solución


isValid()

Determina si la solución es óptima u factible.

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;
}

Devolvedor

Boolean: true si la solución es válida (Status.FEASIBLE o Status.OPTIMAL); false si no es así