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       * In this encoding, variables are partitioned in groups. Kwon and Klieber
59       * claim that the fewest clauses are produced when the size of the groups is
60       * 3, thus leading to 3.5 clauses and introducing n/2 variables.
61       */
62      @Override
63      public IConstr addAtMostOne(ISolver solver, IVecInt literals)
64              throws ContradictionException {
65  
66          return addAtMostOne(solver, literals, 3);
67      }
68  
69      private IConstr addAtMostOne(ISolver solver, IVecInt literals, int groupSize)
70              throws ContradictionException {
71  
72          ConstrGroup constrGroup = new ConstrGroup(false);
73  
74          IVecInt clause = new VecInt();
75          IVecInt clause1 = new VecInt();
76  
77          final int n = literals.size();
78  
79          int nbGroup = (int) Math.ceil((double) literals.size()
80                  / (double) groupSize);
81  
82          if (nbGroup == 1) {
83              for (int i = 0; i < literals.size() - 1; i++) {
84                  for (int j = i + 1; j < literals.size(); j++) {
85                      clause.push(-literals.get(i));
86                      clause.push(-literals.get(j));
87                      constrGroup.add(solver.addClause(clause));
88                      clause.clear();
89                  }
90              }
91              return constrGroup;
92          }
93  
94          int[] c = new int[nbGroup];
95  
96          for (int i = 0; i < nbGroup; i++) {
97              c[i] = solver.nextFreeVarId(true);
98          }
99  
100         int nbVarLastGroup = n - (nbGroup - 1) * groupSize;
101 
102         // Encoding <=1 for each group of groupLitterals
103         for (int i = 0; i < nbGroup; i++) {
104             int size = 0;
105             if (i == nbGroup - 1) {
106                 size = nbVarLastGroup;
107             } else {
108                 size = groupSize;
109             }
110             // Encoding <=1 for each group of groupLitterals
111             for (int j = 0; j < size - 1; j++) {
112                 for (int k = j + 1; k < size; k++) {
113                     clause.push(-literals.get(i * groupSize + j));
114                     clause.push(-literals.get(i * groupSize + k));
115                     constrGroup.add(solver.addClause(clause));
116                     clause.clear();
117                 }
118             }
119 
120             // If a commander variable is true then some variable in its
121             // corresponding group must be true (clause1)
122             // If a commander variable is false then no variable in its group
123             // can be true (clause)
124             clause1.push(-c[i]);
125             for (int j = 0; j < size; j++) {
126                 clause1.push(literals.get(i * groupSize + j));
127                 clause.push(c[i]);
128                 clause.push(-literals.get(i * groupSize + j));
129                 constrGroup.add(solver.addClause(clause));
130                 clause.clear();
131             }
132             constrGroup.add(solver.addClause(clause1));
133             clause1.clear();
134         }
135 
136         // encode <=1 on commander variables
137 
138         constrGroup.add(addAtMostOne(solver, new VecInt(c), groupSize));
139         return constrGroup;
140     }
141 
142     @Override
143     public IConstr addAtMost(ISolver solver, IVecInt literals, int degree)
144             throws ContradictionException {
145         return super.addAtMost(solver, literals, degree);
146         // return addAtMost(solver, literals, degree, degree * 2);
147     }
148 
149     private IConstr addAtMost(ISolver solver, IVecInt literals, int k,
150             int groupSize) throws ContradictionException {
151         ConstrGroup constrGroup = new ConstrGroup(false);
152 
153         IVecInt clause = new VecInt();
154 
155         final int n = literals.size();
156 
157         int nbGroup = (int) Math.ceil((double) n / (double) groupSize);
158 
159         if (nbGroup == 1) {
160             for (IVecInt vec : literals.subset(k + 1)) {
161                 for (int i = 0; i < vec.size(); i++) {
162                     clause.push(-vec.get(i));
163                 }
164                 constrGroup.add(solver.addClause(clause));
165                 clause.clear();
166             }
167             return constrGroup;
168         }
169 
170         int[][] c = new int[nbGroup][k];
171         VecInt vecC = new VecInt();
172 
173         for (int i = 0; i < nbGroup - 1; i++) {
174             for (int j = 0; j < k; j++) {
175                 c[i][j] = solver.nextFreeVarId(true);
176                 vecC.push(c[i][j]);
177             }
178         }
179 
180         int nbVarLastGroup = n - (nbGroup - 1) * groupSize;
181         int nbCForLastGroup;
182         // nbCForLastGroup = Math.min(k, nbVarLastGroup);
183         nbCForLastGroup = k;
184 
185         for (int j = 0; j < nbCForLastGroup; j++) {
186             c[nbGroup - 1][j] = solver.nextFreeVarId(true);
187             vecC.push(c[nbGroup - 1][j]);
188         }
189 
190         VecInt[] groupTab = new VecInt[nbGroup];
191 
192         // Every literal x is in a group Gi
193         // For every group Gi, we construct the group every {Gi \cup {c[i][j], j
194         // =0,...k-1}}
195         for (int i = 0; i < nbGroup - 1; i++) {
196             groupTab[i] = new VecInt();
197 
198             int size = 0;
199             if (i == nbGroup - 1) {
200                 size = nbVarLastGroup;
201             } else {
202                 size = groupSize;
203             }
204 
205             for (int j = 0; j < size; j++) {
206                 groupTab[i].push(literals.get(i * groupSize + j));
207             }
208             for (int j = 0; j < k; j++) {
209                 groupTab[i].push(-c[i][j]);
210             }
211         }
212 
213         int size = nbVarLastGroup;
214         groupTab[nbGroup - 1] = new VecInt();
215         for (int j = 0; j < size; j++) {
216             groupTab[nbGroup - 1].push(literals.get((nbGroup - 1) * groupSize
217                     + j));
218         }
219         for (int j = 0; j < nbCForLastGroup; j++) {
220             groupTab[nbGroup - 1].push(-c[nbGroup - 1][j]);
221         }
222 
223         Binomial bin = new Binomial();
224 
225         // Encode <=k for every Gi \cup {c[i][j], j=0,...k-1}} with Binomial
226         // encoding
227         for (int i = 0; i < nbGroup; i++) {
228             constrGroup.add(bin.addAtMost(solver, groupTab[i], k));
229             System.out.println(constrGroup.getConstr(i).size());
230         }
231 
232         // Encode >=k for every Gi \cup {c[i][j], j=0,...k-1}} with Binomial
233         // encoding
234         for (int i = 0; i < nbGroup; i++) {
235             constrGroup.add(bin.addAtLeast(solver, groupTab[i], k));
236             System.out.println(constrGroup.getConstr(i + nbGroup).size());
237         }
238 
239         for (int i = 0; i < nbGroup; i++) {
240             for (int j = 0; j < k - 1; j++) {
241                 clause.push(-c[i][j]);
242                 clause.push(c[i][j + 1]);
243                 constrGroup.add(solver.addClause(clause));
244                 clause.clear();
245             }
246         }
247 
248         constrGroup.add(addAtMost(solver, vecC, k));
249 
250         return constrGroup;
251     }
252 
253     @Override
254     public IConstr addAtLeast(ISolver solver, IVecInt literals, int k)
255             throws ContradictionException {
256 
257         IVecInt newLits = new VecInt();
258         for (int i = 0; i < literals.size(); i++) {
259             newLits.push(-literals.get(i));
260         }
261 
262         return addAtMost(solver, newLits, literals.size() - k);
263     }
264 
265     @Override
266     public IConstr addExactlyOne(ISolver solver, IVecInt literals)
267             throws ContradictionException {
268         ConstrGroup group = new ConstrGroup();
269 
270         group.add(addAtLeastOne(solver, literals));
271         group.add(addAtMostOne(solver, literals));
272 
273         return group;
274     }
275 
276     @Override
277     public IConstr addExactly(ISolver solver, IVecInt literals, int degree)
278             throws ContradictionException {
279         ConstrGroup group = new ConstrGroup();
280 
281         group.add(addAtLeast(solver, literals, degree));
282         group.add(addAtMost(solver, literals, degree));
283 
284         return group;
285     }
286 }