المحرّك المستخدَم لإنشاء نموذج وحلّ برنامج خطي يحلّ المثال أدناه البرنامج الخطي التالي:
متغيران، 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')}`); }
الطُرق
مستندات تفصيلية
addConstraint(lowerBound, upperBound)
تضيف هذه السمة قيدًا خطيًا جديدًا في النموذج. يتم تحديد الحدّين الأعلى والأدنى للقيود في وقت الإنشاء. يتم تحديد معاملات المتغيّرات من خلال طلبات إلى LinearOptimizationConstraint.setCoefficient(variableName, coefficient).
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 | الحد الأعلى للشرط |
الإرجاع
LinearOptimizationConstraint: القيد الذي تم إنشاؤه
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[][] | المعامِلات التي يتم ضبطها |
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
addVariable(name, lowerBound, upperBound)
تضيف هذه السمة متغيّرًا مستمرًا جديدًا إلى النموذج. يتم الرجوع إلى المتغير باسمه. تم ضبط النوع على VariableType.CONTINUOUS.
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 | الحدّ الأعلى للمتغيّر |
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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 |
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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 | المعامل الموضوعي للمتغير |
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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[] | معاملات الهدف للمتغيرات |
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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();
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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();
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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 | معامل المتغيّر في دالة الهدف |
الإرجاع
LinearOptimizationEngine: محرك تحسين خطي
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')}`);
الإرجاع
LinearOptimizationSolution: حلّ التحسين
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 ثانية |
الإرجاع
LinearOptimizationSolution: حلّ التحسين