Class LinearOptimizationEngine

موتور بهینه‌سازی خطی

موتور مورد استفاده برای مدل‌سازی و حل یک برنامه خطی. مثال زیر برنامه خطی زیر را حل می‌کند:

دو متغیر، 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')}`);
}

روش‌ها

روش نوع بازگشتی شرح مختصر
add Constraint(lowerBound, upperBound) Linear Optimization Constraint یک قید خطی جدید در مدل اضافه می‌کند.
add Constraints(lowerBounds, upperBounds, variableNames, coefficients) Linear Optimization Engine محدودیت‌ها را به صورت دسته‌ای به مدل اضافه می‌کند.
add Variable(name, lowerBound, upperBound) Linear Optimization Engine یک متغیر پیوسته جدید به مدل اضافه می‌کند.
add Variable(name, lowerBound, upperBound, type) Linear Optimization Engine یک متغیر جدید به مدل اضافه می‌کند.
add Variable(name, lowerBound, upperBound, type, objectiveCoefficient) Linear Optimization Engine یک متغیر جدید به مدل اضافه می‌کند.
add Variables(names, lowerBounds, upperBounds, types, objectiveCoefficients) Linear Optimization Engine متغیرها را به صورت دسته‌ای به مدل اضافه می‌کند.
set Maximization() Linear Optimization Engine جهت بهینه‌سازی را برای به حداکثر رساندن تابع هدف خطی تنظیم می‌کند.
set Minimization() Linear Optimization Engine جهت بهینه‌سازی را برای به حداقل رساندن تابع هدف خطی تنظیم می‌کند.
set Objective Coefficient(variableName, coefficient) Linear Optimization Engine ضریب یک متغیر را در تابع هدف خطی تنظیم می‌کند.
solve() Linear Optimization Solution برنامه خطی فعلی را با مهلت پیش‌فرض ۳۰ ثانیه حل می‌کند.
solve(seconds) Linear Optimization Solution برنامه خطی فعلی را حل می‌کند.

مستندات دقیق

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 Bound Number حد پایین قید
upper Bound 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 Bounds Number[] کران‌های پایین محدودیت‌ها
upper Bounds Number[] کران‌های بالایی محدودیت‌ها
variable Names 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 Bound Number حد پایین متغیر
upper Bound 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 Bound Number حد پایین متغیر
upper Bound Number حد بالای متغیر
type Variable Type نوع متغیر، می‌تواند یکی از Variable Type باشد.

بازگشت

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 Bound Number حد پایین متغیر
upper Bound Number حد بالای متغیر
type Variable Type نوع متغیر، می‌تواند یکی از Variable Type باشد.
objective Coefficient 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 Bounds Number[] کران‌های پایین متغیرها
upper Bounds Number[] کران‌های بالایی متغیرها
types Variable Type[] انواع متغیرها، می‌تواند یکی از Variable Type باشد
objective Coefficients 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 Name String نام متغیری که ضریب برای آن تنظیم می‌شود
coefficient Number ضریب متغیر در تابع هدف

بازگشت

LinearOptimizationEngine - یک موتور بهینه‌سازی خطی


solve()

برنامه خطی فعلی را با مهلت پیش‌فرض ۳۰ ثانیه حل می‌کند. راه‌حل یافت‌شده را برمی‌گرداند.

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 مهلت حل مسئله، بر حسب ثانیه؛ حداکثر مهلت ۳۰۰ ثانیه است

بازگشت

LinearOptimizationSolution — راه حل بهینه سازی