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.specs;
31  
32  import java.io.PrintWriter;
33  
34  /**
35   * Access to the information related to a given problem instance.
36   * 
37   * @author leberre
38   */
39  public interface IProblem {
40      /**
41       * Provide a model (if any) for a satisfiable formula. That method should be
42       * called AFTER isSatisfiable() or isSatisfiable(IVecInt) if the formula is
43       * satisfiable. Else an exception UnsupportedOperationException is launched.
44       * 
45       * @return a model of the formula as an array of literals to satisfy.
46       * @see #isSatisfiable()
47       * @see #isSatisfiable(IVecInt)
48       */
49      int[] model();
50  
51      /**
52       * Provide the truth value of a specific variable in the model. That method
53       * should be called AFTER isSatisfiable() if the formula is satisfiable.
54       * Else an exception UnsupportedOperationException is launched.
55       * 
56       * @param var
57       *            the variable id in Dimacs format
58       * @return the truth value of that variable in the model
59       * @since 1.6
60       * @see #model()
61       */
62      boolean model(int var);
63  
64      /**
65       * Provide a prime implicant, i.e. a set of literal that is sufficient to
66       * satisfy all constraints of the problem.
67       * 
68       * 
69       * @return a prime implicant of the formula as an array of literal, Dimacs
70       *         format.
71       * @since 2.3
72       */
73      int[] primeImplicant();
74  
75      /**
76       * Check if a given literal is part of the prime implicant computed by the
77       * {@link #primeImplicant()} method.
78       * 
79       * @param p
80       *            a literal in Dimacs format
81       * @return true iff p belongs to {@link #primeImplicant()}
82       */
83      boolean primeImplicant(int p);
84  
85      /**
86       * Check the satisfiability of the set of constraints contained inside the
87       * solver.
88       * 
89       * @return true if the set of constraints is satisfiable, else false.
90       */
91      boolean isSatisfiable() throws TimeoutException;
92  
93      /**
94       * Check the satisfiability of the set of constraints contained inside the
95       * solver.
96       * 
97       * @param assumps
98       *            a set of literals (represented by usual non null integers in
99       *            Dimacs format).
100      * @param globalTimeout
101      *            whether that call is part of a global process (i.e.
102      *            optimization) or not. if (global), the timeout will not be
103      *            reset between each call.
104      * @return true if the set of constraints is satisfiable when literals are
105      *         satisfied, else false.
106      */
107     boolean isSatisfiable(IVecInt assumps, boolean globalTimeout)
108             throws TimeoutException;
109 
110     /**
111      * Check the satisfiability of the set of constraints contained inside the
112      * solver.
113      * 
114      * @param globalTimeout
115      *            whether that call is part of a global process (i.e.
116      *            optimization) or not. if (global), the timeout will not be
117      *            reset between each call.
118      * @return true if the set of constraints is satisfiable, else false.
119      */
120     boolean isSatisfiable(boolean globalTimeout) throws TimeoutException;
121 
122     /**
123      * Check the satisfiability of the set of constraints contained inside the
124      * solver.
125      * 
126      * @param assumps
127      *            a set of literals (represented by usual non null integers in
128      *            Dimacs format).
129      * @return true if the set of constraints is satisfiable when literals are
130      *         satisfied, else false.
131      */
132     boolean isSatisfiable(IVecInt assumps) throws TimeoutException;
133 
134     /**
135      * Look for a model satisfying all the clauses available in the problem. It
136      * is an alternative to isSatisfiable() and model() methods, as shown in the
137      * pseudo-code: <code>
138      if (isSatisfiable()) {
139      return model();
140      }
141      return null; 
142      </code>
143      * 
144      * @return a model of the formula as an array of literals to satisfy, or
145      *         <code>null</code> if no model is found
146      * @throws TimeoutException
147      *             if a model cannot be found within the given timeout.
148      * @since 1.7
149      */
150     int[] findModel() throws TimeoutException;
151 
152     /**
153      * Look for a model satisfying all the clauses available in the problem. It
154      * is an alternative to isSatisfiable(IVecInt) and model() methods, as shown
155      * in the pseudo-code: <code>
156      if (isSatisfiable(assumpt)) {
157      return model();
158      }
159      return null; 
160      </code>
161      * 
162      * @return a model of the formula as an array of literals to satisfy, or
163      *         <code>null</code> if no model is found
164      * @throws TimeoutException
165      *             if a model cannot be found within the given timeout.
166      * @since 1.7
167      */
168     int[] findModel(IVecInt assumps) throws TimeoutException;
169 
170     /**
171      * To know the number of constraints currently available in the solver.
172      * (without taking into account learned constraints).
173      * 
174      * @return the number of constraints added to the solver
175      */
176     int nConstraints();
177 
178     /**
179      * Declare <code>howmany</code> variables in the problem (and thus in the
180      * vocabulary), that will be represented using the Dimacs format by integers
181      * ranging from 1 to howmany. That feature allows encodings to create
182      * additional variables with identifier starting at howmany+1.
183      * 
184      * @param howmany
185      *            number of variables to create
186      * @return the total number of variables available in the solver (the
187      *         highest variable number)
188      * @see #nVars()
189      */
190     int newVar(int howmany);
191 
192     /**
193      * To know the number of variables used in the solver as declared by
194      * newVar()
195      * 
196      * In case the method newVar() has not been used, the method returns the
197      * number of variables used in the solver.
198      * 
199      * @return the number of variables created using newVar().
200      * @see #newVar(int)
201      */
202     int nVars();
203 
204     /**
205      * To print additional informations regarding the problem.
206      * 
207      * @param out
208      *            the place to print the information
209      * @param prefix
210      *            the prefix to put in front of each line
211      */
212     void printInfos(PrintWriter out, String prefix);
213 
214 }