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.IConstr;
35  import org.sat4j.specs.ISolver;
36  import org.sat4j.specs.IVecInt;
37  import org.sat4j.specs.TimeoutException;
38  
39  /**
40   * This solver decorator allows to detect whether or not the set of constraints
41   * available in the solver has only one solution or not.
42   * 
43   * NOTE THAT THIS DECORATOR CANNOT BE USED WITH SOLVERS USING SPECIFIC DATA
44   * STRUCTURES FOR BINARY OR TERNARY CLAUSES!
45   * 
46   * <code>
47   SingleSolutionDetector problem = 
48   new SingleSolutionDetector(SolverFactory.newMiniSAT());
49   // feed problem/solver as usual
50  
51   if (problem.isSatisfiable()) {
52   if (problem.hasASingleSolution()) {
53   // great, the instance has a unique solution
54   int [] uniquesolution = problem.getModel();
55   } else {
56   // too bad, got more than one
57   }
58   }
59   *  </code>
60   * 
61   * @author leberre
62   * 
63   */
64  public class SingleSolutionDetector extends SolverDecorator<ISolver> {
65  
66      /**
67       * 
68       */
69      private static final long serialVersionUID = 1L;
70  
71      public SingleSolutionDetector(ISolver solver) {
72          super(solver);
73      }
74  
75      /**
76       * Please use that method only after a positive answer from isSatisfiable()
77       * (else a runtime exception will be launched).
78       * 
79       * NOTE THAT THIS FUNCTION SHOULD NOT ONLY BE USED ONCE THE FINAL SOLUTION
80       * IS FOUND, SINCE THE METHOD ADDS CONSTRAINTS INTO THE SOLVER THAT MAY NOT
81       * BE REMOVED UNDER CERTAIN CONDITIONS (UNIT CONSTRAINTS LEARNT FOR
82       * INSTANCE). THAT ISSUE WILL BE RESOLVED ONCE REMOVECONSTR WILL WORK
83       * PROPERLY.
84       * 
85       * @return true iff there is only one way to satisfy all the constraints in
86       *         the solver.
87       * @throws TimeoutException
88       * @see {@link ISolver#removeConstr(IConstr)}
89       */
90      public boolean hasASingleSolution() throws TimeoutException {
91          return hasASingleSolution(new VecInt());
92      }
93  
94      /**
95       * Please use that method only after a positive answer from
96       * isSatisfiable(assumptions) (else a runtime exception will be launched).
97       * 
98       * @param assumptions
99       *            a set of literals (dimacs numbering) that must be satisfied.
100      * @return true iff there is only one way to satisfy all the constraints in
101      *         the solver using the provided set of assumptions.
102      * @throws TimeoutException
103      */
104     public boolean hasASingleSolution(IVecInt assumptions)
105             throws TimeoutException {
106         int[] firstmodel = model();
107         assert firstmodel != null;
108         IVecInt clause = new VecInt(firstmodel.length);
109         for (int q : firstmodel) {
110             clause.push(-q);
111         }
112         boolean result = false;
113         try {
114             IConstr added = addClause(clause);
115             result = !isSatisfiable(assumptions);
116             removeConstr(added);
117         } catch (ContradictionException e) {
118             result = true;
119         }
120         return result;
121     }
122 }