Generated on Mon Aug 27 2012 17:15:43 for Gecode by doxygen 1.8.1.2
opt.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2011
8  *
9  * Last modified:
10  * $Date: 2011-07-14 00:42:33 +1000 (Thu, 14 Jul 2011) $ by $Author: schulte $
11  * $Revision: 12191 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Int { namespace NoOverlap {
39 
40  template<class Dim, int d>
43  : Base<OptBox<Dim,d> >(home,b,n), m(m0) {
44  for (int i=m; i--; )
45  b[n+i].subscribe(home, *this);
46  }
47 
48  template<class Dim, int d>
51  // Partition into mandatory and optional boxes
52  if (n > 1) {
53  int p = Base<OptBox<Dim,d> >::partition(b, 0, n);
54  (void) new (home) OptProp<Dim,d>(home,b,p,n-p);
55  }
56  return ES_OK;
57  }
58 
59  template<class Dim, int d>
60  forceinline size_t
62  for (int i=m; i--; )
63  b[n+i].cancel(home, *this);
64  (void) Base<OptBox<Dim,d> >::dispose(home);
65  return sizeof(*this);
66  }
67 
68 
69  template<class Dim, int d>
72  : Base<OptBox<Dim,d> >(home, shared, p, p.n + p.m), m(p.m) {}
73 
74  template<class Dim, int d>
75  Actor*
76  OptProp<Dim,d>::copy(Space& home, bool share) {
77  return new (home) OptProp<Dim,d>(home,share,*this);
78  }
79 
80  template<class Dim, int d>
81  ExecStatus
83  Region r(home);
84 
85  if (BoolView::me(med) == ME_BOOL_VAL) {
86  // Eliminate excluded boxes
87  for (int i=m; i--; )
88  if (b[n+i].excluded()) {
89  b[n+i].cancel(home,*this);
90  b[n+i] = b[n+(--m)];
91  }
92  // Reconsider optional boxes
93  if (m > 0) {
94  int p = Base<OptBox<Dim,d> >::partition(b+n, 0, m);
95  n += p; m -= p;
96  }
97  }
98 
99  // Number of disjoint boxes
100  int* db = r.alloc<int>(n);
101  for (int i=n; i--; )
102  db[i] = n-1;
103 
104  // Number of boxes to be eliminated
105  int e = 0;
106  for (int i=n; i--; ) {
107  assert(b[i].mandatory());
108  for (int j=i; j--; )
109  if (b[i].nooverlap(b[j])) {
110  assert(db[i] > 0); assert(db[j] > 0);
111  if (--db[i] == 0) e++;
112  if (--db[j] == 0) e++;
113  continue;
114  } else {
115  GECODE_ES_CHECK(b[i].nooverlap(home,b[j]));
116  }
117  }
118 
119  if (m == 0) {
120  if (e == n)
121  return home.ES_SUBSUMED(*this);
122  int i = n-1;
123  while (e > 0) {
124  // Eliminate boxes that do not overlap
125  while (db[i] > 0)
126  i--;
127  b[i].cancel(home, *this);
128  b[i] = b[--n]; b[n] = b[n+m];
129  e--; i--;
130  }
131  if (n < 2)
132  return home.ES_SUBSUMED(*this);
133  }
134 
135  // Check whether some optional boxes must be excluded
136  for (int i=m; i--; ) {
137  assert(b[n+i].optional());
138  for (int j=n; j--; )
139  if (b[n+i].overlap(b[j])) {
140  GECODE_ES_CHECK(b[n+i].exclude(home));
141  b[n+i].cancel(home,*this);
142  b[n+i] = b[n+(--m)];
143  break;
144  }
145  }
146 
147  return ES_NOFIX;
148  }
149 
150 }}}
151 
152 // STATISTICS: int-prop
153