00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef GC_THREADINGSPACE_HH_
00021 #define GC_THREADINGSPACE_HH_
00022
00023 #include <gecode/kernel.hh>
00024 #include <gecode/int.hh>
00025 #include <gecode/search.hh>
00026 #include <gecode/minimodel.hh>
00027
00028 #include <string>
00029 #include <vector>
00030
00031 #include "cpsp/HCore.hh"
00032 #include "cpsp/HPThreadingOptions.hh"
00033 #include "cpsp/gecode/GC_ThreadingSymmBreaker.hh"
00034 #include "cpsp/gecode/GC_StlSetRangeIterator.hh"
00035
00036 #include "cpsp/gecode/gecodeExtensions.hh"
00037
00038 namespace cpsp
00039 {
00040 namespace gecode
00041 {
00042
00043
00044 class IntDomSize {
00045 public:
00046 unsigned int operator()(const Gecode::Int::IntView& i) const {
00047 return i.size();
00048 }
00049 };
00050
00051 typedef Gecode::Space SuperSpace;
00052
00053
00056 enum BranchingType {
00057 BR_NONE = 0,
00058 BR_DFS = 1,
00059
00060 BR_DDS = 2,
00061 BR_SYM = 4
00062 };
00063
00064
00067 class GC_ThreadingSpace : public SuperSpace
00068 {
00069 public:
00071 enum SeqFeatures{ P_SINGLET, H_SINGLET };
00072
00073 typedef std::map< SeqFeatures, std::vector <unsigned int> > SeqFeatureMap;
00074
00075 typedef std::vector<unsigned int> HullLevel;
00076
00078 enum NeighPropLvl{
00079 CUSTOM_2,
00080 NO_PROP
00081 };
00082
00083
00084 protected:
00085
00087 Gecode::IntVarArray domains;
00088
00089 std::vector<int> rank;
00090
00091 public:
00092 GC_ThreadingSpace(const std::string *sequence,
00093 const biu::LatticeFrame* latFrame,
00094 const biu::IndexVec* neighVecs,
00095 SeqFeatureMap* seqFeatureMap,
00096 const HullLevel *hullLvl,
00097 HCore* hCore,
00098 const GC_ThreadingSymmBreaker::GlobalShiftVec* shiftVec,
00099 int branchingType = BR_DFS|BR_SYM,
00100 NeighPropLvl neighPropLvl = CUSTOM_2);
00101 GC_ThreadingSpace(bool share, GC_ThreadingSpace& toCopy);
00102 virtual ~GC_ThreadingSpace();
00103
00104 virtual SuperSpace* copy(bool share);
00105
00108 virtual biu::IndexVec getSolution() const ;
00109 virtual void print() const;
00110
00111 int getRank(int index) const;
00112
00113 virtual void handleSolution(GC_ThreadingSpace* solution)
00114 {}
00115
00116
00117
00118
00119 virtual
00120 int getIndex(const Gecode::Int::IntView &v) const {
00121 for (int i=0;i <domains.size(); i++) {
00122 Gecode::Int::IntView vd(domains[i]);
00123 if (vd.variable() == v.variable())
00124 return i;
00125 }
00126 return -1;
00127 }
00128 virtual
00129 int getIndex(const Gecode::VarBase *vb) const
00130 {
00131 for (int i=0;i <domains.size(); i++) {
00132 if (domains[i].variable() == vb)
00133 return i;
00134 }
00135 return -1;
00136 }
00137 };
00138
00139
00140
00141
00142
00145 class GC_ThreadingSpacePosDist : public GC_ThreadingSpace {
00146 public:
00147
00148 typedef std::vector<Gecode::IntArgs> SolutionVec;
00149
00151 SolutionVec* solPositions;
00152
00153 protected:
00156 unsigned int maxEqual;
00157
00158 public:
00159
00160 GC_ThreadingSpacePosDist(const std::string *sequence,
00161 const biu::LatticeFrame* latFrame,
00162 const biu::IndexVec* neighVecs,
00163 SeqFeatureMap* seqFeatureMap,
00164 const HullLevel *hullLvl,
00165 HCore* hCore,
00166 const GC_ThreadingSymmBreaker::GlobalShiftVec* shiftVec,
00167 int branchingType = BR_DFS|BR_SYM,
00168 SolutionVec* solPositions_ = NULL,
00169 unsigned int maxEqual_ = 0)
00170 : GC_ThreadingSpace( sequence, latFrame, neighVecs, seqFeatureMap,
00171 hullLvl, hCore, shiftVec, branchingType),
00172 solPositions(solPositions_), maxEqual(maxEqual_)
00173 {
00174
00175
00176 constrain(this);
00177 }
00178
00179 GC_ThreadingSpacePosDist(bool share, GC_ThreadingSpacePosDist& toCopy)
00180 : GC_ThreadingSpace(share, toCopy),
00181 solPositions(toCopy.solPositions),
00182 maxEqual(toCopy.maxEqual)
00183 {}
00184
00185 virtual ~GC_ThreadingSpacePosDist()
00186 {}
00187
00188 virtual SuperSpace* copy(bool share)
00189 { return new GC_ThreadingSpacePosDist(share,*this); }
00190
00193 virtual void constrain(GC_ThreadingSpacePosDist* lastSolution)
00194 {
00195
00196 for(SolutionVec::iterator sol = solPositions->begin();
00197 sol != solPositions->end(); sol++)
00198 {
00199 Gecode::atmost(this, domains, (*sol), maxEqual, Gecode::ICL_DOM);
00200 }
00201 }
00204 virtual void handleSolution(GC_ThreadingSpacePosDist* solution)
00205 {
00206
00207 assert(solPositions != NULL);
00208 solPositions->push_back(Gecode::IntArgs(domains.size()));
00209 Gecode::IntArgs& y = *(solPositions->rbegin());
00210 for (int i = y.size(); i--; )
00211 y[i] = solution->domains[i].val();
00212 }
00213
00214 };
00215
00216
00219 class GC_ThreadingSpaceMoveDist : public GC_ThreadingSpace {
00220 public:
00221
00222 typedef std::vector<Gecode::IntArgs> SolutionVec;
00223
00225 SolutionVec* solMoves;
00226
00227 protected:
00230 unsigned int maxEqual;
00231
00233 Gecode::IntVarArray absMoves;
00234
00236 using GC_ThreadingSpace::domains;
00237
00238 public:
00239
00240 GC_ThreadingSpaceMoveDist(const std::string *sequence,
00241 const biu::LatticeFrame* latFrame,
00242 const biu::IndexVec* neighVecs,
00243 SeqFeatureMap* seqFeatureMap,
00244 const HullLevel *hullLvl,
00245 HCore* hCore,
00246 const GC_ThreadingSymmBreaker::GlobalShiftVec* shiftVec,
00247 int branchingType = BR_DFS|BR_SYM,
00248 SolutionVec* solMoves_ = NULL,
00249 unsigned int maxEqual_ = 0)
00250 : GC_ThreadingSpace( sequence, latFrame, neighVecs, seqFeatureMap,
00251 hullLvl, hCore, shiftVec, branchingType, NO_PROP),
00252 solMoves(solMoves_), maxEqual(maxEqual_),
00253 absMoves(this,domains.size()-1)
00254
00255 {
00256 std::set<int> tmp;
00257 assert(neighVecs != NULL);
00258 for(biu::IndexVec::const_iterator it = neighVecs->begin();
00259 it != neighVecs->end(); it++)
00260 tmp.insert(*it);
00261 int neighMin = *(tmp.begin()), neighMax = *(tmp.rbegin());
00262
00263 using namespace Gecode;
00264
00265 for (int i=absMoves.size(); i--; ) {
00266
00267 absMoves[i] = Gecode::IntVar(this, neighMin, neighMax);
00268
00269 GC_StlSetRangeIterator it(&tmp);
00270 Gecode::Int::IntView(absMoves[i]).narrow<GC_StlSetRangeIterator>(this, it);
00271
00272 post(this, (domains[i+1]-domains[i]) == absMoves[i], Gecode::ICL_DOM);
00273 }
00274
00275
00276 constrain(this);
00277 }
00278
00279 GC_ThreadingSpaceMoveDist(bool share, GC_ThreadingSpaceMoveDist& toCopy)
00280 : GC_ThreadingSpace(share, toCopy),
00281 solMoves(toCopy.solMoves),
00282 maxEqual(toCopy.maxEqual), absMoves()
00283 {
00284 absMoves.update(this, share, toCopy.absMoves);
00285 }
00286
00287 virtual ~GC_ThreadingSpaceMoveDist()
00288 {}
00289
00290 virtual SuperSpace* copy(bool share)
00291 { return new GC_ThreadingSpaceMoveDist(share,*this); }
00292
00295 virtual void constrain(GC_ThreadingSpaceMoveDist* lastSolution)
00296 {
00297
00298 for(SolutionVec::iterator sol = solMoves->begin();
00299 sol != solMoves->end(); sol++)
00300 {
00301 Gecode::atmost(this, absMoves, (*sol), maxEqual, Gecode::ICL_DOM);
00302 }
00303 }
00306 virtual void handleSolution(GC_ThreadingSpaceMoveDist* solution)
00307 {
00308
00309
00310
00311 assert(solMoves != NULL );
00312
00313 solMoves->push_back(Gecode::IntArgs(absMoves.size()));
00314 Gecode::IntArgs& y = *(solMoves->rbegin());
00315 for (int i = y.size(); i--; ) {
00316 y[i] = solution->absMoves[i].val();
00317 }
00318
00319 }
00320
00321 };
00322
00323
00324
00328 class GC_ThreadingSpaceShapes : public GC_ThreadingSpace {
00329
00330 protected:
00331
00332 using GC_ThreadingSpace::domains;
00333
00334 public:
00335
00336 GC_ThreadingSpaceShapes(const std::string *sequence,
00337 const biu::LatticeFrame* latFrame,
00338 const biu::IndexVec* neighVecs,
00339 SeqFeatureMap* seqFeatureMap,
00340 const HullLevel *hullLvl,
00341 HCore* hCore,
00342 const GC_ThreadingSymmBreaker::GlobalShiftVec* shiftVec,
00343 int branchingType = BR_DFS|BR_SYM);
00344
00345 GC_ThreadingSpaceShapes(bool share, GC_ThreadingSpaceShapes& toCopy)
00346 : GC_ThreadingSpace(share, toCopy)
00347 {}
00348
00349 virtual ~GC_ThreadingSpaceShapes()
00350 {}
00351
00352 virtual SuperSpace* copy(bool share)
00353 { return new GC_ThreadingSpaceShapes(share,*this); }
00354
00355 };
00356
00357
00358
00359 }
00360 }
00361
00362 #endif