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