El motor que se usa para modelar y resolver un programa lineal. En el siguiente ejemplo, se 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('Value of x: ' + solution.getVariableValue('x')); Logger.log('Value of y: ' + solution.getVariableValue('y')); }
Métodos
Documentación detallada
addConstraint(lowerBound, upperBound)
Agrega una nueva restricción lineal al modelo. Los límites inferior y superior de la restricción se definen en el momento de la creación. Los coeficientes de las variables se definen a través de llamadas a LinearOptimizationConstraint.setCoefficient(variableName, coefficient)
.
var engine = LinearOptimizationService.createEngine(); // Create a linear constraint with the bounds 0 and 10 var 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);
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
lowerBound | Number | límite inferior de la restricción |
upperBound | Number | límite superior de la restricción |
Devolvedor
LinearOptimizationConstraint
: Es la restricción creada.
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
Agrega restricciones por lotes al modelo.
var 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]]);
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
lowerBounds | Number[] | límites inferiores de las restricciones |
upperBounds | Number[] | los límites superiores de las restricciones |
variableNames | String[][] | los nombres de las variables para las que se establecen los coeficientes |
coefficients | Number[][] | coeficientes que se establecen |
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
addVariable(name, lowerBound, upperBound)
Agrega una nueva variable continua al modelo. Se hace referencia a la variable por su nombre. Se establece el tipo en VariableType.CONTINUOUS
.
var engine = LinearOptimizationService.createEngine(); var 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);
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
name | String | nombre único de la variable |
lowerBound | Number | límite inferior de la variable |
upperBound | Number | límite superior de la variable |
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
addVariable(name, lowerBound, upperBound, type)
Agrega una variable nueva al modelo. Se hace referencia a la variable por su nombre.
var engine = LinearOptimizationService.createEngine(); var 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);
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
name | String | nombre único de la variable |
lowerBound | Number | límite inferior de la variable |
upperBound | Number | límite superior de la variable |
type | VariableType | de la variable, puede ser uno de los siguientes: VariableType |
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
Agrega una variable nueva al modelo. Se hace referencia a la variable por su nombre.
var engine = LinearOptimizationService.createEngine(); var 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.
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
name | String | nombre único de la variable |
lowerBound | Number | límite inferior de la variable |
upperBound | Number | límite superior de la variable |
type | VariableType | de la variable, puede ser uno de los siguientes: VariableType |
objectiveCoefficient | Number | coeficiente objetivo de la variable |
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
Agrega variables por lotes al modelo. Se hace referencia a las variables por sus nombres.
var 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]);
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
names | String[] | nombres únicos de las variables |
lowerBounds | Number[] | límites inferiores de las variables |
upperBounds | Number[] | los límites superiores de las variables |
types | VariableType[] | tipos de las variables puede ser uno de los siguientes: VariableType |
objectiveCoefficients | Number[] | los coeficientes objetivos de las variables |
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
setMaximization()
Establece la dirección de optimización para maximizar la función objetivo lineal.
var 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();
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
setMinimization()
Establece la dirección de optimización para minimizar la función objetivo lineal.
var 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();
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
setObjectiveCoefficient(variableName, coefficient)
Establece el coeficiente de una variable en la función objetivo lineal.
var 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);
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
variableName | String | nombre de la variable para la que se establece el coeficiente |
coefficient | Number | coeficiente de la variable en la función objetiva |
Devolvedor
LinearOptimizationEngine
: Un motor de optimización lineal
solve()
Resuelve el programa lineal actual con el plazo predeterminado de 30 segundos. Muestra la solución encontrada.
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 ' + solution.getStatus(); } Logger.log('Value of x: ' + solution.getVariableValue('x'));
Devolvedor
LinearOptimizationSolution
: Es la solución de optimización.
solve(seconds)
Resuelve el programa lineal actual. Muestra la solución encontrada y si es una solución óptima.
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(300); if (!solution.isValid()) { throw 'No solution ' + solution.getStatus(); } Logger.log('Value of x: ' + solution.getVariableValue('x'));
Parámetros
Nombre | Tipo | Descripción |
---|---|---|
seconds | Number | plazo para resolver el problema, en segundos; el plazo máximo es 300 segundos |
Devolvedor
LinearOptimizationSolution
: Es la solución de optimización.