Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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$ by $Author$
15  * $Revision$
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,p) {
63  x0.update(home,p.x0);
64  xs.update(home,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  void
95  x0.reschedule(home,*this, PC_SET_ANY);
97  }
98 
99  template<class View>
100  forceinline size_t
102  x0.cancel(home,*this, PC_SET_ANY);
103  xs.cancel(home,*this, Gecode::Int::PC_INT_BND);
104  (void) Propagator::dispose(home);
105  return sizeof(*this);
106  }
107 
108  template<class View>
109  Actor*
111  return new (home) ChannelSorted(home,*this);
112  }
113 
114  template<class View>
115  ExecStatus
117 
118  int xs_size = xs.size();
119 
120  bool loopFlag;
121 
122  do {
123  loopFlag = false;
124 
125  // Order int vars in xs
126  GECODE_ME_CHECK(xs[0].gq(home,x0.lubMin()));
127  for (int i=xs_size-1; i--; ) {
128  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i+1].gq(home,xs[i].min() + 1));
129  }
130 
131  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[xs_size-1].lq(home,x0.lubMax()));
132  for (int i=xs_size-2; i--; ) {
133  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].lq(home,xs[i+1].max() - 1));
134  }
135 
136  // if y from xs is assigned, add to glb(x0)
137  for (int i=xs_size; i--; ) {
138  if (xs[i].assigned()) {
139  GECODE_ME_CHECK_MODIFIED(loopFlag, x0.include(home,xs[i].val()));
140  }
141  }
142 
143  // intersect every y in xs with lub(x0)
144  for (int i=xs_size; i--; ) {
145  LubRanges<View> ub(x0);
146  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].inter_r(home,ub,false));
147  }
148 
149  // remove gaps between vars in xs from lub(x0)
150  GECODE_ME_CHECK_MODIFIED(loopFlag,
151  x0.exclude(home,Limits::min,xs[0].min()-1));
152  GECODE_ME_CHECK_MODIFIED(loopFlag,
153  x0.exclude(home,xs[xs_size-1].max()+1,
154  Limits::max));
155 
156  for (int i=xs_size-1; i--; ) {
157  int start = xs[i].max() + 1;
158  int end = xs[i+1].min() - 1;
159  if (start<=end) {
160  GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,start,end));
161  }
162  }
163 
164  // try to assign vars in xs from glb(x0)
165  if (x0.glbSize()>0) {
166 
167  LubRanges<View> ub(x0);
169  GlbRanges<View> lb(x0);
171 
172  int i=0;
173  for (; ubv() && lbv() && ubv.val()==lbv.val();
174  ++ubv, ++lbv, i++) {
175  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,lbv.val()));
176  }
177 
178  if (i<xs_size-1 && x0.lubMax()==x0.glbMax()) {
179  LubRanges<View> lbx0(x0);
180  GlbRanges<View> ubx0(x0);
182  inter(lbx0, ubx0);
183 
184  int to = x0.glbMax();
185  int from = to;
186  while (inter()) {
187  from = inter.min();
188  ++inter;
189  }
190 
191  int k=xs_size-1;
192  for (int j=to; j>=from;j--,k--) {
193  GECODE_ME_CHECK_MODIFIED(loopFlag, xs[k].eq(home,j));
194  }
195  }
196  }
197 
198  } while (loopFlag);
199 
200  for (int i=xs_size; i--; )
201  if (!xs[i].assigned())
202  return ES_FIX;
203  return home.ES_SUBSUMED(*this);
204  }
205 
206 }}}
207 
208 // STATISTICS: set-prop
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: array.hpp:1417
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4643
const int min
Smallest allowed integer in integer set.
Definition: set.hh:103
void update(Space &home, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1375
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:53
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as PC_LINEAR_LO)
Definition: sorted.hpp:88
Base-class for propagators.
Definition: core.hpp:1016
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1388
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: sorted.hpp:110
#define forceinline
Definition: config.hpp:182
Propagation has computed fixpoint.
Definition: core.hpp:469
const int max
Largest allowed integer in integer set.
Definition: set.hh:101
Computation spaces.
Definition: core.hpp:1668
#define GECODE_ME_CHECK_MODIFIED(modified, me)
Check whether me is failed or modified, and forward failure.
Definition: macros.hpp:68
int val(void) const
Return current value.
Base-class for both propagators and branchers.
Definition: core.hpp:620
View x0
SetView for the match.
Definition: channel.hh:67
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:95
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual void reschedule(Space &home)
Schedule function.
Definition: sorted.hpp:94
Range iterator for computing intersection (binary)
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.
void cancel(Space &home, Propagator &p, PropCond pc)
Cancel subscription of propagator p with propagation condition pc to all views.
Definition: array.hpp:1396
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:56
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:699
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3172
Propagation cost.
Definition: core.hpp:478
ExecStatus
Definition: core.hpp:464
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:116
virtual size_t dispose(Space &home)
Delete Propagator.
Definition: sorted.hpp:101
ChannelSorted(Space &home, ChannelSorted &p)
Constructor for cloning p.
Definition: sorted.hpp:61
Propagator for set equality
Definition: rel.hh:154
Execution is okay.
Definition: core.hpp:468
Gecode toplevel namespace
int min(void) const
Return smallest value of range.
ViewArray< Gecode::Int::IntView > xs
IntViews that together form the set x0.
Definition: channel.hh:69
int ModEventDelta
Modification event deltas.
Definition: core.hpp:91
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1203
Home class for posting propagators
Definition: core.hpp:846