Class LinearOptimizationEngine

LinearOptimizationEngine

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

MetodoTipo restituitoBreve descrizione
addConstraint(lowerBound, upperBound)LinearOptimizationConstraintAggiunge un nuovo vincolo lineare nel modello.
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)LinearOptimizationEngineAggiunge vincoli in batch al modello.
addVariable(name, lowerBound, upperBound)LinearOptimizationEngineAggiunge una nuova variabile continua al modello.
addVariable(name, lowerBound, upperBound, type)LinearOptimizationEngineAggiunge una nuova variabile al modello.
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)LinearOptimizationEngineAggiunge una nuova variabile al modello.
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)LinearOptimizationEngineAggiunge variabili in gruppo al modello.
setMaximization()LinearOptimizationEngineImposta la direzione di ottimizzazione per massimizzare la funzione dell'obiettivo lineare.
setMinimization()LinearOptimizationEngineImposta la direzione di ottimizzazione per ridurre al minimo la funzione obiettivo lineare.
setObjectiveCoefficient(variableName, coefficient)LinearOptimizationEngineImposta il coefficiente di una variabile nella funzione dell'obiettivo lineare.
solve()LinearOptimizationSolutionRisolve l'attuale programma lineare con la scadenza predefinita di 30 secondi.
solve(seconds)LinearOptimizationSolutionRisolve il programma lineare attuale.

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

NomeTipoDescrizione
lowerBoundNumberlimite inferiore del vincolo
upperBoundNumberlimite 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

NomeTipoDescrizione
lowerBoundsNumber[]limiti inferiori dei vincoli
upperBoundsNumber[]limiti superiori dei vincoli
variableNamesString[][]i nomi delle variabili per le quali vengono impostati i coefficienti
coefficientsNumber[][]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

NomeTipoDescrizione
nameStringnome univoco della variabile
lowerBoundNumberlimite inferiore della variabile
upperBoundNumberlimite 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

NomeTipoDescrizione
nameStringnome univoco della variabile
lowerBoundNumberlimite inferiore della variabile
upperBoundNumberlimite superiore della variabile
typeVariableTypetipo 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

NomeTipoDescrizione
nameStringnome univoco della variabile
lowerBoundNumberlimite inferiore della variabile
upperBoundNumberlimite superiore della variabile
typeVariableTypetipo della variabile, può essere uno dei seguenti: VariableType
objectiveCoefficientNumbercoefficiente 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

NomeTipoDescrizione
namesString[]nomi univoci delle variabili
lowerBoundsNumber[]limiti inferiori delle variabili
upperBoundsNumber[]limiti superiori delle variabili
typesVariableType[]delle variabili, può essere uno dei seguenti: VariableType
objectiveCoefficientsNumber[]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

NomeTipoDescrizione
variableNameStringnome della variabile per la quale viene impostato il coefficiente
coefficientNumbercoefficiente 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

NomeTipoDescrizione
secondsNumberscadenza in secondi per risolvere il problema; la scadenza massima è di 300 secondi

Ritorni

LinearOptimizationSolution: soluzione di ottimizzazione