Generated on Sat Feb 7 2015 02:01:20 for Gecode by doxygen 1.8.9.1
link-multi.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2007
8  *
9  * Last modified:
10  * $Date: 2011-07-07 11:23:22 +0200 (Thu, 07 Jul 2011) $ by $Author: schulte $
11  * $Revision: 12155 $
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/int/channel.hh>
39 
40 namespace Gecode { namespace Int { namespace Channel {
41 
43  class BoolIter {
44  private:
46  const ViewArray<BoolView>& x;
48  const int o;
50  int i;
51  public:
53  BoolIter(const ViewArray<BoolView>& x0, int o0);
55  bool operator ()(void) const;
57  int val(void) const;
59  void operator ++(void);
60  };
61 
64  x(x0), o(o0), i(0) {
65  while ((i<x.size()) && !x[i].zero())
66  i++;
67  }
68  forceinline bool
69  BoolIter::operator ()(void) const {
70  return i<x.size();
71  }
72  forceinline int
73  BoolIter::val(void) const {
74  assert(x[i].zero());
75  return i+o;
76  }
77  forceinline void
79  do {
80  i++;
81  } while ((i<x.size()) && !x[i].zero());
82  }
83 
84 
86  LinkMulti::LinkMulti(Space& home, bool share, LinkMulti& p)
88  (home,share,p), status(S_NONE), o(p.o) {
89  assert(p.status == S_NONE);
90  c.update(home,share,p.c);
91  }
92 
93  Actor*
94  LinkMulti::copy(Space& home, bool share) {
95  return new (home) LinkMulti(home,share,*this);
96  }
97 
98  forceinline size_t
100  Advisors<Advisor> as(c);
101  x.cancel(home,as.advisor());
102  c.dispose(home);
104  ::dispose(home);
105  return sizeof(*this);
106  }
107 
108  PropCost
109  LinkMulti::cost(const Space&, const ModEventDelta& med) const {
110  if ((status == S_ONE) || (IntView::me(med) == ME_INT_VAL))
112  else
113  return PropCost::linear(PropCost::LO, x.size());
114  }
115 
116  ExecStatus
118  if (status == S_RUN)
119  return ES_FIX;
120  // Detect a one
121  if (BoolView::one(d))
122  status = S_ONE;
123  return ES_NOFIX;
124  }
125 
126  ExecStatus
128  int n = x.size();
129 
130  // Always maintain the invariant that y lies inside the x-array
131  assert((y.min()-o >= 0) && (y.max()-o < n));
132 
133  if (y.assigned()) {
134  status = S_RUN;
135  int j=y.val()-o;
136  GECODE_ME_CHECK(x[j].one(home));
137  for (int i=0; i<j; i++)
138  GECODE_ME_CHECK(x[i].zero(home));
139  for (int i=j+1; i<n; i++)
140  GECODE_ME_CHECK(x[i].zero(home));
141  return home.ES_SUBSUMED(*this);
142  }
143 
144  // Check whether there is a one
145  if (status == S_ONE) {
146  status = S_RUN;
147  for (int i=0; true; i++)
148  if (x[i].one()) {
149  for (int j=0; j<i; j++)
150  GECODE_ME_CHECK(x[j].zero(home));
151  for (int j=i+1; j<n; j++)
152  GECODE_ME_CHECK(x[j].zero(home));
153  GECODE_ME_CHECK(y.eq(home,i+o));
154  return home.ES_SUBSUMED(*this);
155  }
156  GECODE_NEVER;
157  }
158 
159  status = S_RUN;
160 
161  redo:
162 
163  // Assign all Boolean views to zero that are outside bounds
164  {
165  int min=y.min()-o;
166  for (int i=0; i<min; i++)
167  GECODE_ME_CHECK(x[i].zero(home));
168  x.drop_fst(min); o += min; n = x.size();
169 
170  int max=y.max()-o;
171  for (int i=max+1; i<n; i++)
172  GECODE_ME_CHECK(x[i].zero(home));
173  x.drop_lst(max); n = x.size();
174  }
175 
176  {
177  // Remove zeros on the left
178  int i=0;
179  while ((i < n) && x[i].zero()) i++;
180  if (i >= n)
181  return ES_FAILED;
182  x.drop_fst(i); o += i; n = x.size();
183  }
184 
185  {
186  // Remove zeros on the right
187  int i=n-1;
188  while ((i >= 0) && x[i].zero()) i--;
189  assert(i >= 0);
190  x.drop_lst(i); n = x.size();
191  }
192 
193  assert(n >= 1);
194 
195  // Is there only one left?
196  if (n == 1) {
197  GECODE_ME_CHECK(x[0].one(home));
198  GECODE_ME_CHECK(y.eq(home,o));
199  return home.ES_SUBSUMED(*this);
200  }
201 
202  // Update bounds
203  GECODE_ME_CHECK(y.gq(home,o));
204  GECODE_ME_CHECK(y.lq(home,o+n-1));
205  if ((y.min() > o) || (y.max() < o+n-1))
206  goto redo;
207 
208  assert((n >= 2) && x[0].none() && x[n-1].none());
209  assert((y.min()-o == 0) && (y.max()-o == n-1));
210 
211  // Propagate from y to Boolean views
212  if ((IntView::me(med) == ME_INT_BND) || (IntView::me(med) == ME_INT_DOM)) {
214  int i=0;
215  do {
216  int j = v.val()-o;
217  if (i < j) {
218  GECODE_ME_CHECK(x[i].zero(home));
219  ++i;
220  } else if (i > j) {
221  ++v;
222  } else {
223  assert(i == j);
224  ++i; ++v;
225  }
226  } while (v() && (i < n));
227  }
228 
229  // Propagate from Boolean views to y
230  if (BoolView::me(med) == ME_BOOL_VAL) {
231  BoolIter bv(x,o);
232  GECODE_ME_CHECK(y.minus_v(home,bv,false));
233  }
234  status = S_NONE;
235  return ES_FIX;
236  }
237 
238 }}}
239 
240 // STATISTICS: int-prop
241 
int val(void) const
Return assigned value (only if assigned)
Definition: int.hpp:70
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:50
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Definition: int.hpp:160
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: link-multi.cpp:127
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
bool operator()(void) const
Test whether further values available.
Definition: link-multi.cpp:69
Mixed (n+1)-ary propagator.
Definition: propagator.hpp:268
A & advisor(void) const
Return advisor.
Definition: core.hpp:3408
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition: link-multi.cpp:117
Base-class for advisors.
Definition: core.hpp:926
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: link-multi.cpp:99
Class to iterate over advisors of a council.
Definition: core.hpp:227
Propagation has computed fixpoint.
Definition: core.hpp:528
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4058
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
ModEvent minus_v(Space &home, I &i, bool depends=true)
Remove from domain the values described by i.
Definition: int.hpp:200
Gecode::IntSet d(v, 7)
int max(void) const
Return maximum of domain.
Definition: int.hpp:62
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
void drop_lst(int i)
Drop views from positions i+1 to size()-1 from array.
Definition: array.hpp:1308
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
void operator++(void)
Move to the next value.
Definition: link-multi.cpp:78
Execution has resulted in failure.
Definition: core.hpp:525
const Gecode::PropCond PC_BOOL_NONE
Propagation condition to be ignored (convenience)
Definition: var-type.hpp:118
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition: int.hpp:115
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:56
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition: var-type.hpp:100
int min(void) const
Return minimum of domain.
Definition: int.hpp:58
const Gecode::ModEvent ME_INT_BND
Domain operation has changed the minimum or maximum of the domain.
Definition: var-type.hpp:65
void drop_fst(int i)
Drop views from positions 0 to i-1 from array.
Definition: array.hpp:1301
void cancel(Space &home, Propagator &p, PropCond pc)
Cancel subscription of propagator p with propagation condition pc to all views.
Definition: array.hpp:1408
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
int val(void) const
Return value.
Definition: link-multi.cpp:73
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
bool one(void) const
Test whether view is assigned to be one.
Definition: bool.hpp:218
Integer view for integer variables.
Definition: view.hpp:129
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Generic domain change information to be supplied to advisors.
Definition: core.hpp:275
Propagation cost.
Definition: core.hpp:537
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition: var-type.hpp:72
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: link-multi.cpp:94
Iterates the values to be removed as defined by an array of Boolean views.
Definition: link-multi.cpp:43
ExecStatus
Definition: core.hpp:523
static ModEvent me(const ModEventDelta &med)
Return modification event for view type in med.
#define forceinline
Definition: config.hpp:132
bool assigned(void) const
Test whether view is assigned.
Definition: view.hpp:448
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: int.hpp:133
BoolIter(const ViewArray< BoolView > &x0, int o0)
Initialize iterator.
Definition: link-multi.cpp:63
Propagation has not computed fixpoint.
Definition: core.hpp:526
int val(void) const
Return current value.
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (low unary if y is assigned, low linear otherwise)
Definition: link-multi.cpp:109
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Gecode toplevel namespace
Link propagator for multiple Boolean views.
Definition: channel.hh:199
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
const Gecode::ModEvent ME_BOOL_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:116
Boolean view for Boolean variables.
Definition: view.hpp:1315