Silnik używany do modelowania i rozwiązywania programu liniowego. Przykład poniżej rozwiązuje ten program liniowy:
2 zmiennych: x
i y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Ograniczenia:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Cel:
Maksymalizacja 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')}`); }
Metody
Szczegółowa dokumentacja
add Constraint(lowerBound, upperBound)
Dodaje nowe ograniczenie liniowe w modelu. Górna i dolna granica ograniczeń są definiowane w momencie ich tworzenia. Współczynniki zmiennych są definiowane za pomocą wywołań funkcji 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);
Parametry
Nazwa | Typ | Opis |
---|---|---|
lower | Number | dolna granica ograniczeń |
upper | Number | górna granica ograniczenia |
Powrót
Linear
– utworzone ograniczenie
add Constraints(lowerBounds, upperBounds, variableNames, coefficients)
Dodaje do modelu ograniczenia zbiorczo.
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], ], );
Parametry
Nazwa | Typ | Opis |
---|---|---|
lower | Number[] | dolnej granicy ograniczeń |
upper | Number[] | górne granice ograniczeń |
variable | String[][] | nazwy zmiennych, dla których ustawiane są współczynniki; |
coefficients | Number[][] | współczynniki są ustawiane; |
Powrót
Linear
— mechanizm optymalizacji liniowej.
add Variable(name, lowerBound, upperBound)
Dodaje do modelu nową zmienną ciągłą. Odwołuje się do niej po nazwie. Typ ma wartość 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);
Parametry
Nazwa | Typ | Opis |
---|---|---|
name | String | unikalna nazwa zmiennej |
lower | Number | dolna granica zmiennej |
upper | Number | górna granica zmiennej |
Powrót
Linear
— mechanizm optymalizacji liniowej.
add Variable(name, lowerBound, upperBound, type)
Dodaje nową zmienną do modelu. Odwołuje się do niej po nazwie.
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, );
Parametry
Nazwa | Typ | Opis |
---|---|---|
name | String | unikalna nazwa zmiennej |
lower | Number | dolna granica zmiennej |
upper | Number | górna granica zmiennej |
type | Variable | typ zmiennej, może być jeden z Variable |
Powrót
Linear
— mechanizm optymalizacji liniowej.
add Variable(name, lowerBound, upperBound, type, objectiveCoefficient)
Dodaje nową zmienną do modelu. Odwołuje się do niej po nazwie.
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.
Parametry
Nazwa | Typ | Opis |
---|---|---|
name | String | unikalna nazwa zmiennej |
lower | Number | dolna granica zmiennej |
upper | Number | górna granica zmiennej |
type | Variable | typ zmiennej, może być jeden z Variable |
objective | Number | obiektywny współczynnik zmiennej. |
Powrót
Linear
— mechanizm optymalizacji liniowej.
add Variables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
Dodaje zmienne zbiorczo do modelu. Odwołania do zmiennych są realizowane za pomocą ich nazw.
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, ], );
Parametry
Nazwa | Typ | Opis |
---|---|---|
names | String[] | unikalne nazwy zmiennych; |
lower | Number[] | dolne granice zmiennych. |
upper | Number[] | górne granice zmiennych |
types | Variable | typy zmiennych, które mogą być jednym z tych: Variable |
objective | Number[] | obiektywnych współczynników zmiennych. |
Powrót
Linear
— mechanizm optymalizacji liniowej.
set Maximization()
Ustawia kierunek optymalizacji na maksymalizację liniowej funkcji celu.
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();
Powrót
Linear
— mechanizm optymalizacji liniowej.
set Minimization()
Ustawia kierunek optymalizacji na minimalizowanie liniowej funkcji celu.
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();
Powrót
Linear
— mechanizm optymalizacji liniowej.
set Objective Coefficient(variableName, coefficient)
Ustawia współczynnik zmiennej w liniowej funkcji celu.
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);
Parametry
Nazwa | Typ | Opis |
---|---|---|
variable | String | nazwa zmiennej, dla której ustawiany jest współczynnik |
coefficient | Number | współczynnik zmiennej w funkcji celu |
Powrót
Linear
— mechanizm optymalizacji liniowej.
solve()
Rozwiązuje bieżący program liniowy z domyślnym terminem 30 sekund. Zwraca znalezione rozwiązanie.
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')}`);
Powrót
Linear
– rozwiązanie optymalizacji,
solve(seconds)
Rozwiązuje bieżący program liniowy. Zwraca znalezione rozwiązanie i informuje, czy jest to rozwiązanie optymalne.
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')}`);
Parametry
Nazwa | Typ | Opis |
---|---|---|
seconds | Number | termin rozwiązania problemu w sekundach; maksymalny termin to 300 sekund |
Powrót
Linear
– rozwiązanie optymalizacji,