Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
sorted.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  * Gabor Szokoli <szokoli@gecode.org>
7  *
8  * Copyright:
9  * Guido Tack, 2004
10  * Christian Schulte, 2004
11  * Gabor Szokoli, 2004
12  *
13  * Last modified:
14  * $Date: 2011-11-03 11:52:07 +0100 (Thu, 03 Nov 2011) $ by $Author: tack $
15  * $Revision: 12452 $
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 
43 
44 #include <gecode/set.hh>
45 #include <gecode/int.hh>
46 #include <gecode/set/rel.hh>
47 
48 namespace Gecode { namespace Set { namespace Channel {
49 
50  template<class View>
54  : Propagator(home), x0(y0), xs(ys) {
55  x0.subscribe(home,*this, PC_SET_ANY);
57  }
58 
59  template<class View>
62  : Propagator(home,share,p) {
63  x0.update(home,share,p.x0);
64  xs.update(home,share,p.xs);
65  }
66 
67  template<class View>
71  unsigned int xs_size = static_cast<unsigned int>(xs.size());
72  GECODE_ME_CHECK(x0.cardMin(home,xs_size));
73  GECODE_ME_CHECK(x0.cardMax(home,xs_size));
74  if (xs_size == 1) {
75  SingletonView sv(xs[0]);
77  SingletonView>::post(home,x0, sv)));
78  } else {
79  // Sharing in xs is handled correctly in the propagator:
80  // if two views in xs are shared, this leads to failure.
81  (void) new (home) ChannelSorted(home,x0,xs);
82  }
83  return ES_OK;
84  }
85 
86  template<class View>
87  PropCost
89  return PropCost::linear(PropCost::LO, xs.size()+1);
90  }
91 
92  template<class View>
93  forceinline size_t
95  x0.cancel(home,*this, PC_SET_ANY);
96  xs.cancel(home,*this, Gecode::Int::PC_INT_BND);
97  (void) Propagator::dispose(home);
98  return sizeof(*this);
99  }
100 
101  template<class View>
102  Actor*
103  ChannelSorted<View>::copy(Space& home, bool share) {
104  return new (home) ChannelSorted(home,share,*this);
105  }
106 
107  template<class View>
108  ExecStatus
110 
111  int xs_size = xs.size();
112 
113  bool loopFlag;
114 
115  do {
116  loopFlag = false;
117 
118  // Order int vars in xs
119  GECODE_ME_CHECK(xs[0].gq(home,x0.lubMin()));
120  for (int i=xs_size-1; i--; ) {
121  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i+1].gq(home,xs[i].min() + 1));
122  }
123 
124  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[xs_size-1].lq(home,x0.lubMax()));
125  for (int i=xs_size-2; i--; ) {
126  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].lq(home,xs[i+1].max() - 1));
127  }
128 
129  // if y from xs is assigned, add to glb(x0)
130  for (int i=xs_size; i--; ) {
131  if (xs[i].assigned()) {
132  GECODE_ME_CHECK_MODIFIED(loopFlag, x0.include(home,xs[i].val()));
133  }
134  }
135 
136  // intersect every y in xs with lub(x0)
137  for (int i=xs_size; i--; ) {
138  LubRanges<View> ub(x0);
139  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].inter_r(home,ub,false));
140  }
141 
142  // remove gaps between vars in xs from lub(x0)
143  GECODE_ME_CHECK_MODIFIED(loopFlag,
144  x0.exclude(home,Limits::min,xs[0].min()-1));
145  GECODE_ME_CHECK_MODIFIED(loopFlag,
146  x0.exclude(home,xs[xs_size-1].max()+1,
147  Limits::max));
148 
149  for (int i=xs_size-1; i--; ) {
150  int start = xs[i].max() + 1;
151  int end = xs[i+1].min() - 1;
152  if (start<=end) {
153  GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,start,end));
154  }
155  }
156 
157  // try to assign vars in xs from glb(x0)
158  if (x0.glbSize()>0) {
159 
160  LubRanges<View> ub(x0);
162  GlbRanges<View> lb(x0);
164 
165  int i=0;
166  for (; ubv() && lbv() && ubv.val()==lbv.val();
167  ++ubv, ++lbv, i++) {
168  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,lbv.val()));
169  }
170 
171  if (i<xs_size-1 && x0.lubMax()==x0.glbMax()) {
172  LubRanges<View> lbx0(x0);
173  GlbRanges<View> ubx0(x0);
175  inter(lbx0, ubx0);
176 
177  int to = x0.glbMax();
178  int from = to;
179  while (inter()) {
180  from = inter.min();
181  ++inter;
182  }
183 
184  int i=xs_size-1;
185  for (int j=to; j>=from;j--,i--) {
186  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,j));
187  }
188  }
189  }
190 
191  } while (loopFlag);
192 
193  for (int i=xs_size; i--; )
194  if (!xs[i].assigned())
195  return ES_FIX;
196  return home.ES_SUBSUMED(*this);
197  }
198 
199 }}}
200 
201 // STATISTICS: set-prop
void update(Space &, bool share, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1387
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:228
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
const int min
Smallest allowed integer in integer set.
Definition: set.hh:101
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
Base-class for propagators.
Definition: core.hpp:755
Propagation has computed fixpoint.
Definition: core.hpp:528
const int max
Largest allowed integer in integer set.
Definition: set.hh:99
Computation spaces.
Definition: core.hpp:1362
#define GECODE_ME_CHECK_MODIFIED(modified, me)
Check whether me is failed or modified, and forward failure.
Definition: macros.hpp:57
Base-class for both propagators and branchers.
Definition: core.hpp:666
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: sorted.hpp:103
View x0
SetView for the match.
Definition: channel.hh:67
int min(void) const
Return smallest value of range.
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
Range iterator for computing intersection (binary)
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1400
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:91
Value iterator from range iterator.
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
static ExecStatus post(Home home, View s, ViewArray< Gecode::Int::IntView > &x)
Post propagator that propagates that s contains the , which are sorted in non-descending order...
Definition: sorted.hpp:69
Singleton set view.
Definition: view.hpp:589
const Gecode::PropCond PC_SET_ANY
Propagate when any bound or the cardinality of a view changes.
Definition: var-type.hpp:248
Propagator for the sorted channel constraint
Definition: channel.hh:64
SetExpr inter(const SetVarArgs &x)
Intersection of set variables.
Definition: set-expr.cpp:696
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
Propagation cost.
Definition: core.hpp:537
ExecStatus
Definition: core.hpp:523
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sorted.hpp:109
#define forceinline
Definition: config.hpp:132
virtual size_t dispose(Space &home)
Delete Propagator.
Definition: sorted.hpp:94
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as PC_LINEAR_LO)
Definition: sorted.hpp:88
Propagator for set equality
Definition: rel.hh:146
Execution is okay.
Definition: core.hpp:527
int val(void) const
Return current value.
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Gecode toplevel namespace
ViewArray< Gecode::Int::IntView > xs
IntViews that together form the set x0.
Definition: channel.hh:69
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
ChannelSorted(Space &home, bool share, ChannelSorted &p)
Constructor for cloning p.
Definition: sorted.hpp:61
Home class for posting propagators
Definition: core.hpp:717