Class LinearOptimizationService
Stay organized with collections
Save and categorize content based on your preferences.
LinearOptimizationService
The linear optimization service, used to model and solve linear and mixed-integer linear
programs. The example below solves the following linear program:
Two variables, x
and y
:
0 ≤ x ≤ 10
0 ≤ y ≤ 5
Constraints:
0 ≤ 2 * x + 5 * y ≤ 10
0 ≤ 10 * x + 3 * y ≤ 20
Objective:
Maximize 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')}`);
}
Properties
Property | Type | Description |
Status | Status | Status of the solver. |
VariableType | VariableType | Type of variables created by the solver. |
Detailed documentation
createEngine()
Creates an engine to to solve linear programs (potentially mixed-integer programs).
// Creates a linear optimization engine.
const engine = LinearOptimizationService.createEngine();
engine.addVariable('x', 0, 10);
// ...
Return
LinearOptimizationEngine
— a linear optimization engine
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-12-02 UTC.
[null,null,["Last updated 2024-12-02 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"]]