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{ junker01:quickxplain:inp, author={Ulrich Junker},
46   *                 title={QUICKXPLAIN: Conflict Detection for Arbitrary
47   *                 Constraint Propagation Algorithms}, booktitle={IJCAI'01
48   *                 Workshop on Modelling and Solving problems with constraints
49   *                 (CONS-1)}, year={2001}, month={August}, address={Seattle, WA,
50   *                 USA}, url={citeseer.ist.psu.edu/junker01quickxplain.html},
51   *                 url={http://www.lirmm.fr/~bessiere/ws_ijcai01/junker.ps.gz} }
52   * 
53   *                 The algorithm has been adapted to work properly in a context
54   *                 where we can afford to add a selector variable to each clause
55   *                 to enable or disable each constraint.
56   * 
57   *                 Note that for the moment, QuickXplain does not work properly
58   *                 in an optimization setting.
59   * 
60   * 
61   * @since 2.1
62   */
63  public class QuickXplain2001Strategy implements MinimizationStrategy {
64  
65      /**
66  	 * 
67  	 */
68      private static final long serialVersionUID = 1L;
69  
70      private boolean computationCanceled;
71  
72      public void cancelExplanationComputation() {
73          this.computationCanceled = true;
74      }
75  
76      public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
77              IVecInt assumps) throws TimeoutException {
78          this.computationCanceled = false;
79          IVecInt encodingAssumptions = new VecInt(constrs.size()
80                  + assumps.size());
81          assumps.copyTo(encodingAssumptions);
82          IVecInt firstExplanation = solver.unsatExplanation();
83          if (solver.isVerbose()) {
84              System.out.print(solver.getLogPrefix() + "initial unsat core ");
85              firstExplanation.sort();
86              for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
87                  System.out.print(constrs.get(-it.next()));
88                  System.out.print(" ");
89              }
90              System.out.println();
91          }
92          Set<Integer> constraintsVariables = constrs.keySet();
93          int p;
94          for (int i = 0; i < firstExplanation.size(); i++) {
95              if (constraintsVariables.contains(p = -firstExplanation.get(i))) {
96                  encodingAssumptions.push(p);
97              }
98          }
99          IVecInt results = new VecInt(encodingAssumptions.size());
100         computeExplanation(solver, encodingAssumptions, assumps.size(),
101                 encodingAssumptions.size() - 1, results);
102         return results;
103     }
104 
105     private void computeExplanation(ISolver solver,
106             IVecInt encodingAssumptions, int start, int end, IVecInt result)
107             throws TimeoutException {
108         if (!solver.isSatisfiable(encodingAssumptions)) {
109             return;
110         }
111         int i = start;
112         encodingAssumptions.set(i, -encodingAssumptions.get(i));
113         assert encodingAssumptions.get(i) < 0;
114         while (!this.computationCanceled
115                 && solver.isSatisfiable(encodingAssumptions)) {
116             if (i == end) {
117                 for (int j = start; j <= end; j++) {
118                     encodingAssumptions.set(j, -encodingAssumptions.get(j));
119                 }
120                 return;
121             }
122             i++;
123             assert encodingAssumptions.get(i) > 0;
124             encodingAssumptions.set(i, -encodingAssumptions.get(i));
125         }
126         result.push(-encodingAssumptions.get(i));
127         if (start == i) {
128             return;
129         }
130         int newend = i - 1;
131         int split = (newend + start) / 2;
132         if (split < newend) {
133             for (int j = split + 1; j < i; j++) {
134                 encodingAssumptions.set(j, -encodingAssumptions.get(j));
135             }
136             computeExplanation(solver, encodingAssumptions, split + 1, newend,
137                     result);
138         }
139         if (start <= split) {
140             for (int j = start; j <= split; j++) {
141                 encodingAssumptions.set(j, -encodingAssumptions.get(j));
142             }
143             computeExplanation(solver, encodingAssumptions, start, split,
144                     result);
145         }
146         if (this.computationCanceled) {
147             throw new TimeoutException();
148         }
149     }
150 
151     @Override
152     public String toString() {
153         return "QuickXplain (IJCAI WS 2001 version) minimization strategy";
154     }
155 }