Class LinearOptimizationSolution
Stay organized with collections
Save and categorize content based on your preferences.
LinearOptimizationSolution
The solution of a linear program. 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 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')}`);
}
Detailed documentation
getObjectiveValue()
Gets the value of the objective function in the current solution.
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()}`);
Return
Number
— the value of the objective function
getStatus()
Gets the status of the solution. Before solving a problem, the status will be 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}`);
Return
Status
— the status of the solver
getVariableValue(variableName)
Gets the value of a variable in the solution created by the last call to 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')}`);
Parameters
Name | Type | Description |
variableName | String | name of the variable |
Return
Number
— the value of the variable in the solution
isValid()
Determines whether the solution is either feasible or optimal.
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()}`;
}
Return
Boolean
— true
if the solution is valid (Status.FEASIBLE
or
Status.OPTIMAL
); false
if not
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\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"]]