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   * Commander encoding for "at most one" and "at most k" cases.
42   * 
43   * The case "at most one" is introduced in W. Klieber and G. Kwon
44   * "Efficient CNF encoding for selecting 1 from N objects" in Fourth Workshop on
45   * Constraints in Formal Verification, 2007.
46   * 
47   * The generalization to the "at most k" case is described in A. M. Frisch and P
48   * . A. Giannaros, "SAT Encodings of the At-Most-k Constraint", in International
49   * Workshop on Modelling and Reformulating Constraint Satisfaction Problems,
50   * 2010
51   * 
52   * @author sroussel
53   * @since 2.3.1
54   */
55  public class Commander extends EncodingStrategyAdapter {
56  
57      /**
58       * 
59       */
60      private static final long serialVersionUID = 1L;
61  
62      /**
63       * In this encoding, variables are partitioned in groups. Kwon and Klieber
64       * claim that the fewest clauses are produced when the size of the groups is
65       * 3, thus leading to 3.5 clauses and introducing n/2 variables.
66       */
67      @Override
68      public IConstr addAtMostOne(ISolver solver, IVecInt literals)
69              throws ContradictionException {
70  
71          return addAtMostOne(solver, literals, 3);
72      }
73  
74      private IConstr addAtMostOne(ISolver solver, IVecInt literals, int groupSize)
75              throws ContradictionException {
76  
77          ConstrGroup constrGroup = new ConstrGroup(false);
78  
79          IVecInt clause = new VecInt();
80          IVecInt clause1 = new VecInt();
81  
82          final int n = literals.size();
83  
84          int nbGroup = (int) Math.ceil((double) literals.size()
85                  / (double) groupSize);
86  
87          if (nbGroup == 1) {
88              for (int i = 0; i < literals.size() - 1; i++) {
89                  for (int j = i + 1; j < literals.size(); j++) {
90                      clause.push(-literals.get(i));
91                      clause.push(-literals.get(j));
92                      constrGroup.add(solver.addClause(clause));
93                      clause.clear();
94                  }
95              }
96              return constrGroup;
97          }
98  
99          int[] c = new int[nbGroup];
100 
101         for (int i = 0; i < nbGroup; i++) {
102             c[i] = solver.nextFreeVarId(true);
103         }
104 
105         int nbVarLastGroup = n - (nbGroup - 1) * groupSize;
106 
107         // Encoding <=1 for each group of groupLitterals
108         for (int i = 0; i < nbGroup; i++) {
109             int size = 0;
110             if (i == nbGroup - 1) {
111                 size = nbVarLastGroup;
112             } else {
113                 size = groupSize;
114             }
115             // Encoding <=1 for each group of groupLitterals
116             for (int j = 0; j < size - 1; j++) {
117                 for (int k = j + 1; k < size; k++) {
118                     clause.push(-literals.get(i * groupSize + j));
119                     clause.push(-literals.get(i * groupSize + k));
120                     constrGroup.add(solver.addClause(clause));
121                     clause.clear();
122                 }
123             }
124 
125             // If a commander variable is true then some variable in its
126             // corresponding group must be true (clause1)
127             // If a commander variable is false then no variable in its group
128             // can be true (clause)
129             clause1.push(-c[i]);
130             for (int j = 0; j < size; j++) {
131                 clause1.push(literals.get(i * groupSize + j));
132                 clause.push(c[i]);
133                 clause.push(-literals.get(i * groupSize + j));
134                 constrGroup.add(solver.addClause(clause));
135                 clause.clear();
136             }
137             constrGroup.add(solver.addClause(clause1));
138             clause1.clear();
139         }
140 
141         // encode <=1 on commander variables
142 
143         constrGroup.add(addAtMostOne(solver, new VecInt(c), groupSize));
144         return constrGroup;
145     }
146 
147     @Override
148     public IConstr addAtMost(ISolver solver, IVecInt literals, int degree)
149             throws ContradictionException {
150         return super.addAtMost(solver, literals, degree);
151     }
152 
153     @Override
154     public IConstr addExactlyOne(ISolver solver, IVecInt literals)
155             throws ContradictionException {
156         ConstrGroup group = new ConstrGroup();
157 
158         group.add(addAtLeastOne(solver, literals));
159         group.add(addAtMostOne(solver, literals));
160 
161         return group;
162     }
163 
164     @Override
165     public IConstr addExactly(ISolver solver, IVecInt literals, int degree)
166             throws ContradictionException {
167         ConstrGroup group = new ConstrGroup();
168 
169         group.add(addAtLeast(solver, literals, degree));
170         group.add(addAtMost(solver, literals, degree));
171 
172         return group;
173     }
174 
175 }