Blame view

src/constraints/nogoods.c 1.35 KB
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
#include <string.h>

/* binary constraint (between X and Y) defined through the disallowed pairs */

int fd_nogoods_propagate(fd_constraint this, fd_int revise)
{
  fd_int other;
  int rx, ox;	// variables' indexes within the constraint
  int value;
  int changed = 0;
  int i;

  rx = revise == VAR(this, 0) ? 0 : 1;
  ox = 1 - rx;

  other = VAR(this, ox);

  // only propagate if other's domain is a singleton
  if (!fd_var_single(other, &value))
    return FD_OK;

  for (i = 0; i < this->nconstants; i += 2)
    if (this->constants[i + ox] == value)
      changed |= _fd_var_del_val(this->constants[i + rx], revise);

  if (changed && fd_domain_empty(revise))
    return FD_NOSOLUTION;

  // XXX: enqueue further updates here?
  if (changed)
    _fd_revise_connected(this, revise);

  return FD_OK;
}

fd_constraint fd_nogoods(fd_int x, fd_int y, int *nogoods, int nnogoods)
{
eef94371   Vasco Pedro   Update to PaCCS v...
38
  fd_constraint c = _fd_constraint_new(2, 2 * nnogoods);
965dadaa   Salvador Abreu   initial commit fr...
39
40
41
42

  if (c)
    {
      c->variables[0] = FD_INT2C_VAR(x); c->variables[1] = FD_INT2C_VAR(y);
eef94371   Vasco Pedro   Update to PaCCS v...
43
#ifdef CONSTRAINT_CLASS
965dadaa   Salvador Abreu   initial commit fr...
44
      c->kind = FD_CONSTR_NOGOODS;
965dadaa   Salvador Abreu   initial commit fr...
45
46
47
48
49
50
51
52
53
54
55
#else /* CONSTRAINT_CLASS */
      c->propagator = fd_nogoods_propagate;
#endif /* CONSTRAINT_CLASS */

      memcpy(c->constants, nogoods, 2 * nnogoods * sizeof(int));

      _fd_var_add_constraint(x, c);
      _fd_var_add_constraint(y, c);

      _fd_add_constraint(c);
    }