View Javadoc

1   /*******************************************************************************
2   * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
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  package org.sat4j.maxsat.reader;
20  
21  import java.io.IOException;
22  import java.io.LineNumberReader;
23  import java.util.Scanner;
24  import java.util.StringTokenizer;
25  
26  import org.sat4j.maxsat.MinCostDecorator;
27  import org.sat4j.reader.DimacsReader;
28  import org.sat4j.reader.ParseFormatException;
29  
30  /**
31   * Simple reader for the weighted maxsat problem.
32   * 
33   * @author daniel
34   *
35   */
36  public class P2DimacsReader extends DimacsReader {
37  
38      /**
39       * 
40       */
41      private static final long serialVersionUID = 1L;
42  
43      public P2DimacsReader(MinCostDecorator solver) {
44          super(solver,"p2cnf");
45      }
46  
47      @Override
48      protected void readProblemLine(LineNumberReader in) throws IOException,
49              ParseFormatException {
50          String line = in.readLine();
51  
52          if (line == null) {
53              throw new ParseFormatException(
54                      "premature end of file: <p cnf ...> expected  on line "
55                              + in.getLineNumber());
56          }
57          StringTokenizer stk = new StringTokenizer(line);
58  
59          if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
60                  && stk.hasMoreTokens() && stk.nextToken().equals(formatString))) {
61              throw new ParseFormatException(
62                      "problem line expected (p cnf ...) on line "
63                              + in.getLineNumber());
64          }
65  
66          int vars;
67  
68          // reads the max var id
69          vars = Integer.parseInt(stk.nextToken());
70          assert vars > 0;
71          solver.newVar(vars);
72          // reads the number of clauses
73          expectedNbOfConstr = Integer.parseInt(stk.nextToken());
74          assert expectedNbOfConstr > 0;
75          solver.setExpectedNumberOfClauses(expectedNbOfConstr);
76  
77          if ("p2cnf".equals(formatString)) {
78              // next line should contain the optimization function`
79              String optLine = in.readLine();
80              if (!optLine.startsWith("min: ")) {
81                  throw new ParseFormatException("p2 file does not contain the function to minimize!");
82              }
83              String optfunction = optLine.substring(5);
84              MinCostDecorator mysolver = (MinCostDecorator)solver;
85              int var, coeff;
86              Scanner values = new Scanner(optfunction);
87              while (values.hasNext()) {
88                  coeff = values.nextInt();
89                  assert values.hasNext();
90                  var = values.nextInt();
91                  mysolver.setCost(var, coeff);
92              }
93          }
94      }
95  
96      
97  }