Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
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
Propagator for negated equality
Definition: rel.hh:261
AtmostOne(Space &home, bool share, AtmostOne &p)
Constructor for cloning p.
Definition: atmostOne.hpp:50
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: atmostOne.cpp:57
void init(const SetView &x)
Initialize with ranges for view x.
Definition: set.hpp:226
ViewArray< SetView > x
Array of views.
Definition: propagator.hpp:146
Handle to region.
Definition: region.hpp:61
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
ModEvent exclude(Space &home, View &x, int s)
Prune view x to exclude all values from s.
Definition: set-op.hpp:141
unsigned int size(void) const
Return size.
Definition: integerset.hpp:97
Gecode::IntArgs i(4, 1, 2, 3, 4)
Range iterator for least upper bound of set variable views
Definition: set.hpp:205
void reset(void)
Reset iterator to start.
Execution has resulted in failure.
Definition: core.hpp:525
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Range iterator for greatest lower bound of set variable views
Definition: set.hpp:236
Range iterator for union of iterators.
Value iterator from range iterator.
unsigned int size(I &i)
Size of all ranges of range iterator i.
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
bool includeI(Space &home, I &i)
Include the set represented by i in this set.
Definition: integerset.hpp:300
void dispose(Space &home)
Free memory used by this set.
Definition: integerset.hpp:64
unsigned int c
Cardinality of the sets.
Definition: distinct.hh:59
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
Range iterator cache
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: atmostOne.cpp:62
ExecStatus
Definition: core.hpp:523
Growing sets of integers.
Definition: var-imp.hpp:209
Propagation has not computed fixpoint.
Definition: core.hpp:526
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Gecode toplevel namespace
Range iterator for computing set difference.
Definition: ranges-diff.hpp:47
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.