Generated on Sat Nov 9 2013 19:18:31 for Gecode by doxygen 1.8.4
atmostOne.cpp
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  *
6  * Copyright:
7  * Guido Tack, 2004
8  *
9  * Last modified:
10  * $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
11  * $Revision: 13068 $
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 #include <gecode/set/distinct.hh>
39 
40 /*
41  * These propagators implement the scheme discussed in
42  *
43  * Andrew Sadler and Carment Gervet: Global Reasoning on Sets.
44  * FORMUL'01 workshop in conjunction with CP 2001.
45  *
46  * Todo: make the propagators incremental.
47  */
48 
49 namespace Gecode { namespace Set { namespace Distinct {
50 
51  /*
52  * "AtMostOneIntersection" propagator
53  *
54  */
55 
56  Actor*
57  AtmostOne::copy(Space& home, bool share) {
58  return new (home) AtmostOne(home,share,*this);
59  }
60 
63  Region r(home);
65  for (int i = x.size(); i--; ) {
66  lubs[i].init(x[i]);
67  }
68  Iter::Ranges::NaryUnion bigT(r, lubs, x.size());
69 
71  as(bigT);
72 
73  while (as()) {
74  int a = as.val(); ++as;
75 
76  // cardSa is the number of sets that contain a in the glb
77  int cardSa = 0;
78  for (int i=x.size(); i--;)
79  if (x[i].contains(a))
80  cardSa++;
81 
82  // bigTa is the union of all lubs that contain a
83  GLBndSet bigTa(home);
84  for (int i=x.size(); i--;) {
85  if (!x[i].notContains(a)) {
86  LubRanges<SetView> xilub(x[i]);
87  bigTa.includeI(home, xilub);
88  }
89  }
90 
91  // maxa is the maximum number of sets that can contain a
92  int maxa = static_cast<int>((bigTa.size() - 1) / (c - 1));
93  bigTa.dispose(home);
94 
95  // Conditional Rule A:
96  // If more sets already contain a than allowed, fail.
97  if (maxa < cardSa)
98  return ES_FAILED;
99 
100  if (maxa == cardSa) {
101  // Conditional Rule B:
102  // All a used up. All other sets (those that don't have a in their
103  // glb already) cannot contain a.
104  for (int i=x.size(); i--;) {
105  if (!x[i].contains(a)) {
106  GECODE_ME_CHECK(x[i].exclude(home, a));
107  }
108  }
109  } else {
111  for (int i = x.size(); i--; ) {
112  lubs2[i].init(x[i]);
113  }
114  Iter::Ranges::NaryUnion bigT2(r, lubs2, x.size());
115 
116  GlbRanges<SetView>* glbs = r.alloc<GlbRanges<SetView> >(cardSa);
117  int count = 0;
118  for (int i=x.size(); i--; ) {
119  if (x[i].contains(a)) {
120  glbs[count].init(x[i]);
121  count++;
122  }
123  }
124  Iter::Ranges::NaryUnion glbsa(r, glbs, cardSa);
126  Iter::Ranges::NaryUnion> deltaA(bigT2, glbsa);
127  Iter::Ranges::Cache deltaAC(r,deltaA);
128  // deltaAC contains all elements that are not yet known to be
129  // in a set together with a.
130  // Formally: \cup_i lub(x_i) - \cup_i {glb(s_i) | a\in glb(s_i)}
131 
132 
133  if (Iter::Ranges::size(deltaAC) == c - 1) {
134  // Conditional Rule C:
135  // If deltaA has exactly c-1 elements, all sets that are not yet
136  // known to contain a cannot contain a if it is impossible that
137  // they contain all of deltaA. Or the other way around:
138  // If a is added to the glb of a set s, deltaA must be a subset of
139  // s, because otherwise s would share at least one more element
140  // with another set that has a in its lower bound.
141  // Weird, eh?
142  for (int i=x.size(); i--; ) {
143  if (!x[i].contains(a) && !x[i].notContains(a)) {
144  deltaAC.reset();
145  LubRanges<SetView> xilub(x[i]);
146  if (!Iter::Ranges::subset(deltaAC, xilub)) {
147  GECODE_ME_CHECK(x[i].exclude(home, a));
148  }
149  }
150  }
151  }
152 
153  }
154 
155  }
156 
157  return ES_NOFIX;
158  }
159 
160 }}}
161 
162 // STATISTICS: set-prop