Class LinearOptimizationSolution
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
线性优化解决方案
线性规划的解。以下示例会解题以下线性规划:
两个变量 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(`Objective value: ${solution.getObjectiveValue()}`);
Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
Logger.log(`Value of y: ${solution.getVariableValue('y')}`);
}
详细文档
getObjectiveValue()
获取当前解中的目标函数值。
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();
Logger.log(`ObjectiveValue: ${solution.getObjectiveValue()}`);
返回
Number
- 目标函数的值
getStatus()
获取解决方案的状态。在解决问题之前,状态将为 NOT_SOLVED
。
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();
const status = solution.getStatus();
if (status !== LinearOptimizationService.Status.FEASIBLE &&
status !== LinearOptimizationService.Status.OPTIMAL) {
throw `No solution ${status}`;
}
Logger.log(`Status: ${status}`);
返回
Status
- 求解器的状态
getVariableValue(variableName)
获取上次调用 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();
Logger.log(`Value of x: ${solution.getVariableValue('x')}`);
参数
名称 | 类型 | 说明 |
variableName | String | 变量的名称 |
返回
Number
- 解中的变量值
isValid()
确定解决方案是否可行或是否最优。
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()}`;
}
返回
Boolean
- 如果解决方案有效(Status.FEASIBLE
或 Status.OPTIMAL
),则为 true
;如果无效,则为 false
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eLinearOptimizationSolution\u003c/code\u003e represents the solution of a linear program, providing methods to access solution details.\u003c/p\u003e\n"],["\u003cp\u003eIt allows retrieval of the objective function's value, solution status, and individual variable values.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example demonstrates solving a linear program with two variables and constraints, maximizing a given objective function.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eisValid()\u003c/code\u003e method helps determine if the solution is feasible or optimal, while \u003ccode\u003egetStatus()\u003c/code\u003e provides detailed solution status information.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetVariableValue()\u003c/code\u003e allows access to the specific values assigned to variables in the solution.\u003c/p\u003e\n"]]],[],null,["# Class LinearOptimizationSolution\n\nLinearOptimizationSolution\n\nThe solution of a linear program. 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 with 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(`Objective value: ${solution.getObjectiveValue()}`);\n Logger.log(`Value of x: ${solution.getVariableValue('x')}`);\n Logger.log(`Value of y: ${solution.getVariableValue('y')}`);\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------------|------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [getObjectiveValue()](#getObjectiveValue()) | `Number` | Gets the value of the objective function in the current solution. |\n| [getStatus()](#getStatus()) | [Status](/apps-script/reference/optimization/status) | Gets the status of the solution. |\n| [getVariableValue(variableName)](#getVariableValue(String)) | `Number` | Gets the value of a variable in the solution created by the last call to [LinearOptimizationEngine.solve()](/apps-script/reference/optimization/linear-optimization-engine#solve()). |\n| [isValid()](#isValid()) | `Boolean` | Determines whether the solution is either feasible or optimal. |\n\nDetailed documentation\n----------------------\n\n### `get``Objective``Value()`\n\nGets the value of the objective function in the current solution.\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n\n// Add variables, constraints and define the objective with addVariable(),\n// addConstraint(), etc\nengine.addVariable('x', 0, 10);\n\n// ...\n\n// Solve the linear program\nconst solution = engine.solve();\nLogger.log(`ObjectiveValue: ${solution.getObjectiveValue()}`);\n```\n\n#### Return\n\n\n`Number` --- the value of the objective function\n\n*** ** * ** ***\n\n### `get``Status()`\n\nGets the status of the solution. Before solving a problem, the status will be `NOT_SOLVED`.\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n\n// Add variables, constraints and define the objective with addVariable(),\n// addConstraint(), etc\nengine.addVariable('x', 0, 10);\n\n// ...\n\n// Solve the linear program\nconst solution = engine.solve();\nconst status = solution.getStatus();\n\nif (status !== LinearOptimizationService.Status.FEASIBLE &&\n status !== LinearOptimizationService.Status.OPTIMAL) {\n throw `No solution ${status}`;\n}\nLogger.log(`Status: ${status}`);\n```\n\n#### Return\n\n\n[Status](/apps-script/reference/optimization/status) --- the status of the solver\n\n*** ** * ** ***\n\n### `get``Variable``Value(variableName)`\n\nGets the value of a variable in the solution created by the last call to [LinearOptimizationEngine.solve()](/apps-script/reference/optimization/linear-optimization-engine#solve()).\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n\n// Add variables, constraints and define the objective with addVariable(),\n// addConstraint(), etc\nengine.addVariable('x', 0, 10);\n\n// ...\n\n// Solve the linear program\nconst solution = engine.solve();\nLogger.log(`Value of x: ${solution.getVariableValue('x')}`);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------------|----------|----------------------|\n| `variable``Name` | `String` | name of the variable |\n\n#### Return\n\n\n`Number` --- the value of the variable in the solution\n\n*** ** * ** ***\n\n### `is``Valid()`\n\nDetermines whether the solution is either feasible or optimal.\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n\n// Add variables, constraints and define the objective with addVariable(),\n// addConstraint(), etc\nengine.addVariable('x', 0, 10);\n\n// ...\n\n// Solve the linear program\nconst solution = engine.solve();\nif (!solution.isValid()) {\n throw `No solution ${solution.getStatus()}`;\n}\n```\n\n#### Return\n\n\n`Boolean` --- `true` if the solution is valid ([Status.FEASIBLE](/apps-script/reference/optimization/status#FEASIBLE) or\n[Status.OPTIMAL](/apps-script/reference/optimization/status#OPTIMAL)); `false` if not"]]