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 *
42 * Ladder encoding for the "at most one" and "exactly one" cases.
43 *
44 * The ladder encoding described in: I. P. Gent and P. Nightingale,
45 * "A new encoding for AllDifferent into SAT", in International Workshop on
46 * Modeling and Reformulating Constraint Satisfaction Problems, 2004
47 *
48 * @author sroussel
49 * @since 2.3.1
50 */
51 public class Ladder extends EncodingStrategyAdapter {
52
53 @Override
54 /**
55 * If n is the number of variables in the constraint,
56 * this encoding adds n variables and 4n clauses
57 * (3n+1 size 2 clauses and n-1 size 3 clauses)
58 */
59 public IConstr addAtMostOne(ISolver solver, IVecInt literals)
60 throws ContradictionException {
61 ConstrGroup group = new ConstrGroup(false);
62 final int n = literals.size() + 1;
63
64 int xN = solver.nextFreeVarId(true);
65 int y[] = new int[n - 1];
66
67 for (int i = 0; i < n - 1; i++) {
68 y[i] = solver.nextFreeVarId(true);
69 }
70
71 IVecInt clause = new VecInt();
72
73 // Constraint \bigwedge_{i=1}{n-2} (\neg y_{i+1} \vee y_i)
74 for (int i = 1; i <= n - 2; i++) {
75 clause.push(-y[i]);
76 clause.push(y[i - 1]);
77 group.add(solver.addClause(clause));
78 clause.clear();
79 }
80
81 // Constraint \bigwedge_{i=2}{n-1} (\neg y_{i-1} \vee y_i \vee x_i)
82 for (int i = 2; i <= n - 1; i++) {
83 clause.push(-y[i - 2]);
84 clause.push(y[i - 1]);
85 clause.push(literals.get(i - 1));
86 group.add(solver.addClause(clause));
87 clause.clear();
88 }
89
90 // Constraint \bigwedge_{i=2}{n-1} (\neg x_i \vee y_{i-1)})
91 for (int i = 2; i <= n - 1; i++) {
92 clause.push(-literals.get(i - 1));
93 clause.push(y[i - 2]);
94 group.add(solver.addClause(clause));
95 clause.clear();
96 }
97
98 // Constraint \bigwedge_{i=2}{n-1} (\neg x_i \vee \neg y_i)
99 for (int i = 2; i <= n - 1; i++) {
100 clause.push(-literals.get(i - 1));
101 clause.push(-y[i - 1]);
102 group.add(solver.addClause(clause));
103 clause.clear();
104 }
105
106 // Constraint y_1 \vee x_1
107 clause.push(y[0]);
108 clause.push(literals.get(0));
109 group.add(solver.addClause(clause));
110 clause.clear();
111
112 // Constraint \neg y_1 \vee \neg x_1
113 clause.push(-y[0]);
114 clause.push(-literals.get(0));
115 group.add(solver.addClause(clause));
116 clause.clear();
117
118 // Constraint \neg y_{n-1} \vee x_n
119 clause.push(-y[n - 2]);
120 clause.push(xN);
121 group.add(solver.addClause(clause));
122 clause.clear();
123
124 // Constraint y_{n-1} \vee \neg x_n
125 clause.push(y[n - 2]);
126 clause.push(-xN);
127 group.add(solver.addClause(clause));
128 clause.clear();
129
130 return group;
131 }
132
133 @Override
134 /**
135 * If n is the number of variables in the constraint,
136 * this encoding adds n-1 variables and 4(n-1) clauses
137 * (3n-2 size 2 clauses and n-2 size 3 clauses)
138 */
139 public IConstr addExactlyOne(ISolver solver, IVecInt literals)
140 throws ContradictionException {
141 ConstrGroup group = new ConstrGroup(false);
142 final int n = literals.size();
143
144 int y[] = new int[n - 1];
145
146 for (int i = 0; i < n - 1; i++) {
147 y[i] = solver.nextFreeVarId(true);
148 }
149
150 IVecInt clause = new VecInt();
151
152 // Constraint \bigwedge_{i=1}{n-2} (\neg y_{i+1} \vee y_i)
153 for (int i = 1; i <= n - 2; i++) {
154 clause.push(-y[i]);
155 clause.push(y[i - 1]);
156 group.add(solver.addClause(clause));
157 clause.clear();
158 }
159
160 // Constraint \bigwedge_{i=2}{n-1} (\neg y_{i-1} \vee y_i \vee x_i)
161 for (int i = 2; i <= n - 1; i++) {
162 clause.push(-y[i - 2]);
163 clause.push(y[i - 1]);
164 clause.push(literals.get(i - 1));
165 group.add(solver.addClause(clause));
166 clause.clear();
167 }
168
169 // Constraint \bigwedge_{i=2}{n-1} (\neg x_i \vee y_{i-1)})
170 for (int i = 2; i <= n - 1; i++) {
171 clause.push(-literals.get(i - 1));
172 clause.push(y[i - 2]);
173 group.add(solver.addClause(clause));
174 clause.clear();
175 }
176
177 // Constraint \bigwedge_{i=2}{n-1} (\neg x_i \vee \neg y_i)
178 for (int i = 2; i <= n - 1; i++) {
179 clause.push(-literals.get(i - 1));
180 clause.push(-y[i - 1]);
181 group.add(solver.addClause(clause));
182 clause.clear();
183 }
184
185 // Constraint y_1 \vee x_1
186 clause.push(y[0]);
187 clause.push(literals.get(0));
188 group.add(solver.addClause(clause));
189 clause.clear();
190
191 // Constraint \neg y_1 \vee \neg x_1
192 clause.push(-y[0]);
193 clause.push(-literals.get(0));
194 group.add(solver.addClause(clause));
195 clause.clear();
196
197 // Constraint \neg y_{n-1} \vee x_n
198 clause.push(-y[n - 2]);
199 clause.push(literals.get(n - 1));
200 group.add(solver.addClause(clause));
201 clause.clear();
202
203 // Constraint y_{n-1} \vee \neg x_n
204 clause.push(y[n - 2]);
205 clause.push(-literals.get(n - 1));
206 group.add(solver.addClause(clause));
207 clause.clear();
208
209 return group;
210 }
211 }