00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "cpsp/gecode/GC_Neighbored2.hh"
00021 #include "cpsp/gecode/GC_StlSetRangeIterator.hh"
00022
00023 #include <biu/assertbiu.hh>
00024 namespace cpsp
00025 {
00026 namespace gecode
00027 {
00028
00029
00030 const unsigned int GC_Neighbored2::MAXPROPSIZEINIT = 50;
00031
00032
00033 GC_Neighbored2::GC_Neighbored2( Gecode::Space* home,
00034 Gecode::Int::IntView x0,
00035 Gecode::Int::IntView x1,
00036 const biu::LatticeFrame* lattice_,
00037 Gecode::IntConLevel conLvl,
00038 unsigned int maxPrSize,
00039 bool maxPrSizeBin,
00040 unsigned int minPrSize)
00041 : GC_BinProp(home,x0,x1),
00042 lattice(lattice_), conLevel(conLvl), maxPropSize(maxPrSize),
00043 maxPropSizeBin(maxPrSizeBin), minPropSize(minPrSize)
00044 {
00045 assertbiu(lattice != NULL, "static lattice variable not initialised");
00046 neighborhood = lattice->getIndexedNeighborhood();
00047
00048
00049 switch (conLevel) {
00050 case Gecode::ICL_VAL : maxPropSize = 1; break;
00051 case Gecode::ICL_DOM : maxPropSize = UINT_MAX; break;
00052 case Gecode::ICL_DEF : conLevel = Gecode::ICL_BND;
00053 case Gecode::ICL_BND :
00054 default : ;
00055 }
00056 }
00057
00058 GC_Neighbored2::GC_Neighbored2( Gecode::Space* home,
00059 bool share,
00060 GC_Neighbored2& p) :
00061 GC_BinProp(home,share,p),
00062 lattice(p.lattice),
00063 neighborhood(p.neighborhood),
00064 conLevel(p.conLevel),
00065 maxPropSize(p.maxPropSize),
00066 maxPropSizeBin(p.maxPropSizeBin),
00067 minPropSize(p.minPropSize)
00068 {}
00069
00070 Gecode::ExecStatus
00071 GC_Neighbored2::post( Gecode::Space* home,
00072 Gecode::Int::IntView x0, Gecode::Int::IntView x1,
00073 const biu::LatticeFrame* lattice,
00074 Gecode::IntConLevel conLvl,
00075 unsigned int maxPrSize,
00076 bool maxPrSizeBin,
00077 unsigned int minPrSize)
00078 {
00079 if (same(x0,x1)) {
00080 return Gecode::ES_FAILED;
00081 } else {
00082 (void) new (home) GC_Neighbored2( home,x0,x1,lattice, conLvl,
00083 maxPrSize, maxPrSizeBin, minPrSize);
00084 }
00085 return Gecode::ES_OK;
00086 }
00087
00088
00089 Gecode::Actor*
00090 GC_Neighbored2::copy(Gecode::Space* home, bool share) {
00091 return new (home) GC_Neighbored2(home,share,*this);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 Gecode::PropCost
00112 GC_Neighbored2::cost(void) const {
00113 if (x0.assigned() || x1.assigned()) {
00114 return Gecode::PC_LINEAR_HI;
00115 }
00116 return Gecode::PC_QUADRATIC_HI;
00117 }
00118
00119 Gecode::ExecStatus
00120 GC_Neighbored2::propagate(Gecode::Space* home) {
00121
00122
00123 if (x0.size() < minPropSize && x1.size() < minPropSize)
00124 return Gecode::ES_SUBSUMED;
00125
00126 Gecode::ModEvent ret = Gecode::ME_GEN_NONE;
00127 if (x0.assigned()) {
00128 ret = removeNonNeighbors(home,x1,x0.val());
00129 }else if (x1.assigned()) {
00130 ret = removeNonNeighbors(home,x0,x1.val());
00131 } else {
00132 ret = removeNonNeighbors(home);
00133 }
00134
00135
00136 GECODE_ME_CHECK(ret);
00137
00138 return (ret == Gecode::ME_GEN_ASSIGNED
00139 ? Gecode::ES_SUBSUMED
00140 : Gecode::ES_FIX);
00141 }
00142
00143 Gecode::ModEvent
00144 GC_Neighbored2::removeNonNeighbors( Gecode::Space* home,
00145 Gecode::Int::IntView& x,
00146 int center)
00147 {
00148 biu::IndexSet data = getIndexedNeighbors(center);
00149 GC_StlSetRangeIterator dataIt(&data);
00150 return x.inter(home, dataIt);
00151 }
00152
00153 Gecode::ModEvent
00154 GC_Neighbored2::removeNonNeighbors(Gecode::Space* home) {
00155
00156
00157
00158 Gecode::Int::IntView* a = &x0, *b = &x1;
00159 if(a->size() > b->size()) {
00160 a = &x1, b = &x0;
00161 }
00162
00163
00164
00165
00166 if ( a->size() > maxPropSize
00167 || (maxPropSizeBin && b->size() > maxPropSize) )
00168 {
00169 return Gecode::ME_GEN_NONE;
00170 }
00171
00172
00173 unsigned int aSize = a->size(), bSize = b->size();
00174
00175
00176 Gecode::ModEvent aStat = Gecode::ME_GEN_NONE,
00177 bStat = Gecode::ME_GEN_NONE;
00178
00179
00180 Gecode::Int::ViewValues<Gecode::Int::IntView> aIt(*a);
00181 biu::IndexSet newDom, actNeighs, toRem;
00182 biu::IndexSet::const_iterator neigh;
00183 bool noNeighInB = true;
00184 while(aIt()) {
00185
00186 actNeighs = getIndexedNeighbors(aIt.val());
00187 noNeighInB = true;
00188
00189 for(neigh = actNeighs.begin(); neigh != actNeighs.end(); neigh++) {
00190 if (b->in(*neigh)) {
00191 noNeighInB = false;
00192 newDom.insert(*neigh);
00193 }
00194 }
00195
00196 if (noNeighInB) {
00197
00198 toRem.insert(aIt.val());
00199 }
00200
00201 ++aIt;
00202 }
00203
00204 GC_StlSetRangeIterator remDomIt(&toRem);
00205 aStat = a->minus(home,remDomIt);
00206
00207
00208 if (aStat != Gecode::ME_GEN_FAILED) {
00209 GC_StlSetRangeIterator newDomIt(&newDom);
00210 bStat = b->inter(home,newDomIt);
00211 } else {
00212 return Gecode::ME_GEN_FAILED;
00213 }
00214
00215
00216 if ( bStat == Gecode::ME_GEN_FAILED ) {
00217 return Gecode::ME_GEN_FAILED;
00218 }
00219 if ( aSize == a->size() && bSize == b->size())
00220 return Gecode::ME_GEN_NONE;
00221 if ( aStat == Gecode::ME_GEN_ASSIGNED
00222 && bStat == Gecode::ME_GEN_ASSIGNED)
00223 return Gecode::ME_GEN_ASSIGNED;
00224 return Gecode::Int::ME_INT_DOM;
00225 }
00226
00227 biu::IndexSet
00228 GC_Neighbored2::getIndexedNeighbors(
00229 const biu::LatticeFrame::index_type center) const
00230 {
00231 biu::IndexSet retSet;
00232
00233 for(biu::IndexVec::const_iterator it = neighborhood.begin();
00234 it != neighborhood.end(); it++) {
00235 retSet.insert(center+(*it));
00236 }
00237 return retSet;
00238 }
00239
00240 }
00241 }