Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
weights.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 #include <gecode/set.hh>
43 #include <gecode/int.hh>
44 
45 namespace Gecode { namespace Set { namespace Int {
46 
48  template<class I>
50  private:
52  int threshold;
54  I iter;
56  const SharedArray<int> elements;
58  const SharedArray<int> weights;
60  int index;
62  void next(void);
63  public:
65 
66  OverweightValues(void);
69  OverweightValues(int t,
70  SharedArray<int>& elements0,
71  SharedArray<int>& weights0,
72  I& i);
74  void init(int t,
75  SharedArray<int>& elements0,
76  SharedArray<int>& weights0,
77  I& i);
79 
81 
82  bool operator ()(void) const;
85  void operator ++(void);
87 
89  int val(void) const;
92  };
93 
94  template<class I>
95  forceinline void
97  while (iter()) {
98  while (elements[index]<iter.val()) index++;
99  assert(elements[index]==iter.val());
100  if (weights[index] > threshold) {
101  return;
102  }
103  ++iter;
104  }
105  }
106 
107  template<class I>
110 
111  template<class I>
114  SharedArray<int>& elements0,
115  SharedArray<int>& weights0,
116  I& i) : threshold(t),
117  iter(i),
118  elements(elements0),
119  weights(weights0),
120  index(0) {
121  next();
122  }
123 
124  template<class I>
125  forceinline void
127  SharedArray<int>& elements0,
128  SharedArray<int>& weights0,
129  I& i) {
130  threshold = t; iter = i;
131  elements = elements0; weights = weights0;
132  index = 0;
133  next();
134  }
135 
136  template<class I>
137  forceinline bool
138  OverweightValues<I>::operator ()(void) const { return iter(); }
139 
140  template<class I>
141  forceinline void
142  OverweightValues<I>::operator ++(void) { ++iter; next(); }
143 
144  template<class I>
145  forceinline int
146  OverweightValues<I>::val(void) const { return elements[index]; }
147 
148  template<class View>
151  const SharedArray<int>& elements0,
152  const SharedArray<int>& weights0,
153  View x0, Gecode::Int::IntView y0)
154  : Propagator(home), elements(elements0), weights(weights0),
155  x(x0), y(y0) {
156  home.notice(*this,AP_DISPOSE);
157  x.subscribe(home,*this, PC_SET_ANY);
158  y.subscribe(home,*this, Gecode::Int::PC_INT_BND);
159  }
160 
161  template<class View>
164  : Propagator(home,p), elements(p.elements), weights(p.weights) {
165  x.update(home,p.x);
166  y.update(home,p.y);
167  }
168 
169  template<class View>
170  inline ExecStatus
172  const SharedArray<int>& weights,
173  View x, Gecode::Int::IntView y) {
174  if (elements.size() != weights.size())
175  throw ArgumentSizeMismatch("Weights");
176  Region r;
177  int* els_arr = r.alloc<int>(elements.size());
178  for (int i=elements.size(); i--;)
179  els_arr[i] = elements[i];
180  IntSet els(els_arr, elements.size());
181  IntSetRanges er(els);
182  GECODE_ME_CHECK(x.intersectI(home, er));
183  (void) new (home) Weights(home,elements,weights,x,y);
184  return ES_OK;
185  }
186 
187  template<class View>
188  PropCost
189  Weights<View>::cost(const Space&, const ModEventDelta&) const {
190  return PropCost::linear(PropCost::LO, y.size()+1);
191  }
192 
193  template<class View>
194  void
196  x.reschedule(home,*this, PC_SET_ANY);
197  y.reschedule(home,*this, Gecode::Int::PC_INT_BND);
198  }
199 
200  template<class View>
201  forceinline size_t
203  home.ignore(*this,AP_DISPOSE);
204  x.cancel(home,*this, PC_SET_ANY);
205  y.cancel(home,*this, Gecode::Int::PC_INT_BND);
206  elements.~SharedArray();
207  weights.~SharedArray();
208  (void) Propagator::dispose(home);
209  return sizeof(*this);
210  }
211 
212  template<class View>
213  Actor*
215  return new (home) Weights(home,*this);
216  }
217 
219  template<class I>
223  I& iter) {
224  int sum = 0;
225  int i = 0;
227  for (; v(); ++v) {
228  // Skip all elements below the current
229  while (elements[i]<v.val()) i++;
230  assert(elements[i] == v.val());
231  sum += weights[i];
232  }
233  assert(!v());
234  return sum;
235  }
236 
237 
239  class IntLess {
240  public:
241  bool operator ()(int x, int y);
242  };
243 
244  forceinline bool
246  return x < y;
247  }
248 
249  template<class View>
250  ExecStatus
252  ModEvent me = ME_SET_NONE;
253 
254  if (!x.assigned()) {
255  // Collect the weights of the elements in the unknown set in an array
256  int size = elements.size();
257  Region r;
258  int* minWeights = r.alloc<int>(size);
259  int* maxWeights = r.alloc<int>(size);
260 
263  for (int i=0; i<size; i++) {
264  if (!urv() || elements[i]<urv.val()) {
265  minWeights[i] = INT_MAX;
266  maxWeights[i] = INT_MIN;
267  } else {
268  assert(elements[i] == urv.val());
269  minWeights[i] = weights[i];
270  maxWeights[i] = weights[i];
271  ++urv;
272  }
273  }
274 
275  // Sort the weights of the unknown elements
276  IntLess il;
277  Support::quicksort<int>(minWeights, size, il);
278  Support::quicksort<int>(maxWeights, size, il);
279 
280  // The maximum number of elements that can still be added to x
281  int delta = static_cast<int>(std::min(x.unknownSize(), x.cardMax() - x.glbSize()));
282 
283  // The weight of the elements already in x
284  GlbRanges<View> glb(x);
285  int glbWeight = weightI<GlbRanges<View> >(elements, weights, glb);
286 
287  // Compute the weight of the current lower bound of x, plus at most
288  // delta-1 further elements with smallest negative weights. This weight
289  // determines which elements in the upper bound cannot possibly be
290  // added to x (those whose weight would exceed the capacity even if
291  // all other elements are minimal)
292  int lowWeight = glbWeight;
293  for (int i=0; i<delta-1; i++) {
294  if (minWeights[i] >= 0)
295  break;
296  lowWeight+=minWeights[i];
297  }
298 
299  // Compute the lowest possible weight of x. If there is another element
300  // with negative weight left, then add its weight to lowWeight.
301  // Otherwise lowWeight is already the lowest possible weight.
302  int lowestWeight = lowWeight;
303  if (delta>0 && minWeights[delta-1]<0)
304  lowestWeight+=minWeights[delta-1];
305 
306  // If after including the minimal number of required elements,
307  // no more element with negative weight is available, then
308  // a tighter lower bound can be computed.
309  if ( (x.cardMin() - x.glbSize() > 0 &&
310  minWeights[x.cardMin() - x.glbSize() - 1] >= 0) ||
311  minWeights[0] >= 0 ) {
312  int lowestPosWeight = glbWeight;
313  for (unsigned int i=0; i<x.cardMin() - x.glbSize(); i++) {
314  lowestPosWeight += minWeights[i];
315  }
316  lowestWeight = std::max(lowestWeight, lowestPosWeight);
317  }
318 
319  // Compute the highest possible weight of x as the weight of the lower
320  // bound plus the weight of the delta heaviest elements still in the
321  // upper bound.
322  int highestWeight = glbWeight;
323  for (int i=0; i<delta; i++) {
324  if (maxWeights[size-i-1]<=0)
325  break;
326  highestWeight += maxWeights[size-i-1];
327  }
328 
329  // Prune the weight using the computed bounds
330  GECODE_ME_CHECK(y.gq(home, lowestWeight));
331  GECODE_ME_CHECK(y.lq(home, highestWeight));
332 
333  // Exclude all elements that are too heavy from the set x.
334  // Elements are too heavy if their weight alone already
335  // exceeds the remaining capacity
336  int remainingCapacity = y.max()-lowWeight;
337 
338  UnknownRanges<View> ur2(x);
341  ov(remainingCapacity, elements, weights, urv2);
344  me = x.excludeI(home, ovr);
345  GECODE_ME_CHECK(me);
346  }
347  if (x.assigned()) {
348  // If x is assigned, just compute its weight and assign y.
349  GlbRanges<View> glb(x);
350  int w =
351  weightI<GlbRanges<View> >(elements, weights, glb);
352  GECODE_ME_CHECK(y.eq(home, w));
353  return home.ES_SUBSUMED(*this);
354  }
355 
356  // return me_modified(me) ? ES_NOFIX : ES_FIX;
357  return ES_NOFIX;
358  }
359 
360 }}}
361 
362 // STATISTICS: set-prop
void cancel(Space &home, Propagator &p, PropCond pc)
Cancel subscription of propagator p with propagation condition pc to view.
Definition: view.hpp:489
void update(Space &home, VarImpView< Var > &y)
Update this view to be a clone of view y.
Definition: view.hpp:529
NodeType t
Type of node.
Definition: bool-expr.cpp:234
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: weights.hpp:251
Value Iterator for values above a certain weight.
Definition: weights.hpp:49
Range iterator for the unknown set.
Definition: var-imp.hpp:406
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4643
Range iterator for integer sets.
Definition: int.hh:272
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
const FloatNum max
Largest allowed float value.
Definition: float.hh:848
SharedArray< int > weights
Weights for the elements in the upper bound.
Definition: int.hh:266
Actor must always be disposed.
Definition: core.hpp:554
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Definition: int.hpp:160
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:384
void operator++(void)
Move iterator to next value (if possible)
Definition: weights.hpp:142
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: weights.hpp:214
int ModEvent
Type for modification events.
Definition: core.hpp:64
Base-class for propagators.
Definition: core.hpp:1016
unsigned int size(void) const
Return size (cardinality) of domain.
Definition: int.hpp:75
Handle to region.
Definition: region.hpp:57
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: view.hpp:494
Weights(Space &home, Weights &p)
Constructor for cloning p.
Definition: weights.hpp:163
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: weights.hpp:202
#define forceinline
Definition: config.hpp:182
Computation spaces.
Definition: core.hpp:1668
int val(void) const
Return current value.
Base-class for both propagators and branchers.
Definition: core.hpp:620
int val(void) const
Return current value.
Definition: weights.hpp:146
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to view.
Definition: view.hpp:483
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual void reschedule(Space &home)
Schedule function.
Definition: weights.hpp:195
int weightI(SharedArray< int > &elements, SharedArray< int > &weights, I &iter)
Compute the weight of the elements in the iterator I.
Definition: weights.hpp:221
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition: int.hpp:115
int size(void) const
Return number of elements.
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:91
Range iterator from value iterator.
Value iterator from range iterator.
static ExecStatus post(Home home, const SharedArray< int > &elements, const SharedArray< int > &weights, View x, Gecode::Int::IntView y)
Post propagator for .
Definition: weights.hpp:171
size_t size
The size of the propagator (used during subsumption)
Definition: core.hpp:1029
Integer sets.
Definition: int.hh:174
const Gecode::ModEvent ME_SET_NONE
Domain operation has not changed domain.
Definition: var-type.hpp:140
Gecode::Int::IntView y
The integer view.
Definition: int.hh:271
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:56
OverweightValues(void)
Default constructor.
Definition: weights.hpp:109
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3139
View x
The set view.
Definition: int.hh:269
bool operator()(int x, int y)
Definition: weights.hpp:245
const Gecode::PropCond PC_SET_ANY
Propagate when any bound or the cardinality of a view changes.
Definition: var-type.hpp:248
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
const int v[7]
Definition: distinct.cpp:263
bool operator()(void) const
Test whether iterator is still at a value or done.
Definition: weights.hpp:138
Integer view for integer variables.
Definition: view.hpp:129
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:769
void init(int t, SharedArray< int > &elements0, SharedArray< int > &weights0, I &i)
Initialize with elements/weights pairs, threshold t and iterator i.
Definition: weights.hpp:126
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:3944
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
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: int.hpp:133
Post propagator for SetVar x
Definition: set.hh:769
Execution is okay.
Definition: core.hpp:468
Propagation has not computed fixpoint.
Definition: core.hpp:467
Propagator for weight of a set
Definition: int.hh:261
Gecode toplevel namespace
SharedArray< int > elements
List of elements in the upper bound.
Definition: int.hh:264
int max(void) const
Return maximum of domain.
Definition: int.hpp:62
LinFloatExpr sum(const FloatVarArgs &x)
Construct linear float expression as sum of float variables.
Definition: float-expr.cpp:548
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as PC_LINEAR_LO)
Definition: weights.hpp:189
int ModEventDelta
Modification event deltas.
Definition: core.hpp:91
Home class for posting propagators
Definition: core.hpp:846
Exception: Arguments are of different size
Definition: exception.hpp:77
Sort order for integers.
Definition: weights.hpp:239