sum.c
3.89 KB
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
/* sum(X, k) */
#include <alloca.h>
static int fd_sum_filter(fd_constraint this)
{
int k;
int min, max;
int *mins, *maxs;
int i;
#ifdef CONSTRAINT_TEMPS
int *base;
assert(!fd__constraint_data_valid(this));
if (constraint_memory[this->index] == NULL)
constraint_memory[this->index] =
malloc((2 + 2 * this->nvariables) * sizeof(*base)); // XXX: NULL
base = constraint_memory[this->index];
mins = base + 2;
maxs = mins + this->nvariables;
#else
mins = alloca(this->nvariables * sizeof(int));
maxs = alloca(this->nvariables * sizeof(int));
#endif
// do bounds filtering
k = this->constants[0];
// sum the minima of the variables' domains
min = 0;
for (i = 0; i < this->nvariables; ++i)
{
min += mins[i] = _fd_var_min(VAR(this, i));
if (min > k)
return FD_NOSOLUTION;
}
// sum the maxima of the variables' domains
max = 0;
for (i = 0; i < this->nvariables; ++i)
max += maxs[i] = _fd_var_max(VAR(this, i));
if (max < k)
return FD_NOSOLUTION;
for (i = 0; i < this->nvariables; ++i)
{
int changed = 0;
if (mins[i] + k - min < maxs[i])
{
_fd_var_del_gt(mins[i] + k - min, VAR(this, i));
changed = 1;
}
if (maxs[i] - (max - k) > mins[i])
{
_fd_var_del_lt(maxs[i] - (max - k), VAR(this, i));
changed = 1;
}
if (changed)
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(this, VAR(this, i));
}
}
#ifdef CONSTRAINT_TEMPS
// save values
*base = min;
*(base + 1) = max;
fd__constraint_remember(this);
#endif /* CONSTRAINT_TEMPS */
return FD_OK;
}
static int fd_sum_propagate2(fd_constraint this, fd_int culprit)
{
#ifdef CONSTRAINT_TEMPS
int k;
int min, max;
int *mins, *maxs;
int *base;
int nmin, nmax, nmin_v, nmax_v;
int v;
int i;
// do bounds filtering
if (!fd__constraint_data_valid(this))
return fd_sum_filter(this); // ignores culprit
base = constraint_memory[this->index];
min = *base;
max = *(base + 1);
mins = base + 2;
maxs = mins + this->nvariables;
k = this->constants[0];
nmin_v = _fd_var_min(culprit);
nmax_v = _fd_var_max(culprit);
// find out where is the culprit
for (v = 0; culprit != VAR(this, v); ++v)
;
if (nmin_v == mins[v] && nmax_v == maxs[v])
return FD_OK; // nothing has (meaningfully) changed
do
{
nmin = min - mins[v] + nmin_v;
nmax = max - maxs[v] + nmax_v;
mins[v] = nmin_v;
maxs[v] = nmax_v;
while (++v < this->nvariables && VAR(this, v) != culprit)
;
}
while (v < this->nvariables);
if (nmin > k || nmax < k)
return FD_NOSOLUTION;
if (nmin != min)
{
for (i = 0; i < this->nvariables; ++i)
if (mins[i] + k - nmin < maxs[i] &&
_fd_var_del_gt(mins[i] + k - nmin, VAR(this, i)))
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(NULL, VAR(this, i));
}
*base = nmin;
}
if (nmax != max)
{
for (i = 0; i < this->nvariables; ++i)
if (maxs[i] - (nmax - k) > mins[i] &&
_fd_var_del_lt(maxs[i] - (nmax - k), VAR(this, i)))
{
if (fd_domain_empty(VAR(this, i)))
return FD_NOSOLUTION;
_fd_revise_connected(NULL, VAR(this, i));
}
*(base + 1) = nmax;
}
return FD_OK;
#else /* CONSTRAINT_TEMPS */
return fd_sum_filter(this); // ignores culprit
#endif /* CONSTRAINT_TEMPS */
}
fd_constraint fd_sum(fd_int *variables, int nvariables, int k)
{
fd_constraint c = _fd_constraint_new(nvariables, 1);
int i;
if (c)
{
for (i = 0; i < nvariables; ++i)
c->variables[i] = FD_INT2C_VAR(variables[i]);
c->constants[0] = k;
#ifdef CONSTRAINT_CLASS
c->kind = FD_CONSTR_SUM;
#else /* CONSTRAINT_CLASS */
c->propagator2 = fd_sum_propagate2;
#endif /* CONSTRAINT_CLASS */
for (i = 0; i < nvariables; ++i)
_fd_var_add_constraint(VAR(c, i), c);
_fd_add_constraint(c);
}
return c;
}