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.Arrays;
33  import java.util.Collection;
34  import java.util.HashSet;
35  
36  import org.sat4j.core.VecInt;
37  import org.sat4j.specs.ContradictionException;
38  import org.sat4j.specs.IConstr;
39  import org.sat4j.specs.ISolver;
40  import org.sat4j.specs.IVecInt;
41  import org.sat4j.specs.IteratorInt;
42  import org.sat4j.specs.TimeoutException;
43  import org.sat4j.tools.GroupClauseSelectorSolver;
44  import org.sat4j.tools.SolverDecorator;
45  
46  /**
47   * Computation of MUS in a structured CNF, i.e. the clauses belong to
48   * components, the explanation is to be extracted in terms of components.
49   * 
50   * @author daniel
51   * 
52   * @param <T>
53   *            a subinterface to ISolver.
54   * @since 2.1
55   */
56  public class HighLevelXplain<T extends ISolver> extends
57          GroupClauseSelectorSolver<T> implements Explainer {
58  
59      private IVecInt assump;
60  
61      private MinimizationStrategy xplainStrategy = new DeletionStrategy();
62  
63      public HighLevelXplain(T solver) {
64          super(solver);
65      }
66  
67      @Override
68      public IConstr addAtLeast(IVecInt literals, int degree)
69              throws ContradictionException {
70          throw new UnsupportedOperationException();
71      }
72  
73      @Override
74      public IConstr addAtMost(IVecInt literals, int degree)
75              throws ContradictionException {
76          throw new UnsupportedOperationException();
77      }
78  
79      /**
80  	 * 
81  	 */
82      private static final long serialVersionUID = 1L;
83  
84      /**
85       * @since 2.2.4
86       * @return
87       * @throws TimeoutException
88       */
89      private IVecInt explanationKeys() throws TimeoutException {
90          assert !isSatisfiable(this.assump);
91          ISolver solver = decorated();
92          if (solver instanceof SolverDecorator<?>) {
93              solver = ((SolverDecorator<? extends ISolver>) solver).decorated();
94          }
95          return this.xplainStrategy.explain(solver, getVarToHighLevel(),
96                  this.assump);
97      }
98  
99      public int[] minimalExplanation() throws TimeoutException {
100         Collection<Integer> components = explain();
101         int[] model = new int[components.size()];
102         int i = 0;
103         for (int c : components) {
104             model[i++] = c;
105         }
106         Arrays.sort(model);
107         return model;
108     }
109 
110     /**
111      * @since 2.1
112      * @return
113      * @throws TimeoutException
114      */
115     public Collection<Integer> explain() throws TimeoutException {
116         IVecInt keys = explanationKeys();
117         Collection<Integer> explanation = new HashSet<Integer>(keys.size());
118         for (IteratorInt it = keys.iterator(); it.hasNext();) {
119             explanation.add(getVarToHighLevel().get(it.next()));
120         }
121         return explanation;
122     }
123 
124     /**
125      * @since 2.1
126      */
127     public void cancelExplanation() {
128         this.xplainStrategy.cancelExplanationComputation();
129     }
130 
131     @Override
132     public int[] findModel() throws TimeoutException {
133         this.assump = VecInt.EMPTY;
134         return super.findModel();
135     }
136 
137     @Override
138     public int[] findModel(IVecInt assumps) throws TimeoutException {
139         this.assump = assumps;
140         return super.findModel(assumps);
141     }
142 
143     @Override
144     public boolean isSatisfiable() throws TimeoutException {
145         this.assump = VecInt.EMPTY;
146         return super.isSatisfiable();
147     }
148 
149     @Override
150     public boolean isSatisfiable(boolean global) throws TimeoutException {
151         this.assump = VecInt.EMPTY;
152         return super.isSatisfiable(global);
153     }
154 
155     @Override
156     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
157         this.assump = assumps;
158         return super.isSatisfiable(assumps);
159     }
160 
161     @Override
162     public boolean isSatisfiable(IVecInt assumps, boolean global)
163             throws TimeoutException {
164         this.assump = assumps;
165         return super.isSatisfiable(assumps, global);
166     }
167 
168     @Override
169     public String toString(String prefix) {
170         System.out.println(prefix
171                 + "High Level Explanation (MUS) enabled solver");
172         System.out.println(prefix + this.xplainStrategy);
173         return super.toString(prefix);
174     }
175 
176     public void setMinimizationStrategy(MinimizationStrategy strategy) {
177         this.xplainStrategy = strategy;
178     }
179 
180 }