KiwiConstraintSolver.

using KiwiConstraintSolver

s = Solver()
x = Variable()
y = Variable()

add_constraint(s, x == 20)
add_constraint(s, x + 2 == y + 10)
update_variables(s)

x.value, y.value
(20.0, 12.0)

This module contains an implementation of the Cassowary constraint solving algorithm, based upon the work by G.J. Badros et al. in 2001. This algorithm is designed primarily for use constraining elements in user interfaces, but works well for many constraints that use floats. Constraints are linear combinations of the problem variables. The notable features of Cassowary that make it ideal for user interfaces are that it is incremental (i.e. you can add and remove constraints at runtime and it will perform the minimum work to update the result) and that the constraints can be violated if necessary, with the order in which they are violated specified by setting a "strength" for each constraint. This allows the solution to gracefully degrade, which is useful for when a user interface needs to compromise on its constraints in order to still be able to display something.

KiwiConstraintSolver.KiwiConstraintSolver — Module

This module contains an implementation of the Cassowary constraint solving algorithm, based upon the work by G.J. Badros et al. in 2001. This algorithm is designed primarily for use constraining elements in user interfaces, but works well for many constraints that use floats. Constraints are linear combinations of the problem variables. The notable features of Cassowary that make it ideal for user interfaces are that it is incremental (i.e. you can add and remove constraints at runtime and it will perform the minimum work to update the result) and that the constraints can be violated if necessary, with the order in which they are violated specified by setting a "strength" for each constraint. This allows the solution to gracefully degrade, which is useful for when a user interface needs to compromise on its constraints in order to still be able to display something.

source
KiwiConstraintSolver.add_constraint — Method

Add a constraint to the solver.

Errors:

  • DuplicateConstraint: The given constraint has already been added to the solver.
  • UnsatisfiableConstraint: The given constraint is required and cannot be satisfied.
source
KiwiConstraintSolver.add_edit_variable — Method

Add an edit variable to the solver.

This method should be called before the suggest_value method is used to supply a suggested value for the given edit variable.

Errors:

  • DuplicateEditVariableException: The given edit variable has already been added to the solver.
  • BadRequiredStrengthException: The given strength is >= required.
source
KiwiConstraintSolver.choose_subject — Method

Choose the subject for solving for the row.

This method will choose the best subject for using as the solve target for the row. An invalid symbol will be returned if there is no valid target.

The symbols are chosen according to the following precedence:

  1. The first symbol representing an external variable.
  2. A negative slack or error tag variable.

If a subject cannot be found, an invalid symbol will be returned.

source
KiwiConstraintSolver.create_row — Method

Create a new Row object for the given constraint.

The terms in the constraint will be converted to cells in the row. Any term in the constraint with a coefficient of zero is ignored. This method uses the getVarSymbol method to get the symbol for the variables added to the row. If the symbol for a given cell variable is basic, the cell variable will be substituted with the basic row.

The necessary slack and error variables will be added to the row. If the constant for the row is negative, the sign for the row will be inverted so the constant becomes positive.

The tag will be updated with the marker and error symbols to use for tracking the movement of the constraint in the tableau.

source
KiwiConstraintSolver.dual_optimize! — Method

Optimize the system using the dual of the simplex method.

The current state of the system should be such that the objective function is optimal, but not feasible. This method will perform an iteration of the dual simplex method to make the solution both optimal and feasible.

Errors:

  • InternalSolverException: The system cannot be dual optimized.
source
KiwiConstraintSolver.get_dual_entering_symbol — Method

Compute the entering symbol for the dual optimize operation.

This method will return the symbol in the row which has a positive coefficient and yields the minimum ratio for its respective symbol in the objective function. The provided row must be infeasible. If no symbol is found which meats the criteria, an invalid symbol is returned.

source
KiwiConstraintSolver.get_entering_symbol — Method

Compute the entering variable for a pivot operation.

This method will return first symbol in the objective function which is non-dummy and has a coefficient less than zero. If no symbol meets the criteria, it means the objective function is at a minimum, and an invalid symbol is returned.

source
KiwiConstraintSolver.get_leaving_row — Method

Compute the row which holds the exit symbol for a pivot.

This method will return an iterator to the row in the row map which holds the exit symbol. If no appropriate exit symbol is found, the end() iterator will be returned. This indicates that the objective function is unbounded.

source
KiwiConstraintSolver.get_marker_leaving_row — Method

Compute the leaving row for a marker variable.

This method will return an iterator to the row in the row map which holds the given marker variable. The row will be chosen according to the following precedence:

  1. The row with a restricted basic varible and a negative coefficient for the marker with the smallest ratio of -constant / coefficient.

  2. The row with a restricted basic variable and the smallest ratio of constant / coefficient.

  3. The last unrestricted row which contains the marker.

If the marker does not exist in any row, the row map end() iterator will be returned. This indicates an internal solver error since the marker should exist somewhere in the tableau.

source
KiwiConstraintSolver.insert! — Function

Insert a symbol into the row with a given coefficient.

If the symbol already exists in the row, the coefficient will be added to the existing coefficient. If the resulting coefficient is zero, the symbol will be removed from the row.

source
KiwiConstraintSolver.insert! — Function

Insert a row into this row with a given coefficient.

The constant and the cells of the other row will be multiplied by the coefficient and added to this row. Any cell with a resulting coefficient of zero will be removed from the row.

source
KiwiConstraintSolver.optimize! — Method

Optimize the system for the given objective function.

This method performs iterations of Phase 2 of the simplex method until the objective function reaches a minimum.

Errors:

  • InternalSolverException: The value of the objective function is unbounded.
source
KiwiConstraintSolver.runtests — Method
KiwiConstraintSolver.runtests(pattern...; kwargs...)

Equivalent to ReTest.retest(KiwiConstraintSolver, pattern...; kwargs...). This function is defined automatically in any module containing a @testset, possibly nested within submodules.

KiwiConstraintSolver.solve! — Method

Solve the row for the given symbols.

This method assumes the row is of the form x = b * y + c and will solve the row such that y = x / b - c / b. The rhs symbol will be removed from the row, the lhs added, and the result divided by the negative inverse of the rhs coefficient.

The lhs symbol must not exist in the row, and the rhs symbol must exist in the row.

source
KiwiConstraintSolver.solve! — Method

Solve the row for the given symbol.

This method assumes the row is of the form a * x + b * y + c = 0 and (assuming solve for x) will modify the row to represent the right hand side of x = -b/a * y - c / a. The target symbol will be removed from the row, and the constant and other cells will be multiplied by the negative inverse of the target coefficient.

The given symbol must exist in the row.

source
KiwiConstraintSolver.substitute! — Method

Substitute a symbol with the data from another row.

Given a row of the form a * x + b and a substitution of the form x = 3 * y + c the row will be updated to reflect the expression 3 * a * y + a * c + b.

If the symbol does not exist in the row, this is a no-op.

source
KiwiConstraintSolver.substitute! — Method

Substitute the parametric symbol with the given row.

This method will substitute all instances of the parametric symbol in the tableau and the objective function with the given row.

source
KiwiConstraintSolver.suggest_value — Method

Suggest a value for the given edit variable.

This method should be used after an edit variable as been added to the solver in order to suggest the value for that variable.

Errors:

UnknownEditVariable: The given edit variable has not been added to the solver.

source