View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le
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.minisat;
20  
21  import org.sat4j.core.ASolverFactory;
22  import org.sat4j.minisat.constraints.ClausalDataStructureCBWL;
23  import org.sat4j.minisat.constraints.MixedDataStructureDanielHT;
24  import org.sat4j.minisat.constraints.MixedDataStructureDanielWL;
25  import org.sat4j.minisat.core.DataStructureFactory;
26  import org.sat4j.minisat.core.IOrder;
27  import org.sat4j.minisat.core.SearchParams;
28  import org.sat4j.minisat.core.Solver;
29  import org.sat4j.minisat.learning.LimitedLearning;
30  import org.sat4j.minisat.learning.MiniSATLearning;
31  import org.sat4j.minisat.learning.NoLearningButHeuristics;
32  import org.sat4j.minisat.learning.PercentLengthLearning;
33  import org.sat4j.minisat.orders.PhaseCachingAutoEraseStrategy;
34  import org.sat4j.minisat.orders.PureOrder;
35  import org.sat4j.minisat.orders.RSATPhaseSelectionStrategy;
36  import org.sat4j.minisat.orders.RandomWalkDecorator;
37  import org.sat4j.minisat.orders.VarOrderHeap;
38  import org.sat4j.minisat.restarts.ArminRestarts;
39  import org.sat4j.minisat.restarts.LubyRestarts;
40  import org.sat4j.minisat.restarts.MiniSATRestarts;
41  import org.sat4j.minisat.restarts.NoRestarts;
42  import org.sat4j.minisat.uip.DecisionUIP;
43  import org.sat4j.minisat.uip.FirstUIP;
44  import org.sat4j.opt.MinOneDecorator;
45  import org.sat4j.specs.ISolver;
46  import org.sat4j.tools.DimacsOutputSolver;
47  import org.sat4j.tools.OptToSatAdapter;
48  
49  /**
50   * User friendly access to pre-constructed solvers.
51   * 
52   * @author leberre
53   */
54  public final class SolverFactory extends ASolverFactory<ISolver> {
55  
56  	/**
57       * 
58       */
59  	private static final long serialVersionUID = 1L;
60  
61  	// thread safe implementation of the singleton design pattern
62  	private static SolverFactory instance;
63  
64  	/**
65  	 * Private constructor. Use singleton method instance() instead.
66  	 * 
67  	 * @see #instance()
68  	 */
69  	private SolverFactory() {
70  		super();
71  	}
72  
73  	private static synchronized void createInstance() {
74  		if (instance == null) {
75  			instance = new SolverFactory();
76  		}
77  	}
78  
79  	/**
80  	 * Access to the single instance of the factory.
81  	 * 
82  	 * @return the singleton of that class.
83  	 */
84  	public static SolverFactory instance() {
85  		if (instance == null) {
86  			createInstance();
87  		}
88  		return instance;
89  	}
90  
91  	/**
92  	 * @return a "default" "minilearning" solver learning clauses of size
93  	 *         smaller than 10 % of the total number of variables with a heap
94  	 *         based var order.
95  	 */
96  	public static Solver<DataStructureFactory> newMiniLearningHeap() {
97  		return newMiniLearningHeap(new MixedDataStructureDanielWL());
98  	}
99  
100 	public static Solver<DataStructureFactory> newMiniLearningHeapEZSimp() {
101 		Solver<DataStructureFactory> solver = newMiniLearningHeap();
102 		solver.setSimplifier(solver.SIMPLE_SIMPLIFICATION);
103 		return solver;
104 	}
105 
106 	public static Solver<DataStructureFactory> newMiniLearningHeapExpSimp() {
107 		Solver<DataStructureFactory> solver = newMiniLearningHeap();
108 		solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
109 		return solver;
110 	}
111 
112 	public static Solver<DataStructureFactory> newMiniLearningHeapRsatExpSimp() {
113 		Solver<DataStructureFactory> solver = newMiniLearningHeapExpSimp();
114 		solver.setOrder(new VarOrderHeap(new RSATPhaseSelectionStrategy()));
115 		return solver;
116 	}
117 
118 	public static Solver<DataStructureFactory> newMiniLearningHeapRsatExpSimpBiere() {
119 		Solver<DataStructureFactory> solver = newMiniLearningHeapRsatExpSimp();
120 		solver.setRestartStrategy(new ArminRestarts());
121 		solver.setSearchParams(new SearchParams(1.1, 100));
122 		return solver;
123 	}
124 
125 	public static Solver<DataStructureFactory> newMiniLearningHeapRsatExpSimpLuby() {
126 		Solver<DataStructureFactory> solver = newMiniLearningHeapRsatExpSimp();
127 		solver.setRestartStrategy(new LubyRestarts());
128 		return solver;
129 	}
130 
131 	private static Solver<DataStructureFactory> newBestCurrentSolverConfiguration(
132 			DataStructureFactory dsf) {
133 		MiniSATLearning<DataStructureFactory> learning = new MiniSATLearning<DataStructureFactory>();
134 		Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(
135 				new FirstUIP(), learning, dsf, new VarOrderHeap(
136 						new RSATPhaseSelectionStrategy()), new ArminRestarts());
137 		solver.setSearchParams(new SearchParams(1.1, 100));
138 		learning.setSolver(solver);
139 		solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
140 		return solver;
141 	}
142 
143 	/**
144 	 * 
145 	 * @since 2.2
146 	 */
147 	public static Solver<DataStructureFactory> newGreedySolver() {
148 		MiniSATLearning<DataStructureFactory> learning = new MiniSATLearning<DataStructureFactory>();
149 		Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(
150 				new FirstUIP(), learning, new MixedDataStructureDanielWL(),
151 				new RandomWalkDecorator(new VarOrderHeap(
152 						new RSATPhaseSelectionStrategy())), new NoRestarts());
153 		// solver.setSearchParams(new SearchParams(1.1, 100));
154 		learning.setSolver(solver);
155 		solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
156 		return solver;
157 	}
158 
159 	/**
160 	 * @since 2.2
161 	 */
162 	public static Solver<DataStructureFactory> newDefaultAutoErasePhaseSaving() {
163 		Solver<DataStructureFactory> solver = newBestWL();
164 		solver.setOrder(new VarOrderHeap(new PhaseCachingAutoEraseStrategy()));
165 		return solver;
166 	}
167 
168 	/**
169 	 * @since 2.1
170 	 */
171 	public static Solver<DataStructureFactory> newBestWL() {
172 		return newBestCurrentSolverConfiguration(new MixedDataStructureDanielWL());
173 	}
174 
175 	/**
176 	 * 
177 	 * @since 2.1
178 	 */
179 	public static Solver<DataStructureFactory> newBestHT() {
180 		return newBestCurrentSolverConfiguration(new MixedDataStructureDanielHT());
181 	}
182 
183 	/**
184 	 * @since 2.1
185 	 */
186 	public static Solver<DataStructureFactory> newGlucose() {
187 		Solver<DataStructureFactory> solver = newBestWL();
188 		solver.setLearnedConstraintsDeletionStrategy(solver.glucose);
189 		solver.setRestartStrategy(new LubyRestarts(512));
190 		return solver;
191 	}
192 
193 	/**
194 	 * @param dsf
195 	 *            a specific data structure factory
196 	 * @return a default "minilearning" solver using a specific data structure
197 	 *         factory, learning clauses of length smaller or equals to 10 % of
198 	 *         the number of variables and a heap based VSIDS heuristics
199 	 */
200 	public static Solver<DataStructureFactory> newMiniLearningHeap(
201 			DataStructureFactory dsf) {
202 		return newMiniLearning(dsf, new VarOrderHeap());
203 	}
204 
205 	/**
206 	 * @return a default minilearning SAT solver choosing periodically to branch
207 	 *         on "pure watched" literals if any. (a pure watched literal l is a
208 	 *         literal that is watched on at least one clause such that its
209 	 *         negation is not watched at all. It is not necessarily a watched
210 	 *         literal.)
211 	 */
212 	public static Solver<DataStructureFactory> newMiniLearningPure() {
213 		return newMiniLearning(new MixedDataStructureDanielWL(),
214 				new PureOrder());
215 	}
216 
217 	/**
218 	 * @return a default minilearning SAT solver choosing periodically to branch
219 	 *         on literal "pure in the original set of clauses" if any.
220 	 */
221 	public static Solver<DataStructureFactory> newMiniLearningCBWLPure() {
222 		return newMiniLearning(new ClausalDataStructureCBWL(), new PureOrder());
223 	}
224 
225 	/**
226 	 * @param dsf
227 	 *            the data structure factory used to represent literals and
228 	 *            clauses
229 	 * @param order
230 	 *            the heuristics
231 	 * @return a SAT solver with learning limited to clauses of length smaller
232 	 *         or equal to 10 percent of the total number of variables, the dsf
233 	 *         data structure, the FirstUIP clause generator and order as
234 	 *         heuristics.
235 	 */
236 	public static Solver<DataStructureFactory> newMiniLearning(
237 			DataStructureFactory dsf, IOrder order) {
238 		// LimitedLearning<DataStructureFactory> learning = new
239 		// PercentLengthLearning<DataStructureFactory>(10);
240 		MiniSATLearning<DataStructureFactory> learning = new MiniSATLearning<DataStructureFactory>();
241 		Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(
242 				new FirstUIP(), learning, dsf, order, new MiniSATRestarts());
243 		learning.setSolver(solver);
244 		return solver;
245 	}
246 
247 	/**
248 	 * @return a default MiniLearning without restarts.
249 	 */
250 	public static Solver<DataStructureFactory> newMiniLearningHeapEZSimpNoRestarts() {
251 		LimitedLearning<DataStructureFactory> learning = new PercentLengthLearning<DataStructureFactory>(
252 				10);
253 		Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(
254 				new FirstUIP(), learning, new MixedDataStructureDanielWL(),
255 				new SearchParams(Integer.MAX_VALUE), new VarOrderHeap(),
256 				new MiniSATRestarts());
257 		learning.setSolver(solver);
258 		solver.setSimplifier(solver.SIMPLE_SIMPLIFICATION);
259 		return solver;
260 	}
261 
262 	/**
263 	 * @return a default MiniLearning with restarts beginning at 1000 conflicts.
264 	 */
265 	public static Solver<DataStructureFactory> newMiniLearningHeapEZSimpLongRestarts() {
266 		LimitedLearning<DataStructureFactory> learning = new PercentLengthLearning<DataStructureFactory>(
267 				10);
268 		Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(
269 				new FirstUIP(), learning, new MixedDataStructureDanielWL(),
270 				new SearchParams(1000), new VarOrderHeap(),
271 				new MiniSATRestarts());
272 		learning.setSolver(solver);
273 		solver.setSimplifier(solver.SIMPLE_SIMPLIFICATION);
274 		return solver;
275 	}
276 
277 	/**
278 	 * @return a SAT solver very close to the original MiniSAT sat solver.
279 	 */
280 	public static Solver<DataStructureFactory> newMiniSATHeap() {
281 		return newMiniSATHeap(new MixedDataStructureDanielWL());
282 	}
283 
284 	/**
285 	 * @return a SAT solver very close to the original MiniSAT sat solver
286 	 *         including easy reason simplification.
287 	 */
288 	public static Solver<DataStructureFactory> newMiniSATHeapEZSimp() {
289 		Solver<DataStructureFactory> solver = newMiniSATHeap();
290 		solver.setSimplifier(solver.SIMPLE_SIMPLIFICATION);
291 		return solver;
292 	}
293 
294 	public static Solver<DataStructureFactory> newMiniSATHeapExpSimp() {
295 		Solver<DataStructureFactory> solver = newMiniSATHeap();
296 		solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
297 		return solver;
298 	}
299 
300 	public static Solver<DataStructureFactory> newMiniSATHeap(
301 			DataStructureFactory dsf) {
302 		MiniSATLearning<DataStructureFactory> learning = new MiniSATLearning<DataStructureFactory>();
303 		Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(
304 				new FirstUIP(), learning, dsf, new VarOrderHeap(),
305 				new MiniSATRestarts());
306 		learning.setDataStructureFactory(solver.getDSFactory());
307 		learning.setVarActivityListener(solver);
308 		return solver;
309 	}
310 
311 	/**
312 	 * @return MiniSAT with decision UIP clause generator.
313 	 */
314 	public static Solver<MixedDataStructureDanielWL> newRelsat() {
315 		MiniSATLearning<MixedDataStructureDanielWL> learning = new MiniSATLearning<MixedDataStructureDanielWL>();
316 		Solver<MixedDataStructureDanielWL> solver = new Solver<MixedDataStructureDanielWL>(
317 				new DecisionUIP(), learning, new MixedDataStructureDanielWL(),
318 				new VarOrderHeap(), new MiniSATRestarts());
319 		learning.setDataStructureFactory(solver.getDSFactory());
320 		learning.setVarActivityListener(solver);
321 		return solver;
322 	}
323 
324 	/**
325 	 * @return MiniSAT with VSIDS heuristics, FirstUIP clause generator for
326 	 *         backjumping but no learning.
327 	 */
328 	public static Solver<MixedDataStructureDanielWL> newBackjumping() {
329 		NoLearningButHeuristics<MixedDataStructureDanielWL> learning = new NoLearningButHeuristics<MixedDataStructureDanielWL>();
330 		Solver<MixedDataStructureDanielWL> solver = new Solver<MixedDataStructureDanielWL>(
331 				new FirstUIP(), learning, new MixedDataStructureDanielWL(),
332 				new VarOrderHeap(), new MiniSATRestarts());
333 		learning.setVarActivityListener(solver);
334 		return solver;
335 	}
336 
337 	/**
338 	 * @return a solver computing models with a minimum number of satisfied
339 	 *         literals.
340 	 */
341 	public static ISolver newMinOneSolver() {
342 		return new OptToSatAdapter(new MinOneDecorator(newDefault()));
343 	}
344 
345 	/**
346 	 * Default solver of the SolverFactory. This solver is meant to be used on
347 	 * challenging SAT benchmarks.
348 	 * 
349 	 * @return the best "general purpose" SAT solver available in the factory.
350 	 * @see #defaultSolver() the same method, polymorphic, to be called from an
351 	 *      instance of ASolverFactory.
352 	 */
353 	public static ISolver newDefault() {
354 		return newMiniLearningHeapRsatExpSimpBiere();
355 	}
356 
357 	@Override
358 	public ISolver defaultSolver() {
359 		return newDefault();
360 	}
361 
362 	/**
363 	 * Small footprint SAT solver.
364 	 * 
365 	 * @return a SAT solver suitable for solving small/easy SAT benchmarks.
366 	 * @see #lightSolver() the same method, polymorphic, to be called from an
367 	 *      instance of ASolverFactory.
368 	 */
369 	public static ISolver newLight() {
370 		return newMiniLearningHeap();
371 	}
372 
373 	@Override
374 	public ISolver lightSolver() {
375 		return newLight();
376 	}
377 
378 	public static ISolver newDimacsOutput() {
379 		return new DimacsOutputSolver();
380 	}
381 
382 }