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 برنامه خطی فعلی را با مهلت پیش فرض 30 ثانیه حل می کند.
solve(seconds) Linear Optimization Solution برنامه خطی فعلی را حل می کند.

مستندات دقیق

add Constraint(lowerBound, upperBound)

یک محدودیت خطی جدید در مدل اضافه می کند. کران بالا و پایین محدودیت در زمان ایجاد تعریف می شود. ضرایب برای متغیرها از طریق فراخوانی Linear Optimization Constraint.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 کران بالای محدودیت

بازگشت

Linear Optimization Constraint - محدودیت ایجاد شده


add Constraints(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[][] ضرایب در حال تعیین

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


add Variable(name, lowerBound, upperBound)

یک متغیر پیوسته جدید به مدل اضافه می کند. متغیر با نام خود ارجاع داده می شود. نوع بر روی Variable Type.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 کران بالای متغیر

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


add Variable(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 باشد

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


add Variable(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 ضریب هدف متغیر

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


add Variables(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[] ضرایب عینی متغیرها

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


set Maximization()

جهت بهینه سازی را برای به حداکثر رساندن تابع هدف خطی تنظیم می کند.

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();

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


set Minimization()

جهت بهینه سازی را برای به حداقل رساندن تابع هدف خطی تنظیم می کند.

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();

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


set Objective Coefficient(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 ضریب متغیر در تابع هدف

بازگشت

Linear Optimization Engine - یک موتور بهینه سازی خطی


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')}`);

بازگشت

Linear Optimization Solution - راه حل بهینه سازی


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 ثانیه است

بازگشت

Linear Optimization Solution - راه حل بهینه سازی