Class LinearOptimizationConstraint Stay organized with collections Save and categorize content based on your preferences.
Page Summary
A
LinearOptimizationConstraintobject represents a linear constraint of the formlowerBound ≤ Sum(a(i) x(i)) ≤ upperBound.The
setCoefficientmethod is used to set the coefficient of a variable within a constraint.By default, variables in a constraint have a coefficient of 0.
The provided example demonstrates how to create a variable and a constraint and then set the coefficient of the variable within that constraint.
Object storing a linear constraint of the formlowerwherelower andupper are constants,a(i) are constantcoefficients andx(i) are variables (unknowns).
The example below creates one variablex with values between0 and5and creates the constraint0 ≤ 2 * x ≤ 5. This is done by first creating a constraintwith the lower bound5 and upper bound5. Then the coefficient for variablex in this constraint is set to2.
constengine=LinearOptimizationService.createEngine();// Create a variable so we can add it to the constraintengine.addVariable('x',0,5);// Create a linear constraint with the bounds 0 and 10constconstraint=engine.addConstraint(0,10);// Set the coefficient of the variable in the constraint. The constraint is now:// 0 <= 2 * x <= 5constraint.setCoefficient('x',2);
Methods
| Method | Return type | Brief description |
|---|---|---|
set | Linear | Sets the coefficient of a variable in the constraint. |
Detailed documentation
setCoefficient(variableName, coefficient)
Sets the coefficient of a variable in the constraint. By default, variables have a coefficientof 0.
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
| Name | Type | Description |
|---|---|---|
variable | String | the name of variable for which the coefficient is being set |
coefficient | Number | coefficient being set |
Return
Linear — this linear optimization constraint
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.