Class LinearOptimizationConstraint
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
線性最佳化限制條件
儲存 lowerBound ≤ Sum(a(i) x(i)) ≤ upperBound
形式線性約束條件的物件,其中 lowerBound
和 upperBound
為常數、a(i)
為常數係數,而 x(i)
為變數 (未知數)。
以下範例會建立一個值介於 0
和 5
之間的變數 x
,並建立限制 0 ≤ 2 * x ≤ 5
。方法是先建立限制條件,其中包含下限 5
和上限 5
。接著,這個限制條件中變數 x
的係數會設為 2
。
const engine = LinearOptimizationService.createEngine();
// Create a variable so we can add it to the constraint
engine.addVariable('x', 0, 5);
// Create a linear constraint with the bounds 0 and 10
const constraint = engine.addConstraint(0, 10);
// Set the coefficient of the variable in the constraint. The constraint is now:
// 0 <= 2 * x <= 5
constraint.setCoefficient('x', 2);
內容詳盡的說明文件
setCoefficient(variableName, coefficient)
設定限制中變數的係數。根據預設,變數的係數為 0。
const engine = LinearOptimizationService.createEngine();
// Create a linear constraint with the bounds 0 and 10
const constraint = engine.addConstraint(0, 10);
// Create a variable so we can add it to the constraint
engine.addVariable('x', 0, 5);
// Set the coefficient of the variable in the constraint. The constraint is now:
// 0 <= 2 * x <= 5
constraint.setCoefficient('x', 2);
參數
名稱 | 類型 | 說明 |
variableName | String | 要設定係數的變數名稱 |
coefficient | Number | 設定的係數 |
回攻員
LinearOptimizationConstraint
:此線性最佳化約束
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eLinearOptimizationConstraint\u003c/code\u003e objects define constraints in the form of \u003ccode\u003elowerBound ≤ Sum(a(i) x(i)) ≤ upperBound\u003c/code\u003e, where \u003ccode\u003ea(i)\u003c/code\u003e are coefficients and \u003ccode\u003ex(i)\u003c/code\u003e are variables.\u003c/p\u003e\n"],["\u003cp\u003eYou can set the coefficient of a variable within a constraint using the \u003ccode\u003esetCoefficient(variableName, coefficient)\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eConstraints are created with lower and upper bounds, and variables are added with their own bounds before being incorporated into the constraint.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code example demonstrates creating a variable, adding a constraint, and setting the coefficient for the variable within that constraint.\u003c/p\u003e\n"]]],[],null,["# Class LinearOptimizationConstraint\n\nLinearOptimizationConstraint\n\nObject storing a linear constraint of the form `lower``Bound ≤ Sum(a(i) x(i)) ≤ upperBound`\nwhere `lower``Bound` and `upper``Bound` are constants, `a(i)` are constant\ncoefficients and `x(i)` are variables (unknowns).\n\nThe example below creates one variable `x` with values between `0` and `5`\nand creates the constraint `0 ≤ 2 * x ≤ 5`. This is done by first creating a constraint\nwith the lower bound `5` and upper bound `5`. Then the coefficient for variable\n`x` in this constraint is set to `2`.\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n// Create a variable so we can add it to the constraint\nengine.addVariable('x', 0, 5);\n// Create a linear constraint with the bounds 0 and 10\nconst constraint = engine.addConstraint(0, 10);\n// Set the coefficient of the variable in the constraint. The constraint is now:\n// 0 \u003c= 2 * x \u003c= 5\nconstraint.setCoefficient('x', 2);\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------------------------------|-----------------------------------|-------------------------------------------------------|\n| [setCoefficient(variableName, coefficient)](#setCoefficient(String,Number)) | [LinearOptimizationConstraint](#) | Sets the coefficient of a variable in the constraint. |\n\nDetailed documentation\n----------------------\n\n### `set``Coefficient(variableName, coefficient)`\n\nSets the coefficient of a variable in the constraint. By default, variables have a coefficient\nof 0.\n\n```javascript\nconst engine = LinearOptimizationService.createEngine();\n// Create a linear constraint with the bounds 0 and 10\nconst constraint = engine.addConstraint(0, 10);\n// Create a variable so we can add it to the constraint\nengine.addVariable('x', 0, 5);\n// Set the coefficient of the variable in the constraint. The constraint is now:\n// 0 \u003c= 2 * x \u003c= 5\nconstraint.setCoefficient('x', 2);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------------|----------|-------------------------------------------------------------|\n| `variable``Name` | `String` | the name of variable for which the coefficient is being set |\n| `coefficient` | `Number` | coefficient being set |\n\n#### Return\n\n\n[LinearOptimizationConstraint](#) --- this linear optimization constraint"]]