00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <gecode/set.hh>
00043 #include <gecode/int.hh>
00044
00045 namespace Gecode { namespace Set { namespace Int {
00046
00048 template<class I>
00049 class OverweightValues {
00050 private:
00052 int threshold;
00054 I iter;
00056 const SharedArray<int> elements;
00058 const SharedArray<int> weights;
00060 int index;
00062 void next(void);
00063 public:
00065
00066
00067 OverweightValues(void);
00069 OverweightValues(int t,
00070 SharedArray<int>& elements0,
00071 SharedArray<int>& weights0,
00072 I& i);
00074 void init(int t,
00075 SharedArray<int>& elements0,
00076 SharedArray<int>& weights0,
00077 I& i);
00079
00081
00082
00083 bool operator ()(void) const;
00085 void operator ++(void);
00087
00088
00089
00090 int val(void) const;
00092 };
00093
00094 template<class I>
00095 forceinline void
00096 OverweightValues<I>::next(void) {
00097 while (iter()) {
00098 while (elements[index]<iter.val()) index++;
00099 assert(elements[index]==iter.val());
00100 if (weights[index] > threshold) {
00101 return;
00102 }
00103 ++iter;
00104 }
00105 }
00106
00107 template<class I>
00108 forceinline
00109 OverweightValues<I>::OverweightValues(void) {}
00110
00111 template<class I>
00112 forceinline
00113 OverweightValues<I>::OverweightValues(int t,
00114 SharedArray<int>& elements0,
00115 SharedArray<int>& weights0,
00116 I& i) : threshold(t),
00117 iter(i),
00118 elements(elements0),
00119 weights(weights0),
00120 index(0) {
00121 next();
00122 }
00123
00124 template<class I>
00125 forceinline void
00126 OverweightValues<I>::init(int t,
00127 SharedArray<int>& elements0,
00128 SharedArray<int>& weights0,
00129 I& i) {
00130 threshold = t; iter = i;
00131 elements = elements0; weights = weights0;
00132 index = 0;
00133 next();
00134 }
00135
00136 template<class I>
00137 forceinline bool
00138 OverweightValues<I>::operator ()(void) const { return iter(); }
00139
00140 template<class I>
00141 forceinline void
00142 OverweightValues<I>::operator ++(void) { ++iter; next(); }
00143
00144 template<class I>
00145 forceinline int
00146 OverweightValues<I>::val(void) const { return elements[index]; }
00147
00148 template<class View>
00149 forceinline
00150 Weights<View>::Weights(Home home,
00151 const SharedArray<int>& elements0,
00152 const SharedArray<int>& weights0,
00153 View x0, Gecode::Int::IntView y0)
00154 : Propagator(home), elements(elements0), weights(weights0),
00155 x(x0), y(y0) {
00156 home.notice(*this,AP_DISPOSE);
00157 x.subscribe(home,*this, PC_SET_ANY);
00158 y.subscribe(home,*this, Gecode::Int::PC_INT_BND);
00159 }
00160
00161 template<class View>
00162 forceinline
00163 Weights<View>::Weights(Space& home, bool share, Weights& p)
00164 : Propagator(home,share,p) {
00165 x.update(home,share,p.x);
00166 y.update(home,share,p.y);
00167 elements.update(home,share,p.elements);
00168 weights.update(home,share,p.weights);
00169 }
00170
00171 template<class View>
00172 inline ExecStatus
00173 Weights<View>::post(Home home, const SharedArray<int>& elements,
00174 const SharedArray<int>& weights,
00175 View x, Gecode::Int::IntView y) {
00176 if (elements.size() != weights.size())
00177 throw ArgumentSizeMismatch("Weights");
00178 Region r(home);
00179 int* els_arr = r.alloc<int>(elements.size());
00180 for (int i=elements.size(); i--;)
00181 els_arr[i] = elements[i];
00182 IntSet els(els_arr, elements.size());
00183 IntSetRanges er(els);
00184 GECODE_ME_CHECK(x.intersectI(home, er));
00185 (void) new (home) Weights(home,elements,weights,x,y);
00186 return ES_OK;
00187 }
00188
00189 template<class View>
00190 PropCost
00191 Weights<View>::cost(const Space&, const ModEventDelta&) const {
00192 return PropCost::linear(PropCost::LO, y.size()+1);
00193 }
00194
00195 template<class View>
00196 forceinline size_t
00197 Weights<View>::dispose(Space& home) {
00198 home.ignore(*this,AP_DISPOSE);
00199 x.cancel(home,*this, PC_SET_ANY);
00200 y.cancel(home,*this, Gecode::Int::PC_INT_BND);
00201 elements.~SharedArray();
00202 weights.~SharedArray();
00203 (void) Propagator::dispose(home);
00204 return sizeof(*this);
00205 }
00206
00207 template<class View>
00208 Actor*
00209 Weights<View>::copy(Space& home, bool share) {
00210 return new (home) Weights(home,share,*this);
00211 }
00212
00214 template<class I>
00215 forceinline
00216 int weightI(SharedArray<int>& elements,
00217 SharedArray<int>& weights,
00218 I& iter) {
00219 int sum = 0;
00220 int i = 0;
00221 Iter::Ranges::ToValues<I> v(iter);
00222 for (; v(); ++v) {
00223
00224 while (elements[i]<v.val()) i++;
00225 assert(elements[i] == v.val());
00226 sum += weights[i];
00227 }
00228 assert(!v());
00229 return sum;
00230 }
00231
00232
00234 class IntLess {
00235 public:
00236 bool operator ()(int x, int y);
00237 };
00238
00239 forceinline bool
00240 IntLess::operator ()(int x, int y) {
00241 return x < y;
00242 }
00243
00244 template<class View>
00245 ExecStatus
00246 Weights<View>::propagate(Space& home, const ModEventDelta&) {
00247
00248 ModEvent me = ME_SET_NONE;
00249
00250 if (!x.assigned()) {
00251
00252 int size = elements.size();
00253 Region r(home);
00254 int* currentWeights = r.alloc<int>(size);
00255
00256 UnknownRanges<View> ur(x);
00257 Iter::Ranges::ToValues<UnknownRanges<View> > urv(ur);
00258 for (int i=0; i<size; i++) {
00259 if (!urv() || elements[i]<urv.val()) {
00260 currentWeights[i] = 0;
00261 } else {
00262 assert(elements[i] == urv.val());
00263 currentWeights[i] = weights[i];
00264 ++urv;
00265 }
00266 }
00267
00268
00269 IntLess il;
00270 Support::quicksort<int>(currentWeights, size, il);
00271
00272
00273 int delta = static_cast<int>(std::min(x.unknownSize(), x.cardMax() - x.glbSize()));
00274
00275
00276 GlbRanges<View> glb(x);
00277 int glbWeight = weightI<GlbRanges<View> >(elements, weights, glb);
00278
00279
00280
00281
00282
00283
00284 int lowWeight = glbWeight;
00285 for (int i=0; i<delta-1; i++) {
00286 if (currentWeights[i] >= 0)
00287 break;
00288 lowWeight+=currentWeights[i];
00289 }
00290
00291
00292
00293
00294 int lowestWeight = lowWeight;
00295 if (delta>0 && currentWeights[delta-1]<0)
00296 lowestWeight+=currentWeights[delta-1];
00297
00298
00299
00300
00301 if ( (x.cardMin() - x.glbSize() > 0 &&
00302 currentWeights[x.cardMin() - x.glbSize() - 1] >= 0) ||
00303 currentWeights[0] >= 0 ) {
00304 int lowestPosWeight = glbWeight;
00305 for (unsigned int i=0; i<x.cardMin() - x.glbSize(); i++) {
00306 lowestPosWeight += currentWeights[i];
00307 }
00308 lowestWeight = std::max(lowestWeight, lowestPosWeight);
00309 }
00310
00311
00312
00313
00314 int highestWeight = glbWeight;
00315 for (int i=0; i<delta; i++) {
00316 if (currentWeights[size-i-1]<=0)
00317 break;
00318 highestWeight += currentWeights[size-i-1];
00319 }
00320
00321
00322 GECODE_ME_CHECK(y.gq(home, lowestWeight));
00323 GECODE_ME_CHECK(y.lq(home, highestWeight));
00324
00325
00326
00327
00328 int remainingCapacity = y.max()-lowWeight;
00329
00330 UnknownRanges<View> ur2(x);
00331 Iter::Ranges::ToValues<UnknownRanges<View> > urv2(ur2);
00332 OverweightValues<Iter::Ranges::ToValues<UnknownRanges<View> > >
00333 ov(remainingCapacity, elements, weights, urv2);
00334 Iter::Values::ToRanges<OverweightValues<
00335 Iter::Ranges::ToValues<UnknownRanges<View> > > > ovr(ov);
00336 me = x.excludeI(home, ovr);
00337 GECODE_ME_CHECK(me);
00338 }
00339 if (x.assigned()) {
00340
00341 GlbRanges<View> glb(x);
00342 int w =
00343 weightI<GlbRanges<View> >(elements, weights, glb);
00344 GECODE_ME_CHECK(y.eq(home, w));
00345 return home.ES_SUBSUMED(*this);
00346 }
00347
00348 return me_modified(me) ? ES_NOFIX : ES_FIX;
00349 }
00350
00351 }}}
00352
00353