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 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\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"]]