1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
43
44
45
46
47
48
49
50
51
52
53
54
55 public class Commander extends EncodingStrategyAdapter {
56
57
58
59
60 private static final long serialVersionUID = 1L;
61
62
63
64
65
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
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
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
126
127
128
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
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 }