View Javadoc

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