965dadaa
Salvador Abreu
initial commit fr...
|
1
2
3
4
5
6
|
/* knapsack2(X, Y, Z) == min(Z) <= sum(X . Y) <= max(Z) */
// XXX: assumes all values are non-negative
static int fd_knapsack2_filter(fd_constraint this)
{
|
965dadaa
Salvador Abreu
initial commit fr...
|
7
8
9
10
11
|
#ifdef CONSTRAINT_TEMPS
int ub, lb;
int min, max;
int terms = (this->nvariables - 1) / 2;
int *mins, *maxs; // XXX: trouble possible if a variable appears repeated
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
12
|
int i;
|
965dadaa
Salvador Abreu
initial commit fr...
|
13
14
15
16
17
18
19
20
21
22
23
|
int *base;
assert(!fd__constraint_data_valid(this));
if (!constraint_memory[this->index])
constraint_memory[this->index] = malloc((2 * 2 * terms + 4) * sizeof(int));
base = constraint_memory[this->index];
mins = base + 4;
maxs = mins + 2 * terms;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
24
25
26
27
|
lb = _fd_var_min(VAR(this, this->nvariables - 1)); // lower bound
ub = _fd_var_max(VAR(this, this->nvariables - 1)); // upper bound
|
965dadaa
Salvador Abreu
initial commit fr...
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// sum the minima of the terms
min = 0;
for (i = 0; i < terms; ++i)
{
mins[i] = _fd_var_min(VAR(this, i));
mins[terms + i] = _fd_var_min(VAR(this, terms + i));
min += mins[i] * mins[terms + i];
if (min > ub)
return FD_NOSOLUTION;
}
// sum the maxima of the terms
max = 0;
for (i = 0; i < terms; ++i)
{
maxs[i] = _fd_var_max(VAR(this, i));
maxs[terms + i] = _fd_var_max(VAR(this, terms + i));
max += maxs[i] * maxs[terms + i];
}
if (max < lb)
return FD_NOSOLUTION;
// XXX: poor man's propagation
if (min == ub)
for (i = 0; i < terms; ++i)
{
int min_x = mins[i];
int min_y = mins[terms + i];
if (min_x != 0 || min_y != 0)
{
if (min_y != 0 && _fd_var_del_gt(min_x, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (min_x != 0 && _fd_var_del_gt(min_y, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
}
else if (max == lb)
for (i = 0; i < terms; ++i)
{
int max_x = maxs[i];
int max_y = maxs[terms + i];
if (max_x != 0 && max_y != 0)
{
if (_fd_var_del_lt(max_x, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (_fd_var_del_lt(max_y, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
}
else if (max > ub)
for (i = 0; i < terms; ++i)
{
int min_x = mins[i];
int max_x = maxs[i];
int min_y = mins[terms + i];
int max_y = maxs[terms + i];
if (min_x * (max_y - min_y) > ub - min)
{
max_y = (ub - min + min_x * min_y) / min_x;
_fd_var_del_gt(max_y, VAR(this, terms + i));
_fd_revise_connected(this, VAR(this, terms + i));
}
if ((max_x - min_x) * min_y > ub - min)
{
max_x = (ub - min + min_x * min_y) / min_y;
_fd_var_del_gt(max_x, VAR(this, i));
_fd_revise_connected(this, VAR(this, i));
}
}
if ((min > lb && _fd_var_del_lt(min, VAR(this, this->nvariables - 1))) |
(max < ub && _fd_var_del_gt(max, VAR(this, this->nvariables - 1))))
_fd_revise_connected(this, VAR(this, this->nvariables - 1));
// save values
*base = lb;
*(base + 1) = ub;
*(base + 2) = min;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
122
|
*(base + 3) = max;
|
965dadaa
Salvador Abreu
initial commit fr...
|
123
124
125
126
127
128
129
|
fd__constraint_remember(this);
return FD_OK;
#else /* CONSTRAINT_TEMPS */
int ub, lb;
int min, max;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
130
|
int terms = (this->nvariables - 1) / 2;
|
965dadaa
Salvador Abreu
initial commit fr...
|
131
132
|
int *mins, *maxs; // XXX: trouble possible if a variable appears repeated
int i;
|
965dadaa
Salvador Abreu
initial commit fr...
|
133
134
135
136
137
138
|
mins = alloca(2 * terms * sizeof(*mins));
maxs = alloca(2 * terms * sizeof(*maxs));
lb = _fd_var_min(VAR(this, this->nvariables - 1)); // lower bound
ub = _fd_var_max(VAR(this, this->nvariables - 1)); // upper bound
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
139
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// sum the minima of the terms
min = 0;
for (i = 0; i < terms; ++i)
{
mins[i] = _fd_var_min(VAR(this, i));
mins[terms + i] = _fd_var_min(VAR(this, terms + i));
min += mins[i] * mins[terms + i];
if (min > ub)
return FD_NOSOLUTION;
}
// sum the maxima of the terms
max = 0;
for (i = 0; i < terms; ++i)
{
maxs[i] = _fd_var_max(VAR(this, i));
maxs[terms + i] = _fd_var_max(VAR(this, terms + i));
max += maxs[i] * maxs[terms + i];
}
if (max < lb)
return FD_NOSOLUTION;
// XXX: poor man's propagation
if (min == ub)
for (i = 0; i < terms; ++i)
{
int min_x = mins[i];
int min_y = mins[terms + i];
if (min_x != 0 || min_y != 0)
{
if (min_y != 0 && _fd_var_del_gt(min_x, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (min_x != 0 && _fd_var_del_gt(min_y, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
}
else if (max == lb)
for (i = 0; i < terms; ++i)
{
int max_x = maxs[i];
int max_y = maxs[terms + i];
if (max_x != 0 && max_y != 0)
{
if (_fd_var_del_lt(max_x, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (_fd_var_del_lt(max_y, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
}
else if (max > ub)
for (i = 0; i < terms; ++i)
{
int min_x = mins[i];
int max_x = maxs[i];
int min_y = mins[terms + i];
int max_y = maxs[terms + i];
if (min_x * (max_y - min_y) > ub - min)
{
max_y = (ub - min + min_x * min_y) / min_x;
_fd_var_del_gt(max_y, VAR(this, terms + i));
_fd_revise_connected(this, VAR(this, terms + i));
}
if ((max_x - min_x) * min_y > ub - min)
{
max_x = (ub - min + min_x * min_y) / min_y;
_fd_var_del_gt(max_x, VAR(this, i));
_fd_revise_connected(this, VAR(this, i));
}
}
if ((min > lb && _fd_var_del_lt(min, VAR(this, this->nvariables - 1))) |
(max < ub && _fd_var_del_gt(max, VAR(this, this->nvariables - 1))))
_fd_revise_connected(this, VAR(this, this->nvariables - 1));
return FD_OK;
#endif /* CONSTRAINT_TEMPS */
}
static int fd_knapsack2_propagate2(fd_constraint this, fd_int culprit)
{
#ifdef CONSTRAINT_TEMPS
int ub, lb;
unsigned int min, max;
int terms = (this->nvariables - 1) / 2;
int *mins, *maxs; // XXX: trouble possible if a variable appears repeated
int i;
int *base;
int x, y;
int nmin, nmin_x, nmax, nmax_x;
if (!fd__constraint_data_valid(this))
return fd_knapsack2_filter(this); // ignores culprit
// bounds filtering
base = constraint_memory[this->index];
mins = base + 4;
maxs = mins + 2 * terms;
lb = *base;
ub = *(base + 1);
min = *(base + 2);
max = *(base + 3);
if (culprit == VAR(this, this->nvariables - 1))
{
// the culprit is the sum variable
int nlb, nub;
nlb = _fd_var_min(culprit);
nub = _fd_var_max(culprit);
if (nlb == lb && nub == ub)
return FD_OK;
if (nlb != lb)
{
if (max < nlb)
return FD_NOSOLUTION;
if (max == nlb)
for (i = 0; i < terms; ++i)
{
int max_x = maxs[i];
int max_y = maxs[terms + i];
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
283
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
284
285
286
287
288
289
290
291
292
|
if (max_x != 0 && max_y != 0)
{
if (_fd_var_del_lt(max_x, VAR(this, i)))
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, i));
}
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
293
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
294
|
if (_fd_var_del_lt(max_y, VAR(this, terms + i)))
|
965dadaa
Salvador Abreu
initial commit fr...
|
295
296
297
298
299
300
301
302
303
|
{
if (fd_domain_empty(VAR(this, terms + i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, terms + i));
}
}
}
|