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.io.PrintWriter;
33  import java.util.Map;
34  import java.util.Set;
35  
36  import org.sat4j.core.VecInt;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVecInt;
39  import org.sat4j.specs.IteratorInt;
40  import org.sat4j.specs.TimeoutException;
41  
42  /**
43   * An implementation of the deletion based minimization.
44   * 
45   * 
46   * @since 2.1
47   */
48  public class DeletionStrategy implements MinimizationStrategy {
49  
50      /**
51  	 * 
52  	 */
53      private static final long serialVersionUID = 1L;
54  
55      private boolean computationCanceled;
56  
57      public void cancelExplanationComputation() {
58          this.computationCanceled = true;
59      }
60  
61      public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
62              IVecInt assumps) throws TimeoutException {
63          this.computationCanceled = false;
64          IVecInt encodingAssumptions = new VecInt(constrs.size()
65                  + assumps.size());
66          assumps.copyTo(encodingAssumptions);
67          IVecInt firstExplanation = solver.unsatExplanation();
68          IVecInt results = new VecInt(firstExplanation.size());
69          if (firstExplanation.size() == 1) {
70              results.push(-firstExplanation.get(0));
71              return results;
72          }
73          if (solver.isVerbose()) {
74              System.out.print(solver.getLogPrefix() + "initial unsat core ");
75              firstExplanation.sort();
76              for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
77                  System.out.print(constrs.get(-it.next()));
78                  System.out.print(" ");
79              }
80              System.out.println();
81              solver.printStat(new PrintWriter(System.out, true), "c ");
82          }
83          for (int i = 0; i < firstExplanation.size();) {
84              if (assumps.contains(firstExplanation.get(i))) {
85                  firstExplanation.delete(i);
86              } else {
87                  i++;
88              }
89          }
90          Set<Integer> constraintsVariables = constrs.keySet();
91          IVecInt remainingVariables = new VecInt(constraintsVariables.size());
92          for (Integer v : constraintsVariables) {
93              remainingVariables.push(v);
94          }
95          int p;
96          for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
97              p = it.next();
98              if (p < 0) {
99                  p = -p;
100             }
101             remainingVariables.remove(p);
102         }
103 
104         remainingVariables.copyTo(encodingAssumptions);
105         int unsatcorebegin = encodingAssumptions.size();
106         firstExplanation.copyTo(encodingAssumptions);
107         assert !solver.isSatisfiable(encodingAssumptions);
108         int unsatcorelimit = encodingAssumptions.size() - 1;
109         for (int i = unsatcorebegin; i < unsatcorelimit; i++) {
110             if (this.computationCanceled) {
111                 throw new TimeoutException();
112             }
113             encodingAssumptions.set(i, -encodingAssumptions.get(i));
114             if (solver.isVerbose()) {
115                 System.out.println(solver.getLogPrefix() + "checking "
116                         + constrs.get(encodingAssumptions.get(i)) + " ...");
117             }
118             if (solver.isSatisfiable(encodingAssumptions)) {
119                 encodingAssumptions.set(i, -encodingAssumptions.get(i));
120                 results.push(-encodingAssumptions.get(i));
121                 if (solver.isVerbose()) {
122                     System.out.println(solver.getLogPrefix() + "mandatory.");
123                 }
124             } else {
125                 if (solver.isVerbose()) {
126                     System.out.println(solver.getLogPrefix() + "not needed.");
127                 }
128             }
129         }
130         if (results.size() == 0) {
131             // the last group must be the cause of the inconsistency
132             results.push(-encodingAssumptions.get(unsatcorelimit));
133             if (solver.isVerbose()) {
134                 System.out.println(solver.getLogPrefix()
135                         + "skipping last test,the remaining element "
136                         + constrs.get(encodingAssumptions.get(unsatcorelimit))
137                         + " is causing the inconsistency!");
138             }
139         } else {
140             encodingAssumptions.set(unsatcorelimit,
141                     -encodingAssumptions.get(unsatcorelimit));
142             if (solver.isVerbose()) {
143                 System.out.println(solver.getLogPrefix() + "checking "
144                         + constrs.get(encodingAssumptions.get(unsatcorelimit))
145                         + " ...");
146             }
147             if (solver.isSatisfiable(encodingAssumptions)) {
148                 encodingAssumptions.set(unsatcorelimit,
149                         -encodingAssumptions.get(unsatcorelimit));
150                 results.push(-encodingAssumptions.get(unsatcorelimit));
151                 if (solver.isVerbose()) {
152                     System.out.println(solver.getLogPrefix() + "mandatory.");
153                 }
154             } else {
155                 if (solver.isVerbose()) {
156                     System.out.println(solver.getLogPrefix() + "not needed.");
157                 }
158             }
159         }
160         return results;
161     }
162 
163     @Override
164     public String toString() {
165         return "Deletion based minimization strategy";
166     }
167 }