00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "cpsp/gecode/GC_ThreadingSpace.hh"
00025 #include "cpsp/gecode/GC_Search.hh"
00026 #include "cpsp/gecode/GC_StlSetRangeIterator.hh"
00027 #include "cpsp/gecode/GC_LatticeNeighbored2.hh"
00028 #include "cpsp/gecode/GC_ValNearCenter.hh"
00029 #include "cpsp/gecode/GC_RankViewSel.hh"
00030 #include "cpsp/gecode/GC_FirstSolBranching.hh"
00031
00032 #include <gecode/kernel.hh>
00033 #include <gecode/int.hh>
00034 #include <gecode/search.hh>
00035 #include <gecode/minimodel.hh>
00036 #include <gecode/int/branch.hh>
00037
00038 #include <biu/LatticeFrame.hh>
00039
00040 #ifdef TMPOUT
00041 #include <iostream>
00042 #endif
00043
00044 namespace cpsp
00045 {
00046 namespace gecode
00047 {
00048
00049
00050 void print(biu::IndexSet& x, const biu::LatticeFrame* latFrame) {
00051 std::cout<<"( ";
00052 for (biu::IndexSet::const_iterator it = x.begin(); it != x.end(); it++)
00053 std::cout <<latFrame->getPoint(*it) <<"| ";
00054 std::cout <<")";
00055 std::cout.flush();
00056 }
00057
00058
00059 void GC_ThreadingSpace::print() const {
00060 std::cerr<<"\n";
00061 for (int i=0; i< domains.size(); i++) {
00062 const Gecode::Int::IntView vd(domains[i]);
00063 Gecode::Int::ViewRanges< Gecode::Int::IntView > v(vd);
00064 std::cerr <<" " <<i <<"(";
00065 while(v()) {
00066 if (v.min() != v.max())
00067 std::cerr <<v.min() <<".." <<v.max()<<"," ;
00068 else
00069 std::cerr <<v.min() <<",";
00070 ++v;
00071 }
00072 std::cerr <<")\n";
00073 }
00074 std::cerr<<"\n";
00075 }
00076
00077 GC_ThreadingSpace::GC_ThreadingSpace(const std::string *sequence,
00078 const biu::LatticeFrame* latFrame,
00079 const biu::IndexVec* neighVecs,
00080 SeqFeatureMap* seqFeatureMap,
00081 const HullLevel *hullLvl,
00082 HCore* hCore,
00083 const GC_ThreadingSymmBreaker::GlobalShiftVec* shiftVec,
00084 int branchingType,
00085 NeighPropLvl neighPropLvl ) :
00086 domains( this, sequence->size(),
00087 0, Gecode::Limits::Int::int_max), rank(sequence->size(), 999)
00088 {
00089 assert(hCore != NULL);
00090
00091 biu::IndexSet data;
00092 GC_StlSetRangeIterator dataIt;
00093 Gecode::IntSet dataSet;
00094 Gecode::IntVarArgs hVars(hCore->getSize()), pVars(domains.size() - hCore->getSize());
00095 int hVarI = 0, pVarI = 0;
00096
00097
00098
00099 int i=0;
00100
00101 for (i=0; i<domains.size(); i++) {
00102
00103 data = hCore->getIndexedHull(latFrame, hullLvl->at(i));
00104
00105 dataIt.init(&data);
00106
00107 Gecode::Int::IntView actDom(domains[i]);
00108
00109 GECODE_ME_FAIL(this, actDom.inter(this,dataIt));
00110
00111 if (hullLvl->at(i) == 0) {
00112 hVars[hVarI++] = domains[i];
00113 } else {
00114 pVars[pVarI++] = domains[i];
00115 }
00116 }
00117
00118
00119 std::vector<unsigned int>::const_iterator it;
00120
00121 data = hCore->getIndexedPSinglets(latFrame);
00122
00123 dataIt.init(&data);
00124
00125 for ( it = (*seqFeatureMap)[P_SINGLET].begin();
00126 it != (*seqFeatureMap)[P_SINGLET].end(); it++)
00127 {
00128
00129 Gecode::Int::IntView actDom(domains[*it]);
00130
00131 GECODE_ME_FAIL(this, actDom.inter(this,dataIt));
00132
00133 rank[*it] = 0;
00134 }
00135
00136
00137
00138 data = hCore->getIndexedHSinglets(latFrame);
00139
00140 dataIt.init(&data);
00141
00142 for ( it = (*seqFeatureMap)[H_SINGLET].begin();
00143 it != (*seqFeatureMap)[H_SINGLET].end(); it++)
00144 {
00145
00146 Gecode::Int::IntView actDom(domains[*it]);
00147
00148 GECODE_ME_FAIL(this, actDom.inter(this,dataIt));
00149
00150 rank[*it] = 0;
00151 }
00152
00153
00154 {
00155 std::string seqPattern = "HHPP";
00156 std::string::size_type pos = 0, shift = 1;
00157 while( (pos = sequence->find(seqPattern,pos)) != std::string::npos) {
00158 pos += shift;
00159 rank [pos] = 1;
00160 }
00161 seqPattern = "PPHH";
00162 pos = 0, shift = 2;
00163 while( (pos = sequence->find(seqPattern,pos)) != std::string::npos) {
00164 pos += shift;
00165 rank [pos] = 1;
00166 }
00167 if (domains.size() > 1) {
00168 if ((*sequence)[0] != (*sequence)[1])
00169 rank[0] = 1;
00170 if ((*sequence)[sequence->size()-1] != (*sequence)[sequence->size()-2])
00171 rank[sequence->size()-1] = 1;
00172 }
00173 }
00174
00175
00176
00177
00178
00179
00180 Gecode::distinct(this, hVars, Gecode::ICL_DOM);
00181 Gecode::distinct(this, pVars, Gecode::ICL_DOM);
00182
00183
00184 switch(neighPropLvl) {
00185
00186 case CUSTOM_2 : {
00187 for(i=1; i<domains.size(); i++) {
00188 GECODE_ES_FAIL(this,
00189 GC_LatticeNeighbored2::post( this,
00190 domains[i-1], domains[i],
00191 neighVecs,
00192 Gecode::ICL_DOM,
00193 150
00194 ));
00195 }
00196 break; }
00197 case NO_PROP :
00198 default :
00199 break;
00200 }
00201
00202
00203 GC_ValNearCenter::latFrame = latFrame;
00204
00205
00206
00207 if ( branchingType != BR_NONE ) {
00208
00209 assertbiu( !(branchingType & BR_DDS) , "DDS branching currently not supported");
00210
00211
00212 Gecode::ViewArray<MyViewType> xv(this,domains.size());
00213 for (i=0; i< domains.size(); i++)
00214 xv[i] = IdxIntView(domains[i], i);
00215
00216
00217 if ( branchingType & BR_SYM ) {
00218
00219
00220 Gecode::BoolVarArray cpInit = GC_ThreadingSymmBreaker::initCpByCore(this, hCore, latFrame, shiftVec);
00221 GC_ThreadingSymmBreaker symmBreaker(this,latFrame, shiftVec, cpInit);
00222
00223 new (this)
00224 Gecode::Search::SBViewValBranching
00225
00226 <MyViewType,
00227 int,
00228
00229 GC_RankViewSel<Gecode::Int::Branch::ByDegreeMax>,
00230 GC_ValNearCenter,
00231 GC_ThreadingSymmBreaker>(this, xv,symmBreaker);
00232 }
00233
00234
00235 if ( branchingType & BR_DFS ) {
00236 typedef Gecode::ViewValBranching< MyViewType,
00237 int,
00238 GC_RankViewSel<Gecode::Int::Branch::ByDegreeMax>,
00239 GC_ValNearCenter
00240 > DFS;
00241
00242
00243 new (this) DFS(this, xv);
00244 }
00245 }
00246 }
00247
00248
00249
00250
00251
00252 GC_ThreadingSpace::GC_ThreadingSpace(bool share, GC_ThreadingSpace& toCopy):
00253 SuperSpace(share, toCopy), domains(), rank(toCopy.rank)
00254 {
00255 domains.update(this, share, toCopy.domains);
00256
00257 }
00258
00259
00260 GC_ThreadingSpace::~GC_ThreadingSpace()
00261 {
00262 }
00263
00264
00265 SuperSpace*
00266 GC_ThreadingSpace::copy(bool share) {
00267 return new GC_ThreadingSpace(share, *this);
00268 }
00269
00270 biu::IndexVec
00271 GC_ThreadingSpace::getSolution() const {
00272 biu::IndexVec retVec;
00273 for (int i=0; i<domains.size();i++) {
00274 if (domains[i].assigned()) {
00275 retVec.push_back((biu::LatticeFrame::index_type)domains[i].val());
00276 } else {
00277 std::cerr <<"Error: Variable "<<i<<" is not assigned !!! size = "<<domains[i].size()<<"\n";
00278 std::cerr <<" Are you using DDS ?\n";
00279 }
00280 }
00281 return retVec;
00282 }
00283
00284
00285 int
00286 GC_ThreadingSpace::getRank(int index) const {
00287 assert(index >= 0 && index < (int)rank.size());
00288 return rank[index];
00289 }
00290
00291
00292
00294
00296
00297
00298
00299 GC_ThreadingSpaceShapes::GC_ThreadingSpaceShapes(
00300 const std::string *sequence,
00301 const biu::LatticeFrame* latFrame,
00302 const biu::IndexVec* neighVecs,
00303 SeqFeatureMap* seqFeatureMap,
00304 const HullLevel *hullLvl,
00305 HCore* hCore,
00306 const GC_ThreadingSymmBreaker::GlobalShiftVec* shiftVec,
00307 int branchingType)
00308 : GC_ThreadingSpace( sequence, latFrame, neighVecs, seqFeatureMap,
00309 hullLvl, hCore, shiftVec, BR_NONE)
00310 {
00311
00312 if ( branchingType != BR_NONE ) {
00313
00314 assertbiu( !(branchingType & BR_DDS) , "DDS branching currently not supported");
00315
00316
00317 Gecode::ViewArray<MyViewType> xvH(this,hCore->getSize());
00318 Gecode::ViewArray<MyViewType> xvP(this,domains.size() - hCore->getSize());
00319
00320 unsigned int iH=0, iP=0;
00321 for (int i=0; i< domains.size(); i++) {
00322 if ((*sequence)[i] == 'H') {
00323 xvH[iH++] = IdxIntView(domains[i], i);
00324 } else {
00325 xvP[iP++] = IdxIntView(domains[i], i);
00326 }
00327 }
00328 if ( iH != hCore->getSize() || iP != domains.size()-hCore->getSize() )
00329 std::cerr <<" init failed: iH=" <<iH <<"/" <<hCore->getSize() <<" iP=" <<iP <<"/" <<(domains.size()-hCore->getSize())<<std::endl;
00330
00331
00332 if ( branchingType & BR_SYM ) {
00333
00334
00335 Gecode::BoolVarArray cpInit = GC_ThreadingSymmBreaker::initCpByCore(this, hCore, latFrame, shiftVec);
00336 GC_ThreadingSymmBreaker symmBreaker(this,latFrame, shiftVec, cpInit);
00337
00338 new (this)
00339 Gecode::Search::SBViewValBranching
00340
00341 <MyViewType,
00342 int,
00343
00344 GC_RankViewSel<Gecode::Int::Branch::ByDegreeMax>,
00345 GC_ValNearCenter,
00346 GC_ThreadingSymmBreaker>(this, xvH,symmBreaker);
00347 }
00348
00349
00350 if ( branchingType & BR_DFS ) {
00351 new (this) Gecode::ViewValBranching< MyViewType,
00352 int,
00353 GC_RankViewSel<Gecode::Int::Branch::ByDegreeMax>,
00354 GC_ValNearCenter
00355 >(this, xvH);
00356 }
00357
00358 new (this) FirstSolBranching< MyViewType,
00359 int,
00360 GC_RankViewSel<Gecode::Int::Branch::ByDegreeMax>,
00361 GC_ValNearCenter
00362 >( this, xvP );
00363 }
00364 }
00365
00366
00367
00368
00369 }
00370 }
00371
00372