Class LinearOptimizationService
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
שירותאופטימיזציהלינארית
שירות האופטימיזציה הליניארית, המשמש ליצירת מודלים ולפתרון תוכניות ליניאריות ותוכניות ליניאריות עם משתני רכיבים שלמים. בדוגמה הבאה מוצגת פתרון לתוכנית ליניארית הבאה:
שני משתנים, 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 using 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')}`);
}
מאפיינים
נכס | סוג | תיאור |
Status | Status | הסטטוס של הפתרון. |
VariableType | VariableType | סוג המשתנים שנוצרו על ידי הפתרון. |
מסמכים מפורטים
createEngine()
יצירת מנוע לפתרון תוכניות ליניאריות (אולי תוכניות שלמים-מספרים מעורבים).
// Creates a linear optimization engine.
const engine = LinearOptimizationService.createEngine();
engine.addVariable('x', 0, 10);
// ...
חזרה
LinearOptimizationEngine
– מנוע אופטימיזציה לינארי
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[null,null,["עדכון אחרון: 2025-07-26 (שעון UTC)."],[[["\u003cp\u003eThe Linear Optimization Service enables the modeling and resolution of linear and mixed-integer linear programs within Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eIt provides functionalities to define variables, constraints, and objectives for optimization problems.\u003c/p\u003e\n"],["\u003cp\u003eThe service utilizes a dedicated engine, created via \u003ccode\u003ecreateEngine()\u003c/code\u003e, to process and solve the defined linear programs.\u003c/p\u003e\n"],["\u003cp\u003eSolutions can be retrieved and assessed for validity, providing values for optimized variables or indicating an infeasible solution.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can access detailed documentation and examples for utilizing the Linear Optimization Service effectively.\u003c/p\u003e\n"]]],["The `LinearOptimizationService` solves linear and mixed-integer linear programs. Key actions include creating an engine via `createEngine()`, adding variables (e.g., 'x', 'y') with bounds using `addVariable()`, and defining constraints with `addConstraint()` and `setCoefficient()`. The objective is set using `setObjectiveCoefficient()`, specifying maximization with `setMaximization()`. Finally, `solve()` computes the solution, and results are accessed via methods such as `getVariableValue()`. The service also includes properties like `Status` and `VariableType`.\n"],null,["# Class LinearOptimizationService\n\nLinearOptimizationService\n\nThe linear optimization service, used to model and solve linear and mixed-integer linear\nprograms. The example below solves the following linear program:\n\nTwo variables, `x` and `y`: \n\n\n`0 ≤ x ≤ 10`\n\n\n`0 ≤ y ≤ 5`\n\n\nConstraints: \n\n\n`0 ≤ 2 * x + 5 * y ≤ 10`\n\n\n`0 ≤ 10 * x + 3 * y ≤ 20`\n\n\nObjective: \n\nMaximize `x + y`\n\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n\n// Add variables, constraints and define the objective using addVariable(),\n// addConstraint(), etc. Add two variables, 0 \u003c= x \u003c= 10 and 0 \u003c= y \u003c= 5\nengine.addVariable('x', 0, 10);\nengine.addVariable('y', 0, 5);\n\n// Create the constraint: 0 \u003c= 2 * x + 5 * y \u003c= 10\nlet constraint = engine.addConstraint(0, 10);\nconstraint.setCoefficient('x', 2);\nconstraint.setCoefficient('y', 5);\n\n// Create the constraint: 0 \u003c= 10 * x + 3 * y \u003c= 20\nconstraint = engine.addConstraint(0, 20);\nconstraint.setCoefficient('x', 10);\nconstraint.setCoefficient('y', 3);\n\n// Set the objective to be x + y\nengine.setObjectiveCoefficient('x', 1);\nengine.setObjectiveCoefficient('y', 1);\n\n// Engine should maximize the objective.\nengine.setMaximization();\n\n// Solve the linear program\nconst solution = engine.solve();\nif (!solution.isValid()) {\n Logger.log(`No solution ${solution.getStatus()}`);\n} else {\n Logger.log(`Value of x: ${solution.getVariableValue('x')}`);\n Logger.log(`Value of y: ${solution.getVariableValue('y')}`);\n}\n``` \n\n### Properties\n\n| Property | Type | Description |\n|------------------|-------------------------------------------------------------------|------------------------------------------|\n| `Status` | [Status](/apps-script/reference/optimization/status) | Status of the solver. |\n| `Variable``Type` | [VariableType](/apps-script/reference/optimization/variable-type) | Type of variables created by the solver. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|\n| [createEngine()](#createEngine()) | [LinearOptimizationEngine](/apps-script/reference/optimization/linear-optimization-engine) | Creates an engine to to solve linear programs (potentially mixed-integer programs). |\n\nDetailed documentation\n----------------------\n\n### `create``Engine()`\n\nCreates an engine to to solve linear programs (potentially mixed-integer programs).\n\n```javascript\n// Creates a linear optimization engine.\nconst engine = LinearOptimizationService.createEngine();\nengine.addVariable('x', 0, 10);\n\n// ...\n```\n\n#### Return\n\n\n[LinearOptimizationEngine](/apps-script/reference/optimization/linear-optimization-engine) --- a linear optimization engine"]]