Class LinearOptimizationEngine

  • LinearOptimizationEngine is used to model and solve linear programs by defining variables, constraints, and objectives.

  • You can add variables with specified bounds and types using methods likeaddVariable andaddVariables.

  • Constraints with defined bounds can be added usingaddConstraint oraddConstraints, and variable coefficients within these constraints are set separately.

  • The objective function is defined by setting the coefficient for each variable usingsetObjectiveCoefficient.

  • The optimization direction can be set to maximization or minimization usingsetMaximization orsetMinimization.

  • Thesolve() method is used to find the solution to the defined linear program.

LinearOptimizationEngine

The engine used to model and solve a linear program. The example below solves the followinglinear program:

Two variables,x andy:
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(`Value of x:${solution.getVariableValue('x')}`);Logger.log(`Value of y:${solution.getVariableValue('y')}`);}

Methods

MethodReturn typeBrief description
addConstraint(lowerBound, upperBound)LinearOptimizationConstraintAdds a new linear constraint in the model.
addConstraints(lowerBounds, upperBounds, variableNames, coefficients)LinearOptimizationEngineAdds constraints in batch to the model.
addVariable(name, lowerBound, upperBound)LinearOptimizationEngineAdds a new continuous variable to the model.
addVariable(name, lowerBound, upperBound, type)LinearOptimizationEngineAdds a new variable to the model.
addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)LinearOptimizationEngineAdds a new variable to the model.
addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)LinearOptimizationEngineAdds variables in batch to the model.
setMaximization()LinearOptimizationEngineSets the optimization direction to maximizing the linear objective function.
setMinimization()LinearOptimizationEngineSets the optimization direction to minimizing the linear objective function.
setObjectiveCoefficient(variableName, coefficient)LinearOptimizationEngineSets the coefficient of a variable in the linear objective function.
solve()LinearOptimizationSolutionSolves the current linear program with the default deadline of 30 seconds.
solve(seconds)LinearOptimizationSolutionSolves the current linear program.

Detailed documentation

addConstraint(lowerBound, upperBound)

Adds a new linear constraint in the model. The upper and lower bound of the constraint aredefined at creation time. Coefficients for the variables are defined via calls toLinearOptimizationConstraint.setCoefficient(variableName, coefficient).

constengine=LinearOptimizationService.createEngine();// Create a linear constraint with the bounds 0 and 10constconstraint=engine.addConstraint(0,10);// Create a variable so we can add it to the constraintengine.addVariable('x',0,5);// Set the coefficient of the variable in the constraint. The constraint is now:// 0 <= 2 * x <= 5constraint.setCoefficient('x',2);

Parameters

NameTypeDescription
lowerBoundNumberlower bound of the constraint
upperBoundNumberupper bound of the constraint

Return

LinearOptimizationConstraint — the constraint created


addConstraints(lowerBounds, upperBounds, variableNames, coefficients)

Adds constraints in batch to the model.

constengine=LinearOptimizationService.createEngine();// Add a boolean variable 'x' (integer >= 0 and <= 1) and a real (continuous >=// 0 and <= 100) variable 'y'.engine.addVariables(['x','y'],[0,0],[1,100],[LinearOptimizationService.VariableType.INTEGER,LinearOptimizationService.VariableType.CONTINUOUS,],);// Adds two constraints://   0 <= x + y <= 3//   1 <= 10 * x - y <= 5engine.addConstraints([0.0,1.0],[3.0,5.0],[['x','y'],['x','y'],],[[1,1],[10,-1],],);

Parameters

NameTypeDescription
lowerBoundsNumber[]lower bounds of the constraints
upperBoundsNumber[]upper bounds of the constraints
variableNamesString[][]the names of variables for which the coefficients are being set
coefficientsNumber[][]coefficients being set

Return

LinearOptimizationEngine — a linear optimization engine


addVariable(name, lowerBound, upperBound)

Adds a new continuous variable to the model. The variable is referenced by its name. The typeis set toVariableType.CONTINUOUS.

constengine=LinearOptimizationService.createEngine();constconstraint=engine.addConstraint(0,10);// Add a boolean variable (integer >= 0 and <= 1)engine.addVariable('x',0,1,LinearOptimizationService.VariableType.INTEGER);// Add a real (continuous) variable. Notice the lack of type specification.engine.addVariable('y',0,100);

Parameters

NameTypeDescription
nameStringunique name of the variable
lowerBoundNumberlower bound of the variable
upperBoundNumberupper bound of the variable

Return

LinearOptimizationEngine — a linear optimization engine


addVariable(name, lowerBound, upperBound, type)

Adds a new variable to the model. The variable is referenced by its name.

