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.tools;
31  
32  import org.sat4j.core.VecInt;
33  import org.sat4j.specs.ContradictionException;
34  import org.sat4j.specs.ISolver;
35  import org.sat4j.specs.IVecInt;
36  import org.sat4j.specs.TimeoutException;
37  
38  /**
39   * Another solver decorator that counts the number of solutions.
40   * 
41   * Note that this approach is quite naive so do not expect it to work on large
42   * examples. The number of solutions will be wrong if the SAT solver does not
43   * provide a complete assignment.
44   * 
45   * The class is expected to be used that way:
46   * 
47   * <pre>
48   * SolutionCounter counter = new SolverCounter(SolverFactory.newDefault());
49   * try {
50   *     int nbSol = counter.countSolutions();
51   *     // the exact number of solutions is nbSol
52   *     ...
53   *  } catch (TimeoutException te) {
54   *     int lowerBound = counter.lowerBound();
55   *     // the solver found lowerBound solutions so far.
56   *     ...
57   *  }
58   * </pre>
59   * 
60   * @author leberre
61   * 
62   */
63  public class SolutionCounter extends SolverDecorator<ISolver> {
64  
65      /**
66       * 
67       */
68      private static final long serialVersionUID = 1L;
69  
70      private int lowerBound;
71  
72      public SolutionCounter(ISolver solver) {
73          super(solver);
74      }
75  
76      /**
77       * Get the number of solutions found before the timeout occurs.
78       * 
79       * @return the number of solutions found so far.
80       * @since 2.1
81       */
82      public int lowerBound() {
83          return this.lowerBound;
84      }
85  
86      /**
87       * Naive approach to count the solutions available in a boolean formula:
88       * each time a solution is found, a new clause is added to prevent it to be
89       * found again.
90       * 
91       * @return the number of solution found.
92       * @throws TimeoutException
93       *             if the timeout given to the solver is reached.
94       */
95      public long countSolutions() throws TimeoutException {
96          this.lowerBound = 0;
97          boolean trivialfalsity = false;
98  
99          while (!trivialfalsity && isSatisfiable(true)) {
100             this.lowerBound++;
101             int[] last = model();
102             IVecInt clause = new VecInt(last.length);
103             for (int q : last) {
104                 clause.push(-q);
105             }
106             try {
107                 addClause(clause);
108             } catch (ContradictionException e) {
109                 trivialfalsity = true;
110             }
111         }
112         return this.lowerBound;
113     }
114 }