Generated on Fri Aug 24 2012 04:52:08 for Gecode by doxygen 1.8.1.2
basic.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Mikael Lagerkvist <lagerkvist@gecode.org>
5  *
6  * Contributing authors:
7  * Christian Schulte <schulte@gecode.org>
8  *
9  * Copyright:
10  * Mikael Lagerkvist, 2007
11  * Christian Schulte, 2008
12  *
13  * Last modified:
14  * $Date: 2010-07-15 01:46:18 +1000 (Thu, 15 Jul 2010) $ by $Author: schulte $
15  * $Revision: 11192 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 namespace Gecode { namespace Int { namespace Extensional {
43 
44  /*
45  * The propagator proper
46  *
47  */
48 
49  template<class View, bool shared>
52  const TupleSet& t)
53  : Base<View>(home,x,t) {
54  }
55 
56  template<class View, bool shared>
59  const TupleSet& t) {
60  // All variables in the correct domain
61  for (int i = x.size(); i--; ) {
62  GECODE_ME_CHECK(x[i].gq(home, t.min()));
63  GECODE_ME_CHECK(x[i].lq(home, t.max()));
64  }
65  (void) new (home) Basic<View,shared>(home,x,t);
66  return ES_OK;
67  }
68 
69  template<class View, bool shared>
72  : Base<View>(home,share,p) {
73  }
74 
75  template<class View, bool shared>
76  PropCost
77  Basic<View,shared>::cost(const Space&, const ModEventDelta& med) const {
78  if (View::me(med) == ME_INT_VAL)
79  return PropCost::quadratic(PropCost::HI,x.size());
80  else
81  return PropCost::cubic(PropCost::HI,x.size());
82  }
83 
84  template<class View, bool shared>
85  Actor*
86  Basic<View,shared>::copy(Space& home, bool share) {
87  return new (home) Basic<View,shared>(home,share,*this);
88  }
89 
90  template<class View, bool shared>
93  // Set up datastructures
94 
95  // Bit-sets for amortized O(1) access to domains
96  Region r(home);
97  BitSet* dom = r.alloc<BitSet>(x.size());
98  init_dom(home, dom);
99 
100  // Bit-sets for processed values.
101  BitSet* has_support = r.alloc<BitSet>(x.size());
102  for (int i = x.size(); i--; )
103  has_support[i].init(home, ts()->domsize);
104 
105 
106  // Values to prune
107  Support::StaticStack<int,Region> nq(r,static_cast<int>(ts()->domsize));
108 
109  // Run algorithm
110 
111  // Check consistency for each view-value pair
112  for (int i = x.size(); i--; ) {
113  for (ViewValues<View> vv(x[i]); vv(); ++vv) {
114  // Value offset for indexing
115  int val = vv.val() - ts()->min;
116  if (!has_support[i].get(static_cast<unsigned int>(val))) {
117  // Find support for value vv.val() in view
118  Tuple l = find_support(dom, i, val);
119  if (l == NULL) {
120  // No possible supports left
121  nq.push(vv.val());
122  } else {
123  // Mark values as supported
124  // Only forward direction marking is needed since all
125  // previous values have been checked
126  for (int j = i; j--; ) {
127  has_support[j].set(static_cast<unsigned int>(l[j]- ts()->min));
128  assert(has_support[j].get(l[j] - ts()->min));
129  }
130  }
131  }
132  }
133 
134  // Prune values for x[i] which do not have support anymore
135  while (!nq.empty())
136  GECODE_ME_CHECK(x[i].nq(home,nq.pop()));
137  }
138 
139  for (int i = x.size(); i--; )
140  if (!x[i].assigned())
141  return shared ? ES_NOFIX : ES_FIX;
142  return home.ES_SUBSUMED(*this);
143  }
144 
145 }}}
146 
147 // STATISTICS: int-prop
148