constengine=LinearOptimizationService.createEngine();constconstraint=engine.addConstraint(0,10);// Add a boolean variable (integer >= 0 and <= 1)engine.addVariable('x',0,1,LinearOptimizationService.VariableType.INTEGER);// Add a real (continuous) variableengine.addVariable('y',0,100,LinearOptimizationService.VariableType.CONTINUOUS,);

Parameters

NameTypeDescription
nameStringunique name of the variable
lowerBoundNumberlower bound of the variable
upperBoundNumberupper bound of the variable
typeVariableTypetype of the variable, can be one ofVariableType

Return

LinearOptimizationEngine — a linear optimization engine


addVariable(name, lowerBound, upperBound, type, objectiveCoefficient)

Adds a new variable to the model. The variable is referenced by its name.

constengine=LinearOptimizationService.createEngine();constconstraint=engine.addConstraint(0,10);// Add a boolean variable (integer >= 0 and <= 1)engine.addVariable('x',0,1,LinearOptimizationService.VariableType.INTEGER,2,);// The objective is now 2 * x.// Add a real (continuous) variableengine.addVariable('y',0,100,LinearOptimizationService.VariableType.CONTINUOUS,-5,);// The objective is now 2 * x - 5 * y.

Parameters

NameTypeDescription
nameStringunique name of the variable
lowerBoundNumberlower bound of the variable
upperBoundNumberupper bound of the variable
typeVariableTypetype of the variable, can be one ofVariableType
objectiveCoefficientNumberobjective coefficient of the variable

Return

LinearOptimizationEngine — a linear optimization engine


addVariables(names, lowerBounds, upperBounds, types, objectiveCoefficients)

Adds variables in batch to the model. The variables are referenced by their names.

constengine=LinearOptimizationService.createEngine();// Add a boolean variable 'x' (integer >= 0 and <= 1) and a real (continuous >=0// and <= 100) variable 'y'.engine.addVariables(['x','y'],[0,0],[1,100],[LinearOptimizationService.VariableType.INTEGER,LinearOptimizationService.VariableType.CONTINUOUS,],);

Parameters

NameTypeDescription
namesString[]unique names of the variables
lowerBoundsNumber[]lower bounds of the variables
upperBoundsNumber[]upper bounds of the variables
typesVariableType[]types of the variables, can be one ofVariableType
objectiveCoefficientsNumber[]objective coefficients of the variables

Return

LinearOptimizationEngine — a linear optimization engine


setMaximization()

Sets the optimization direction to maximizing the linear objective function.

constengine=LinearOptimizationService.createEngine();// Add a real (continuous) variable. Notice the lack of type specification.engine.addVariable('y',0,100);// Set the coefficient of 'y' in the objective.// The objective is now 5 * yengine.setObjectiveCoefficient('y',5);// We want to maximize.engine.setMaximization();

Return

LinearOptimizationEngine — a linear optimization engine


setMinimization()

Sets the optimization direction to minimizing the linear objective function.

constengine=LinearOptimizationService.createEngine();// Add a real (continuous) variable. Notice the lack of type specification.engine.addVariable('y',0,100);// Set the coefficient of 'y' in the objective.// The objective is now 5 * yengine.setObjectiveCoefficient('y',5);// We want to minimizeengine.setMinimization();

Return

LinearOptimizationEngine — a linear optimization engine


setObjectiveCoefficient(variableName, coefficient)

Sets the coefficient of a variable in the linear objective function.

constengine=LinearOptimizationService.createEngine();// Add a real (continuous) variable. Notice the lack of type specification.engine.addVariable('y',0,100);// Set the coefficient of 'y' in the objective.// The objective is now 5 * yengine.setObjectiveCoefficient('y',5);

Parameters

NameTypeDescription
variableNameStringname of variable for which the coefficient is being set
coefficientNumbercoefficient of the variable in the objective function

Return

LinearOptimizationEngine — a linear optimization engine


solve()

Solves the current linear program with the default deadline of 30 seconds. Returns the solution found.

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()}`;}Logger.log(`Value of x:${solution.getVariableValue('x')}`);

Return

LinearOptimizationSolution — solution of the optimization


solve(seconds)

Solves the current linear program. Returns the solution found. and if it is an optimalsolution.

constengine=LinearOptimizationService.createEngine();// Add variables, constraints and define the objective with addVariable(),// addConstraint(), etcengine.addVariable('x',0,10);// ...// Solve the linear programconstsolution=engine.solve(300);if(!solution.isValid()){throw`No solution${solution.getStatus()}`;}Logger.log(`Value of x:${solution.getVariableValue('x')}`);

Parameters

NameTypeDescription
secondsNumberdeadline for solving the problem, in seconds; the maximum deadline is 300 seconds

Return

LinearOptimizationSolution — solution of the optimization

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.