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  
24  import org.sat4j.core.VecInt;
25  import org.sat4j.csp.Encoding;
26  import org.sat4j.csp.Evaluable;
27  import org.sat4j.csp.Var;
28  import org.sat4j.specs.ContradictionException;
29  import org.sat4j.specs.ISolver;
30  import org.sat4j.specs.IVec;
31  import org.sat4j.specs.IVecInt;
32  import org.sat4j.specs.IteratorInt;
33  
34  public class BinarySupportEncoding implements Encoding {
35  
36      private final Map<Integer, IVecInt> supportsa = new HashMap<Integer, IVecInt>();
37  
38      private final Map<Integer, IVecInt> supportsb = new HashMap<Integer, IVecInt>();
39  
40      private static final Encoding instance = new BinarySupportEncoding();
41  
42      private BinarySupportEncoding() {
43          // nothing here
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          generateClauses(scope.get(0), supportsa, solver);
53          generateClauses(scope.get(1), supportsb, solver);
54  
55      }
56  
57      public void onInit(ISolver solver, IVec<Var> scope) {
58          supportsa.clear();
59          supportsb.clear();
60      }
61  
62      public void onNogood(ISolver solver, IVec<Var> scope,
63              Map<Evaluable, Integer> tuple) throws ContradictionException {
64      }
65  
66      public void onSupport(ISolver solver, IVec<Var> scope,
67              Map<Evaluable, Integer> tuple) throws ContradictionException {
68          Var vara = scope.get(0);
69          Integer va = tuple.get(vara);
70          Var varb = scope.get(1);
71          Integer vb = tuple.get(varb);
72          addSupport(va, varb, vb, supportsa);
73          addSupport(vb, vara, va, supportsb);
74      }
75  
76      private void addSupport(Integer head, Evaluable v, Integer value,
77              Map<Integer, IVecInt> supports) {
78          IVecInt sup = supports.get(head);
79          if (sup == null) {
80              sup = new VecInt();
81              supports.put(head, sup);
82          }
83          sup.push(v.translate(value.intValue()));
84      }
85  
86      private void generateClauses(Evaluable v, Map<Integer, IVecInt> supports,
87              ISolver solver) throws ContradictionException {
88          IVecInt clause = new VecInt();
89          for (IteratorInt  it =  v.domain().iterator() ; it.hasNext();) {
90              Integer key = new Integer(it.next());
91              clause.clear();
92              IVecInt support = supports.get(key);
93              clause.push(-v.translate(key.intValue()));
94              if (support != null) {
95                  for (IteratorInt  iterator = support.iterator() ; iterator.hasNext();)
96                      clause.push(iterator.next());
97              }
98              solver.addClause(clause);
99          }
100     }
101 
102 }