Generated on Tue Jul 18 2017 18:41:42 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: 2016-10-25 12:52:26 +0200 (Tue, 25 Oct 2016) $ by $Author: schulte $
15  * $Revision: 15233 $
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>
163  Weights<View>::Weights(Space& home, bool share, Weights& p)
164  : Propagator(home,share,p) {
165  x.update(home,share,p.x);
166  y.update(home,share,p.y);
167  elements.update(home,share,p.elements);
168  weights.update(home,share,p.weights);
169  }
170 
171  template<class View>
172  inline ExecStatus
174  const SharedArray<int>& weights,
175  View x, Gecode::Int::IntView y) {
176  if (elements.size() != weights.size())
177  throw ArgumentSizeMismatch("Weights");
178  Region r(home);
179  int* els_arr = r.alloc<int>(elements.size());
180  for (int i=elements.size(); i--;)
181  els_arr[i] = elements[i];
182  IntSet els(els_arr, elements.size());
183  IntSetRanges er(els);
184  GECODE_ME_CHECK(x.intersectI(home, er));
185  (void) new (home) Weights(home,elements,weights,x,y);
186  return ES_OK;
187  }
188 
189  template<class View>
190  PropCost
191  Weights<View>::cost(const Space&, const ModEventDelta&) const {
192  return PropCost::linear(PropCost::LO, y.size()+1);
193  }
194 
195  template<class View>
196  void
198  x.reschedule(home,*this, PC_SET_ANY);
199  y.reschedule(home,*this, Gecode::Int::PC_INT_BND);
200  }
201 
202  template<class View>
203  forceinline size_t
205  home.ignore(*this,AP_DISPOSE);
206  x.cancel(home,*this, PC_SET_ANY);
207  y.cancel(home,*this, Gecode::Int::PC_INT_BND);
208  elements.~SharedArray();
209  weights.~SharedArray();
210  (void) Propagator::dispose(home);
211  return sizeof(*this);
212  }
213 
214  template<class View>
215  Actor*
216  Weights<View>::copy(Space& home, bool share) {
217  return new (home) Weights(home,share,*this);
218  }
219 
221  template<class I>
225  I& iter) {
226  int sum = 0;
227  int i = 0;
229  for (; v(); ++v) {
230  // Skip all elements below the current
231  while (elements[i]<v.val()) i++;
232  assert(elements[i] == v.val());
233  sum += weights[i];
234  }
235  assert(!v());
236  return sum;
237  }
238 
239 
241  class IntLess {
242  public:
243  bool operator ()(int x, int y);
244  };
245 
246  forceinline bool
248  return x < y;
249  }
250 
251  template<class View>
252  ExecStatus
254  ModEvent me = ME_SET_NONE;
255 
256  if (!x.assigned()) {
257  // Collect the weights of the elements in the unknown set in an array
258  int size = elements.size();
259  Region r(home);
260  int* minWeights = r.alloc<int>(size);
261  int* maxWeights = r.alloc<int>(size);
262 
265  for (int i=0; i<size; i++) {
266  if (!urv() || elements[i]<urv.val()) {
267  minWeights[i] = INT_MAX;
268  maxWeights[i] = INT_MIN;
269  } else {
270  assert(elements[i] == urv.val());
271  minWeights[i] = weights[i];
272  maxWeights[i] = weights[i];
273  ++urv;
274  }
275  }
276 
277  // Sort the weights of the unknown elements
278  IntLess il;
279  Support::quicksort<int>(minWeights, size, il);
280  Support::quicksort<int>(maxWeights, size, il);
281 
282  // The maximum number of elements that can still be added to x
283  int delta = static_cast<int>(std::min(x.unknownSize(), x.cardMax() - x.glbSize()));
284 
285  // The weight of the elements already in x
286  GlbRanges<View> glb(x);
287  int glbWeight = weightI<GlbRanges<View> >(elements, weights, glb);
288 
289  // Compute the weight of the current lower bound of x, plus at most
290  // delta-1 further elements with smallest negative weights. This weight
291  // determines which elements in the upper bound cannot possibly be
292  // added to x (those whose weight would exceed the capacity even if
293  // all other elements are minimal)
294  int lowWeight = glbWeight;
295  for (int i=0; i<delta-1; i++) {
296  if (minWeights[i] >= 0)
297  break;
298  lowWeight+=minWeights[i];
299  }
300 
301  // Compute the lowest possible weight of x. If there is another element
302  // with negative weight left, then add its weight to lowWeight.
303  // Otherwise lowWeight is already the lowest possible weight.
304  int lowestWeight = lowWeight;
305  if (delta>0 && minWeights[delta-1]<0)
306  lowestWeight+=minWeights[delta-1];
307 
308  // If after including the minimal number of required elements,
309  // no more element with negative weight is available, then
310  // a tighter lower bound can be computed.
311  if ( (x.cardMin() - x.glbSize() > 0 &&
312  minWeights[x.cardMin() - x.glbSize() - 1] >= 0) ||
313  minWeights[0] >= 0 ) {
314  int lowestPosWeight = glbWeight;
315  for (unsigned int i=0; i<x.cardMin() - x.glbSize(); i++) {
316  lowestPosWeight += minWeights[i];
317  }
318  lowestWeight = std::max(lowestWeight, lowestPosWeight);
319  }
320 
321  // Compute the highest possible weight of x as the weight of the lower
322  // bound plus the weight of the delta heaviest elements still in the
323  // upper bound.
324  int highestWeight = glbWeight;
325  for (int i=0; i<delta; i++) {
326  if (maxWeights[size-i-1]<=0)
327  break;
328  highestWeight += maxWeights[size-i-1];
329  }
330 
331  // Prune the weight using the computed bounds
332  GECODE_ME_CHECK(y.gq(home, lowestWeight));
333  GECODE_ME_CHECK(y.lq(home, highestWeight));
334 
335  // Exclude all elements that are too heavy from the set x.
336  // Elements are too heavy if their weight alone already
337  // exceeds the remaining capacity
338  int remainingCapacity = y.max()-lowWeight;
339 
340  UnknownRanges<View> ur2(x);
343  ov(remainingCapacity, elements, weights, urv2);
346  me = x.excludeI(home, ovr);
347  GECODE_ME_CHECK(me);
348  }
349  if (x.assigned()) {
350  // If x is assigned, just compute its weight and assign y.
351  GlbRanges<View> glb(x);
352  int w =
353  weightI<GlbRanges<View> >(elements, weights, glb);
354  GECODE_ME_CHECK(y.eq(home, w));
355  return home.ES_SUBSUMED(*this);
356  }
357 
358  // return me_modified(me) ? ES_NOFIX : ES_FIX;
359  return ES_NOFIX;
360  }
361 
362 }}}
363 
364 // 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
NodeType t
Type of node.
Definition: bool-expr.cpp:234
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: weights.hpp:253
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:4841
Range iterator for integer sets.
Definition: int.hh:274
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
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:630
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:326
void operator++(void)
Move iterator to next value (if possible)
Definition: weights.hpp:142
int ModEvent
Type for modification events.
Definition: core.hpp:142
Base-class for propagators.
Definition: core.hpp:1092
unsigned int size(void) const
Return size (cardinality) of domain.
Definition: int.hpp:75
Handle to region.
Definition: region.hpp:61
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: view.hpp:494
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: weights.hpp:204
Computation spaces.
Definition: core.hpp:1748
int val(void) const
Return current value.
Base-class for both propagators and branchers.
Definition: core.hpp:696
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: weights.hpp:216
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:197
int weightI(SharedArray< int > &elements, SharedArray< int > &weights, I &iter)
Compute the weight of the elements in the iterator I.
Definition: weights.hpp:223
Weights(Space &home, bool share, Weights &p)
Constructor for cloning p.
Definition: weights.hpp:163
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:173
size_t size
The size of the propagator (used during subsumption)
Definition: core.hpp:1105
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
void update(Space &home, bool share, VarImpView< Var > &y)
Update this view to be a clone of view y.
Definition: view.hpp:529
#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:3321
View x
The set view.
Definition: int.hh:269
bool operator()(int x, int y)
Definition: weights.hpp:247
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:784
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
void update(Space &home, bool share, SharedHandle &sh)
Updating during cloning.
Definition: core.hpp:3127
Integer view for integer variables.
Definition: view.hpp:129
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:784
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:4141
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3354
Propagation cost.
Definition: core.hpp:554
ExecStatus
Definition: core.hpp:540
#define forceinline
Definition: config.hpp:173
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:784
Execution is okay.
Definition: core.hpp:544
Propagation has not computed fixpoint.
Definition: core.hpp:543
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:191
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
Home class for posting propagators
Definition: core.hpp:922
Exception: Arguments are of different size
Definition: exception.hpp:77
Sort order for integers.
Definition: weights.hpp:241