this repo has no description
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 * 7 * Contributing authors: 8 * Gabor Szokoli <szokoli@gecode.org> 9 * 10 * Copyright: 11 * Guido Tack, 2004 12 * Christian Schulte, 2004 13 * Gabor Szokoli, 2004 14 * 15 * This file is part of Gecode, the generic constraint 16 * development environment: 17 * http://www.gecode.org 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining 20 * a copy of this software and associated documentation files (the 21 * "Software"), to deal in the Software without restriction, including 22 * without limitation the rights to use, copy, modify, merge, publish, 23 * distribute, sublicense, and/or sell copies of the Software, and to 24 * permit persons to whom the Software is furnished to do so, subject to 25 * the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be 28 * included in all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37 * 38 */ 39 40namespace Gecode { namespace Set { namespace RelOp { 41 42 /* 43 * "Nary partition" propagator 44 * 45 */ 46 47 template<class View0, class View1> 48 forceinline 49 PartitionN<View0,View1>::PartitionN(Home home, ViewArray<View0>& x, View1 y) 50 : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home, x, y) { 51 shared = Gecode::shared(x) || viewarrayshared(x,y); 52 } 53 54 template<class View0, class View1> 55 forceinline 56 PartitionN<View0,View1>::PartitionN(Home home, ViewArray<View0>& x, 57 const IntSet& z, View1 y) 58 : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home, x, y) { 59 shared = Gecode::shared(x) || viewarrayshared(x,y); 60 IntSetRanges rz(z); 61 unionOfDets.includeI(home, rz); 62 } 63 64 template<class View0, class View1> 65 forceinline 66 PartitionN<View0,View1>::PartitionN(Space& home, PartitionN& p) 67 : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,p), 68 shared(p.shared) { 69 unionOfDets.update(home,p.unionOfDets); 70 } 71 72 template<class View0, class View1> 73 Actor* 74 PartitionN<View0,View1>::copy(Space& home) { 75 return new (home) PartitionN(home,*this); 76 } 77 78 template<class View0, class View1> 79 ExecStatus PartitionN<View0,View1>::post(Home home, ViewArray<View0>& x, 80 View1 y) { 81 switch (x.size()) { 82 case 0: 83 GECODE_ME_CHECK(y.cardMax(home, 0)); 84 return ES_OK; 85 case 1: 86 return Rel::Eq<View0,View1>::post(home, x[0], y); 87 default: 88 (void) new (home) PartitionN<View0,View1>(home,x,y); 89 return ES_OK; 90 } 91 } 92 93 template<class View0, class View1> 94 ExecStatus PartitionN<View0,View1>::post(Home home, ViewArray<View0>& x, 95 const IntSet& z, View1 y) { 96 (void) new (home) PartitionN<View0,View1>(home,x,z,y); 97 return ES_OK; 98 } 99 100 template<class View0, class View1> 101 PropCost PartitionN<View0,View1>::cost(const Space&, const ModEventDelta&) const { 102 return PropCost::quadratic(PropCost::LO, x.size()+1); 103 } 104 105 template<class View0, class View1> 106 ExecStatus 107 PartitionN<View0,View1>::propagate(Space& home, const ModEventDelta& med) { 108 109 ModEvent me0 = View0::me(med); 110 ModEvent me1 = View1::me(med); 111 bool ubevent = Rel::testSetEventUB(me0, me1); 112 bool lbevent = Rel::testSetEventLB(me0, me1); 113 bool anybevent = Rel::testSetEventAnyB(me0, me1); 114 bool cardevent = Rel::testSetEventCard(me0, me1); 115 116 bool modified = false; 117 bool oldModified = false; 118 119 do { 120 oldModified = modified; 121 modified = false; 122 if (oldModified || anybevent) 123 GECODE_ES_CHECK(partitionNXiUB(home,modified, x, y,unionOfDets)); 124 if (modified || oldModified || anybevent) 125 GECODE_ES_CHECK(partitionNXiLB(home,modified, x, y,unionOfDets)); 126 if (modified || oldModified || ubevent) 127 GECODE_ES_CHECK(partitionNYUB(home,modified, x, y,unionOfDets)); 128 if (modified || oldModified || lbevent) 129 GECODE_ES_CHECK(partitionNYLB(home,modified, x, y,unionOfDets)); 130 if (modified || oldModified || ubevent) 131 GECODE_ES_CHECK(unionNXiUB(home,modified, x, y,unionOfDets)); 132 if (modified || oldModified || cardevent) 133 GECODE_ES_CHECK(partitionNCard(home,modified, x, y,unionOfDets)); 134 } while (modified); 135 136 //removing assigned sets from x, accumulating the value: 137 for(int i=0;i<x.size();i++) { 138 //Do not reverse! Eats away the end of the array! 139 while (i<x.size() && x[i].assigned()) { 140 GlbRanges<View0> det(x[i]); 141 unionOfDets.includeI(home,det); 142 x.move_lst(i); 143 } 144 } 145 // When we run out of variables, make a final check and disolve: 146 if (x.size()==0) { 147 BndSetRanges all1(unionOfDets); 148 GECODE_ME_CHECK( y.intersectI(home,all1) ); 149 BndSetRanges all2(unionOfDets); 150 GECODE_ME_CHECK( y.includeI(home,all2) ); 151 unionOfDets.dispose(home); 152 return home.ES_SUBSUMED(*this); 153 } 154 155 return shared ? ES_NOFIX : ES_FIX; 156 } 157 158}}} 159 160// STATISTICS: set-prop