Blame view

src/constraints/knapsack.c 3.17 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
/* knapsack */
/* knapsack(X, Y) == min(Y) <= sum(X) <= max(Y) */

// XXX: assumes all values are non-negative

static int fd_knapsack_propagate2(fd_constraint this, fd_int culprit)
{
  int ub, lb;
  int min, max;
  int terms = this->nvariables - 1;
  int i;

  // XXX: ignoring culprit

  lb = _fd_var_min(VAR(this, this->nvariables - 1));	// lower bound
  ub = _fd_var_max(VAR(this, this->nvariables - 1));	// upper bound

  // sum the minima of the variables' domains
  min = 0;

  for (i = 0; i < terms; ++i)
    {
      min += _fd_var_min(VAR(this, i));

      if (min > ub)
	return FD_NOSOLUTION;
    }

  // sum the maxima of the variables' domains
  max = 0;

  for (i = 0; i < terms; ++i)
    max += _fd_var_max(VAR(this, i));

  if (max < lb)
    return FD_NOSOLUTION;

  // XXX: poor man's propagation
  if (min == ub)
    for (i = 0; i < terms; ++i)
      {
	int min_x = _fd_var_min(VAR(this, i));
	int min_y = _fd_var_min(VAR(this, terms + i));
	int changed_x = 0, changed_y = 0;

	if (min_x != 0 || min_y != 0)
	  if (min_x == 0)
	    {
	      if (_fd_var_del_gt(0, VAR(this, i)))
		_fd_revise_connected(this, VAR(this, i));
	    }
	  else if (min_y == 0)
	    {
	      if (_fd_var_del_gt(0, VAR(this, terms + i)))
		_fd_revise_connected(this, VAR(this, terms + i));
	    }
	  else // min_x != 0 && min_y != 0
	    {
	      if (_fd_var_del_gt(min_x, VAR(this, i)))
		_fd_revise_connected(this, VAR(this, i));

	      if (_fd_var_del_gt(min_y, VAR(this, terms + i)))
		_fd_revise_connected(this, VAR(this, terms + i));
	    }
      }
  else if (max == lb)
    for (i = 0; i < terms; ++i)
      {
	int max_x = _fd_var_max(VAR(this, i));
	int max_y = _fd_var_max(VAR(this, terms + i));
	int changed_x = 0, changed_y = 0;

	if (max_x != 0 || max_y != 0)
	  if (max_x == 0)
	    {
	      if (_fd_var_del_lt(0, VAR(this, i)))	   // XXX: delete < 0???
		_fd_revise_connected(this, VAR(this, i));
	    }
	  else if (max_y == 0)
	    {
	      if (_fd_var_del_lt(0, VAR(this, terms + i))) // XXX: delete < 0???
		_fd_revise_connected(this, VAR(this, terms + i));
	    }
	  else // max_x != 0 && max_y != 0
	    {
	      if (_fd_var_del_lt(max_x, VAR(this, i)))
		_fd_revise_connected(this, VAR(this, i));

	      if (_fd_var_del_lt(max_y, VAR(this, terms + i)))
		_fd_revise_connected(this, VAR(this, terms + i));
	    }
      }

  if (_fd_var_del_lt(min, VAR(this, this->nvariables - 1)) |
      _fd_var_del_gt(max, VAR(this, this->nvariables - 1)))
    {
      if (fd_domain_empty(VAR(this, this->nvariables - 1)))
	return FD_NOSOLUTION;

      _fd_revise_connected(this, VAR(this, this->nvariables - 1));
    }

  return FD_OK;
}

fd_constraint fd_knapsack(fd_int variables[], int nvariables, fd_int limits)
{
eef94371   Vasco Pedro   Update to PaCCS v...
108
  fd_constraint c = _fd_constraint_new(nvariables + 1, 0);
965dadaa   Salvador Abreu   initial commit fr...
109
110
111
112
113
114
115
  int i;

  if (c)
    {
      for (i = 0; i < nvariables; ++i)
	c->variables[i] = FD_INT2C_VAR(variables[i]);
      c->variables[nvariables] = FD_INT2C_VAR(limits);
eef94371   Vasco Pedro   Update to PaCCS v...
116
#ifdef CONSTRAINT_CLASS
965dadaa   Salvador Abreu   initial commit fr...
117
      c->kind = FD_CONSTR_KNAPSACK;
965dadaa   Salvador Abreu   initial commit fr...
118
119
120
121
122
123
124
125
126
#else /* CONSTRAINT_CLASS */
      c->propagator2 = fd_knapsack_propagate2;
#endif /* CONSTRAINT_CLASS */

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

      _fd_add_constraint(c);
    }