Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

plpgen

0.2.1 • Public • Published

PLPGEN

PLPGEN is a utility for generating propositional problems in the format proposed by DIMACS and used by SATLib.

Installation

$ npm install plpgen

Usage

var plpgen = require('plpgen');

Create Clause Sets

Module plpgen exports the following functions, all of which return a set of clauses as a string that conforms to the DIMACS conventions:

pigeonHoles (holes)

Creates an instance of the Pigeon Hole Problem (i.e., the impossibility to fit n+1 pigeons into n holes when each hole can hold at most one pigeon). The number of holes is specified through parameter holes, which must be an integer greater than or equal to 1. The resulting set of clauses is guaranteed to be unsatisfiable.

nQueens (n)

Creates an instance of the n-Queens Problem, a theoretical chess problem that poses the question whether n queens can be placed on an n by n chess board so that no queen threatens any other queen. The resulting set of clauses is satisfiable for all n except n = 2 and n = 3. The number of propositional variables is the square of n, and the truth value of variable i represents the presence (true) or absence (false) of a queen in row 1 + (i - 1) div n and column 1 + (i - 1) mod n.

See also: nQueensFromAssignment

rndClauseSet (size, maxVar, minLits, maxLits)

Creates a set of random clauses. The number of clauses is specified through size, the number of propositional variables through maxVar. The optional parameters minLits and maxLits specify the minimal and maximal number of literals per clause, respectively. If omitted or invalid minLits will be 3. maxLits will be equal to minLits if omitted or invalid or less than minLits.

Note that the set of random clauses created with this function may contain duplicates, but guarantees to have exactly the given size.

See also: rndClauseSetNoDuplicates

rndClauseSetNoDuplicates (size, maxVar, minLits, maxLits)

Creates a set of random clauses like rndClauseSet, but guarantees that no clause occurs more than once. However, the final set of clauses may contain less clauses than desired, in particular if the number of distinct clauses is less than the given size.

Note that neither function for creating random sets of clauses guarantees that the set of clauses is free of redundancies in terms of one clause subsuming another. (This function does guarantee that only if minLits is equal to maxLits.) Furthermore, both functions may produce satisfiable or unsatisfiable sets of clauses.

sudokuToPL (Sudoku)

Transforms a classic Sudoku problem into a set of propositional clauses with the property that the set of clauses is satisfiable if and only if the given Sudoku problem has a solution that complies with the Sudoku rules. From an assignment satisfying the set of clauses the solved or completed Sudoku can be created with the help of sudokuFromAssignment.

The Sudoku problem is specified through parameter sudoku. Its value can be either a string or an array. If it is an array, the elements of the array are the rows. These can again be either strings or arrays of characters. If sudoku is a string, the rows may be separated via newline characters, but do not have to be. Without newline characters the first nine characters represent the first row and so on. The characters (or numbers) '1' through '9' obviously represent cells occupied with the respective number. All other characters represent an empty cell and are replaced with '*'.

A Sudoku problem is transformed into a set of 10287 + n clauses, where n is the number of non-empty cells, each clause drawing its literals from a pool of 729 propositional variables. Not surprisingly (given the backtracking nature of Sudoku problems) Tableaux-based methods are the weapon of choice for tackling such a set of clauses.

Convenience Functions

Module plpgen also exports the following convenience functions to interpret or visualize results (assignments) obtained for certain clause sets.

nQueensFromAssignment (assignment)

Transforms the given assignment (i.e., associations of propositional variables with the value true or false) into a chess board represented by an array of strings. Each string is a row and consists of characters '+' (empty square) and 'X' (square occupied by a queen).

Example:

Assuming that module propsat has been installed, the following code can be used to solve the n-Queens problem (in this case for n=4):

var assignment = require('propsat').createTableaux(plpgen.nQueens(4)).run().assignment;
if (assignment)
  console.log(plpgen.nQueensFromAssignment(assignment).join('\n'));
else
  console.log('No solution');

The output of this particular run would be:

-SOLUTION-
++X+
X+++
+++X
+X++

sudokuFromAssignment (assignment)

Transforms the given assignment (i.e., associations of propositional variables with the value true or false) into a completed Sudoku. The return value is an array of strings. Each string represents a row and hence has length 9. (The array naturally also has length 9.) The assignment must be an object with properties named 1 through 729 (i.e., the propositional variables used when transforming a Sudoku into a set of clauses in DIMACS format; see sudokuToPL). The values should be either true or false, although anything that evaluates to the desired Boolean value is accepted.

Note that this method does not check compliance with Sudoku rules other than that each cell is assigned one number and one number only. However, if the given assignment is legit in that it satisfies the clauses produced by sudokuToPL the completed Sudoku will comply with all Sudoku rules.

Typically you would present a system capable of finding an assignment for a given set of propositional clauses with the clauses produced by sudokuToPL, and then use sudokuFromAssignment to create the completed or solved Sudoku.

Example:

Assuming that module propsat has been installed and that variable sudoku holds a Sudoku problem, the following code can be used to solve Sudoku problems:

var assignment = require('propsat').createTableaux(plpgen.sudokuToPL(sudoku)).run().assignment;
if (assignment)
{
  console.log('-SOLUTION-');
  console.log(plpgen.sudokuFromAssignment(assignment).join('\n'));
}
else
  console.log('INVALID');

Install

npm i plpgen

DownloadsWeekly Downloads

1

Version

0.2.1

License

BSD

Unpacked Size

29.4 kB

Total Files

9

Last publish

Collaborators

  • avatar