המנוע שמשמש ליצירת מודל של תוכנית ליניארית ולפתרון שלה. בדוגמה הבאה מוצגת פתרון לתוכנית הליניארית הבאה:
שני משתנים, x
ו-y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
אילוצים:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
מטרה עסקית:
הגדלת 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')}`); }
Methods
מסמכים מפורטים
addConstraint(lowerBound, upperBound)
הוספת אילוץ לינארי חדש למודל. הגבול העליון והתחתון של האילוץ מוגדרים בזמן היצירה. מקדמים של המשתנים מוגדרים באמצעות קריאות ל-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);
פרמטרים
שם | סוג | תיאור |
---|---|---|
lower | Number | הגבול התחתון של האילוץ |
upper | Number | הגבול העליון של האילוץ |
חזרה
Linear
– האילוץ שנוצר
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)
הוספת אילוצים ברצף למודל.
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], ], );
פרמטרים
שם | סוג | תיאור |
---|---|---|
lower | Number[] | גבולות התחתון של האילוצים |
upper | Number[] | גבולות עליונים של האילוצים |
variable | String[][] | שמות המשתנים שלגביהם מגדירים את המקדמים |
coefficients | Number[][] | מקדמים שמוגדרים |
חזרה
Linear
– מנוע אופטימיזציה לינארי
addVariable(name, lowerBound, upperBound)
הוספת משתנה רציף חדש למודל. הפניה למשתנה מתבצעת לפי השם שלו. הסוג מוגדר ל-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);
פרמטרים
שם | סוג | תיאור |
---|---|---|
name | String | השם הייחודי של המשתנה |
lower | Number | הגבול התחתון של המשתנה |
upper | Number | הגבול העליון של המשתנה |
חזרה
Linear
– מנוע אופטימיזציה לינארי
addVariable(name, lowerBound, upperBound, type)
הוספת משתנה חדש למודל. הפניה למשתנה מתבצעת לפי השם שלו.
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, );
פרמטרים
שם | סוג | תיאור |
---|---|---|
name | String | השם הייחודי של המשתנה |
lower | Number | הגבול התחתון של המשתנה |
upper | Number | הגבול העליון של המשתנה |
type | Variable | הסוג של המשתנה, יכול להיות אחד מהערכים הבאים: Variable |
חזרה
Linear
– מנוע אופטימיזציה לינארי
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)
הוספת משתנה חדש למודל. הפניה למשתנה מתבצעת לפי השם שלו.
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.
פרמטרים
שם | סוג | תיאור |
---|---|---|
name | String | השם הייחודי של המשתנה |
lower | Number | הגבול התחתון של המשתנה |
upper | Number | הגבול העליון של המשתנה |
type | Variable | הסוג של המשתנה, יכול להיות אחד מהערכים הבאים: Variable |
objective | Number | מקדם היעד של המשתנה |
חזרה
Linear
– מנוע אופטימיזציה לינארי
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)
הוספת משתנים ברצף למודל. ההפניות למשתנים נעשות לפי השמות שלהם.
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, ], );
פרמטרים
שם | סוג | תיאור |
---|---|---|
names | String[] | שמות ייחודיים של המשתנים |
lower | Number[] | גבולות התחתון של המשתנים |
upper | Number[] | גבולות עליונים של המשתנים |
types | Variable | סוגי המשתנים, יכול להיות אחד מ-Variable |
objective | Number[] | מקדמי היעד של המשתנים |
חזרה
Linear
– מנוע אופטימיזציה לינארי
setMaximization()
הגדרת כיוון האופטימיזציה למיקסום של פונקציית היעד הליניארית.
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();
חזרה
Linear
– מנוע אופטימיזציה לינארי
setMinimization()
מגדיר את כיוון האופטימיזציה לצמצום פונקציית היעד הליניארית.
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();
חזרה
Linear
– מנוע אופטימיזציה לינארי
setObjectiveCoefficient(variableName, coefficient)
הגדרת המקדמים של המשתנים בפונקציית היעד הליניארית.
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);
פרמטרים
שם | סוג | תיאור |
---|---|---|
variable | String | שם המשתנה שעבורו מוגדר המכפיל |
coefficient | Number | המכפיל של המשתנה בפונקציית היעד |
חזרה
Linear
– מנוע אופטימיזציה לינארי
solve()
פתרון התוכנית הליניארית הנוכחית עם מועד היעד שמוגדר כברירת מחדל של 30 שניות. הפונקציה מחזירה את הפתרון שנמצא.
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')}`);
חזרה
Linear
— הפתרון של האופטימיזציה
solve(seconds)
פתרון התוכנית הליניארית הנוכחית. הפונקציה מחזירה את הפתרון שנמצא, ואם הוא פתרון אופטימלי.
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')}`);
פרמטרים
שם | סוג | תיאור |
---|---|---|
seconds | Number | מועד אחרון לפתרון הבעיה, בשניות. מועד היעד המקסימלי הוא 300 שניות. |
חזרה
Linear
— הפתרון של האופטימיזציה