# ui/autolayout/layoutsolver (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/autolayout/layoutsolver/

Constraint solver based on Kiwi dual simplex.

## LayoutSolver

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(); // 30

Dynamic input (drag, etc):
  solver.addEditVariable(x, LayoutStrength.strong);
  solver.suggestValue(x, 50);
  solver.updateVariables();

Example: Solve a width with a margin of 16 on both sides

```js
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);
```

- `constructor()` (constructor, since 0.2.1): Constructor.
- `addConstraint(constraint)` (method, since 0.2.1): Adds a constraint.
  - `constraint` LayoutConstraint: Constraint to add. (throws if already added or if required constraints contradict)
- `removeConstraint(constraint)` (method, since 0.2.1): Removes a constraint.
  - `constraint` LayoutConstraint: Constraint to remove. (throws if not added)
- `hasConstraint(constraint)` (method, since 0.2.1): Whether the constraint is registered.
  - `constraint` LayoutConstraint: Constraint to look for.
  - returns boolean: True if the constraint was added to the solver.
- `addEditVariable(variable, strength)` (method, since 0.2.1): Adds an edit variable. Must be registered before using suggestValue.
  - `variable` LayoutVariable: Variable to register as an edit variable. (throws if already registered)
  - `strength` number: Edit strength. (clamped to 0 ~ required, throws if required or above)

  Example: Register an edit variable and remove it again

  ```js
  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);
  ```

- `removeEditVariable(variable)` (method, since 0.2.1): Removes an edit variable.
  - `variable` LayoutVariable: Edit variable to unregister. (throws if not registered)
- `hasEditVariable(variable)` (method, since 0.2.1): Whether the edit variable is registered.
  - `variable` LayoutVariable: Variable to look for.
  - returns boolean: True if registered as an edit variable.
- `suggestValue(variable, value)` (method, since 0.2.1): 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.
  - `variable` LayoutVariable: Edit variable registered with addEditVariable. (throws if not registered)
  - `value` number: Desired value.

  Example: Keep a drag value within 0 ~ 300

  ```js
  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);
  ```

- `updateVariables()` (method, since 0.2.1): Updates variable values. Applies the constant of each external row of the tableau to each LayoutVariable.

  Example: Copy the solution into the variables

  ```js
  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);
  ```

- `getOrCreateSymbol(variable)` (method, since 0.2.1): Internal: LayoutVariable → LayoutSymbol mapping (created if missing).
  - `variable` LayoutVariable: External variable.
  - returns LayoutSymbol: External symbol paired with the variable. (creates and remembers a new one if none)
- `createRow(constraint, tag)` (method, since 0.2.1): Internal: constraint → row creation. Fills the tag with marker / other symbols.
  - `constraint` LayoutConstraint: Constraint to turn into a row.
  - `tag` LayoutTag: Tag to fill with the marker and other symbols.
  - returns LayoutRow: New row holding the constraint. (flips the sign if the constant term is negative)
- `chooseSubject(row, tag)` (method, since 0.2.1): Internal: chooses the subject symbol from a row.
  1) External variables first. 2) slack/error terms with negative coefficients. 3) invalid if neither.
  - `row` LayoutRow: Row to choose a subject from.
  - `tag` LayoutTag: Constraint tag of that row.
  - returns LayoutSymbol: Chosen symbol. (an invalid symbol if none)
- `addWithArtificialVariable(row)` (method, since 0.2.1): Internal: forcibly adds a row with an artificial variable. Returns whether it succeeded.
  - `row` LayoutRow: Row to add.
  - returns boolean: True if the row was added by driving the artificial variable to 0.
- `substitute(symbol, row)` (method, since 0.2.1): Internal: substitutes row for every occurrence of symbol in all rows and the objective.
  - `symbol` LayoutSymbol: Symbol to substitute.
  - `row` LayoutRow: Row to put in place of symbol.
- `optimize(objective)` (method, since 0.2.1): Internal: primary (primal) optimization. Repeats until the objective has no more negative coefficients.
  - `objective` LayoutRow: Objective row to minimize. (the solver objective row or the artificial row)
- `dualOptimize()` (method, since 0.2.1): Internal: dual simplex optimization. Handles infeasible rows.
- `getLeavingSymbol(enteringSymbol)` (method, since 0.2.1): Internal: determines the leaving symbol. (Bland's rule variant + ratio test)
  - `enteringSymbol` LayoutSymbol: Entering symbol.
  - returns LayoutSymbol: Symbol of the leaving row. (the one with the smallest ratio, an invalid symbol if none)
- `getDualEnteringSymbol(row)` (method, since 0.2.1): Internal: determines the dual entering symbol.
  - `row` LayoutRow: Row whose constant term is negative.
  - returns LayoutSymbol: Entering symbol. (the one with the smallest ratio, an invalid symbol if none)
- `getMarkerLeavingSymbol(marker)` (method, since 0.2.1): Internal: determines the row the marker leaves as leaving.
  - `marker` LayoutSymbol: Marker symbol to move out when removing a constraint.
  - returns LayoutSymbol: 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)
- `removeConstraintEffects(constraint, tag)` (method, since 0.2.1): Internal: removes the effect of the constraint's error variables on the objective function.
  - `constraint` LayoutConstraint: Constraint to remove.
  - `tag` LayoutTag: Tag of that constraint.
- `removeMarkerEffect(markerSymbol, strength)` (method, since 0.2.1): Internal: removes the effect of a single error variable.
  - `markerSymbol` LayoutSymbol: Error symbol.
  - `strength` number: Constraint strength.
