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