class Cassowary::Solver

Overview

An incremental solver for a system of linear constraints.

Add and remove constraints at any time, steer edit variables with #suggest, and call #update to write the solution into the variables.

x = Variable.new("x")
solver = Solver.new
solver.add(x >= 10)
solver.add((x == 0).strength(:weak))
solver.update
x.value # => 10.0

Defined in:

cassowary/solver.cr

Instance Method Summary

Instance Method Detail

def [](variable : Variable) : Float64 #

Returns the value the solver assigns variable, without writing it into the variable; zero for a variable no constraint uses.

Unlike #update, this lets several solvers share a variable.


[View source]
def add(constraint : Constraint) : Constraint #

Adds constraint to the solver and returns it.

Raises DuplicateConstraint if the constraint is already in the solver and UnsatisfiableConstraint if it is required and conflicts with the required constraints. A rejected constraint leaves the solver unchanged.


[View source]
def add(constraints : Enumerable(Constraint)) : Nil #

Adds every constraint of constraints, in order.

Raises like #add; the constraints added before the failing one are removed again, so a failure leaves the solver's constraints unchanged.


[View source]
def add(first : Constraint, *rest : Constraint) : Nil #

Adds every constraint of constraints, in order.

Raises like #add; the constraints added before the failing one are removed again, so a failure leaves the solver's constraints unchanged.


[View source]
def add_edit(variable : Variable, strength : Strength | Strength::Preset = Strength::STRONG) : Nil #

Makes variable an edit variable with strength, so #suggest can steer it.

Raises DuplicateEdit if variable is already an edit variable and BadRequiredStrength if strength is required.


[View source]
def constraints : Array(Constraint) #

Returns the constraints added with #add, in the order they were added.


[View source]
def dump(io : IO) : Nil #

Writes the solver's internal state to io, for debugging.

The format is not stable.


[View source]
def dump : String #

Writes the solver's internal state to io, for debugging.

The format is not stable.


[View source]
def edit(variable : Variable, strength : Strength | Strength::Preset = Strength::STRONG, & : Edit -> ) : Nil #

Makes variable an edit variable with strength for the duration of the block, and yields an Edit that steers it.

Raises like #add_edit. The edit variable is removed when the block returns or raises.

solver.edit(width) do |edit|
  edit.suggest(250)
  solver.update
end

[View source]
def explain(constraint : Constraint) : Array(Constraint) #

Returns a minimal set of required constraints in the solver that constraint conflicts with: together they reject it, and without any one of them they accept it. Other required constraints may conflict with it as well.

The set is empty when constraint can be added or contradicts itself on its own. Leaves the solver unchanged. It costs one trial solve per constraint involved in the conflict, so call it to diagnose a rejection: a few hundred constraints take well under a second, a conflict through a thousand takes seconds.


[View source]
def has?(constraint : Constraint) : Bool #

Returns whether constraint is in the solver.


[View source]
def has_edit?(variable : Variable) : Bool #

Returns whether variable is an edit variable.


[View source]
def remove(constraint : Constraint) : Nil #

Removes constraint from the solver.

Raises UnknownConstraint if the constraint is not in the solver.


[View source]
def remove(constraints : Enumerable(Constraint)) : Nil #

Removes every constraint of constraints.

Raises UnknownConstraint before removing anything if one of them is not in the solver.


[View source]
def remove(first : Constraint, *rest : Constraint) : Nil #

Removes every constraint of constraints.

Raises UnknownConstraint before removing anything if one of them is not in the solver.


[View source]
def remove_edit(variable : Variable) : Nil #

Stops variable from being an edit variable.

Raises UnknownEdit if variable is not an edit variable.


[View source]
def reset : Nil #

Removes every constraint and edit variable.


[View source]
def suggest(variable : Variable, value : Number) : Nil #

Suggests value for the edit variable variable.

Raises UnknownEdit if variable is not an edit variable.


[View source]
def suggest(values : Enumerable(Tuple(Variable, Number))) : Nil #

Suggests a value for each edit variable in values, a collection of {variable, value} pairs, at once.

Raises UnknownEdit, before changing anything, if a variable is not an edit variable.

solver.suggest([{width, 250}, {height, 100}])

[View source]
def update : Nil #

Writes the current solution into the value of every variable in the solver.


[View source]
def update(& : Variable -> ) : Nil #

Writes the current solution into the value of every variable in the solver, and yields each variable whose value changed.


[View source]