Generated on Sat Feb 7 2015 02:01:23 for Gecode by doxygen 1.8.9.1
gcc.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Patrick Pekczynski <pekczynski@ps.uni-sb.de>
5  * Guido Tack <tack@gecode.org>
6  *
7  * Contributing authors:
8  * Christian Schulte <schulte@gecode.org>
9  *
10  * Copyright:
11  * Patrick Pekczynski, 2004
12  * Christian Schulte, 2009
13  * Guido Tack, 2006
14  *
15  * Last modified:
16  * $Date: 2013-03-13 01:18:32 +0100 (Wed, 13 Mar 2013) $ by $Author: tack $
17  * $Revision: 13516 $
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 #include <gecode/int/gcc.hh>
45 
46 namespace Gecode {
47 
48  namespace {
49 
51  template<class X>
52  struct LessP {
53  bool operator ()(const std::pair<X,int>& lhs,
54  const std::pair<X,int>& rhs) {
55  return lhs.second < rhs.second;
56  }
57  };
58 
60  IntVar unify(Home home, IntVar x, IntVar y) {
61  rel(home, x, IRT_EQ, y);
62  return x;
63  }
65  IntSet unify(Home, const IntSet& x, const IntSet& y) {
66  IntSetRanges xr(x);
67  IntSetRanges yr(y);
68  Iter::Ranges::Inter<IntSetRanges,IntSetRanges> i(xr,yr);
69  IntSet z(i);
70  return z;
71  }
72 
74  template<class A>
75  void removeDuplicates(Home home, A& c, IntArgs& v) {
76  typedef typename A::value_type S;
77  typedef std::pair<S,int> P;
78  Region re(home);
79  P* a = re.alloc<P>(c.size());
80  for (int i=c.size(); i--;)
81  a[i] = P(c[i],v[i]);
82  LessP<S> l;
83  Support::quicksort(a,c.size(),l);
84  A cc;
85  IntArgs vv;
86  int cur = a[0].second-1;
87  for (int i=0; i<c.size(); i++) {
88  if (a[i].second==cur) {
89  cc[cc.size()-1] = unify(home, cc[cc.size()-1], a[i].first);
90  } else {
91  cc << a[i].first;
92  vv << a[i].second;
93  cur = a[i].second;
94  }
95  }
96  re.free<P>(a,c.size());
97  c = cc;
98  v = vv;
99  }
100 
101  }
102 
103  void count(Home home, const IntVarArgs& x,
104  const IntVarArgs& _c, const IntArgs& _v,
105  IntConLevel icl) {
106  using namespace Int;
107  IntVarArgs c(_c);
108  IntArgs v(_v);
109  if (v.size() != c.size())
110  throw ArgumentSizeMismatch("Int::count");
111  if (x.same(home))
112  throw ArgumentSame("Int::count");
113  if (home.failed())
114  return;
115 
116  removeDuplicates(home,c,v);
117 
118  ViewArray<IntView> xv(home, x);
119  ViewArray<GCC::CardView> cv(home, c.size());
120  // set the cardinality
121  for (int i = v.size(); i--; )
122  cv[i].init(c[i],v[i]);
123  switch (icl) {
124  case ICL_BND:
126  (GCC::Bnd<GCC::CardView>::post(home,xv,cv)));
127  break;
128  case ICL_DOM:
130  (GCC::Dom<GCC::CardView>::post(home,xv,cv)));
131  break;
132  default:
134  (GCC::Val<GCC::CardView>::post(home,xv,cv)));
135  }
136  }
137 
138  // domain is 0..|cards|- 1
139  void count(Home home, const IntVarArgs& x, const IntVarArgs& c,
140  IntConLevel icl) {
141  IntArgs values(c.size());
142  for (int i = c.size(); i--; )
143  values[i] = i;
144  count(home, x, c, values, icl);
145  }
146 
147  // constant cards
148  void count(Home home, const IntVarArgs& x,
149  const IntSetArgs& _c, const IntArgs& _v,
150  IntConLevel icl) {
151  using namespace Int;
152  IntSetArgs c(_c);
153  IntArgs v(_v);
154  if (v.size() != c.size())
155  throw ArgumentSizeMismatch("Int::count");
156  if (x.same(home))
157  throw ArgumentSame("Int::count");
158  for (int i=c.size(); i--; ) {
159  Limits::check(v[i],"Int::count");
160  Limits::check(c[i].min(),"Int::count");
161  Limits::check(c[i].max(),"Int::count");
162  }
163 
164  if (home.failed())
165  return;
166 
167  removeDuplicates(home,c,v);
168 
169  ViewArray<IntView> xv(home, x);
170 
171  for (int i = v.size(); i--; ) {
172  if (c[i].ranges() > 1) {
173  // Found hole, so create temporary variables
174  ViewArray<GCC::CardView> cv(home, v.size());
175  for (int j = v.size(); j--; )
176  cv[j].init(home,c[j],v[j]);
177  switch (icl) {
178  case ICL_BND:
180  (GCC::Bnd<GCC::CardView>::post(home, xv, cv)));
181  break;
182  case ICL_DOM:
184  (GCC::Dom<GCC::CardView>::post(home, xv, cv)));
185  break;
186  default:
188  (GCC::Val<GCC::CardView>::post(home, xv, cv)));
189  }
190  return;
191  }
192  }
193 
194  // No holes: create CardConsts
195  ViewArray<GCC::CardConst> cv(home, c.size());
196 
197  for (int i = c.size(); i--; )
198  cv[i].init(home,c[i].min(),c[i].max(),v[i]);
199 
200  switch (icl) {
201  case ICL_BND:
203  (GCC::Bnd<GCC::CardConst>::post(home, xv, cv)));
204  break;
205  case ICL_DOM:
207  (GCC::Dom<GCC::CardConst>::post(home, xv, cv)));
208  break;
209  default:
211  (GCC::Val<GCC::CardConst>::post(home, xv, cv)));
212  }
213  }
214 
215  // domain is 0..|cards|- 1
216  void count(Home home, const IntVarArgs& x, const IntSetArgs& c,
217  IntConLevel icl) {
218  IntArgs values(c.size());
219  for (int i = c.size(); i--; )
220  values[i] = i;
221  count(home, x, c, values, icl);
222  }
223 
224  void count(Home home, const IntVarArgs& x,
225  const IntSet& c, const IntArgs& v,
226  IntConLevel icl) {
227  IntSetArgs cards(v.size());
228  for (int i = v.size(); i--; )
229  cards[i] = c;
230  count(home, x, cards, v, icl);
231  }
232 
233 }
234 
235 // STATISTICS: int-post
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3446
Value consistent global cardinality propagator.
Definition: gcc.hh:67
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
Gecode::IntArgs i(4, 1, 2, 3, 4)
void quicksort(Type *l, Type *r, Less &less)
Standard quick sort.
Definition: sort.hpp:134
Equality ( )
Definition: int.hh:904
Domain consistent global cardinality propagator.
Definition: gcc.hh:219
Integer sets.
Definition: int.hh:171
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
const int v[7]
Definition: distinct.cpp:207
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
Create c
Definition: gcc.cpp:314
void values(Home home, const IntVarArgs &x, IntSet y, IntConLevel icl=ICL_DEF)
Post constraint .
Definition: minimodel.hh:1869
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
Exception: Arguments contain same variable multiply
Definition: exception.hpp:84
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Bounds propagation or consistency.
Definition: int.hh:939
Gecode toplevel namespace
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition: limits.hpp:50
Home class for posting propagators
Definition: core.hpp:717
Exception: Arguments are of different size
Definition: exception.hpp:77
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition: macros.hpp:96
Bounds consistent global cardinality propagator.
Definition: gcc.hh:115
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Domain propagation or consistency.
Definition: int.hh:940
bool same(const Space &home) const
Test whether array contains same variable multiply.
Definition: array.hpp:2085
Multi _c(Gecode::IntArgs(3, 1, 2, 3))