Stay organized with collectionsSave and categorize content based on your preferences.
LinearOptimizationSolution
The solution of a linear program. The example below solves the following linear program:
Two variables,xandy: 0 ≤ x ≤ 10 0 ≤ y ≤ 5
Constraints: 0 ≤ 2 * x + 5 * y ≤ 10 0 ≤ 10 * x + 3 * y ≤ 20
Objective: Maximizex + y
constengine=LinearOptimizationService.createEngine();// Add variables, constraints and define the objective with addVariable(),// addConstraint(), etc. Add two variables, 0 <= x <= 10 and 0 <= y <= 5engine.addVariable('x',0,10);engine.addVariable('y',0,5);// Create the constraint: 0 <= 2 * x + 5 * y <= 10letconstraint=engine.addConstraint(0,10);constraint.setCoefficient('x',2);constraint.setCoefficient('y',5);// Create the constraint: 0 <= 10 * x + 3 * y <= 20constraint=engine.addConstraint(0,20);constraint.setCoefficient('x',10);constraint.setCoefficient('y',3);// Set the objective to be x + yengine.setObjectiveCoefficient('x',1);engine.setObjectiveCoefficient('y',1);// Engine should maximize the objectiveengine.setMaximization();// Solve the linear programconstsolution=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')}`);}
Determines whether the solution is either feasible or optimal.
Detailed documentation
getObjectiveValue()
Gets the value of the objective function in the current solution.
constengine=LinearOptimizationService.createEngine();// Add variables, constraints and define the objective with addVariable(),// addConstraint(), etcengine.addVariable('x',0,10);// ...// Solve the linear programconstsolution=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 beNOT_SOLVED.
constengine=LinearOptimizationService.createEngine();// Add variables, constraints and define the objective with addVariable(),// addConstraint(), etcengine.addVariable('x',0,10);// ...// Solve the linear programconstsolution=engine.solve();conststatus=solution.getStatus();if(status!==LinearOptimizationService.Status.FEASIBLE&&status!==LinearOptimizationService.Status.OPTIMAL){throw`No solution${status}`;}Logger.log(`Status:${status}`);
constengine=LinearOptimizationService.createEngine();// Add variables, constraints and define the objective with addVariable(),// addConstraint(), etcengine.addVariable('x',0,10);// ...// Solve the linear programconstsolution=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.
constengine=LinearOptimizationService.createEngine();// Add variables, constraints and define the objective with addVariable(),// addConstraint(), etcengine.addVariable('x',0,10);// ...// Solve the linear programconstsolution=engine.solve();if(!solution.isValid()){throw`No solution${solution.getStatus()}`;}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["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"]]