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 ReplayXplain 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   * @author daniel
62   * @since 2.1
63   */
64  public class InsertionStrategy implements MinimizationStrategy {
65  
66      /**
67  	 * 
68  	 */
69      private static final long serialVersionUID = 1L;
70  
71      private boolean computationCanceled;
72  
73      /**
74       * @since 2.1
75       */
76      public void cancelExplanationComputation() {
77          this.computationCanceled = true;
78      }
79  
80      /**
81       * @since 2.1
82       */
83      public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
84              IVecInt assumps) throws TimeoutException {
85          this.computationCanceled = false;
86          IVecInt encodingAssumptions = new VecInt(constrs.size()
87                  + assumps.size());
88          assumps.copyTo(encodingAssumptions);
89          IVecInt firstExplanation = solver.unsatExplanation();
90          if (firstExplanation.size() == 1) {
91              IVecInt results = new VecInt();
92              results.push(-firstExplanation.get(0));
93              return results;
94          }
95          if (solver.isVerbose()) {
96              System.out.print(solver.getLogPrefix() + "initial unsat core ");
97              firstExplanation.sort();
98              for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
99                  System.out.print(constrs.get(-it.next()));
100                 System.out.print(" ");
101             }
102             System.out.println();
103         }
104         for (int i = 0; i < firstExplanation.size();) {
105             if (assumps.contains(firstExplanation.get(i))) {
106                 firstExplanation.delete(i);
107             } else {
108                 i++;
109             }
110         }
111         Set<Integer> constraintsVariables = constrs.keySet();
112         IVecInt remainingVariables = new VecInt(constraintsVariables.size());
113         for (Integer v : constraintsVariables) {
114             remainingVariables.push(v);
115         }
116         int p;
117         for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
118             p = it.next();
119             if (p < 0) {
120                 p = -p;
121             }
122             remainingVariables.remove(p);
123             encodingAssumptions.push(p);
124         }
125         remainingVariables.copyTo(encodingAssumptions);
126         boolean shouldContinue;
127         int startingPoint = assumps.size();
128         do {
129             shouldContinue = false;
130             int i = startingPoint;
131             encodingAssumptions.set(i, -encodingAssumptions.get(i));
132             assert encodingAssumptions.get(i) < 0;
133             while (!this.computationCanceled
134                     && solver.isSatisfiable(encodingAssumptions)) {
135                 i++;
136                 assert encodingAssumptions.get(i) > 0;
137                 encodingAssumptions.set(i, -encodingAssumptions.get(i));
138             }
139             if (!this.computationCanceled && i > startingPoint) {
140                 assert !solver.isSatisfiable(encodingAssumptions);
141                 if (i < encodingAssumptions.size()) {
142                     // latest constraint is for sure responsible for the
143                     // inconsistency.
144                     int tmp = encodingAssumptions.get(i);
145                     for (int j = i; j > startingPoint; j--) {
146                         encodingAssumptions.set(j,
147                                 -encodingAssumptions.get(j - 1));
148                     }
149                     encodingAssumptions.set(startingPoint, tmp);
150                     if (solver.isVerbose()) {
151                         System.out.println(solver.getLogPrefix()
152                                 + constrs.get(tmp) + " is mandatory ");
153                     }
154                 }
155                 shouldContinue = true;
156             }
157             startingPoint++;
158         } while (!this.computationCanceled && shouldContinue
159                 && solver.isSatisfiable(encodingAssumptions));
160         if (this.computationCanceled) {
161             throw new TimeoutException();
162         }
163         IVecInt constrsKeys = new VecInt(startingPoint);
164         for (int i = assumps.size(); i < startingPoint; i++) {
165             constrsKeys.push(-encodingAssumptions.get(i));
166         }
167         return constrsKeys;
168     }
169 
170     @Override
171     public String toString() {
172         return "Replay (Insertion-based) minimization strategy";
173     }
174 }