Class LinearOptimizationService
Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
DoğrusalOptimizasyonHizmeti
Doğrusal ve karma tam sayı doğrusal programları modellemek ve çözmek için kullanılan doğrusal optimizasyon hizmeti. Aşağıdaki örnekte aşağıdaki doğrusal program çözülmektedir:
x
ve y
adlı iki değişken:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Kısıtlamalar:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Hedef:
x + y
değerini artırma
const engine = LinearOptimizationService.createEngine();
// Add variables, constraints and define the objective using 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')}`);
}
Özellikler
Mülk | Tür | Açıklama |
Status | Status | Çözümleyicinin durumu. |
VariableType | VariableType | Çözümleyici tarafından oluşturulan değişken türü. |
Ayrıntılı dokümanlar
createEngine()
Doğrusal programları (potansiyel olarak karma tam sayı programları) çözmek için bir motor oluşturur.
// Creates a linear optimization engine.
const engine = LinearOptimizationService.createEngine();
engine.addVariable('x', 0, 10);
// ...
Return
LinearOptimizationEngine
: doğrusal optimizasyon motoru
Aksi belirtilmediği sürece bu sayfanın içeriği Creative Commons Atıf 4.0 Lisansı altında ve kod örnekleri Apache 2.0 Lisansı altında lisanslanmıştır. Ayrıntılı bilgi için Google Developers Site Politikaları'na göz atın. Java, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-07-26 UTC.
[null,null,["Son güncelleme tarihi: 2025-07-26 UTC."],[[["\u003cp\u003eThe Linear Optimization Service enables the modeling and resolution of linear and mixed-integer linear programs within Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eIt provides functionalities to define variables, constraints, and objectives for optimization problems.\u003c/p\u003e\n"],["\u003cp\u003eThe service utilizes a dedicated engine, created via \u003ccode\u003ecreateEngine()\u003c/code\u003e, to process and solve the defined linear programs.\u003c/p\u003e\n"],["\u003cp\u003eSolutions can be retrieved and assessed for validity, providing values for optimized variables or indicating an infeasible solution.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can access detailed documentation and examples for utilizing the Linear Optimization Service effectively.\u003c/p\u003e\n"]]],["The `LinearOptimizationService` solves linear and mixed-integer linear programs. Key actions include creating an engine via `createEngine()`, adding variables (e.g., 'x', 'y') with bounds using `addVariable()`, and defining constraints with `addConstraint()` and `setCoefficient()`. The objective is set using `setObjectiveCoefficient()`, specifying maximization with `setMaximization()`. Finally, `solve()` computes the solution, and results are accessed via methods such as `getVariableValue()`. The service also includes properties like `Status` and `VariableType`.\n"],null,["# Class LinearOptimizationService\n\nLinearOptimizationService\n\nThe linear optimization service, used to model and solve linear and mixed-integer linear\nprograms. The example below solves the following linear program:\n\nTwo variables, `x` and `y`: \n\n\n`0 ≤ x ≤ 10`\n\n\n`0 ≤ y ≤ 5`\n\n\nConstraints: \n\n\n`0 ≤ 2 * x + 5 * y ≤ 10`\n\n\n`0 ≤ 10 * x + 3 * y ≤ 20`\n\n\nObjective: \n\nMaximize `x + y`\n\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n\n// Add variables, constraints and define the objective using addVariable(),\n// addConstraint(), etc. Add two variables, 0 \u003c= x \u003c= 10 and 0 \u003c= y \u003c= 5\nengine.addVariable('x', 0, 10);\nengine.addVariable('y', 0, 5);\n\n// Create the constraint: 0 \u003c= 2 * x + 5 * y \u003c= 10\nlet constraint = engine.addConstraint(0, 10);\nconstraint.setCoefficient('x', 2);\nconstraint.setCoefficient('y', 5);\n\n// Create the constraint: 0 \u003c= 10 * x + 3 * y \u003c= 20\nconstraint = engine.addConstraint(0, 20);\nconstraint.setCoefficient('x', 10);\nconstraint.setCoefficient('y', 3);\n\n// Set the objective to be x + y\nengine.setObjectiveCoefficient('x', 1);\nengine.setObjectiveCoefficient('y', 1);\n\n// Engine should maximize the objective.\nengine.setMaximization();\n\n// Solve the linear program\nconst solution = engine.solve();\nif (!solution.isValid()) {\n Logger.log(`No solution ${solution.getStatus()}`);\n} else {\n Logger.log(`Value of x: ${solution.getVariableValue('x')}`);\n Logger.log(`Value of y: ${solution.getVariableValue('y')}`);\n}\n``` \n\n### Properties\n\n| Property | Type | Description |\n|------------------|-------------------------------------------------------------------|------------------------------------------|\n| `Status` | [Status](/apps-script/reference/optimization/status) | Status of the solver. |\n| `Variable``Type` | [VariableType](/apps-script/reference/optimization/variable-type) | Type of variables created by the solver. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|\n| [createEngine()](#createEngine()) | [LinearOptimizationEngine](/apps-script/reference/optimization/linear-optimization-engine) | Creates an engine to to solve linear programs (potentially mixed-integer programs). |\n\nDetailed documentation\n----------------------\n\n### `create``Engine()`\n\nCreates an engine to to solve linear programs (potentially mixed-integer programs).\n\n```javascript\n// Creates a linear optimization engine.\nconst engine = LinearOptimizationService.createEngine();\nengine.addVariable('x', 0, 10);\n\n// ...\n```\n\n#### Return\n\n\n[LinearOptimizationEngine](/apps-script/reference/optimization/linear-optimization-engine) --- a linear optimization engine"]]