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.xplain;
31  
32  import java.util.Map;
33  import java.util.Set;
34  
35  import org.sat4j.core.VecInt;
36  import org.sat4j.specs.ISolver;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.specs.IteratorInt;
39  import org.sat4j.specs.TimeoutException;
40  
41  /**
42   * An implementation of the QuickXplain algorithm as explained by Ulrich Junker
43   * in the following paper:
44   * 
45   * @inproceedings{DBLP:conf/aaai/Junker02, author = {Ulrich Junker}, title =
46   *                                         {Preference-Based Search and
47   *                                         Multi-Criteria Optimization},
48   *                                         booktitle = {AAAI/IAAI}, year =
49   *                                         {2002}, pages = {34-40}, bibsource =
50   *                                         {DBLP, http://dblp.uni-trier.de} }
51   * 
52   * 
53   *                                         The algorithm has been adapted to
54   *                                         work properly in a context where we
55   *                                         can afford to add a selector variable
56   *                                         to each clause to enable or disable
57   *                                         each constraint.
58   * 
59   *                                         Note that for the moment, QuickXplain
60   *                                         does not work properly in an
61   *                                         optimization setting.
62   * 
63   * 
64   * @since 2.1
65   */
66  public class QuickXplainStrategy implements MinimizationStrategy {
67  
68      /**
69  	 * 
70  	 */
71      private static final long serialVersionUID = 1L;
72  
73      private boolean computationCanceled;
74  
75      public void cancelExplanationComputation() {
76          this.computationCanceled = true;
77      }
78  
79      public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
80              IVecInt assumps) throws TimeoutException {
81          this.computationCanceled = false;
82          IVecInt encodingAssumptions = new VecInt(constrs.size()
83                  + assumps.size());
84          assumps.copyTo(encodingAssumptions);
85          IVecInt firstExplanation = solver.unsatExplanation();
86          IVecInt results = new VecInt(firstExplanation.size());
87          if (firstExplanation.size() == 1) {
88              results.push(-firstExplanation.get(0));
89              return results;
90          }
91          if (solver.isVerbose()) {
92              System.out.print(solver.getLogPrefix() + "initial unsat core ");
93              firstExplanation.sort();
94              for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
95                  System.out.print(constrs.get(-it.next()));
96                  System.out.print(" ");
97              }
98              System.out.println();
99          }
100         for (int i = 0; i < firstExplanation.size();) {
101             if (assumps.contains(firstExplanation.get(i))) {
102                 firstExplanation.delete(i);
103             } else {
104                 i++;
105             }
106         }
107         Set<Integer> constraintsVariables = constrs.keySet();
108         IVecInt remainingVariables = new VecInt(constraintsVariables.size());
109         for (Integer v : constraintsVariables) {
110             remainingVariables.push(v);
111         }
112         int p;
113         for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
114             p = it.next();
115             if (p < 0) {
116                 p = -p;
117             }
118             remainingVariables.remove(p);
119             encodingAssumptions.push(p);
120         }
121         int unsatcorelimit = encodingAssumptions.size() - 1;
122 
123         remainingVariables.copyTo(encodingAssumptions);
124         computeExplanation(solver, constrs, encodingAssumptions,
125                 assumps.size(), unsatcorelimit, results);
126         return results;
127     }
128 
129     private void computeExplanation(ISolver solver, Map<Integer, ?> constrs,
130             IVecInt encodingAssumptions, int start, int end, IVecInt result)
131             throws TimeoutException {
132         if (solver.isVerbose()) {
133             System.out.println(solver.getLogPrefix() + "qxplain " + start + "/"
134                     + end);
135         }
136         if (!solver.isSatisfiable(encodingAssumptions)) {
137             return;
138         }
139         if (start == end) {
140             result.push(encodingAssumptions.get(start));
141             encodingAssumptions.set(start, -encodingAssumptions.get(start));
142             if (solver.isVerbose()) {
143                 System.out.println(solver.getLogPrefix()
144                         + constrs.get(-encodingAssumptions.get(start))
145                         + " is mandatory ");
146             }
147             return;
148         }
149         int split = (end + start) / 2;
150         if (split < end) {
151             for (int j = start; j <= split; j++) {
152                 encodingAssumptions.set(j, -encodingAssumptions.get(j));
153             }
154             computeExplanation(solver, constrs, encodingAssumptions, split + 1,
155                     end, result);
156         }
157         if (start <= split) {
158             for (int j = start; j <= split; j++) {
159                 encodingAssumptions.set(j, -encodingAssumptions.get(j));
160             }
161             computeExplanation(solver, constrs, encodingAssumptions, start,
162                     split, result);
163         }
164         if (this.computationCanceled) {
165             throw new TimeoutException();
166         }
167     }
168 
169     @Override
170     public String toString() {
171         return "QuickXplain (AAAI 2004 version) minimization strategy";
172     }
173 
174 }