It goes well with a wide variety of constraint satisfaction problems -- Even ones you cook up yourself!
TableSalt:new (domain, sizeX, sizeY) |
Creates a new TableSalt instance. |
TableSalt:setAddVarsAfterAnyChange (bool) |
switch to toggle when additional constraints should be added for solveConstraints. |
TableSalt:getAddVarsAfterAnyChange () |
returns where it's adding constraints. |
TableSalt:getIDByName (n) |
Returns the id given a variable name |
TableSalt:getIDByPair (x, y) |
Returns the id given a pair. |
TableSalt:getValueByID (i) |
Returns the value given the id |
TableSalt:getValueByName (n) |
Returns the value given a name |
TableSalt:getValueByPair (x, y) |
Returns the value given a pair. |
TableSalt:getDomainByID (i) |
Returns the domain given the id. |
TableSalt:getDomainByName (n) |
Returns the domain given a name |
TableSalt:getDomainByPair (x, y) |
Returns the domain given a pair. |
TableSalt:addConstraintByIDs (section, pepperConstraint, ...) |
add a constraint to the board via IDs. |
TableSalt:addConstraintByPairs (section, pepperConstraint, ...) |
add a constraint to the board via x,y position. |
TableSalt:addConstraintByNames (section, pepperConstraint, ...) |
add a constraint based on the name given in the input table. |
TableSalt:addConstraintForEachRow (pepperConstraint, ...) |
adds a constraint for each row. |
TableSalt:addConstraintForEachColumn (pepperConstraint, ...) |
adds a constraint for each column. |
TableSalt:addConstraintForAll (pepperConstraint, ...) |
adds a constraint for all values. |
TableSalt:isFilled () |
determines if each variable has a value |
TableSalt:isSolved () |
determines if the problem is solved based on the constraints that were added |
TableSalt:solveConstraints (specificCellID) |
runs the AC3 algorithm to reduce domains/solve the problem |
TableSalt:getSmallestDomainID () |
returns the id associated with the variable with the smallest domain. |
TableSalt:solveForwardCheck () |
runs the forward check algorithm in the current state. |
TableSalt:solve () |
solve the constraint satisfaction problem. |
TableSalt:print () |
prints out the problem either as a table, a row, or a grid. |
-
TableSalt:new (domain, sizeX, sizeY)
-
Creates a new TableSalt instance. This will initialize a table, where each cell has a unique id which hold
a value and a domain that can be accessed through various getters, as described below.
Parameters:
- domain
the domain for each of the cells in the problem
- sizeX
the length of the input or a table representing the variables
- sizeY
(optional) - the height of the domain space if the CSP exists over a grid
Returns:
a new TableSalt instance
Usage:
local TableSalt = require('TableSalt/TableSalt')
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"}) local sudoku = TableSalt:new({1,2,3,4,5,6,7,8,9}, 9, 9) local linear = TableSalt:new({1,2,3,4,5,6,7,8,9}, 9)
-
TableSalt:setAddVarsAfterAnyChange (bool)
-
switch to toggle when additional constraints should be added for solveConstraints.
When this is true, it will act like the classic AC3 algorithm
and add constraints after any domain has changed. When it's false, it will only add contraints after a value has
been set (aka, the domain has been reduced to 1). If the problem is easily solved by constraints, setting this to
true will incur a huge speedup (as in the case for sudoku).
Parameters:
Usage:
local sudoku = TableSalt:new({1,2,3,4,5,6,7,8,9}, 9, 9)
sudoku:setAddVarsAfterAnyChange(false)
-
TableSalt:getAddVarsAfterAnyChange ()
-
returns where it's adding constraints.
Returns:
true
if after any domain change. false
if only when a variable is assigned
-
TableSalt:getIDByName (n)
-
Returns the id given a variable name
Parameters:
- n
the name of a variable used in the problem
Returns:
the id associated with the given name
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:getIDByName("NSW")
-
TableSalt:getIDByPair (x, y)
-
Returns the id given a pair.
(0,0)
represents the top left
Parameters:
- x
the x position from the left
- y
the y position from the top
Returns:
the id associated with the pair
Usage:
local sudoku = TableSalt:new({1,2,3,4,5,6,7,8,9}, 9, 9)
sudoku:getIDByPair(5,5)
-
TableSalt:getValueByID (i)
-
Returns the value given the id
Parameters:
Returns:
the value associated with the given id or nil
if it hasn't been set.
Usage:
local linear = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 8)
linear:solve()
linear:getValueByID(7)
-
TableSalt:getValueByName (n)
-
Returns the value given a name
Parameters:
- n
the name of the variable
Returns:
the value associated with the given name or nil
if it hasn't been set.
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:solveForwardCheck()
aussie:getValueByName("WA")
-
TableSalt:getValueByPair (x, y)
-
Returns the value given a pair.
(0,0)
represents the top left
Parameters:
- x
the x position from the left
- y
the y position from the top
Returns:
the value associated with the pair or nil
if it hasn't been set.
Usage:
local sudoku = TableSalt:new({1,2,3,4,5,6,7,8,9}, 9, 9)
sudoku:solve()
sudoku:getValueByPair(5,5)
-
TableSalt:getDomainByID (i)
-
Returns the domain given the id.
Parameters:
Returns:
the domain (as a table) associated with the id. {nil}
if empty
Usage:
local linear = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 8)
linear:solveConstraints()
linear:getDomainyID(7)
-
TableSalt:getDomainByName (n)
-
Returns the domain given a name
Parameters:
- n
the name of the variable
Returns:
the domain (as a table) associated with the given name. {nil}
if empty
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:solveForwardCheck()
aussie:getDomainByName("WA")
-
TableSalt:getDomainByPair (x, y)
-
Returns the domain given a pair.
(0,0)
represents the top left
Parameters:
- x
the x position from the left
- y
the y position from the top
Returns:
the domain (as a table) associated with the given pair. {nil}
if empty
Usage:
local sudoku = TableSalt:new({1,2,3,4,5,6,7,8,9}, 9, 9)
sudoku:solveConstraints()
sudoku:getDomainByPair(5,5) sudoku:solveForwardCheck()
sudoku:getDomainByPair(5,5)
-
TableSalt:addConstraintByIDs (section, pepperConstraint, ...)
-
add a constraint to the board via IDs. For more information, check out PepperConstraints.md
Parameters:
- section
a table of id's
- pepperConstraint
a function which reduces domains based on a constraint
- ...
any additional arguments pepperConstraint requires
Usage:
local linear = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9)
linear:addConstraintByIDs({1, 3, 5}, Pepper.allDiff)
-
TableSalt:addConstraintByPairs (section, pepperConstraint, ...)
-
add a constraint to the board via x,y position. For more information, check out PepperConstraints.md
Parameters:
- section
a table of x,y pairs
- pepperConstraint
a function which reduces domains based on a constraint
- ...
any additional arguments pepperConstraint requires
Usage:
local sudoku = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9, 9)
sudoku:addConstraintByPairs({ {1, 1}, {5, 2}, {7, 2} }, Pepper.setVal, 4)
-
TableSalt:addConstraintByNames (section, pepperConstraint, ...)
-
add a constraint based on the name given in the input table. For more information, check out PepperConstraints.md
Parameters:
- section
a table of names
- pepperConstraint
a function which reduces domains based on a constraint
- ...
any additional arguments pepperConstraint requires
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:addConstraintByNames({ "WA", "NT", "SA" }, Pepper.allDiff)
-
TableSalt:addConstraintForEachRow (pepperConstraint, ...)
-
adds a constraint for each row. This is handy for grid based problems. For more information, check out PepperConstraints.md
Parameters:
- pepperConstraint
a function which reduces domains based on a constraint
- ...
any additional arguments pepperConstraint requires
Usage:
local sudoku = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9, 9)
sudoku:addConstraintForEachRow(Pepper.allDiff)
-
TableSalt:addConstraintForEachColumn (pepperConstraint, ...)
-
adds a constraint for each column. This is handy for grid based problems. For more information, check out PepperConstraints.md
Parameters:
- pepperConstraint
a function which reduces domains based on a constraint
- ...
any additional arguments pepperConstraint requires
Usage:
local sudoku = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9, 9)
sudoku:addConstraintForEachColumn(Pepper.allDiff)
-
TableSalt:addConstraintForAll (pepperConstraint, ...)
-
adds a constraint for all values. For more information, check out PepperConstraints.md
Parameters:
- pepperConstraint
a function which reduces domains based on a constraint
- ...
any additional arguments pepperConstraint requires
Usage:
local linear = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9)
linear:addConstraintForAll(Pepper.allDiff)
-
TableSalt:isFilled ()
-
determines if each variable has a value
Returns:
true
if each cell has a value, false
otherwise
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:isFilled()
-
TableSalt:isSolved ()
-
determines if the problem is solved based on the constraints that were added
Returns:
true
if all constraints are satisfied (domain has been reduced to 1 value). false
otherwise
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:isSolved() aussie:solveForwardCheck()
aussie:isSolved()
-
TableSalt:solveConstraints (specificCellID)
-
runs the AC3 algorithm to reduce domains/solve the problem
Parameters:
- specificCellID
(optional) useful for running constrains only associated with one cell. If omitted, solveConstraints will use all constraints
Returns:
true
if the problem was solved. false
otherwise
-
TableSalt:getSmallestDomainID ()
-
returns the id associated with the variable with the smallest domain.
If there's a tie, it uses the degree heuristic which picks the variable with the larger number of constraints
Returns:
the id of the variable with the smallest domain
Usage:
linear = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9)
linear:solveConstraints()
linear:getSmallestDomainID()
-
TableSalt:solveForwardCheck ()
-
runs the forward check algorithm in the current state.
It goes through each variable, tries a value from the domain, runs solveConstraints to prune,
backtracks if necessary, and finally determines a solution.
Returns:
true
if the problem is solved. false
otherwise
Usage:
local aussie = TableSalt:new({"Red", "Green", "Blue"}, {"WA", "NT", "SA", "Q", "NSW", "V", "T"})
aussie:solveForwardCheck()
-
TableSalt:solve ()
-
solve the constraint satisfaction problem.
This will call solveConstraints to reduce the domains and then solveForwardCheck to finish solving the problem
Returns:
true
if the problem was able to be solved. false
if not.
Usage:
local sudoku = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9, 9)
sudoku:setAddVarsAfterAnyChange(false)
sudoku:addConstraintForEachColumn(Pepper.allDiff)
sudoku:solve()
sudoku:print() -- print out the problem.
-
TableSalt:print ()
-
prints out the problem either as a table, a row, or a grid. How it prints out is dependent on how the inputs were given.
If the variables were given as a table, it will print out as a table. Otherwise it will print out as a grid.
Usage:
local sudoku = TableSalt:new({1, 2, 3, 4, 5, 6, 7, 8, 9}, 9, 9)
sudoku:print()
sudoku:solve()
sudoku:print()