Class LinearOptimizationEngine

เครื่องมือเพิ่มประสิทธิภาพเชิงเส้น

เครื่องมือที่ใช้สร้างโมเดลและแก้ปัญหาโปรแกรมเชิงเส้น ตัวอย่างด้านล่างแสดงวิธีแก้ปัญหาโปรแกรมเชิงเส้นต่อไปนี้

ตัวแปร 2 ตัว ได้แก่ 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เพิ่มข้อจำกัดเชิงเส้นใหม่ในโมเดล
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)LinearOptimizationEngineเพิ่มข้อจำกัดแบบเป็นกลุ่มลงในโมเดล
addVariable(name, lowerBound, upperBound)LinearOptimizationEngineเพิ่มตัวแปรต่อเนื่องใหม่ลงในโมเดล
addVariable(name, lowerBound, upperBound, type)LinearOptimizationEngineเพิ่มตัวแปรใหม่ลงในโมเดล
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)LinearOptimizationEngineเพิ่มตัวแปรใหม่ลงในโมเดล
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)LinearOptimizationEngineเพิ่มตัวแปรหลายรายการลงในโมเดล
setMaximization()LinearOptimizationEngineตั้งค่าทิศทางการเพิ่มประสิทธิภาพเพื่อเพิ่มฟังก์ชันวัตถุประสงค์เชิงเส้นให้สูงสุด
setMinimization()LinearOptimizationEngineตั้งค่าทิศทางการเพิ่มประสิทธิภาพเพื่อลดฟังก์ชันวัตถุประสงค์เชิงเส้น
setObjectiveCoefficient(variableName, coefficient)LinearOptimizationEngineกำหนดค่าสัมประสิทธิ์ของตัวแปรในฟังก์ชันวัตถุประสงค์เชิงเส้น
solve()LinearOptimizationSolutionแก้ปัญหาโปรแกรมเชิงเส้นปัจจุบันโดยกำหนดเวลาเริ่มต้นที่ 30 วินาที
solve(seconds)LinearOptimizationSolutionแก้ปัญหาโปรแกรมเชิงเส้นปัจจุบัน

เอกสารประกอบโดยละเอียด

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
lowerBoundNumberขอบเขตล่างของข้อจำกัด
upperBoundNumberขอบเขตบนของข้อจำกัด

รีเทิร์น

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],
    ],
);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
lowerBoundsNumber[]ขอบเขตล่างของข้อจำกัด
upperBoundsNumber[]ขอบเขตบนของข้อจำกัด
variableNamesString[][]ชื่อของตัวแปรที่จะตั้งค่าสัมประสิทธิ์
coefficientsNumber[][]ค่าสัมประสิทธิ์ที่กำหนด

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อที่ไม่ซ้ำกันของตัวแปร
lowerBoundNumberขอบเขตล่างของตัวแปร
upperBoundNumberขอบเขตบนของตัวแปร

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อที่ไม่ซ้ำกันของตัวแปร
lowerBoundNumberขอบเขตล่างของตัวแปร
upperBoundNumberขอบเขตบนของตัวแปร
typeVariableTypeประเภทของตัวแปร อาจเป็นอย่างใดอย่างหนึ่งต่อไปนี้ VariableType

รีเทิร์น

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.

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อที่ไม่ซ้ำกันของตัวแปร
lowerBoundNumberขอบเขตล่างของตัวแปร
upperBoundNumberขอบเขตบนของตัวแปร
typeVariableTypeประเภทของตัวแปร อาจเป็นอย่างใดอย่างหนึ่งต่อไปนี้ VariableType
objectiveCoefficientNumberสัมประสิทธิ์วัตถุประสงค์ของตัวแปร

รีเทิร์น

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,
    ],
);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
namesString[]ชื่อที่ไม่ซ้ำกันของตัวแปร
lowerBoundsNumber[]ขอบเขตล่างของตัวแปร
upperBoundsNumber[]ขอบเขตบนของตัวแปร
typesVariableType[]ประเภทของตัวแปร อาจเป็นอย่างใดอย่างหนึ่งต่อไปนี้ VariableType
objectiveCoefficientsNumber[]ค่าสัมประสิทธิ์วัตถุประสงค์ของตัวแปร

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
variableNameStringชื่อตัวแปรที่จะตั้งค่าสัมประสิทธิ์
coefficientNumberสัมประสิทธิ์ของตัวแปรในฟังก์ชันวัตถุประสงค์

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
secondsNumberกำหนดเวลาในการแก้ปัญหาเป็นวินาที โดยกำหนดเวลาสูงสุดคือ 300 วินาที

รีเทิร์น

LinearOptimizationSolution — โซลูชันการเพิ่มประสิทธิภาพ