يشير ذلك المصطلح إلى المحرّك المستخدَم لوضع نموذج وحلّ برنامج خطي. يحل المثال أدناه البرنامج الخطي التالي:
متغيّران، 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);
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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]]);
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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);
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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);
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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.
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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]);
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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);
المَعلمات
الاسم | Type | الوصف |
---|---|---|
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'));
المَعلمات
الاسم | Type | الوصف |
---|---|---|
seconds | Number | موعد نهائي لحل المشكلة بالثواني؛ الحد الأقصى للموعد النهائي هو 300 ثانية |
استرجاع الكرة
LinearOptimizationSolution
— حل التحسين