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.util.Map;
31  import java.util.Set;
32  
33  import org.sat4j.core.VecInt;
34  import org.sat4j.specs.ISolver;
35  import org.sat4j.specs.IVecInt;
36  import org.sat4j.specs.IteratorInt;
37  import org.sat4j.specs.TimeoutException;
38  
39  /**
40   * An implementation of the ReplayXplain algorithm as explained by Ulrich Junker
41   * in the following paper:
42   * 
43   * @inproceedings{ junker01:quickxplain:inp, author={Ulrich Junker},
44   *                 title={QUICKXPLAIN: Conflict Detection for Arbitrary
45   *                 Constraint Propagation Algorithms}, booktitle={IJCAI'01
46   *                 Workshop on Modelling and Solving problems with constraints
47   *                 (CONS-1)}, year={2001}, month={August}, address={Seattle, WA,
48   *                 USA}, url={citeseer.ist.psu.edu/junker01quickxplain.html},
49   *                 url={http://www.lirmm.fr/~bessiere/ws_ijcai01/junker.ps.gz} }
50   * 
51   *                 The algorithm has been adapted to work properly in a context
52   *                 where we can afford to add a selector variable to each clause
53   *                 to enable or disable each constraint.
54   * 
55   *                 Note that for the moment, QuickXplain does not work properly
56   *                 in an optimization setting.
57   * 
58   * 
59   * @author daniel
60   * @since 2.1
61   */
62  public class InsertionStrategy implements MinimizationStrategy {
63  
64  	private boolean computationCanceled;
65  
66  	/**
67  	 * @since 2.1
68  	 */
69  	public void cancelExplanationComputation() {
70  		computationCanceled = true;
71  	}
72  
73  	/**
74  	 * @since 2.1
75  	 */
76  	public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
77  			IVecInt assumps) throws TimeoutException {
78  		computationCanceled = false;
79  		IVecInt encodingAssumptions = new VecInt(constrs.size()
80  				+ assumps.size());
81  		assumps.copyTo(encodingAssumptions);
82  		IVecInt firstExplanation = solver.unsatExplanation();
83  		if (firstExplanation.size() == 1) {
84  			IVecInt results = new VecInt();
85  			results.push(-firstExplanation.get(0));
86  			return results;
87  		}
88  		if (solver.isVerbose()) {
89  			System.out.print(solver.getLogPrefix() + "initial unsat core ");
90  			firstExplanation.sort();
91  			for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
92  				System.out.print(constrs.get(-it.next()));
93  				System.out.print(" ");
94  			}
95  			System.out.println();
96  		}
97  		for (int i = 0; i < firstExplanation.size();) {
98  			if (assumps.contains(firstExplanation.get(i))) {
99  				firstExplanation.delete(i);
100 			} else {
101 				i++;
102 			}
103 		}
104 		Set<Integer> constraintsVariables = constrs.keySet();
105 		IVecInt remainingVariables = new VecInt(constraintsVariables.size());
106 		for (Integer v : constraintsVariables) {
107 			remainingVariables.push(v);
108 		}
109 		int p;
110 		for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
111 			p = it.next();
112 			if (p < 0) {
113 				p = -p;
114 			}
115 			remainingVariables.remove(p);
116 			encodingAssumptions.push(p);
117 		}
118 		remainingVariables.copyTo(encodingAssumptions);
119 		boolean shouldContinue;
120 		int startingPoint = assumps.size();
121 		do {
122 			shouldContinue = false;
123 			int i = startingPoint;
124 			encodingAssumptions.set(i, -encodingAssumptions.get(i));
125 			assert encodingAssumptions.get(i) < 0;
126 			while (!computationCanceled
127 					&& solver.isSatisfiable(encodingAssumptions)) {
128 				i++;
129 				assert encodingAssumptions.get(i) > 0;
130 				encodingAssumptions.set(i, -encodingAssumptions.get(i));
131 			}
132 			if (!computationCanceled && i > startingPoint) {
133 				assert !solver.isSatisfiable(encodingAssumptions);
134 				if (i < encodingAssumptions.size()) {
135 					// latest constraint is for sure responsible for the
136 					// inconsistency.
137 					int tmp = encodingAssumptions.get(i);
138 					for (int j = i; j > startingPoint; j--) {
139 						encodingAssumptions.set(j,
140 								-encodingAssumptions.get(j - 1));
141 					}
142 					encodingAssumptions.set(startingPoint, tmp);
143 					if (solver.isVerbose()) {
144 						System.out.println(solver.getLogPrefix()
145 								+ constrs.get(tmp) + " is mandatory ");
146 					}
147 				}
148 				shouldContinue = true;
149 			}
150 			startingPoint++;
151 		} while (!computationCanceled && shouldContinue
152 				&& solver.isSatisfiable(encodingAssumptions));
153 		if (computationCanceled) {
154 			throw new TimeoutException();
155 		}
156 		IVecInt constrsKeys = new VecInt(startingPoint);
157 		for (int i = assumps.size(); i < startingPoint; i++) {
158 			constrsKeys.push(-encodingAssumptions.get(i));
159 		}
160 		return constrsKeys;
161 	}
162 
163 	@Override
164 	public String toString() {
165 		return "Replay (Insertion-based) minimization strategy";
166 	}
167 }