Blame view

src/constraints/var-eq-times.c 4.46 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
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
/* X = Y * Z */

// XXX: assuming non-negative values 

static int fd_var_eq_times_filter(fd_constraint this)
{
  fd_int x, y, z;
  int min_x, max_x, min_y, max_y, min_z, max_z;
  int changed_x, changed_y, changed_z;

  x = VAR(this, 0);
  y = VAR(this, 1);
  z = VAR(this, 2);

  // XXX: just do (a kind of) bounds consistency, for now

  min_x = _fd_var_min(x);
  max_x = _fd_var_max(x);

  min_z = _fd_var_min(z);
  max_z = _fd_var_max(z);

  if (max_z > 0)
    changed_y =  _fd_var_del_lt(min_x / max_z, y);
  if (min_z > 0)
    changed_y |= _fd_var_del_gt(max_x / min_z, y);

  if (changed_y && fd_domain_empty(y))
    {
      return FD_NOSOLUTION;

      _fd_revise_connected(NULL, y);
    }

  min_y = _fd_var_min(y);
  max_y = _fd_var_max(y);

  if (max_y > 0)
    changed_z =  _fd_var_del_lt(min_x / max_y, z);
  if (min_y > 0)
    changed_z |= _fd_var_del_gt(max_x / min_y, z);

  if (changed_z && fd_domain_empty(z))
    {
      return FD_NOSOLUTION;

      _fd_revise_connected(NULL, z);

      min_z = _fd_var_min(z);
      max_z = _fd_var_max(z);
    }

  changed_x =  _fd_var_del_lt(min_y * min_z, x);
  changed_x |= _fd_var_del_gt(max_y * max_z, x);

  if (changed_x && fd_domain_empty(x))
    {
      return FD_NOSOLUTION;

      _fd_revise_connected(NULL, x);
    }

#if CONSTRAINT_TEMPS > 3
  if (fd_var_single(x, 0) && fd_var_single(y, 0) && fd_var_single(z, 0))
    fd__constraint_set_entailed(this);
#endif

  return FD_OK;
}

int fd_var_eq_times_propagate2(fd_constraint this, fd_int culprit)
{
  fd_int x, y, z;
  int min_x, max_x, min_y, max_y, min_z, max_z;
  int changed_x = 0, changed_y = 0, changed_z = 0;

  x = VAR(this, 0);
  y = VAR(this, 1);
  z = VAR(this, 2);

  // XXX: just do bounds consistency, for now

  if (culprit == x)
    {
      min_x = _fd_var_min(x);
      max_x = _fd_var_max(x);

      min_z = _fd_var_min(z);
      max_z = _fd_var_max(z);

      if (max_z > 0)
	changed_y =  _fd_var_del_lt(min_x / max_z, y);
      if (min_z > 0)
	changed_y |= _fd_var_del_gt(max_x / min_z, y);

      if (changed_y && fd_domain_empty(y))
	return FD_NOSOLUTION;

      min_y = _fd_var_min(y);
      max_y = _fd_var_max(y);

      if (max_y > 0)
	changed_z =  _fd_var_del_lt(min_x / max_y, z);
      if (min_y > 0)
	changed_z |= _fd_var_del_gt(max_x / min_y, z);

      if (changed_z && fd_domain_empty(z))
	return FD_NOSOLUTION;
    }
  else if (culprit == y) // culprit == y || culprit == z
    {
      min_y = _fd_var_min(y);
      max_y = _fd_var_max(y);

      min_z = _fd_var_min(z);
      max_z = _fd_var_max(z);

      changed_x =  _fd_var_del_lt(min_y * min_z, x);
      changed_x |= _fd_var_del_gt(max_y * max_z, x);

      if (changed_x && fd_domain_empty(x))
	return FD_NOSOLUTION;

      min_x = _fd_var_min(x);
      max_x = _fd_var_max(x);

      if (max_y > 0)
	changed_z =  _fd_var_del_lt(min_x / max_y, z);
      if (min_y > 0)
	changed_z |= _fd_var_del_gt(max_x / min_y, z);

      if (changed_z && fd_domain_empty(z))
	return FD_NOSOLUTION;
    }
  else // culprit == z
    {
      min_y = _fd_var_min(y);
      max_y = _fd_var_max(y);

      min_z = _fd_var_min(z);
      max_z = _fd_var_max(z);

      changed_x =  _fd_var_del_lt(min_y * min_z, x);
      changed_x |= _fd_var_del_gt(max_y * max_z, x);

      if (changed_x && fd_domain_empty(x))
	return FD_NOSOLUTION;

      min_x = _fd_var_min(x);
      max_x = _fd_var_max(x);

      if (max_z > 0)
	changed_y =  _fd_var_del_lt(min_x / max_z, y);
      if (min_z > 0)
	changed_y |= _fd_var_del_gt(max_x / min_z, y);

      if (changed_y && fd_domain_empty(y))
	return FD_NOSOLUTION;
    }

  if (changed_x)
    _fd_revise_connected(NULL, x);	// XXX: NULL or this?

  if (changed_y)
    _fd_revise_connected(NULL, y);	// XXX: NULL or this?

  if (changed_z)
    _fd_revise_connected(NULL, z);	// XXX: NULL or this?

#if CONSTRAINT_TEMPS > 3
  if (fd_var_single(x, 0) && fd_var_single(y, 0) && fd_var_single(z, 0))
    fd__constraint_set_entailed(this);
#endif

  return FD_OK;
}

fd_constraint fd_var_eq_times(fd_int x, fd_int y, fd_int z)
{
eef94371   Vasco Pedro   Update to PaCCS v...
180
  fd_constraint c = _fd_constraint_new(3, 0);
965dadaa   Salvador Abreu   initial commit fr...
181
182
183
184
185
186

  if (c)
    {
      c->variables[0] = FD_INT2C_VAR(x);
      c->variables[1] = FD_INT2C_VAR(y);
      c->variables[2] = FD_INT2C_VAR(z);
eef94371   Vasco Pedro   Update to PaCCS v...
187
#ifdef CONSTRAINT_CLASS
965dadaa   Salvador Abreu   initial commit fr...
188
      c->kind = FD_CONSTR_VAR_EQ_TIMES;
965dadaa   Salvador Abreu   initial commit fr...
189
190
191
192
193
194
195
196
197
198
#else /* CONSTRAINT_CLASS */
      c->propagator2 = fd_var_eq_times_propagate2;
#endif /* CONSTRAINT_CLASS */

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

      _fd_add_constraint(c);
    }