965dadaa
Salvador Abreu
initial commit fr...
|
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/* sum_prod */
/* sum_prod(X, Y, k) == X . Y = k */
// XXX: assumes all values are non-negative
static int fd_sum_prod_filter(fd_constraint this)
{
#ifdef CONSTRAINT_TEMPS
int k;
int min, max;
int terms = this->nvariables / 2;
int *mins, *maxs;
int *base;
int i;
assert(!fd__constraint_data_valid(this));
if (constraint_memory[this->index] == NULL)
constraint_memory[this->index] = malloc((2 + 4 * terms) * sizeof(*base));
base = constraint_memory[this->index];
mins = base + 2;
maxs = base + 2 + 2 * terms;
k = this->constants[0];
// 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 > k)
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 < k)
return FD_NOSOLUTION;
// XXX: poor man's propagation
if (min == k)
{
for (i = 0; i < terms; ++i)
{
int xmin = mins[i];
int ymin = mins[terms + i];
if (ymin != 0 && maxs[i] > xmin && _fd_var_del_gt(xmin, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (xmin != 0 && maxs[terms + i] > ymin &&
_fd_var_del_gt(ymin, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
fd__constraint_set_entailed(this);
}
else if (max == k)
{
for (i = 0; i < terms; ++i)
{
int xmax = maxs[i];
int ymax = maxs[terms + i];
if (xmax != 0 && ymax != 0)
{
if (mins[i] < xmax && _fd_var_del_lt(xmax, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (mins[terms + i] < ymax &&
_fd_var_del_lt(ymax, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
}
fd__constraint_set_entailed(this);
}
else // min < k < max
{
for (i = 0; i < terms; ++i)
{
int xmin = mins[i];
int xmax = maxs[i];
int ymin = mins[terms + i];
int ymax = maxs[terms + i];
int changed_x = 0, changed_y = 0;
if (min + (xmax - xmin) * ymin > k)
{
int nxmax = (k - min) / ymin + xmin;
changed_x = _fd_var_del_gt(nxmax, VAR(this, i));
}
if (min + xmin * (ymax - ymin) > k)
{
int nymax = (k - min) / xmin + ymin;
changed_y = _fd_var_del_gt(nymax, VAR(this, terms + i));
}
if (max - (xmax - xmin) * ymax < k)
{
int nxmin = (k - max) / ymax + xmax;
changed_x |= _fd_var_del_lt(nxmin, VAR(this, i));
}
if (max - xmax * (ymax - ymin) < k)
{
int nymin = (k - max) / xmax + ymax;
changed_y |= _fd_var_del_lt(nymin, VAR(this, terms + i));
}
if (changed_x)
assert(!fd_domain_empty(VAR(this, i))),
_fd_revise_connected(NULL, VAR(this, i));
if (changed_y)
assert(!fd_domain_empty(VAR(this, terms + i))),
_fd_revise_connected(NULL, VAR(this, terms + i));
}
}
// save values
*base = min;
*(base + 1) = max;
fd__constraint_remember(this);
return FD_OK;
#else /* CONSTRAINT_TEMPS */
int k;
int min, max;
int terms = this->nvariables / 2;
int i;
k = this->constants[0];
// sum the minima of the terms
min = 0;
for (i = 0; i < terms; ++i)
{
min += _fd_var_min(VAR(this, i)) * _fd_var_min(VAR(this, terms + i));
if (min > k)
return FD_NOSOLUTION;
}
// sum the maxima of the terms
max = 0;
for (i = 0; i < terms; ++i)
max += _fd_var_max(VAR(this, i)) * _fd_var_max(VAR(this, terms + i));
if (max < k)
return FD_NOSOLUTION;
// XXX: poor man's propagation
if (min == k)
for (i = 0; i < terms; ++i)
{
int xmin = _fd_var_min(VAR(this, i));
int ymin = _fd_var_min(VAR(this, terms + i));
if (ymin != 0 && _fd_var_del_gt(xmin, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (xmin != 0 && _fd_var_del_gt(ymin, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
else if (max == k)
for (i = 0; i < terms; ++i)
{
int xmax = _fd_var_max(VAR(this, i));
int ymax = _fd_var_max(VAR(this, terms + i));
if (xmax != 0 && ymax != 0)
{
if (_fd_var_del_lt(xmax, VAR(this, i)))
_fd_revise_connected(this, VAR(this, i));
if (_fd_var_del_lt(ymax, VAR(this, terms + i)))
_fd_revise_connected(this, VAR(this, terms + i));
}
}
return FD_OK;
#endif /* CONSTRAINT_TEMPS */
}
static int fd_sum_prod_propagate2(fd_constraint this, fd_int culprit)
{
#ifdef CONSTRAINT_TEMPS
int k;
int min, max;
int terms = this->nvariables / 2;
int *mins, *maxs;
int *base;
int nmin, nmax, nmin_v, nmax_v;
int i;
if (!fd__constraint_data_valid(this))
return fd_sum_prod_filter(this);
// fetch values
base = constraint_memory[this->index];
min = *base;
max = *(base + 1);
mins = base + 2;
maxs = base + 2 + 2 * terms;
k = this->constants[0];
// get the new bounds of culprit's domain
nmin_v = _fd_var_min(culprit);
nmax_v = _fd_var_max(culprit);
// find out which term(s) culprit belongs to
for (i = 0; culprit != VAR(this, i); ++i)
;
if (nmin_v == mins[i] && nmax_v == maxs[i])
return FD_OK; // nothing has (meaningfully) changed
nmin = min;
nmax = max;
do
{
int j = (i + terms) % this->nvariables;
nmin += (nmin_v - mins[i]) * mins[j];
nmax -= (maxs[i] - nmax_v) * maxs[j];
mins[i] = nmin_v;
maxs[i] = nmax_v;
// find out which other terms culprit belongs to
for (++i; i < this->nvariables && culprit != VAR(this, i); ++i)
;
}
while (i < this->nvariables);
if (nmin > k || nmax < k)
return FD_NOSOLUTION;
if (nmin == min && nmax == max)
return FD_OK; // nothing has (meaningfully) changed
// XXX: poor man's propagation
if (nmin == k)
{
for (i = 0; i < terms; ++i)
{
int xmin = mins[i];
int ymin = mins[terms + i];
if (ymin != 0 && maxs[i] > xmin && _fd_var_del_gt(xmin, VAR(this, i)))
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, i));
}
if (xmin != 0 && maxs[terms + i] > ymin &&
_fd_var_del_gt(ymin, VAR(this, terms + i)))
{
if (fd_domain_empty(VAR(this, terms + i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, terms + i));
}
}
fd__constraint_set_entailed(this);
}
else if (nmax == k)
{
for (i = 0; i < terms; ++i)
{
int xmax = maxs[i];
int ymax = maxs[terms + i];
if (xmax != 0 && ymax != 0)
{
if (mins[i] < xmax && _fd_var_del_lt(xmax, VAR(this, i)))
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, i));
}
if (mins[terms + i] < ymax &&
_fd_var_del_lt(ymax, VAR(this, terms + i)))
{
if (fd_domain_empty(VAR(this, terms + i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, terms + i));
}
}
}
fd__constraint_set_entailed(this);
}
else // nmin < k < nmax
{
for (i = 0; i < terms; ++i)
{
int xmin = mins[i];
int xmax = maxs[i];
int ymin = mins[terms + i];
int ymax = maxs[terms + i];
int changed_x = 0, changed_y = 0;
if (nmin + (xmax - xmin) * ymin > k)
{
int nxmax = (k - nmin) / ymin + xmin;
changed_x = _fd_var_del_gt(nxmax, VAR(this, i));
}
if (nmin + xmin * (ymax - ymin) > k)
{
int nymax = (k - nmin) / xmin + ymin;
changed_y = _fd_var_del_gt(nymax, VAR(this, terms + i));
}
if (nmax - (xmax - xmin) * ymax < k)
{
int nxmin = (k - nmax) / ymax + xmax;
changed_x |= _fd_var_del_lt(nxmin, VAR(this, i));
}
if (nmax - xmax * (ymax - ymin) < k)
{
int nymin = (k - nmax) / xmax + ymax;
changed_y |= _fd_var_del_lt(nymin, VAR(this, terms + i));
}
if (changed_x)
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(NULL, VAR(this, i));
}
if (changed_y)
{
if (fd_domain_empty(VAR(this, terms + i)))
return FD_NOSOLUTION;
_fd_revise_connected(NULL, VAR(this, terms + i));
}
}
}
// save new values
*base = nmin;
*(base + 1) = nmax;
return FD_OK;
#else /* CONSTRAINT_TEMPS */
return fd_sum_prod_filter(this); // ignores culprit
#endif /* CONSTRAINT_TEMPS */
}
fd_constraint fd_sum_prod(fd_int *xs, fd_int *ys, int nvariables, int k)
{
|