ui/autolayout/layoutsolver
Kiwi dual simplex 기반 제약 솔버.
Constraint solver based on Kiwi dual simplex.
LayoutSolver
Kiwi dual simplex 기반 제약 솔버. 사용:
const solver = new LayoutSolver();
const x = new LayoutVariable("x");
const y = new LayoutVariable("y");
solver.addConstraint(LayoutExpression.fromVariable(x).equalTo(10));
solver.addConstraint(LayoutExpression.fromVariable(y).equalTo(
LayoutExpression.fromVariable(x).add(20)
));
solver.updateVariables();
x.getValue(); // 10
y.getValue(); // 30동적 입력 (드래그 등):
solver.addEditVariable(x, LayoutStrength.strong);
solver.suggestValue(x, 50);
solver.updateVariables();Constraint solver based on Kiwi dual simplex. Usage:
const solver = new LayoutSolver();
const x = new LayoutVariable("x");
const y = new LayoutVariable("y");
solver.addConstraint(LayoutExpression.fromVariable(x).equalTo(10));
solver.addConstraint(LayoutExpression.fromVariable(y).equalTo(
LayoutExpression.fromVariable(x).add(20)
));
solver.updateVariables();
x.getValue(); // 10
y.getValue(); // 30Dynamic input (drag, etc):
solver.addEditVariable(x, LayoutStrength.strong);
solver.suggestValue(x, 50);
solver.updateVariables();import { LayoutSolver, LayoutVariable, LayoutExpression, LayoutStrength } from "./build/vanilla.js";
const solver = new LayoutSolver();
const viewWidth = new LayoutVariable("viewWidth");
const left = new LayoutVariable("left");
const width = new LayoutVariable("width");
const viewWidthExpression = LayoutExpression.fromVariable(viewWidth);
const leftExpression = LayoutExpression.fromVariable(left);
const rightExpression = leftExpression.add(width);
const leftConstraint = leftExpression.equalTo(16);
const rightMarginExpression = viewWidthExpression.subtract(16);
const rightConstraint = rightExpression.equalTo(rightMarginExpression);
solver.addConstraint(leftConstraint);
solver.addConstraint(rightConstraint);
solver.addEditVariable(viewWidth, LayoutStrength.strong);
solver.suggestValue(viewWidth, 640);
solver.updateVariables();
const wideWidth = width.getValue();
solver.suggestValue(viewWidth, 360);
solver.updateVariables();
const narrowWidth = width.getValue();
console.log(wideWidth, narrowWidth);제약 추가.
Adds a constraint.
- constraintLayoutConstraint
- 넣을 제약. (이미 넣은 것이거나 required 제약끼리 모순이면 예외)Constraint to add. (throws if already added or if required constraints contradict)
짝Counterpart: vanilla-engine LayoutSolver.add_constraint
제약 제거.
Removes a constraint.
- constraintLayoutConstraint
- 뺄 제약. (넣지 않은 것이면 예외)Constraint to remove. (throws if not added)
짝Counterpart: vanilla-engine LayoutSolver.remove_constraint
제약 등록 여부.
Whether the constraint is registered.
- constraintLayoutConstraint
- 찾을 제약.Constraint to look for.
반환Returnsboolean 솔버에 넣은 제약이면 참.True if the constraint was added to the solver.
짝Counterpart: vanilla-engine LayoutSolver.has_constraint
편집(edit) 변수 추가. suggestValue 사용 전 반드시 등록.
Adds an edit variable. Must be registered before using suggestValue.
- variableLayoutVariable
- 편집 변수로 등록할 변수. (이미 등록했으면 예외)Variable to register as an edit variable. (throws if already registered)
- strengthnumber
- 편집 강도. (0 ~ required 로 자르고, required 이상이면 예외)Edit strength. (clamped to 0 ~ required, throws if required or above)
짝Counterpart: vanilla-engine LayoutSolver.add_edit_variable
import { LayoutSolver, LayoutVariable, LayoutStrength } from "./build/vanilla.js";
const solver = new LayoutSolver();
const width = new LayoutVariable("width");
solver.addEditVariable(width, LayoutStrength.strong);
const isEditVariable = solver.hasEditVariable(width);
solver.suggestValue(width, 240);
solver.updateVariables();
const widthValue = width.getValue();
solver.removeEditVariable(width);
const isEditVariableAfterRemove = solver.hasEditVariable(width);
console.log(isEditVariable, widthValue, isEditVariableAfterRemove);편집 변수 제거.
Removes an edit variable.
- variableLayoutVariable
- 등록을 풀 편집 변수. (등록하지 않은 것이면 예외)Edit variable to unregister. (throws if not registered)
짝Counterpart: vanilla-engine LayoutSolver.remove_edit_variable
편집 변수 등록 여부.
Whether the edit variable is registered.
- variableLayoutVariable
- 찾을 변수.Variable to look for.
반환Returnsboolean 편집 변수로 등록했으면 참.True if registered as an edit variable.
짝Counterpart: vanilla-engine LayoutSolver.has_edit_variable
편집 변수에 권장 값 제안. (드래그 갱신 등)
- 솔버는 가능한 그 값을 만족시키되 다른 제약과 충돌하면 강도에 따라 양보.
Suggests a value for an edit variable. (drag updates, etc)
- The solver satisfies that value as much as possible, but yields by strength if it conflicts with other constraints.
- variableLayoutVariable
- addEditVariable 로 등록한 편집 변수. (등록하지 않은 것이면 예외)Edit variable registered with addEditVariable. (throws if not registered)
- valuenumber
- 바라는 값.Desired value.
짝Counterpart: vanilla-engine LayoutSolver.suggest_value
import { LayoutSolver, LayoutVariable, LayoutExpression, LayoutStrength } from "./build/vanilla.js";
const solver = new LayoutSolver();
const dragX = new LayoutVariable("dragX");
const dragExpression = LayoutExpression.fromVariable(dragX);
const minimumConstraint = dragExpression.greaterThanOrEqualTo(0);
const maximumConstraint = dragExpression.lessThanOrEqualTo(300);
solver.addConstraint(minimumConstraint);
solver.addConstraint(maximumConstraint);
solver.addEditVariable(dragX, LayoutStrength.strong);
solver.suggestValue(dragX, 120);
solver.updateVariables();
const insideValue = dragX.getValue();
solver.suggestValue(dragX, 480);
solver.updateVariables();
const clampedValue = dragX.getValue();
console.log(insideValue, clampedValue);변수 값 갱신. tableau 의 external 행 상수항을 각 LayoutVariable 에 반영.
Updates variable values. Applies the constant of each external row of the tableau to each LayoutVariable.
짝Counterpart: vanilla-engine LayoutSolver.update_variables
import { LayoutSolver, LayoutVariable, LayoutExpression } from "./build/vanilla.js";
const solver = new LayoutSolver();
const height = new LayoutVariable("height");
const heightExpression = LayoutExpression.fromVariable(height);
const heightConstraint = heightExpression.equalTo(48);
solver.addConstraint(heightConstraint);
const valueBeforeUpdate = height.getValue();
solver.updateVariables();
const valueAfterUpdate = height.getValue();
console.log(valueBeforeUpdate, valueAfterUpdate);내부: LayoutVariable → LayoutSymbol 매핑 (없으면 생성).
Internal: LayoutVariable → LayoutSymbol mapping (created if missing).
- variableLayoutVariable
- 외부 변수.External variable.
반환ReturnsLayoutSymbol 변수에 짝지은 external 심볼. (없으면 새로 만들어 기억한다)External symbol paired with the variable. (creates and remembers a new one if none)
내부: 제약 → 행 생성. tag 에 marker / other 심볼을 채운다.
Internal: constraint → row creation. Fills the tag with marker / other symbols.
- constraintLayoutConstraint
- 행으로 바꿀 제약.Constraint to turn into a row.
- tagLayoutTag
- marker, other 심볼을 채울 태그.Tag to fill with the marker and other symbols.
반환ReturnsLayoutRow 제약을 옮긴 새 행. (상수항이 음수면 부호를 뒤집는다)New row holding the constraint. (flips the sign if the constant term is negative)
내부: 행에서 subject 심볼 선택. 1) 외부 변수 우선. 2) slack/error 의 음수 계수 항. 3) 둘 다 없으면 invalid.
Internal: chooses the subject symbol from a row. 1) External variables first. 2) slack/error terms with negative coefficients. 3) invalid if neither.
- rowLayoutRow
- subject 를 고를 행.Row to choose a subject from.
- tagLayoutTag
- 그 행의 제약 태그.Constraint tag of that row.
반환ReturnsLayoutSymbol 고른 심볼. (없으면 invalid 심볼)Chosen symbol. (an invalid symbol if none)
내부: 인공 변수로 행 강제 추가. 성공 여부 반환.
Internal: forcibly adds a row with an artificial variable. Returns whether it succeeded.
- rowLayoutRow
- 넣을 행.Row to add.
반환Returnsboolean 인공 변수를 0 으로 만들어 넣었으면 참.True if the row was added by driving the artificial variable to 0.
내부: 모든 행과 objective 에서 symbol 의 등장을 row 로 치환.
Internal: substitutes row for every occurrence of symbol in all rows and the objective.
- symbolLayoutSymbol
- 치환할 심볼.Symbol to substitute.
- rowLayoutRow
- symbol 대신 넣을 행.Row to put in place of symbol.
내부: 1차 (primal) 최적화. objective 가 더 이상 음수 계수를 갖지 않을 때까지 반복.
Internal: primary (primal) optimization. Repeats until the objective has no more negative coefficients.
- objectiveLayoutRow
- 최소화할 목적 행. (솔버의 목적 행이나 인공 행)Objective row to minimize. (the solver objective row or the artificial row)
내부: dual simplex 최적화. infeasible 행을 처리.
Internal: dual simplex optimization. Handles infeasible rows.
내부: leaving symbol 결정. (Bland's rule 변형 + 비율 테스트)
Internal: determines the leaving symbol. (Bland's rule variant + ratio test)
- enteringSymbolLayoutSymbol
- 들어올 심볼.Entering symbol.
반환ReturnsLayoutSymbol 나갈 행의 심볼. (비율이 가장 작은 것, 없으면 invalid 심볼)Symbol of the leaving row. (the one with the smallest ratio, an invalid symbol if none)
내부: dual entering symbol 결정.
Internal: determines the dual entering symbol.
- rowLayoutRow
- 상수항이 음수인 행.Row whose constant term is negative.
반환ReturnsLayoutSymbol 들어올 심볼. (비율이 가장 작은 것, 없으면 invalid 심볼)Entering symbol. (the one with the smallest ratio, an invalid symbol if none)
내부: marker 가 leaving 으로 떠날 행 결정.
Internal: determines the row the marker leaves as leaving.
- markerLayoutSymbol
- 제약을 빼며 내보낼 marker 심볼.Marker symbol to move out when removing a constraint.
반환ReturnsLayoutSymbol marker 와 바꿔 피벗할 행의 심볼. (음수 계수 비율 최소, 양수 계수 비율 최소, external 행 차례, 없으면 invalid 심볼)Symbol of the row to pivot with the marker. (in order: smallest ratio with a negative coefficient, smallest ratio with a positive coefficient, an external row, an invalid symbol if none)
내부: 제약의 오차 변수가 목적함수에 끼친 영향을 제거.
Internal: removes the effect of the constraint's error variables on the objective function.
- constraintLayoutConstraint
- 뺄 제약.Constraint to remove.
- tagLayoutTag
- 그 제약의 태그.Tag of that constraint.
내부: 단일 오차 변수 영향 제거.
Internal: removes the effect of a single error variable.
- markerSymbolLayoutSymbol
- 오차 심볼.Error symbol.
- strengthnumber
- 제약 강도.Constraint strength.