Generated on Thu Apr 11 13:59:08 2019 for Gecode by doxygen 1.6.3

int.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2005
00009  *     Mikael Lagerkvist, 2006
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 namespace Test { namespace Int {
00037 
00038   /*
00039    * Assignments
00040    *
00041    */
00042   inline
00043   Assignment::Assignment(int n0, const Gecode::IntSet& d0)
00044     : n(n0), d(d0) {}
00045   inline int
00046   Assignment::size(void) const {
00047     return n;
00048   }
00049   inline
00050   Assignment::~Assignment(void) {}
00051 
00052 
00053   inline
00054   CpltAssignment::CpltAssignment(int n, const Gecode::IntSet& d)
00055     : Assignment(n,d),
00056       dsv(new Gecode::IntSetValues[static_cast<unsigned int>(n)]) {
00057     for (int i=n; i--; )
00058       dsv[i].init(d);
00059   }
00060   inline bool
00061   CpltAssignment::operator()(void) const {
00062     return dsv[0]();
00063   }
00064   inline int
00065   CpltAssignment::operator[](int i) const {
00066     assert((i>=0) && (i<n));
00067     return dsv[i].val();
00068   }
00069   inline
00070   CpltAssignment::~CpltAssignment(void) {
00071     delete [] dsv;
00072   }
00073 
00074 
00075   forceinline int
00076   RandomAssignment::randval(void) {
00077     unsigned int skip = Base::rand(d.size());
00078     for (Gecode::IntSetRanges it(d); true; ++it) {
00079       if (it.width() > skip)
00080         return it.min() + static_cast<int>(skip);
00081       skip -= it.width();
00082     }
00083     GECODE_NEVER;
00084     return 0;
00085   }
00086 
00087   inline
00088   RandomAssignment::RandomAssignment(int n, const Gecode::IntSet& d, int a0)
00089     : Assignment(n,d), vals(new int[static_cast<size_t>(n)]), a(a0) {
00090     for (int i=n; i--; )
00091       vals[i] = randval();
00092   }
00093 
00094   inline bool
00095   RandomAssignment::operator()(void) const {
00096     return a>0;
00097   }
00098   inline int
00099   RandomAssignment::operator[](int i) const {
00100     assert((i>=0) && (i<n));
00101     return vals[i];
00102   }
00103   inline
00104   RandomAssignment::~RandomAssignment(void) {
00105     delete [] vals;
00106   }
00107 
00108   forceinline int
00109   RandomMixAssignment::randval(const Gecode::IntSet& d) {
00110     unsigned int skip = Base::rand(d.size());
00111     for (Gecode::IntSetRanges it(d); true; ++it) {
00112       if (it.width() > skip)
00113         return it.min() + static_cast<int>(skip);
00114       skip -= it.width();
00115     }
00116     GECODE_NEVER;
00117     return 0;
00118   }
00119 
00120   inline
00121   RandomMixAssignment::RandomMixAssignment(int n0, const Gecode::IntSet& d0,
00122                                            int n1, const Gecode::IntSet& d1,
00123                                            int a0)
00124     : Assignment(n0+n1,d0),vals(new int[static_cast<size_t>(n0+n1)]),
00125       a(a0),_n1(n1),_d1(d1) {
00126     for (int i=n0; i--; )
00127       vals[i] = randval(d);
00128     for (int i=n1; i--; )
00129       vals[n0+i] = randval(_d1);
00130   }
00131 
00132   inline bool
00133   RandomMixAssignment::operator()(void) const {
00134     return a>0;
00135   }
00136 
00137   inline int
00138   RandomMixAssignment::operator[](int i) const {
00139     assert((i>=0) && (i<n));
00140     return vals[i];
00141   }
00142 
00143   inline
00144   RandomMixAssignment::~RandomMixAssignment(void) {
00145     delete [] vals;
00146   }
00147 
00148   /*
00149    * Tests with integer constraints
00150    *
00151    */
00152   forceinline bool
00153   Test::eqv(void) const {
00154     return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
00155   }
00156   forceinline bool
00157   Test::imp(void) const {
00158     return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
00159   }
00160   forceinline bool
00161   Test::pmi(void) const {
00162     return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
00163   }
00164   inline
00165   Test::Test(const std::string& p, const std::string& s,
00166              int a, const Gecode::IntSet& d, bool r,
00167              Gecode::IntPropLevel i)
00168     : Base(p+s), arity(a), dom(d),
00169       reified(r), rms((1 << Gecode::RM_EQV) |
00170                       (1 << Gecode::RM_IMP) |
00171                       (1 << Gecode::RM_PMI)),
00172       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00173       testsearch(true), testfix(true) {}
00174 
00175   inline
00176   Test::Test(const std::string& s,
00177              int a, const Gecode::IntSet& d, bool r,
00178              Gecode::IntPropLevel i)
00179     : Base("Int::"+s), arity(a), dom(d),
00180       reified(r), rms((1 << Gecode::RM_EQV) |
00181                       (1 << Gecode::RM_IMP) |
00182                       (1 << Gecode::RM_PMI)),
00183       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00184       testsearch(true), testfix(true) {}
00185 
00186   inline
00187   Test::Test(const std::string& p, const std::string& s,
00188              int a, int min, int max, bool r,
00189              Gecode::IntPropLevel i)
00190     : Base(p+s), arity(a), dom(min,max),
00191       reified(r), rms((1 << Gecode::RM_EQV) |
00192                       (1 << Gecode::RM_IMP) |
00193                       (1 << Gecode::RM_PMI)),
00194       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00195       testsearch(true), testfix(true) {}
00196 
00197   inline
00198   Test::Test(const std::string& s,
00199              int a, int min, int max, bool r, Gecode::IntPropLevel i)
00200     : Base("Int::"+s), arity(a), dom(min,max),
00201       reified(r), rms((1 << Gecode::RM_EQV) |
00202                       (1 << Gecode::RM_IMP) |
00203                       (1 << Gecode::RM_PMI)),
00204       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00205       testsearch(true), testfix(true) {}
00206 
00207   inline
00208   std::string
00209   Test::str(Gecode::IntPropLevel ipl) {
00210     using namespace Gecode;
00211     std::stringstream s;
00212     switch (vbd(ipl)) {
00213     case IPL_VAL: s << "Val"; break;
00214     case IPL_BND: s << "Bnd"; break;
00215     case IPL_DOM: s << "Dom"; break;
00216     default: s << "Def"; break;
00217     }
00218     if (ipl & IPL_BASIC) s << "+B";
00219     if (ipl & IPL_ADVANCED) s << "+A";
00220     return s.str();
00221   }
00222 
00223   inline
00224   std::string
00225   Test::str(Gecode::IntRelType irt) {
00226     using namespace Gecode;
00227     switch (irt) {
00228     case IRT_LQ: return "Lq";
00229     case IRT_LE: return "Le";
00230     case IRT_GQ: return "Gq";
00231     case IRT_GR: return "Gr";
00232     case IRT_EQ: return "Eq";
00233     case IRT_NQ: return "Nq";
00234     default: ;
00235     }
00236     GECODE_NEVER;
00237     return "NONE";
00238   }
00239 
00240   inline std::string
00241   Test::str(Gecode::BoolOpType bot) {
00242     using namespace Gecode;
00243     switch (bot) {
00244     case BOT_AND: return "And";
00245     case BOT_OR:  return "Or";
00246     case BOT_IMP: return "Imp";
00247     case BOT_EQV: return "Eqv";
00248     case BOT_XOR: return "Xor";
00249     default: GECODE_NEVER;
00250     }
00251     GECODE_NEVER;
00252     return "NONE";
00253   }
00254 
00255   inline
00256   std::string
00257   Test::str(bool b) {
00258     std::stringstream s;
00259     if (b)
00260       s << "+";
00261     else 
00262       s << "-";
00263     return s.str();
00264   }
00265 
00266   inline
00267   std::string
00268   Test::str(int i) {
00269     std::stringstream s;
00270     s << i;
00271     return s.str();
00272   }
00273 
00274   inline
00275   std::string
00276   Test::str(const Gecode::IntArgs& x) {
00277     std::string s = "";
00278     for (int i=0; i<x.size()-1; i++)
00279       s += str(x[i]) + ",";
00280     return "[" + s + str(x[x.size()-1]) + "]";
00281   }
00282 
00283   template<class T>
00284   inline bool
00285   Test::cmp(T x, Gecode::IntRelType r, T y) {
00286     using namespace Gecode;
00287     switch (r) {
00288     case IRT_EQ: return x == y;
00289     case IRT_NQ: return x != y;
00290     case IRT_LQ: return x <= y;
00291     case IRT_LE: return x < y;
00292     case IRT_GR: return x > y;
00293     case IRT_GQ: return x >= y;
00294     default: ;
00295     }
00296     return false;
00297   }
00298 
00299 
00300 
00301   inline
00302   IntPropLevels::IntPropLevels(void)
00303     : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
00304   inline bool
00305   IntPropLevels::operator()(void) const {
00306     return i>=0;
00307   }
00308   inline void
00309   IntPropLevels::operator++(void) {
00310     i--;
00311   }
00312   inline Gecode::IntPropLevel
00313   IntPropLevels::ipl(void) const {
00314     return ipls[i];
00315   }
00316 
00317 
00318   inline
00319   IntPropBasicAdvanced::IntPropBasicAdvanced(void)
00320     : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
00321   inline bool
00322   IntPropBasicAdvanced::operator()(void) const {
00323     return i>=0;
00324   }
00325   inline void
00326   IntPropBasicAdvanced::operator++(void) {
00327     i--;
00328   }
00329   inline Gecode::IntPropLevel
00330   IntPropBasicAdvanced::ipl(void) const {
00331     return ipls[i];
00332   }
00333 
00334 
00335   inline
00336   IntRelTypes::IntRelTypes(void)
00337     : i(sizeof(irts)/sizeof(Gecode::IntRelType)-1) {}
00338   inline void
00339   IntRelTypes::reset(void) {
00340     i = sizeof(irts)/sizeof(Gecode::IntRelType)-1;
00341   }
00342   inline bool
00343   IntRelTypes::operator()(void) const {
00344     return i>=0;
00345   }
00346   inline void
00347   IntRelTypes::operator++(void) {
00348     i--;
00349   }
00350   inline Gecode::IntRelType
00351   IntRelTypes::irt(void) const {
00352     return irts[i];
00353   }
00354 
00355   inline
00356   BoolOpTypes::BoolOpTypes(void)
00357     : i(sizeof(bots)/sizeof(Gecode::BoolOpType)-1) {}
00358   inline bool
00359   BoolOpTypes::operator()(void) const {
00360     return i>=0;
00361   }
00362   inline void
00363   BoolOpTypes::operator++(void) {
00364     i--;
00365   }
00366   inline Gecode::BoolOpType
00367   BoolOpTypes::bot(void) const {
00368     return bots[i];
00369   }
00370 
00371 }}
00372 
00373 // STATISTICS: test-int
00374