1

I have a .cnf file which contains numbers as Conjunctive Normal Form.

I need to read and store them in a data structure (matrix or list) to be able to work with them as index. (I need this to solve a 3-SAT problem.)

How can I read and store them in Java?

c This Formular is generated by mcnf
c
c    horn? no 
c    forced? no 
c    mixed sat? no 
c    clause length = 3 
c
p cnf 20  91 
 10 -3 16 0
-8 20 -19 0
2 -6 -20 0
-7 9 3 0
3 15 -14 0
4 15 20 0
11 -9 -6 0
3 -17 19 0
11 5 -12 0
10 3 -15 0
2 15 18 0
-15 12 11 0
18 -19 -8 0
13 20 9 0
11 -10 -14 0
4 18 -9 0
-7 -17 5 0
-7 11 -15 0
6 2 20 0
16 -18 -17 0
4 -13 -20 0
11 17 -8 0
13 -11 -9 0
-11 13 19 0
12 -19 14 0
10 -1 -20 0
19 -20 13 0
13 2 11 0
17 19 -18 0
19 -20 -10 0
-18 16 15 0
-18 7 -20 0
1 -14 -17 0
1 -11 -18 0
-18 8 13 0
-8 4 16 0
-10 1 13 0
9 3 -20 0
-13 4 8 0
17 -11 18 0
18 20 2 0
-20 -1 4 0
-19 2 -9 0
-9 -16 -15 0
-2 12 9 0
5 19 6 0
-8 -5 -13 0
-18 20 -6 0
5 -18 12 0
2 5 19 0
-5 -8 -11 0
-20 -17 11 0
-18 -14 -16 0
-3 -18 -7 0
-11 20 17 0
-1 -15 -13 0
9 -5 11 0
-17 -7 -1 0
-6 -1 -16 0
-3 -15 -19 0
17 14 11 0
-17 12 13 0
16 12 -2 0
14 10 -16 0
8 -4 5 0
-5 16 17 0
-18 -1 -15 0
11 -15 -13 0
16 -9 -7 0
-8 -15 2 0
-19 -10 1 0
12 -15 -20 0
13 -10 9 0
17 7 18 0
20 15 -2 0
-6 -7 -1 0
14 11 15 0
18 13 -9 0
-4 -12 -2 0
-13 -5 -9 0
5 13 16 0
20 -14 -15 0
19 -20 18 0
19 -17 13 0
3 19 14 0
6 3 20 0
-8 -20 -2 0
12 -10 -19 0
-2 -5 -8 0
13 -4 -11 0
-5 -10 19 0
%
0
PEN
  • 21
  • 1
  • 4
  • If you ask your search engine for "Java DIMACS", you get hits as [this one](http://kahina.org/trac/browser/trunk/src/org/kahina/logic/sat/io/cnf/DimacsCnfParser.java). Familiarize yourself with the DIMACS format for CNF. Each clause line is a list of literals terminated with a 0. Negated literals are encoded as negative integers. The p line specifies the number of variables and clauses. – Axel Kemper Jun 09 '16 at 07:14
  • @AxelKemper i didn't know that Dimacs term, thank you very much! – PEN Jun 09 '16 at 12:25
  • @AxelKemper thanks for the Kahina source. When I did Google search about DIMACS, i couldn't find better parser than it. But seems that I am not able to implement that code, it is too big, and I think its classes/packages are tightly coupled! :( Any easier way to do this? – PEN Jun 10 '16 at 01:21
  • Java + Dimacs, I feel the need to point you http://sat4j.org It will read and solve your problem, it is well documented and efficient. – Valentin Montmirail Jul 22 '16 at 13:27

2 Answers2

2

From a birds-view perspective, the CNF reader pseudo code looks like this (in C#):

StreamReader cnf = openReader(fileName);
int noOfVars = 0;

while (!cnf.EndOfStream)
{
    line = cnf.ReadLine().Trim();

    if (line.Length >= 1)
    {
       c = line[0];
       if ((noOfVars > 0) && 
           ((c == '-') || ((c >= '0') && (c <= '9'))))
       {
           Clause cl = new Clause(line);

           ListOfClauses.Add(cl);
       }
       else if (c == 'c')
       {
           processCStatement(line);
       }
       else if (c == 'p')
       {
           processPStatement(line, ref noOfVars, ref noOfClauses);
       }
       else
       {
           error("Statement has neither 'c' nor 'p'  in first column: " + line[0]);
           break;
       }
   }
}

To construct a Clause object from a CNF line:

 public Clause(string line)
 {
       int id = -1;
       string[] arr = line.Split(whitespaceSeparator, StringSplitOptions.RemoveEmptyEntries);

       if (arr.Length < 1)
       {
           raise("Empty clause!");
       }

       foreach (string s in arr)
       {
           try
           {
               id = int.Parse(s);
           }
           catch (Exception)
           {
               raise("Invalid literal: " + s);
           }

           if (id != 0)
           {
               Literal lit = new Literal(id);
               this.Add(lit);
           }
       }

       if (id != 0)
       {
           raise("Line does not end with '0'");
       }

       //  sort literals and remove duplicates
       this.unify();
   }

This pseudo code assumes that the CNF ist stored as list of Clause objects. Each Clause is a list of Literal objects. A Literal has a positive variable ID and an inverted or non-inverted polarity.

In terms of performance, it might be better to store the literals as integer arrays (or even bit-sets) rather than as list of objects.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
0

If you want to use the library SAT4J (http://www.sat4j.org/), it will read a .cnf in Java without any problem.

public class Example {

    public static void main(String[] args) {
        ISolver solver = SolverFactory.newDefault();
        Reader reader = new DimacsReader(solver);    
        // CNF filename is given on the command line 
        try {
            IProblem problem = reader.parseInstance(args[0]);
       } catch (FileNotFoundException e) {

       } catch (ParseFormatException e) {

        } catch (IOException e) {

        }
     }
 }

This library is designed to read and solve CNF formula :). But you can use it just to read as you wanted :).

Valentin Montmirail
  • 2,594
  • 1
  • 25
  • 53