exactly-var.c
4.21 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
/* exactly-var(Xn, Y, k) == #{ x \in Xn | x = k } = Y */
static int fd_exactly_var_filter(fd_constraint this)
{
int k, c;
int set, possible;
int value;
int i;
if (fd_var_single(VAR(this, this->nvariables - 1), &c))
{
k = this->constants[0];
set = possible = 0;
// count the variables that are set to k and those which contain k
// in their domain
for (i = 0; i < this->nvariables - 1; ++i)
if (fd_var_single(VAR(this, i), &value))
{
if (value == k)
// if there are more than c variables set to k, fail
if (++set > c)
return FD_NOSOLUTION;
}
else if (_fd_var_contains_val(VAR(this, i), k))
possible++;
// see if it still possible to satisfy the constraint
if (set + possible < c)
return FD_NOSOLUTION;
if (set + possible == c)
{
for (i = 0; possible > 0; ++i)
if (!fd_var_single(VAR(this, i), NULL) &&
_fd_var_contains_val(VAR(this, i), k))
{
_fd_var_set_value(VAR(this, i), k);
possible--;
_fd_revise_connected(this, VAR(this, i));
}
fd__constraint_set_entailed(this);
return FD_OK;
}
if (set == c)
{
for (i = 0; possible > 0; ++i)
if (!fd_var_single(VAR(this, i), NULL) &&
_fd_var_del_val(k, VAR(this, i)))
{
possible--;
_fd_revise_connected(this, VAR(this, i));
}
fd__constraint_set_entailed(this);
return FD_OK;
}
}
else // !fd_var_single(Y)
{
int cmin, cmax;
int changed = 0;
cmax = _fd_var_max(VAR(this, this->nvariables - 1));
k = this->constants[0];
set = possible = 0;
// count the variables that are set to k and those which contain k
// in their domain
for (i = 0; i < this->nvariables - 1; ++i)
if (fd_var_single(VAR(this, i), &value))
{
if (value == k)
// if there are more than d-max(Y) variables set to k, fail
if (++set > cmax)
return FD_NOSOLUTION;
}
else if (_fd_var_contains_val(VAR(this, i), k))
possible++;
cmin = _fd_var_min(VAR(this, this->nvariables - 1));
// if it isn't possible to set d-min(Y) variables to k, fail
if (set + possible < cmin)
return FD_NOSOLUTION;
if (set + possible == cmin)
{
for (i = 0; possible > 0; ++i)
if (!fd_var_single(VAR(this, i), NULL) &&
_fd_var_contains_val(VAR(this, i), k))
{
_fd_var_set_value(VAR(this, i), k);
possible--;
_fd_revise_connected(this, VAR(this, i));
}
_fd_var_set_value(VAR(this, this->nvariables - 1), cmin);
_fd_revise_connected(this, VAR(this, this->nvariables - 1));
fd__constraint_set_entailed(this);
return FD_OK;
}
if (set == cmax)
{
for (i = 0; possible > 0; ++i)
if (!fd_var_single(VAR(this, i), NULL) &&
_fd_var_del_val(k, VAR(this, i)))
{
possible--;
_fd_revise_connected(this, VAR(this, i));
}
_fd_var_set_value(VAR(this, this->nvariables - 1), cmax);
_fd_revise_connected(this, VAR(this, this->nvariables - 1));
fd__constraint_set_entailed(this);
return FD_OK;
}
if (set > cmin)
changed = _fd_var_del_lt(set, VAR(this, this->nvariables - 1));
if (set + possible < cmax)
changed = _fd_var_del_gt(set + possible, VAR(this, this->nvariables - 1));
if (changed)
_fd_revise_connected(this, VAR(this, this->nvariables - 1));
}
// the constraint can still be satisfied: set < c && set + possible > c
return FD_OK;
}
static int fd_exactly_var_propagate2(fd_constraint this, fd_int culprit)
{
// only revise if culprit's domain is a singleton
if (!fd_var_single(culprit, NULL))
return FD_OK;
return fd_exactly_var_filter(this);
}
fd_constraint fd_exactly_var(fd_int *variables, int nvariables, fd_int card, int k)
{
fd_constraint c = _fd_constraint_new(nvariables + 1, 1);
int i;
if (c)
{
for (i = 0; i < nvariables; ++i)
c->variables[i] = FD_INT2C_VAR(variables[i]);
c->variables[nvariables] = FD_INT2C_VAR(card);
c->constants[0] = k;
#ifdef CONSTRAINT_CLASS
c->kind = FD_CONSTR_EXACTLY_VAR;
#else /* CONSTRAINT_CLASS */
c->propagator2 = fd_exactly_var_propagate2;
#endif /* CONSTRAINT_CLASS */
for (i = 0; i <= nvariables; ++i)
_fd_var_add_constraint(VAR(c, i), c);
_fd_add_constraint(c);
}
return c;
}