Blame view

src/constraints/max.c 1.9 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
/* maximisation */

#include "bound.h"

static int fd_max_filter(fd_constraint this)
{
  fd_int x = VAR(this, 0);

  if (_fd_var_del_le(_fd_bound_value(), x))
    {
      if (fd_domain_empty(x))
	return FD_NOSOLUTION;

      _fd_revise_connected(this, x);
    }

  return FD_OK;
}

int fd_max_propagate2(fd_constraint this, fd_int culprit)
{
  int changed = 0;

  changed = _fd_var_del_le(_fd_bound_value(), culprit);

  if (changed)
    {
      if (fd_domain_empty(culprit))
	return FD_NOSOLUTION;

      _fd_revise_connected(this, culprit); // XXX
    }

  return FD_OK;
}

#ifndef USE_STORE
#error "fd_max requires USE_STORE"
#endif

int fd_max_satisfied(fd_constraint this, _fd_store store, int *value)
{
  int v;

  // XXX: `store' appears in the expansion of DOMAIN()
  _fd_val_single(DOMAIN(VAR(this, 0)), &v);

  if (value)
    *value = v;

  return v > _fd_bound_value();
}

int fd_max_valid_bound(int current, int new)
{
  return new > current;
}

int fd_max_compare(fd_constraint this, _fd_store s1, _fd_store s2)
{
  int v1, v2;

  // XXX: `store' appears in the expansion of DOMAIN()

  {
    _fd_store store = s1;

    _fd_val_single(DOMAIN(VAR(this, 0)), &v1);
  }

  {
    _fd_store store = s2;

    _fd_val_single(DOMAIN(VAR(this, 0)), &v2);
  }

  return v2 - v1;
}

bool fd_max_bound_variable(fd_constraint this, int bound)
{
  return _fd_var_del_le(bound, VAR(this, 0)) != 0;
}

fd_constraint fd_max(fd_int variable)
{
eef94371   Vasco Pedro   Update to PaCCS v...
87
  fd_constraint c = _fd_constraint_new(1, 0);
965dadaa   Salvador Abreu   initial commit fr...
88
89
90
91

  if (c)
    {
      c->variables[0] = FD_INT2C_VAR(variable);
eef94371   Vasco Pedro   Update to PaCCS v...
92
#ifdef CONSTRAINT_CLASS
965dadaa   Salvador Abreu   initial commit fr...
93
      c->kind = FD_CONSTR_MAX;
965dadaa   Salvador Abreu   initial commit fr...
94
95
96
97
98
99
100
101
102
103
104
#else /* CONSTRAINT_CLASS */
      c->propagator2 = fd_max_propagate2;
#endif /* CONSTRAINT_CLASS */

      _fd_var_add_constraint(variable, c);

      _fd_add_constraint(c);
    }

  _fd_init_bound(MIN_VALUE - 1, FD_INT2C_VAR(variable), c, fd_max_satisfied,
		 fd_max_valid_bound, fd_max_compare, fd_max_bound_variable);