View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.maxsat.reader;
31  
32  import java.io.IOException;
33  import java.math.BigInteger;
34  
35  import org.sat4j.maxsat.WeightedMaxSatDecorator;
36  import org.sat4j.reader.DimacsReader;
37  import org.sat4j.reader.ParseFormatException;
38  import org.sat4j.specs.ContradictionException;
39  
40  /**
41   * Simple reader for the weighted maxsat problem.
42   * 
43   * @author daniel
44   * 
45   */
46  public class WDimacsReader extends DimacsReader {
47  
48      protected BigInteger weight;
49      protected BigInteger top;
50  
51      @Override
52      protected void flushConstraint() throws ContradictionException {
53          try {
54              this.decorator.addSoftClause(this.weight, this.literals);
55          } catch (IllegalArgumentException ex) {
56              if (isVerbose()) {
57                  System.err.println("c Skipping constraint " + this.literals);
58              }
59          }
60  
61      }
62  
63      @Override
64      protected boolean handleLine() throws ContradictionException, IOException,
65              ParseFormatException {
66          this.weight = this.scanner.nextBigInteger();
67          return super.handleLine();
68      }
69  
70      /**
71       * 
72       */
73      private static final long serialVersionUID = 1L;
74  
75      private final WeightedMaxSatDecorator decorator;
76  
77      public WDimacsReader(WeightedMaxSatDecorator solver) {
78          super(solver, "wcnf");
79          this.decorator = solver;
80      }
81  
82      public WDimacsReader(WeightedMaxSatDecorator solver, String format) {
83          super(solver, format);
84          this.decorator = solver;
85      }
86  
87      @Override
88      protected void readProblemLine() throws IOException, ParseFormatException {
89          String line = this.scanner.nextLine().trim();
90  
91          if (line == null) {
92              throw new ParseFormatException(
93                      "premature end of file: <p cnf ...> expected");
94          }
95          String[] tokens = line.split("\\s+");
96          if (tokens.length < 4 || !"p".equals(tokens[0])
97                  || !this.formatString.equals(tokens[1])) {
98              throw new ParseFormatException("problem line expected (p cnf ...)");
99          }
100 
101         int vars;
102 
103         // reads the max var id
104         vars = Integer.parseInt(tokens[2]);
105         assert vars > 0;
106         this.solver.newVar(vars);
107         // reads the number of clauses
108         this.expectedNbOfConstr = Integer.parseInt(tokens[3]);
109         assert this.expectedNbOfConstr > 0;
110         this.solver.setExpectedNumberOfClauses(this.expectedNbOfConstr);
111 
112         if ("wcnf".equals(this.formatString)) {
113             // assume we are in weighted MAXSAT mode
114             if (tokens.length == 5) {
115                 this.top = new BigInteger(tokens[4]);
116             } else {
117                 this.top = WeightedMaxSatDecorator.SAT4J_MAX_BIG_INTEGER;
118             }
119             this.decorator.setTopWeight(this.top);
120         }
121     }
122 
123 }