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

assign.cpp

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  *
00006  *  Contributing authors:
00007  *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
00008  *
00009  *  Copyright:
00010  *     Christian Schulte, 2008
00011  *     Vincent Barichard, 2012
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "test/assign.hh"
00039 
00040 #include <gecode/search.hh>
00041 
00042 namespace Test { namespace Assign {
00043 
00045   class IntTestSpace : public Gecode::Space {
00046   public:
00048     Gecode::IntVarArray x;
00050     IntTestSpace(int n, Gecode::IntSet& d)
00051       : x(*this, n, d) {}
00053     IntTestSpace(IntTestSpace& s)
00054       : Gecode::Space(s) {
00055       x.update(*this, s.x);
00056     }
00058     virtual Gecode::Space* copy(void) {
00059       return new IntTestSpace(*this);
00060     }
00061   };
00062 
00064   class BoolTestSpace : public Gecode::Space {
00065   public:
00067     Gecode::BoolVarArray x;
00069     BoolTestSpace(int n)
00070       : x(*this, n, 0, 1) {}
00072     BoolTestSpace(BoolTestSpace& s)
00073       : Gecode::Space(s) {
00074       x.update(*this, s.x);
00075     }
00077     virtual Gecode::Space* copy(void) {
00078       return new BoolTestSpace(*this);
00079     }
00080   };
00081 
00082 #ifdef GECODE_HAS_SET_VARS
00083 
00085   class SetTestSpace : public Gecode::Space {
00086   public:
00088     Gecode::SetVarArray x;
00090     SetTestSpace(int n, const Gecode::IntSet& d)
00091       : x(*this, n, Gecode::IntSet::empty, d) {}
00093     SetTestSpace(SetTestSpace& s)
00094       : Gecode::Space(s) {
00095       x.update(*this, s.x);
00096     }
00098     virtual Gecode::Space* copy(void) {
00099       return new SetTestSpace(*this);
00100     }
00101   };
00102 
00103 #endif
00104 
00105 #ifdef GECODE_HAS_FLOAT_VARS
00106 
00108   class FloatTestSpace : public Gecode::Space {
00109   public:
00111     Gecode::FloatVarArray x;
00113     FloatTestSpace(int n, const Gecode::FloatVal& d)
00114       : x(*this, n, d.min(), d.max()) {}
00116     FloatTestSpace(FloatTestSpace& s)
00117       : Gecode::Space(s) {
00118       x.update(*this, s.x);
00119     }
00121     virtual Gecode::Space* copy(void) {
00122       return new FloatTestSpace(*this);
00123     }
00124   };
00125 
00126 #endif
00127 
00133 
00134   const char* int_assign_name[] = {
00135     "INT_ASSIGN_MIN",
00136     "INT_ASSIGN_MED",
00137     "INT_ASSIGN_MAX",
00138     "INT_ASSIGN_RND",
00139     "INT_ASSIGN"
00140   };
00142   const int n_int_assign =
00143     sizeof(int_assign_name)/sizeof(const char*);
00145   int int_val(const Gecode::Space&, Gecode::IntVar x, int) {
00146     return x.min();
00147   }
00149 
00155 
00156   const char* bool_assign_name[] = {
00157     "BOOL_ASSIGN_MIN",
00158     "BOOL_ASSIGN_MAX",
00159     "BOOL_ASSIGN_RND",
00160     "BOOL_ASSIGN"
00161   };
00163   const int n_bool_assign =
00164     sizeof(bool_assign_name)/sizeof(const char*);
00166   int bool_val(const Gecode::Space&, Gecode::BoolVar x, int) {
00167     return x.min();
00168   }
00170 
00171   IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
00172     : Base("Int::Assign::"+s), arity(a), dom(d) {
00173   }
00174 
00175   bool
00176   IntTest::run(void) {
00177     using namespace Gecode;
00178     IntTestSpace* root = new IntTestSpace(arity,dom);
00179     post(*root, root->x);
00180     (void) root->status();
00181 
00182     for (int val = 0; val<n_int_assign; val++) {
00183       IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone());
00184       Gecode::Search::Options o;
00185       o.a_d = Base::rand(10);
00186       o.c_d = Base::rand(10);
00187 
00188       Rnd r(1);
00189       IntAssign ia;
00190       switch (val) {
00191       case 0: ia = INT_ASSIGN_MIN(); break;
00192       case 1: ia = INT_ASSIGN_MED(); break;
00193       case 2: ia = INT_ASSIGN_MAX(); break;
00194       case 3: ia = INT_ASSIGN_RND(r); break;
00195       case 4: ia = INT_ASSIGN(&int_val); break;
00196       default: GECODE_NEVER;
00197       }
00198 
00199       assign(*clone, clone->x, ia);
00200       Gecode::DFS<IntTestSpace> e_s(clone, o);
00201       delete clone;
00202 
00203       // Find number of solutions
00204       int solutions = 0;
00205       while (Space* s = e_s.next()) {
00206         delete s; solutions++;
00207       }
00208       if (solutions != 1) {
00209         std::cout << "FAILURE" << std::endl
00210                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00211                   << "\t" << int_assign_name[val] << std::endl;
00212         delete root;
00213         return false;
00214       }
00215     }
00216     delete root;
00217     return true;
00218   }
00219 
00220   BoolTest::BoolTest(const std::string& s, int a)
00221     : Base("Bool::Assign::"+s), arity(a) {
00222   }
00223 
00224   bool
00225   BoolTest::run(void) {
00226     using namespace Gecode;
00227     BoolTestSpace* root = new BoolTestSpace(arity);
00228     post(*root, root->x);
00229     (void) root->status();
00230 
00231     for (int val = n_bool_assign; val--; ) {
00232       BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone());
00233       Gecode::Search::Options o;
00234       o.a_d = Base::rand(10);
00235       o.c_d = Base::rand(10);
00236       Rnd r(1);
00237       BoolAssign ia;
00238       switch (val) {
00239       case 0: ia = BOOL_ASSIGN_MIN(); break;
00240       case 1: ia = BOOL_ASSIGN_MAX(); break;
00241       case 2: ia = BOOL_ASSIGN_RND(r); break;
00242       case 3: ia = BOOL_ASSIGN(&bool_val); break;
00243       default: GECODE_NEVER;
00244       }
00245 
00246       assign(*clone, clone->x, ia);
00247       Gecode::DFS<BoolTestSpace> e_s(clone, o);
00248       delete clone;
00249 
00250       // Find number of solutions
00251       int solutions = 0;
00252       while (Space* s = e_s.next()) {
00253         delete s; solutions++;
00254       }
00255       if (solutions != 1) {
00256         std::cout << "FAILURE" << std::endl
00257                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00258                   << "\t" << int_assign_name[val] << std::endl;
00259         delete root;
00260         return false;
00261       }
00262     }
00263     delete root;
00264     return true;
00265   }
00266 
00267 #ifdef GECODE_HAS_SET_VARS
00268 
00274 
00275   const char* set_assign_name[] = {
00276     "SET_ASSIGN_MIN_INC",
00277     "SET_ASSIGN_MIN_EXC",
00278     "SET_ASSIGN_MED_INC",
00279     "SET_ASSIGN_MED_EXC",
00280     "SET_ASSIGN_MAX_INC",
00281     "SET_ASSIGN_MAX_EXC",
00282     "SET_ASSIGN_RND_INC",
00283     "SET_ASSIGN_RND_EXC",
00284     "SET_ASSIGN"
00285   };
00287   const int n_set_assign =
00288     sizeof(set_assign_name)/sizeof(const char*);
00290   int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
00291     Gecode::SetVarUnknownRanges r(x);
00292     return r.min();
00293   }
00295 
00296   SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
00297     : Base("Set::Assign::"+s), arity(a), dom(d) {
00298   }
00299 
00300   bool
00301   SetTest::run(void) {
00302     using namespace Gecode;
00303     SetTestSpace* root = new SetTestSpace(arity,dom);
00304     post(*root, root->x);
00305     (void) root->status();
00306 
00307     for (int val = n_set_assign; val--; ) {
00308       SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone());
00309       Gecode::Search::Options o;
00310       o.a_d = Base::rand(10);
00311       o.c_d = Base::rand(10);
00312 
00313       Rnd r(1);
00314 
00315       SetAssign sa;
00316       switch (val) {
00317       case 0: sa = SET_ASSIGN_MIN_INC(); break;
00318       case 1: sa = SET_ASSIGN_MIN_EXC(); break;
00319       case 2: sa = SET_ASSIGN_MED_INC(); break;
00320       case 3: sa = SET_ASSIGN_MED_EXC(); break;
00321       case 4: sa = SET_ASSIGN_MAX_INC(); break;
00322       case 5: sa = SET_ASSIGN_MAX_EXC(); break;
00323       case 6: sa = SET_ASSIGN_RND_INC(r); break;
00324       case 7: sa = SET_ASSIGN_RND_EXC(r); break;
00325       case 8: sa = SET_ASSIGN(&set_val); break;
00326       default: GECODE_NEVER;
00327       }
00328 
00329       assign(*clone, clone->x, sa);
00330       Gecode::DFS<SetTestSpace> e_s(clone, o);
00331       delete clone;
00332 
00333       // Find number of solutions
00334       int solutions = 0;
00335       while (Space* s = e_s.next()) {
00336         delete s; solutions++;
00337       }
00338       if (solutions != 1) {
00339         std::cout << "FAILURE" << std::endl
00340                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00341                   << "\t" << set_assign_name[val] << std::endl;
00342         delete root;
00343         return false;
00344       }
00345     }
00346     delete root;
00347     return true;
00348   }
00349 
00350 #endif
00351 
00352 #ifdef GECODE_HAS_FLOAT_VARS
00353 
00359 
00360   const char* float_assign_name[] = {
00361     "FLOAT_ASSIGN_MIN",
00362     "FLOAT_ASSIGN_MAX",
00363     "FLOAT_ASSIGN_RND",
00364     "FLOAT_ASSIGN"
00365   };
00367   const int n_float_assign =
00368     sizeof(float_assign_name)/sizeof(const char*);
00370   Gecode::FloatNumBranch float_val(const Gecode::Space&,
00371                                    Gecode::FloatVar x, int) {
00372     Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
00373     return nl;
00374   }
00376 
00377   FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
00378     : Base("Float::Assign::"+s), arity(a), dom(d) {
00379   }
00380 
00381   bool
00382   FloatTest::run(void) {
00383     using namespace Gecode;
00384     FloatTestSpace* root = new FloatTestSpace(arity,dom);
00385     post(*root, root->x);
00386     (void) root->status();
00387 
00388     for (int val = n_float_assign; val--; ) {
00389       FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone());
00390       Gecode::Search::Options o;
00391       o.a_d = Base::rand(10);
00392       o.c_d = Base::rand(10);
00393 
00394       Rnd r(1);
00395 
00396       FloatAssign fa;
00397       switch (val) {
00398       case 0: fa = FLOAT_ASSIGN_MIN(); break;
00399       case 1: fa = FLOAT_ASSIGN_MAX(); break;
00400       case 2: fa = FLOAT_ASSIGN_RND(r); break;
00401       case 3: fa = FLOAT_ASSIGN(&float_val); break;
00402       default: GECODE_NEVER;
00403       }
00404 
00405       assign(*clone, clone->x, fa);
00406       Gecode::DFS<FloatTestSpace> e_s(clone, o);
00407       delete clone;
00408 
00409       // Find number of solutions
00410       int solutions = 0;
00411       while (Space* s = e_s.next()) {
00412         delete s; solutions++;
00413       }
00414       if (solutions != 1) {
00415         std::cout << "FAILURE" << std::endl
00416                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00417                   << "\t" << float_assign_name[val] << std::endl;
00418         delete root;
00419         return false;
00420       }
00421     }
00422     delete root;
00423     return true;
00424   }
00425 
00426 #endif
00427 
00428 }}
00429 
00430 // STATISTICS: test-branch