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.csp;
20  
21  import org.sat4j.core.ASolverFactory;
22  import org.sat4j.minisat.constraints.MixedDataStructureDanielWL;
23  import org.sat4j.minisat.core.DataStructureFactory;
24  import org.sat4j.minisat.core.ILits;
25  import org.sat4j.minisat.core.Solver;
26  import org.sat4j.minisat.learning.MiniSATLearning;
27  import org.sat4j.minisat.orders.VarOrderHeap;
28  import org.sat4j.minisat.restarts.MiniSATRestarts;
29  import org.sat4j.minisat.uip.FirstUIP;
30  import org.sat4j.specs.ISolver;
31  import org.sat4j.tools.DimacsOutputSolver;
32  
33  /**
34   * User friendly access to pre-constructed solvers.
35   * 
36   * @author leberre
37   */
38  public class SolverFactory extends ASolverFactory<ISolver> {
39  
40      /**
41       * 
42       */
43      private static final long serialVersionUID = 1L;
44  
45      // thread safe implementation of the singleton design pattern
46      private static SolverFactory instance;
47  
48      /**
49       * Private constructor. Use singleton method instance() instead.
50       * 
51       * @see #instance()
52       */
53      private SolverFactory() {
54          super();
55      }
56  
57      private static synchronized void createInstance() {
58          if (instance == null) {
59              instance = new SolverFactory();
60          }
61      }
62  
63      /**
64       * Access to the single instance of the factory.
65       * 
66       * @return the singleton of that class.
67       */
68      public static SolverFactory instance() {
69          if (instance == null) {
70              createInstance();
71          }
72          return instance;
73      }
74  
75      /**
76       * @param dsf
77       *                the data structure used for representing clauses and lits
78       * @return MiniSAT the data structure dsf.
79       */
80      public static <L extends ILits> Solver<DataStructureFactory> newMiniSAT(
81              DataStructureFactory dsf) {
82          MiniSATLearning<DataStructureFactory> learning = new MiniSATLearning<DataStructureFactory>();
83          Solver<DataStructureFactory> solver = new Solver<DataStructureFactory>(new FirstUIP(), learning, dsf,
84                  new VarOrderHeap(), new MiniSATRestarts());
85          learning.setDataStructureFactory(solver.getDSFactory());
86          learning.setVarActivityListener(solver);
87          return solver;
88      }
89  
90  
91      
92      /**
93       * Default solver of the SolverFactory. This solver is meant to be used on
94       * challenging SAT benchmarks.
95       * 
96       * @return the best "general purpose" SAT solver available in the factory.
97       * @see #defaultSolver() the same method, polymorphic, to be called from an
98       *      instance of ASolverFactory.
99       */
100     public static ISolver newDefault() {
101         Solver<DataStructureFactory> solver = newMiniSAT(new MixedDataStructureDanielWL());
102         solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
103         return solver;
104     }
105 
106     @Override
107     public ISolver defaultSolver() {
108         return newDefault();
109     }
110 
111     /**
112      * Small footprint SAT solver.
113      * 
114      * @return a SAT solver suitable for solving small/easy SAT benchmarks.
115      * @see #lightSolver() the same method, polymorphic, to be called from an
116      *      instance of ASolverFactory.
117      */
118     public static ISolver newLight() {
119     	return newMiniSAT(new MixedDataStructureDanielWL());
120     }
121 
122     @Override
123     public ISolver lightSolver() {
124         return newLight();
125     }
126 
127     public static ISolver newDimacsOutput() {
128         return new DimacsOutputSolver();
129     }
130 
131 }