Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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$ by $Author$
11  * $Revision$
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 
85  Actor*
87  return new (home) LinkMulti(home,*this);
88  }
89 
90  forceinline size_t
92  Advisors<Advisor> as(c);
93  x.cancel(home,as.advisor());
94  c.dispose(home);
96  ::dispose(home);
97  return sizeof(*this);
98  }
99 
100  PropCost
101  LinkMulti::cost(const Space&, const ModEventDelta& med) const {
102  if ((status == S_ONE) || (IntView::me(med) == ME_INT_VAL))
104  else
105  return PropCost::linear(PropCost::LO, x.size());
106  }
107 
108  void
110  if (status == S_ONE)
111  BoolView::schedule(home,*this,ME_BOOL_VAL);
112  y.reschedule(home,*this,PC_INT_DOM);
113  }
114 
115  ExecStatus
117  if (status == S_RUN)
118  return ES_FIX;
119  // Detect a one
120  if (BoolView::one(d))
121  status = S_ONE;
122  return ES_NOFIX;
123  }
124 
125  ExecStatus
127  int n = x.size();
128 
129  // Always maintain the invariant that y lies inside the x-array
130  assert((y.min()-o >= 0) && (y.max()-o < n));
131 
132  if (y.assigned()) {
133  status = S_RUN;
134  int j=y.val()-o;
135  GECODE_ME_CHECK(x[j].one(home));
136  for (int i=0; i<j; i++)
137  GECODE_ME_CHECK(x[i].zero(home));
138  for (int i=j+1; i<n; i++)
139  GECODE_ME_CHECK(x[i].zero(home));
140  return home.ES_SUBSUMED(*this);
141  }
142 
143  // Check whether there is a one
144  if (status == S_ONE) {
145  status = S_RUN;
146  for (int i=0; true; i++)
147  if (x[i].one()) {
148  for (int j=0; j<i; j++)
149  GECODE_ME_CHECK(x[j].zero(home));
150  for (int j=i+1; j<n; j++)
151  GECODE_ME_CHECK(x[j].zero(home));
152  GECODE_ME_CHECK(y.eq(home,i+o));
153  return home.ES_SUBSUMED(*this);
154  }
155  GECODE_NEVER;
156  }
157 
158  status = S_RUN;
159 
160  redo:
161 
162  // Assign all Boolean views to zero that are outside bounds
163  {
164  int min=y.min()-o;
165  for (int i=0; i<min; i++)
166  GECODE_ME_CHECK(x[i].zero(home));
167  x.drop_fst(min); o += min; n = x.size();
168 
169  int max=y.max()-o;
170  for (int i=max+1; i<n; i++)
171  GECODE_ME_CHECK(x[i].zero(home));
172  x.drop_lst(max); n = x.size();
173  }
174 
175  {
176  // Remove zeros on the left
177  int i=0;
178  while ((i < n) && x[i].zero()) i++;
179  if (i >= n)
180  return ES_FAILED;
181  x.drop_fst(i); o += i; n = x.size();
182  }
183 
184  {
185  // Remove zeros on the right
186  int i=n-1;
187  while ((i >= 0) && x[i].zero()) i--;
188  assert(i >= 0);
189  x.drop_lst(i); n = x.size();
190  }
191 
192  assert(n >= 1);
193 
194  // Is there only one left?
195  if (n == 1) {
196  GECODE_ME_CHECK(x[0].one(home));
197  GECODE_ME_CHECK(y.eq(home,o));
198  return home.ES_SUBSUMED(*this);
199  }
200 
201  // Update bounds
202  GECODE_ME_CHECK(y.gq(home,o));
203  GECODE_ME_CHECK(y.lq(home,o+n-1));
204  if ((y.min() > o) || (y.max() < o+n-1))
205  goto redo;
206 
207  assert((n >= 2) && x[0].none() && x[n-1].none());
208  assert((y.min()-o == 0) && (y.max()-o == n-1));
209 
210  // Propagate from y to Boolean views
211  if ((IntView::me(med) == ME_INT_BND) || (IntView::me(med) == ME_INT_DOM)) {
213  int i=0;
214  do {
215  int j = v.val()-o;
216  if (i < j) {
217  GECODE_ME_CHECK(x[i].zero(home));
218  ++i;
219  } else if (i > j) {
220  ++v;
221  } else {
222  assert(i == j);
223  ++i; ++v;
224  }
225  } while (v() && (i < n));
226  }
227 
228  // Propagate from Boolean views to y
229  if (BoolView::me(med) == ME_BOOL_VAL) {
230  BoolIter bv(x,o);
231  GECODE_ME_CHECK(y.minus_v(home,bv,false));
232  }
233  status = S_NONE;
234  return ES_FIX;
235  }
236 
237 }}}
238 
239 // STATISTICS: int-prop
240 
virtual void reschedule(Space &home)
Schedule function.
Definition: link-multi.cpp:109
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4643
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:50
bool one(void) const
Test whether view is assigned to be one.
Definition: bool.hpp:218
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: link-multi.cpp:126
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:53
bool assigned(void) const
Test whether view is assigned.
Definition: var.hpp:123
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition: link-multi.cpp:116
Base-class for advisors.
Definition: core.hpp:1218
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: link-multi.cpp:91
Class to iterate over advisors of a council.
Definition: core.hpp:157
#define forceinline
Definition: config.hpp:182
Propagation has computed fixpoint.
Definition: core.hpp:469
bool operator()(void) const
Test whether further values available.
Definition: link-multi.cpp:69
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4660
Computation spaces.
Definition: core.hpp:1668
int val(void) const
Return current value.
Base-class for both propagators and branchers.
Definition: core.hpp:620
Gecode::IntSet d(v, 7)
Gecode::FloatVal c(-8, 8)
void drop_lst(int i)
Drop views from positions i+1 to size()-1 from array.
Definition: array.hpp:1296
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:466
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
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:1289
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
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: link-multi.cpp:86
const int v[7]
Definition: distinct.cpp:263
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:769
int val(void) const
Return value.
Definition: link-multi.cpp:73
Generic domain change information to be supplied to advisors.
Definition: core.hpp:205
Propagation cost.
Definition: core.hpp:478
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition: var-type.hpp:72
Iterates the values to be removed as defined by an array of Boolean views.
Definition: link-multi.cpp:43
ExecStatus
Definition: core.hpp:464
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:101
static ModEvent me(const ModEventDelta &med)
Return modification event for view type in med.
Definition: view.hpp:514
BoolIter(const ViewArray< BoolView > &x0, int o0)
Initialize iterator.
Definition: link-multi.cpp:63
Propagation has not computed fixpoint.
Definition: core.hpp:467
Gecode toplevel namespace
Link propagator for multiple Boolean views.
Definition: channel.hh:201
static void schedule(Space &home, Propagator &p, ModEvent me)
Schedule propagator p with modification event me.
Definition: view.hpp:509
int ModEventDelta
Modification event deltas.
Definition: core.hpp:91
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1203
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
A & advisor(void) const
Return advisor.
Definition: core.hpp:3880
const Gecode::ModEvent ME_BOOL_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:116