موتوری که برای مدلسازی و حل یک برنامه خطی استفاده می شود. مثال زیر برنامه خطی زیر را حل می کند:
دو متغیر 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
- راه حل بهینه سازی