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.IOptimizationProblem;
35  import org.sat4j.specs.ISolver;
36  import org.sat4j.specs.IVecInt;
37  import org.sat4j.specs.TimeoutException;
38  
39  public class OptToSatAdapter extends SolverDecorator<ISolver> {
40  
41      /**
42       * 
43       */
44      private static final long serialVersionUID = 1L;
45  
46      IOptimizationProblem problem;
47  
48      boolean optimalValueForced = false;
49      private final IVecInt assumps = new VecInt();
50  
51      private long begin;
52  
53      private final SolutionFoundListener sfl;
54  
55      public OptToSatAdapter(IOptimizationProblem problem) {
56          this(problem, SolutionFoundListener.VOID);
57      }
58  
59      public OptToSatAdapter(IOptimizationProblem problem,
60              SolutionFoundListener sfl) {
61          super((ISolver) problem);
62          this.problem = problem;
63          this.sfl = sfl;
64      }
65  
66      @Override
67      public void reset() {
68          super.reset();
69          this.optimalValueForced = false;
70      }
71  
72      @Override
73      public boolean isSatisfiable() throws TimeoutException {
74          return isSatisfiable(VecInt.EMPTY);
75      }
76  
77      @Override
78      public boolean isSatisfiable(boolean global) throws TimeoutException {
79          return isSatisfiable();
80      }
81  
82      @Override
83      public boolean isSatisfiable(IVecInt myAssumps, boolean global)
84              throws TimeoutException {
85          return isSatisfiable(myAssumps);
86      }
87  
88      @Override
89      public boolean isSatisfiable(IVecInt myAssumps) throws TimeoutException {
90          this.assumps.clear();
91          myAssumps.copyTo(this.assumps);
92          this.begin = System.currentTimeMillis();
93          if (this.problem.hasNoObjectiveFunction()) {
94              return this.problem.isSatisfiable(myAssumps);
95          }
96          boolean satisfiable = false;
97          try {
98              while (this.problem.admitABetterSolution(myAssumps)) {
99                  satisfiable = true;
100                 sfl.onSolutionFound(this.problem.model());
101                 this.problem.discardCurrentSolution();
102                 if (isVerbose()) {
103                     System.out.println(getLogPrefix()
104                             + "Current objective function value: "
105                             + this.problem.getObjectiveValue() + "("
106                             + (System.currentTimeMillis() - this.begin)
107                             / 1000.0 + "s)");
108                 }
109             }
110             sfl.onUnsatTermination();
111         } catch (ContradictionException ce) {
112             sfl.onUnsatTermination();
113         }
114         return satisfiable;
115     }
116 
117     @Override
118     public int[] model() {
119         return this.problem.model();
120     }
121 
122     @Override
123     public boolean model(int var) {
124         return this.problem.model(var);
125     }
126 
127     @Override
128     public int[] modelWithInternalVariables() {
129         return decorated().modelWithInternalVariables();
130     }
131 
132     @Override
133     public int[] findModel() throws TimeoutException {
134         if (isSatisfiable()) {
135             return model();
136         }
137         return null;
138     }
139 
140     @Override
141     public int[] findModel(IVecInt assumps) throws TimeoutException {
142         if (isSatisfiable(assumps)) {
143             return model();
144         }
145         return null;
146     }
147 
148     @Override
149     public String toString(String prefix) {
150         return prefix + "Optimization to SAT adapter\n"
151                 + super.toString(prefix);
152     }
153 
154     /**
155      * Allow to easily check is the solution returned by isSatisfiable is
156      * optimal or not.
157      * 
158      * @return true is the solution found is indeed optimal.
159      */
160     public boolean isOptimal() {
161         return this.problem.isOptimal();
162     }
163 }