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.pb;
31  
32  import java.io.PrintWriter;
33  
34  import org.sat4j.core.VecInt;
35  import org.sat4j.specs.ContradictionException;
36  import org.sat4j.specs.IOptimizationProblem;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.specs.TimeoutException;
39  
40  /**
41   * Utility class to use optimization solvers instead of simple SAT solvers in
42   * code meant for SAT solvers.
43   * 
44   * @author daniel
45   */
46  public class OptToPBSATAdapter extends PBSolverDecorator {
47  
48      /**
49       * 
50       */
51      private static final long serialVersionUID = 1L;
52  
53      IOptimizationProblem problem;
54  
55      boolean modelComputed = false;
56  
57      private final IVecInt assumps = new VecInt();
58  
59      private long begin;
60  
61      public OptToPBSATAdapter(IOptimizationProblem problem) {
62          super((IPBSolver) problem);
63          this.problem = problem;
64      }
65  
66      @Override
67      public boolean isSatisfiable() throws TimeoutException {
68          this.modelComputed = false;
69          this.assumps.clear();
70          this.begin = System.currentTimeMillis();
71          if (this.problem.hasNoObjectiveFunction()) {
72              return this.modelComputed = this.problem.isSatisfiable();
73          }
74          return this.problem.admitABetterSolution();
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.modelComputed = false;
91          this.assumps.clear();
92          myAssumps.copyTo(this.assumps);
93          this.begin = System.currentTimeMillis();
94          if (this.problem.hasNoObjectiveFunction()) {
95              return this.modelComputed = this.problem.isSatisfiable(myAssumps);
96          }
97          return this.problem.admitABetterSolution(myAssumps);
98      }
99  
100     @Override
101     public int[] model() {
102         return model(new PrintWriter(System.out));
103     }
104 
105     /**
106      * Compute a minimal model according to the objective function of the
107      * IPBProblem decorated.
108      * 
109      * @param out
110      *            a writer to display information in verbose mode
111      * @since 2.3.2
112      */
113     public int[] model(PrintWriter out) {
114         if (this.modelComputed) {
115             return this.problem.model();
116         }
117         try {
118             assert this.problem.admitABetterSolution(this.assumps);
119             assert !this.problem.hasNoObjectiveFunction();
120             do {
121                 this.problem.discardCurrentSolution();
122                 if (isVerbose()) {
123                     out.println(getLogPrefix()
124                             + "Current objective function value: "
125                             + this.problem.getObjectiveValue() + "("
126                             + (System.currentTimeMillis() - this.begin)
127                             / 1000.0 + "s)");
128                 }
129             } while (this.problem.admitABetterSolution(this.assumps));
130         } catch (TimeoutException e) {
131             if (isVerbose()) {
132                 out.println(getLogPrefix() + "Solver timed out after "
133                         + (System.currentTimeMillis() - this.begin) / 1000.0
134                         + "s)");
135             }
136         } catch (ContradictionException e) {
137             // OK, optimal model found
138         }
139         this.modelComputed = true;
140         return this.problem.model();
141     }
142 
143     @Override
144     public boolean model(int var) {
145         if (!this.modelComputed) {
146             model();
147         }
148         return this.problem.model(var);
149     }
150 
151     @Override
152     public String toString(String prefix) {
153         return prefix + "Optimization to Pseudo Boolean adapter\n"
154                 + super.toString(prefix);
155     }
156 
157     public boolean isOptimal() {
158         return this.problem.isOptimal();
159     }
160 
161     /**
162      * Return the value of the objective function in the last model found.
163      * 
164      * @return
165      * @since 2.3.2
166      */
167     public Number getCurrentObjectiveValue() {
168         return this.problem.getObjectiveValue();
169     }
170 }