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