Class LinearOptimizationService
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
线性优化服务
线性优化服务,用于对线性和混合整数线性规划进行建模和求解。以下示例会解题以下线性规划:
两个变量 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 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')}`);
}
详细文档
createEngine()
创建一个引擎来求解线性规划(可能为混合整数规划)。
// Creates a linear optimization engine.
const engine = LinearOptimizationService.createEngine();
engine.addVariable('x', 0, 10);
// ...
返回
LinearOptimizationEngine
- 线性优化引擎
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\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"]]