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:
Massimizzare il 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')); }
Metodi
Documentazione dettagliata
addConstraint(lowerBound, upperBound)
Aggiunge un nuovo vincolo lineare nel modello. I limiti superiore e inferiore del vincolo vengono definiti al momento della creazione. I coefficienti per le variabili vengono definiti tramite chiamate 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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
lowerBound | Number | limite inferiore del vincolo |
upperBound | Number | limite superiore del vincolo |
Ritorni
LinearOptimizationConstraint
: vincolo creato
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
Aggiunge vincoli in batch al modello.
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]]);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
lowerBounds | Number[] | limiti inferiori dei vincoli |
upperBounds | Number[] | limiti superiori dei vincoli |
variableNames | String[][] | i nomi delle variabili per le quali vengono impostati i coefficienti |
coefficients | Number[][] | coefficienti in fase di impostazione |
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
addVariable(name, lowerBound, upperBound)
Aggiunge una nuova variabile continua al modello. Il nome della variabile fa riferimento alla variabile. Il tipo
è impostato su 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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
name | String | nome univoco della variabile |
lowerBound | Number | limite inferiore della variabile |
upperBound | Number | limite superiore della variabile |
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
addVariable(name, lowerBound, upperBound, type)
Aggiunge una nuova variabile al modello. Il nome della variabile fa riferimento alla variabile.
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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
name | String | nome univoco della variabile |
lowerBound | Number | limite inferiore della variabile |
upperBound | Number | limite superiore della variabile |
type | VariableType | tipo della variabile, può essere uno dei seguenti: VariableType |
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
Aggiunge una nuova variabile al modello. Il nome della variabile fa riferimento alla variabile.
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.
Parametri
Nome | Tipo | Descrizione |
---|---|---|
name | String | nome univoco della variabile |
lowerBound | Number | limite inferiore della variabile |
upperBound | Number | limite superiore della variabile |
type | VariableType | tipo della variabile, può essere uno dei seguenti: VariableType |
objectiveCoefficient | Number | coefficiente oggettivo della variabile |
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
Aggiunge variabili in gruppo al modello. I nomi delle variabili fanno riferimento alle variabili.
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]);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
names | String[] | nomi univoci delle variabili |
lowerBounds | Number[] | limiti inferiori delle variabili |
upperBounds | Number[] | limiti superiori delle variabili |
types | VariableType[] | delle variabili, può essere uno dei seguenti: VariableType |
objectiveCoefficients | Number[] | coefficienti oggettivi delle variabili |
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
setMaximization()
Imposta la direzione di ottimizzazione per massimizzare la funzione dell'obiettivo lineare.
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();
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
setMinimization()
Imposta la direzione di ottimizzazione per ridurre al minimo la funzione obiettivo lineare.
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();
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
setObjectiveCoefficient(variableName, coefficient)
Imposta il coefficiente di una variabile nella funzione dell'obiettivo lineare.
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);
Parametri
Nome | Tipo | Descrizione |
---|---|---|
variableName | String | nome della variabile per la quale viene impostato il coefficiente |
coefficient | Number | coefficiente della variabile nella funzione obiettivo |
Ritorni
LinearOptimizationEngine
: un motore di ottimizzazione lineare
solve()
Risolve l'attuale programma lineare con la scadenza predefinita di 30 secondi. Restituisce la soluzione trovata.
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'));
Ritorni
LinearOptimizationSolution
: soluzione di ottimizzazione
solve(seconds)
Risolve il programma lineare attuale. Restituisce la soluzione trovata e indica se è una soluzione ottimale.
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'));
Parametri
Nome | Tipo | Descrizione |
---|---|---|
seconds | Number | scadenza in secondi per risolvere il problema; la scadenza massima è di 300 secondi |
Ritorni
LinearOptimizationSolution
: soluzione di ottimizzazione