Der Engine, mit der ein lineares Programm modelliert und gelöst wird. Im folgenden Beispiel wird das folgende lineare Programm gelöst:
Zwei Variablen, x
und y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Einschränkungen:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Zielvorhaben:
Wert maximieren 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')}`); }
Methoden
Detaillierte Dokumentation
addConstraint(lowerBound, upperBound)
Fügt dem Modell eine neue lineare Einschränkung hinzu. Die Ober- und Untergrenze der Einschränkung werden beim Erstellen definiert. Die Koeffizienten für die Variablen werden über Aufrufe von Linear
definiert.
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);
Parameter
Name | Typ | Beschreibung |
---|---|---|
lower | Number | Untergrenze der Einschränkung |
upper | Number | Obergrenze der Einschränkung |
Rückflug
Linear
– die erstellte Einschränkung
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
Fügen Sie dem Modell Einschränkungen im Batch hinzu.
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], ], );
Parameter
Name | Typ | Beschreibung |
---|---|---|
lower | Number[] | Untergrenzen der Einschränkungen |
upper | Number[] | Obergrenzen der Einschränkungen |
variable | String[][] | die Namen der Variablen, für die die Koeffizienten festgelegt werden |
coefficients | Number[][] | Koeffizienten werden festgelegt |
Rückflug
Linear
– ein lineares Optimierungsmodul
addVariable(name, lowerBound, upperBound)
Dem Modell wird eine neue kontinuierliche Variable hinzugefügt. Die Variable wird über ihren Namen referenziert. Der Typ ist auf Variable
festgelegt.
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);
Parameter
Name | Typ | Beschreibung |
---|---|---|
name | String | eindeutiger Name der Variablen |
lower | Number | Untergrenze der Variablen |
upper | Number | Obergrenze der Variablen |
Rückflug
Linear
– ein lineares Optimierungsmodul
addVariable(name, lowerBound, upperBound, type)
Fügt dem Modell eine neue Variable hinzu. Die Variable wird über ihren Namen referenziert.
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, );
Parameter
Name | Typ | Beschreibung |
---|---|---|
name | String | eindeutiger Name der Variablen |
lower | Number | Untergrenze der Variablen |
upper | Number | Obergrenze der Variablen |
type | Variable | Der Typ der Variablen. Kann einer der folgenden Werte sein: Variable |
Rückflug
Linear
– ein lineares Optimierungsmodul
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
Fügt dem Modell eine neue Variable hinzu. Die Variable wird über ihren Namen referenziert.
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.
Parameter
Name | Typ | Beschreibung |
---|---|---|
name | String | eindeutiger Name der Variablen |
lower | Number | Untergrenze der Variablen |
upper | Number | Obergrenze der Variablen |
type | Variable | Der Typ der Variablen. Kann einer der folgenden Werte sein: Variable |
objective | Number | Objektiver Koeffizient der Variablen |
Rückflug
Linear
– ein lineares Optimierungsmodul
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
Fügt dem Modell mehrere Variablen gleichzeitig hinzu. Die Variablen werden anhand ihrer Namen referenziert.
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, ], );
Parameter
Name | Typ | Beschreibung |
---|---|---|
names | String[] | eindeutige Namen der Variablen |
lower | Number[] | Untergrenzen der Variablen |
upper | Number[] | Obergrenzen der Variablen |
types | Variable | Typen der Variablen, kann einer der folgenden Werte sein: Variable |
objective | Number[] | objektive Koeffizienten der Variablen |
Rückflug
Linear
– ein lineares Optimierungsmodul
setMaximization()
Legt die Optimierungsrichtung auf die Maximierung der linearen Zielfunktion fest.
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();
Rückflug
Linear
– ein lineares Optimierungsmodul
setMinimization()
Legt die Optimierungsrichtung auf die Minimierung der linearen Zielfunktion fest.
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();
Rückflug
Linear
– ein lineares Optimierungsmodul
setObjectiveCoefficient(variableName, coefficient)
Legt den Koeffizienten einer Variablen in der linearen Zielfunktion fest.
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);
Parameter
Name | Typ | Beschreibung |
---|---|---|
variable | String | Name der Variablen, für die der Koeffizient festgelegt wird |
coefficient | Number | Koeffizient der Variablen in der Zielfunktion |
Rückflug
Linear
– ein lineares Optimierungsmodul
solve()
Löst das aktuelle lineare Programm mit dem Standardzeitlimit von 30 Sekunden. Die gefundene Lösung wird zurückgegeben.
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')}`);
Rückflug
Linear
– Lösung der Optimierung
solve(seconds)
Lösen Sie das aktuelle lineare Programm. Gibt die gefundene Lösung zurück und ob es sich um eine optimale Lösung handelt.
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')}`);
Parameter
Name | Typ | Beschreibung |
---|---|---|
seconds | Number | Frist für die Behebung des Problems in Sekunden; das maximale Zeitlimit beträgt 300 Sekunden |
Rückflug
Linear
– Lösung der Optimierung