Generated on Tue Mar 5 2013 22:37:30 for Gecode by doxygen 1.8.3.1
partition.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  * Christian Schulte <schulte@gecode.org>
6  *
7  * Contributing authors:
8  * Gabor Szokoli <szokoli@gecode.org>
9  *
10  * Copyright:
11  * Guido Tack, 2004
12  * Christian Schulte, 2004
13  * Gabor Szokoli, 2004
14  *
15  * Last modified:
16  * $Date: 2011-05-24 00:48:31 +1000 (Tue, 24 May 2011) $ by $Author: tack $
17  * $Revision: 12018 $
18  *
19  * This file is part of Gecode, the generic constraint
20  * development environment:
21  * http://www.gecode.org
22  *
23  * Permission is hereby granted, free of charge, to any person obtaining
24  * a copy of this software and associated documentation files (the
25  * "Software"), to deal in the Software without restriction, including
26  * without limitation the rights to use, copy, modify, merge, publish,
27  * distribute, sublicense, and/or sell copies of the Software, and to
28  * permit persons to whom the Software is furnished to do so, subject to
29  * the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be
32  * included in all copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
38  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
39  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41  *
42  */
43 
44 namespace Gecode { namespace Set { namespace RelOp {
45 
46  /*
47  * "Nary partition" propagator
48  *
49  */
50 
51  template<class View0, class View1>
54  : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home, x, y) {
55  shared = x.shared(home) || viewarrayshared(home,x,y);
56  }
57 
58  template<class View0, class View1>
61  const IntSet& z, View1 y)
62  : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home, x, y) {
63  shared = x.shared(home) || viewarrayshared(home,x,y);
64  IntSetRanges rz(z);
65  unionOfDets.includeI(home, rz);
66  }
67 
68  template<class View0, class View1>
71  : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,share,p),
72  shared(p.shared) {
74  }
75 
76  template<class View0, class View1>
77  Actor*
78  PartitionN<View0,View1>::copy(Space& home, bool share) {
79  return new (home) PartitionN(home,share,*this);
80  }
81 
82  template<class View0, class View1>
84  View1 y) {
85  switch (x.size()) {
86  case 0:
87  GECODE_ME_CHECK(y.cardMax(home, 0));
88  return ES_OK;
89  case 1:
90  return Rel::Eq<View0,View1>::post(home, x[0], y);
91  default:
92  (void) new (home) PartitionN<View0,View1>(home,x,y);
93  return ES_OK;
94  }
95  }
96 
97  template<class View0, class View1>
99  const IntSet& z, View1 y) {
100  (void) new (home) PartitionN<View0,View1>(home,x,z,y);
101  return ES_OK;
102  }
103 
104  template<class View0, class View1>
106  return PropCost::quadratic(PropCost::LO, x.size()+1);
107  }
108 
109  template<class View0, class View1>
110  ExecStatus
112 
113  ModEvent me0 = View0::me(med);
114  ModEvent me1 = View1::me(med);
115  bool ubevent = Rel::testSetEventUB(me0, me1);
116  bool lbevent = Rel::testSetEventLB(me0, me1);
117  bool anybevent = Rel::testSetEventAnyB(me0, me1);
118  bool cardevent = Rel::testSetEventCard(me0, me1);
119 
120  bool modified = false;
121  bool oldModified = false;
122 
123  do {
124  oldModified = modified;
125  modified = false;
126  if (oldModified || anybevent)
127  GECODE_ES_CHECK(partitionNXiUB(home,modified, x, y,unionOfDets));
128  if (modified || oldModified || anybevent)
129  GECODE_ES_CHECK(partitionNXiLB(home,modified, x, y,unionOfDets));
130  if (modified || oldModified || ubevent)
131  GECODE_ES_CHECK(partitionNYUB(home,modified, x, y,unionOfDets));
132  if (modified || oldModified || lbevent)
133  GECODE_ES_CHECK(partitionNYLB(home,modified, x, y,unionOfDets));
134  if (modified || oldModified || ubevent)
135  GECODE_ES_CHECK(unionNXiUB(home,modified, x, y,unionOfDets));
136  if (modified || oldModified || cardevent)
137  GECODE_ES_CHECK(partitionNCard(home,modified, x, y,unionOfDets));
138  } while (modified);
139 
140  //removing assigned sets from x, accumulating the value:
141  for(int i=0;i<x.size();i++){
142  //Do not reverse! Eats away the end of the array!
143  while (i<x.size() && x[i].assigned()) {
144  GlbRanges<View0> det(x[i]);
145  unionOfDets.includeI(home,det);
146  x.move_lst(i);
147  }
148  }
149  // When we run out of variables, make a final check and disolve:
150  if (x.size()==0) {
151  BndSetRanges all1(unionOfDets);
152  GECODE_ME_CHECK( y.intersectI(home,all1) );
153  BndSetRanges all2(unionOfDets);
154  GECODE_ME_CHECK( y.includeI(home,all2) );
155  unionOfDets.dispose(home);
156  return home.ES_SUBSUMED(*this);
157  }
158 
159  return shared ? ES_NOFIX : ES_FIX;
160  }
161 
162 }}}
163 
164 // STATISTICS: set-prop