O mecanismo usado para modelar e resolver um programa linear. O exemplo abaixo resolve o seguinte programa linear:
Duas variáveis, x
e y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Restrições:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Objetivo:
Maximizar 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(`Value of x: ${solution.getVariableValue('x')}`); Logger.log(`Value of y: ${solution.getVariableValue('y')}`); }
Métodos
Documentação detalhada
addConstraint(lowerBound, upperBound)
Adiciona uma nova restrição linear ao modelo. Os limites superior e inferior da restrição são
definidos no momento da criação. Os coeficientes das variáveis são definidos por chamadas para Linear
.
const engine = LinearOptimizationService.createEngine(); // Create a linear constraint with the bounds 0 and 10 const 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
Nome | Tipo | Descrição |
---|---|---|
lower | Number | limite inferior da restrição |
upper | Number | limite superior da restrição |
Retornar
Linear
: a restrição criada
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
Adiciona restrições em lote ao modelo.
const 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
Nome | Tipo | Descrição |
---|---|---|
lower | Number[] | limites inferiores das restrições |
upper | Number[] | limites superiores das restrições |
variable | String[][] | os nomes das variáveis para as quais os coeficientes estão sendo definidos |
coefficients | Number[][] | coeficientes sendo definidos |
Retornar
Linear
: um mecanismo de otimização linear
addVariable(name, lowerBound, upperBound)
Adiciona uma nova variável contínua ao modelo. A variável é referenciada pelo nome. O tipo
é definido como Variable
.
const engine = LinearOptimizationService.createEngine(); const 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
Nome | Tipo | Descrição |
---|---|---|
name | String | nome exclusivo da variável |
lower | Number | limite inferior da variável |
upper | Number | limite superior da variável |
Retornar
Linear
: um mecanismo de otimização linear
addVariable(name, lowerBound, upperBound, type)
Adiciona uma nova variável ao modelo. A variável é referenciada pelo nome.
const engine = LinearOptimizationService.createEngine(); const 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
Nome | Tipo | Descrição |
---|---|---|
name | String | nome exclusivo da variável |
lower | Number | limite inferior da variável |
upper | Number | limite superior da variável |
type | Variable | tipo da variável, pode ser um dos Variable |
Retornar
Linear
: um mecanismo de otimização linear
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
Adiciona uma nova variável ao modelo. A variável é referenciada pelo nome.
const engine = LinearOptimizationService.createEngine(); const 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
Nome | Tipo | Descrição |
---|---|---|
name | String | nome exclusivo da variável |
lower | Number | limite inferior da variável |
upper | Number | limite superior da variável |
type | Variable | tipo da variável, pode ser um dos Variable |
objective | Number | coeficiente objetivo da variável |
Retornar
Linear
: um mecanismo de otimização linear
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
Adiciona variáveis em lote ao modelo. As variáveis são referenciadas pelos nomes.
const 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
Nome | Tipo | Descrição |
---|---|---|
names | String[] | nomes exclusivos das variáveis |
lower | Number[] | limites mínimos das variáveis |
upper | Number[] | limites superiores das variáveis |
types | Variable | tipos das variáveis, pode ser um dos Variable |
objective | Number[] | coeficientes objetivos das variáveis |
Retornar
Linear
: um mecanismo de otimização linear
setMaximization()
Define a direção da otimização para maximizar a função objetiva linear.
const 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();
Retornar
Linear
: um mecanismo de otimização linear
setMinimization()
Define a direção de otimização para minimizar a função objetiva linear.
const 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();
Retornar
Linear
: um mecanismo de otimização linear
setObjectiveCoefficient(variableName, coefficient)
Define o coeficiente de uma variável na função de objetivo linear.
const 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
Nome | Tipo | Descrição |
---|---|---|
variable | String | nome da variável para a qual o coeficiente está sendo definido |
coefficient | Number | coeficiente da variável na função objetivo |
Retornar
Linear
: um mecanismo de otimização linear
solve()
Resolve o programa linear atual com o prazo padrão de 30 segundos. Retorna a solução encontrada.
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()}`; } Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
Retornar
Linear
: solução da otimização
solve(seconds)
Resolve o programa linear atual. Retorna a solução encontrada e se ela é uma solução ideal.
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(300); if (!solution.isValid()) { throw `No solution ${solution.getStatus()}`; } Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
Parâmetros
Nome | Tipo | Descrição |
---|---|---|
seconds | Number | prazo para resolver o problema, em segundos; o prazo máximo é de 300 segundos |
Retornar
Linear
: solução da otimização