### Navigation

- index
- modules |
- next |
- previous |
- Sat »

# SAT-Solvers via DIMACS Files

Sage supports calling SAT solvers using the popular DIMACS format. This module implements infrastructure to make it easy to add new such interfaces and some example interfaces.

Currently, interfaces to RSat [RS] and Glucose [GL] are included by default.

Note

Our SAT solver interfaces are 1-based, i.e., literals start at 1. This is consistent with the popular DIMACS format for SAT solving but not with Pythion’s 0-based convention. However, this also allows to construct clauses using simple integers.

AUTHORS:

- Martin Albrecht (2012): first version

## Classes and Methods

class `sage.sat.solvers.dimacs.``DIMACS`(command=None, filename=None, verbosity=0, **kwds)

  Bases: `sage.sat.solvers.satsolver.SatSolver`

  Generic DIMACS Solver.

  Note

  Usually, users won’t have to use this class directly but some class which inherits from this class.

  `__init__`(command=None, filename=None, verbosity=0, **kwds)

    Construct a new generic DIMACS solver.

    INPUT:

    - `command` - a named format string with the command to run. The string must contain {input} and may contain {output} if the solvers writes the solution to an output file. For example “sat-solver {input}” is a valid command. If `None` then the class variable `command` is used. (default: `None`)
    - `filename` - a filename to write clauses to in DIMACS format, must be writable. If `None` a temporary filename is chosen automatically. (default: `None`)
    - `verbosity` - a verbosity level, where zero means silent and anything else means verbose output. (default: `0`)
    - `**kwds` - accepted for compatibility with other solves, ignored.

    TESTS:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: DIMACS()
        DIMACS Solver: ''

  `__call__`(assumptions=None)

    Run ‘command’ and collect output.

    INPUT:

    - `assumptions` - ignored, accepted for compatibility with other solvers (default: `None`)

    TESTS:

    This class is not meant to be called directly:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: fn = tmp_filename()
        sage: solver = DIMACS(filename=fn)
        sage: solver.add_clause( (1, -2 , 3) )
        sage: solver()
        Traceback (most recent call last):
        ...
        ValueError: No SAT solver command selected.

  `add_clause`(lits)

    Add a new clause to set of clauses.

    INPUT:

    - `lits` - a tuple of integers != 0

    Note

    If any element `e` in `lits` has `abs(e)` greater than the number of variables generated so far, then new variables are created automatically.

    EXAMPLE:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: solver = DIMACS()
        sage: solver.var()
        1
        sage: solver.var(decision=True)
        2
        sage: solver.add_clause( (1, -2 , 3) )
        sage: solver
        DIMACS Solver: ''

  `clauses`(filename=None)

    Return original clauses.

    INPUT:

    - `filename` - if not `None` clauses are written to `filename` in DIMACS format (default: `None`)

    OUTPUT:

    > If `filename` is `None` then a list of `lits, is_xor, rhs` tuples is returned, where `lits` is a tuple of literals, `is_xor` is always `False` and `rhs` is always `None`.

    > If `filename` points to a writable file, then the list of original clauses is written to that file in DIMACS format.

    EXAMPLE:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: fn = tmp_filename()
        sage: solver = DIMACS()
        sage: solver.add_clause( (1, 2, 3) )
        sage: solver.clauses()
        [((1, 2, 3), False, None)]

        sage: solver.add_clause( (1, 2, -3) )
        sage: solver.clauses(fn)
        sage: print open(fn).read()
        p cnf 3 2
        1 2 3 0
        1 2 -3 0
        <BLANKLINE>

  `nvars`()

    Return the number of variables.

    EXAMPLE:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: solver = DIMACS()
        sage: solver.var()
        1
        sage: solver.var(decision=True)
        2
        sage: solver.nvars()
        2

  static `render_dimacs`(clauses, filename, nlits)

    Produce DIMACS file `filename` from `clauses`.

    INPUT:

    - `clauses` - a list of clauses, either in simple format as a list of literals or in extended format for CryptoMiniSat: a tuple of literals, `is_xor` and `rhs`.
    - `filename` - the file to write to
    - `nlits -- the number of literals appearing in ``clauses`

    EXAMPLE:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: fn = tmp_filename()
        sage: solver = DIMACS()
        sage: solver.add_clause( (1, 2, -3) )
        sage: DIMACS.render_dimacs(solver.clauses(), fn, solver.nvars())
        sage: print open(fn).read()
        p cnf 3 1
        1 2 -3 0
        <BLANKLINE>

    This is equivalent to:

        sage: solver.clauses(fn)
        sage: print open(fn).read()
        p cnf 3 1
        1 2 -3 0
        <BLANKLINE>

    This function also accepts a “simple” format:

        sage: DIMACS.render_dimacs([ (1,2), (1,2,-3) ], fn, 3)
        sage: print open(fn).read()
        p cnf 3 2
        1 2 0
        1 2 -3 0
        <BLANKLINE>

  `var`(decision=None)

    Return a new variable.

    INPUT:

    - `decision` - accepted for compatibility with other solvers, ignored.

    EXAMPLE:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: solver = DIMACS()
        sage: solver.var()
        1

  `write`(filename=None)

    Write DIMACS file.

    INPUT:

    - `filename` - if `None` default filename specified at initialization is used for writing to (default: `None`)

    EXAMPLE:

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: fn = tmp_filename()
        sage: solver = DIMACS(filename=fn)
        sage: solver.add_clause( (1, -2 , 3) )
        sage: _ = solver.write()
        sage: for line in open(fn).readlines():
        ...      print line,
        p cnf 3 1
        1 -2 3 0

        sage: from sage.sat.solvers.dimacs import DIMACS
        sage: fn = tmp_filename()
        sage: solver = DIMACS()
        sage: solver.add_clause( (1, -2 , 3) )
        sage: _ = solver.write(fn)
        sage: for line in open(fn).readlines():
        ...      print line,
        p cnf 3 1
        1 -2 3 0

class `sage.sat.solvers.dimacs.``Glucose`(command=None, filename=None, verbosity=0, **kwds)

  Bases: `sage.sat.solvers.dimacs.DIMACS`

  An instance of the Glucose solver.

  For information on Glucose see: http://www.lri.fr/~simon/?page=glucose

class `sage.sat.solvers.dimacs.``RSat`(command=None, filename=None, verbosity=0, **kwds)

  Bases: `sage.sat.solvers.dimacs.DIMACS`

  An instance of the RSat solver.

  For information on RSat see: http://reasoning.cs.ucla.edu/rsat/

### Navigation

- index
- modules |
- next |
- previous |
- Sat »