Il motore utilizzato per modellare e risolvere un programma lineare. L'esempio seguente risolve il seguente programma lineare:
Due variabili, x
e y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Vincoli:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Obiettivo:
Massimizza 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')}`); }
Metodi
Documentazione dettagliata
addConstraint(lowerBound, upperBound)
Aggiunge una nuova limitazione lineare nel modello. I limiti superiore e inferiore della limitazione vengono definiti al momento della creazione. I coefficienti per le variabili sono definiti tramite chiamate a 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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
lower | Number | limite inferiore del vincolo |
upper | Number | limite superiore del vincolo |
Invio
Linear
: il vincolo creato
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
Aggiunge vincoli in batch al modello.
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], ], );
Parametri
Nome | Tipo | Descrizione |
---|---|---|
lower | Number[] | limiti inferiori dei vincoli |
upper | Number[] | limiti superiori dei vincoli |
variable | String[][] | i nomi delle variabili per le quali vengono impostati i coefficienti |
coefficients | Number[][] | coefficienti impostati |
Invio
Linear
: un motore di ottimizzazione lineare
addVariable(name, lowerBound, upperBound)
Aggiunge una nuova variabile continua al modello. Viene fatto riferimento alla variabile tramite il relativo nome. Il valore type è impostato su 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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
name | String | nome univoco della variabile |
lower | Number | limite inferiore della variabile |
upper | Number | limite superiore della variabile |
Invio
Linear
: un motore di ottimizzazione lineare
addVariable(name, lowerBound, upperBound, type)
Aggiunge una nuova variabile al modello. Viene fatto riferimento alla variabile tramite il relativo 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, );
Parametri
Nome | Tipo | Descrizione |
---|---|---|
name | String | nome univoco della variabile |
lower | Number | limite inferiore della variabile |
upper | Number | limite superiore della variabile |
type | Variable | tipo della variabile, può essere uno dei valori Variable |
Invio
Linear
: un motore di ottimizzazione lineare
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
Aggiunge una nuova variabile al modello. Viene fatto riferimento alla variabile tramite il relativo 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.
Parametri
Nome | Tipo | Descrizione |
---|---|---|
name | String | nome univoco della variabile |
lower | Number | limite inferiore della variabile |
upper | Number | limite superiore della variabile |
type | Variable | tipo della variabile, può essere uno dei valori Variable |
objective | Number | coefficiente obiettivo della variabile |
Invio
Linear
: un motore di ottimizzazione lineare
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
Aggiunge le variabili al modello in blocco. Le variabili vengono richiamate tramite i relativi nomi.
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, ], );
Parametri
Nome | Tipo | Descrizione |
---|---|---|
names | String[] | nomi univoci delle variabili |
lower | Number[] | limiti inferiori delle variabili |
upper | Number[] | limiti superiori delle variabili |
types | Variable | tipi di variabili, può essere uno dei seguenti: Variable |
objective | Number[] | coefficienti oggettivi delle variabili |
Invio
Linear
: un motore di ottimizzazione lineare
setMaximization()
Imposta la direzione di ottimizzazione per massimizzare la funzione obiettivo lineare.
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();
Invio
Linear
: un motore di ottimizzazione lineare
setMinimization()
Imposta la direzione di ottimizzazione in modo da ridurre al minimo la funzione obiettivo lineare.
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();
Invio
Linear
: un motore di ottimizzazione lineare
setObjectiveCoefficient(variableName, coefficient)
Imposta il coefficiente di una variabile nella funzione obiettivo lineare.
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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
variable | String | nome della variabile per cui viene impostato il coefficiente |
coefficient | Number | coefficiente della variabile nella funzione obiettivo |
Invio
Linear
: un motore di ottimizzazione lineare
solve()
Risolve il programma lineare corrente con la scadenza predefinita di 30 secondi. Restituisce la soluzione trovata.
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')}`);
Invio
Linear
- soluzione dell'ottimizzazione
solve(seconds)
Risolve il programma lineare corrente. Restituisce la soluzione trovata e indica se si tratta di una soluzione ottimale.
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')}`);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
seconds | Number | scadenza per la risoluzione del problema, in secondi; la scadenza massima è di 300 secondi |
Invio
Linear
- soluzione dell'ottimizzazione