Blame view

src/constraints/element-var.c 3.48 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
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
/* element-var */
/* element-var(X, y, z) == X[y] = z (0 <= y < |X|) */

static int fd_element_var_filter(fd_constraint this)
{
  int length = this->nvariables - 2;
  fd_int index, value;
  int imax, min, max;
  int v;
  int i;

  // do some filtering...

  index = VAR(this, this->nvariables - 2);

  // check the index variable
  if (_fd_var_del_ge(length, index))
    {
      if (fd_domain_empty(index))
	return FD_NOSOLUTION;

      _fd_revise_connected(this, index);
    }

  value = VAR(this, this->nvariables - 1);

  // see if the index is already fixed
  if (fd_var_single(index, &v))
    {
      if (_fd_var_intersect(VAR(this, v), value))
	{
	  if (fd_domain_empty(VAR(this, v)))
	    return FD_NOSOLUTION;

	  _fd_revise_connected(this, VAR(this, v));
	}

      if (_fd_var_intersect(value, VAR(this, v)))
	{
	  if (fd_domain_empty(value))
	    return FD_NOSOLUTION;

	  _fd_revise_connected(this, value);
	}

      return FD_OK;
    }

  // do some bounds filtering on the value variable
  imax = _fd_var_max(index);

  min = _fd_var_min(VAR(this, imax));
  max = _fd_var_max(VAR(this, imax));

  for (i = _fd_var_min(index); i < imax; ++i)
    {
      int m;

      if ((m = _fd_var_min(VAR(this, i))) < min)
	min = m;
      else if ((m = _fd_var_max(VAR(this, i))) > max)
	max = m;
    }

  if (_fd_var_del_lt(min, value) | _fd_var_del_gt(max, value))
    {
      if (fd_domain_empty(value))
	return FD_NOSOLUTION;

      _fd_revise_connected(this, value);
    }

  // XXX: more...

  return FD_OK;
}

static int fd_element_var_propagate2(fd_constraint this, fd_int culprit)
{
  int length = this->nvariables - 2;
  fd_int index, value;
  int i;

  // do some filtering...

  index = VAR(this, this->nvariables - 2);
  value = VAR(this, this->nvariables - 1);

  // see if the culprit is the index variable
  if (culprit == index)
    {
      if (fd_var_single(index, &i))
	{
	  if (_fd_var_intersect(VAR(this, i), value))
	    {
	      if (fd_domain_empty(VAR(this, i)))
		return FD_NOSOLUTION;

	      _fd_revise_connected(this, VAR(this, i));
	    }

	  if (_fd_var_intersect(value, VAR(this, i)))
	    {
	      if (fd_domain_empty(value))
		return FD_NOSOLUTION;

	      _fd_revise_connected(this, value);
	    }
	}

      return FD_OK;
    }

  // see if the culprit is the value variable
  if (culprit == value)
    {
      int k;

      if (fd_var_single(value, &k))
	{
	  // check which variables have k in their domain
	  int imax = _fd_var_max(index);

	  for (i = _fd_var_min(index); i <= imax; ++i)
	    if (!_fd_var_contains_val(VAR(this, i), k) &&
		_fd_var_del_val(i, index))
	      {
		if (fd_domain_empty(index))
		  return FD_NOSOLUTION;

		_fd_revise_connected(NULL, index);
	      }
	}

      return FD_OK;
    }

  // XXX: filter for non-index and non-value variables

  return FD_OK;
}

fd_constraint fd_element_var(fd_int *variables, int nvariables, fd_int index,
			     fd_int value)
{
eef94371   Vasco Pedro   Update to PaCCS v...
146
  fd_constraint c = _fd_constraint_new(nvariables + 2, 0);
965dadaa   Salvador Abreu   initial commit fr...
147
148
149
150
151
152
153
154
155
156
  int i;

  // XXX: add a constraint 0 <= INDEX < nvariables

  if (c)
    {
      for (i = 0; i < nvariables; ++i)
	c->variables[i] = FD_INT2C_VAR(variables[i]);
      c->variables[nvariables] = FD_INT2C_VAR(index);
      c->variables[nvariables + 1] = FD_INT2C_VAR(value);
eef94371   Vasco Pedro   Update to PaCCS v...
157
#ifdef CONSTRAINT_CLASS
965dadaa   Salvador Abreu   initial commit fr...
158
      c->kind = FD_CONSTR_ELEMENT_VAR;
965dadaa   Salvador Abreu   initial commit fr...
159
160
161
162
163
164
165
166
167
#else /* CONSTRAINT_CLASS */
      c->propagator2 = fd_element_var_propagate2;
#endif /* CONSTRAINT_CLASS */

      for (i = 0; i < c->nvariables; ++i)
	_fd_var_add_constraint(VAR(c, i), c);

      _fd_add_constraint(c);
    }