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  
31  package org.sat4j.tools.encoding;
32  
33  import org.sat4j.core.ConstrGroup;
34  import org.sat4j.core.VecInt;
35  import org.sat4j.specs.ContradictionException;
36  import org.sat4j.specs.IConstr;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVecInt;
39  
40  /**
41   * Binomial encoding for the "at most one" and "at most k" cases.
42   * 
43   * For the "at most one" case, this encoding is equivalent to the one referred
44   * to in the literature as the pair-wise or naive encoding. For the "at most k"
45   * case, the previous encoding is generalized with binomial selection (see A. M.
46   * Frisch and P. A. Giannaros, "SAT Encodings of the At-Most-k Constraint", in
47   * International Workshop on Modelling and Reformulating Constraint Satisfaction
48   * Problems, 2010 for details).
49   * 
50   * @author stephanieroussel
51   * @since 2.3.1
52   */
53  public class Binomial extends EncodingStrategyAdapter {
54  
55      /**
56       * 
57       */
58      private static final long serialVersionUID = 1L;
59  
60      @Override
61      public IConstr addAtMost(ISolver solver, IVecInt literals, int degree)
62              throws ContradictionException {
63          ConstrGroup group = new ConstrGroup();
64  
65          IVecInt clause = new VecInt();
66  
67          if (degree == 1) {
68              return addAtMostOne(solver, literals);
69          }
70  
71          for (IVecInt vec : literals.subset(degree + 1)) {
72              for (int i = 0; i < vec.size(); i++) {
73                  clause.push(-vec.get(i));
74              }
75              group.add(solver.addClause(clause));
76              clause.clear();
77          }
78          return group;
79  
80      }
81  
82      @Override
83      public IConstr addAtMostOne(ISolver solver, IVecInt literals)
84              throws ContradictionException {
85          ConstrGroup group = new ConstrGroup();
86  
87          IVecInt clause = new VecInt();
88  
89          for (int i = 0; i < literals.size() - 1; i++) {
90              for (int j = i + 1; j < literals.size(); j++) {
91                  clause.push(-literals.get(i));
92                  clause.push(-literals.get(j));
93                  group.add(solver.addClause(clause));
94                  clause.clear();
95              }
96          }
97          return group;
98      }
99  
100     @Override
101     public IConstr addExactlyOne(ISolver solver, IVecInt literals)
102             throws ContradictionException {
103         ConstrGroup group = new ConstrGroup();
104 
105         group.add(addAtLeastOne(solver, literals));
106         group.add(addAtMostOne(solver, literals));
107 
108         return group;
109     }
110 
111     @Override
112     public IConstr addExactly(ISolver solver, IVecInt literals, int degree)
113             throws ContradictionException {
114         ConstrGroup group = new ConstrGroup();
115 
116         group.add(addAtLeast(solver, literals, degree));
117         group.add(addAtMost(solver, literals, degree));
118 
119         return group;
120     }
121 
122 }