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  package org.sat4j.csp.encodings;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.TreeSet;
25  
26  import org.sat4j.core.VecInt;
27  import org.sat4j.csp.Encoding;
28  import org.sat4j.csp.Evaluable;
29  import org.sat4j.csp.Var;
30  import org.sat4j.specs.ContradictionException;
31  import org.sat4j.specs.ISolver;
32  import org.sat4j.specs.IVec;
33  import org.sat4j.specs.IVecInt;
34  import org.sat4j.specs.IteratorInt;
35  
36  public class GeneralizedSupportEncoding implements Encoding {
37  
38      private final Map<Set<Integer>, IVecInt> supports = new HashMap<Set<Integer>, IVecInt>();
39  
40      private static final Encoding INSTANCE = new GeneralizedSupportEncoding();
41  
42      private GeneralizedSupportEncoding() {
43  
44      }
45  
46      public static Encoding instance() {
47          return INSTANCE;
48      }
49  
50      public void onFinish(ISolver solver, IVec<Var> scope)
51              throws ContradictionException {
52          // TODO Auto-generated method stub
53  
54      }
55  
56      public void onInit(ISolver solver, IVec<Var> scope) {
57          supports.clear();
58          int[] acc = new int[scope.size()];
59          fill(0, scope, acc, supports);
60      }
61  
62      public void onNogood(ISolver solver, IVec<Var> scope,
63              Map<Evaluable, Integer> tuple) throws ContradictionException {
64  
65      }
66  
67      public void onSupport(ISolver solver, IVec<Var> scope,
68              Map<Evaluable, Integer> tuple) throws ContradictionException {
69          for (int i = 0; i < scope.size(); i++) {
70              Set<Integer> set = new TreeSet<Integer>();
71              Var vari = scope.get(i);
72              for (int j = 0; j < scope.size(); j++) {
73                  if (i != j) {
74                      set.add(scope.get(j).translate(tuple.get(vari)));
75                  }
76              }
77              IVecInt support = supports.get(set);
78              assert support != null;
79              support.push(vari.translate(tuple.get(vari)));
80          }
81  
82      }
83  
84      private void fill(int n, IVec<Var> scope, int[] acc,
85              Map<Set<Integer>, IVecInt> theSupports) {
86          if (n == scope.size()) {
87              for (int j = 0; j < acc.length; j++) {
88                  Set<Integer> set = new TreeSet<Integer>();
89                  for (int i = 0; i < acc.length; i++)
90                      if (i != j)
91                          set.add(scope.get(i).translate(acc[i]));
92                  theSupports.put(set, new VecInt());
93              }
94          } else
95              for (IteratorInt iterator = scope.get(n).domain().iterator() ; iterator.hasNext();) {
96                  acc[n] = iterator.next();
97                  fill(n + 1, scope, acc, theSupports);
98              }
99  
100     }
101 }