Generated on Tue May 22 09:40:17 2018 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       }
00197 
00198       assign(*clone, clone->x, ia);
00199       Gecode::DFS<IntTestSpace> e_s(clone, o);
00200       delete clone;
00201 
00202       // Find number of solutions
00203       int solutions = 0;
00204       while (Space* s = e_s.next()) {
00205         delete s; solutions++;
00206       }
00207       if (solutions != 1) {
00208         std::cout << "FAILURE" << std::endl
00209                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00210                   << "\t" << int_assign_name[val] << std::endl;
00211         delete root;
00212         return false;
00213       }
00214     }
00215     delete root;
00216     return true;
00217   }
00218 
00219   BoolTest::BoolTest(const std::string& s, int a)
00220     : Base("Bool::Assign::"+s), arity(a) {
00221   }
00222 
00223   bool
00224   BoolTest::run(void) {
00225     using namespace Gecode;
00226     BoolTestSpace* root = new BoolTestSpace(arity);
00227     post(*root, root->x);
00228     (void) root->status();
00229 
00230     for (int val = n_bool_assign; val--; ) {
00231       BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone());
00232       Gecode::Search::Options o;
00233       o.a_d = Base::rand(10);
00234       o.c_d = Base::rand(10);
00235       Rnd r(1);
00236       BoolAssign ia;
00237       switch (val) {
00238       case 0: ia = BOOL_ASSIGN_MIN(); break;
00239       case 1: ia = BOOL_ASSIGN_MAX(); break;
00240       case 2: ia = BOOL_ASSIGN_RND(r); break;
00241       case 3: ia = BOOL_ASSIGN(&bool_val); break;
00242       }
00243 
00244       assign(*clone, clone->x, ia);
00245       Gecode::DFS<BoolTestSpace> e_s(clone, o);
00246       delete clone;
00247 
00248       // Find number of solutions
00249       int solutions = 0;
00250       while (Space* s = e_s.next()) {
00251         delete s; solutions++;
00252       }
00253       if (solutions != 1) {
00254         std::cout << "FAILURE" << std::endl
00255                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00256                   << "\t" << int_assign_name[val] << std::endl;
00257         delete root;
00258         return false;
00259       }
00260     }
00261     delete root;
00262     return true;
00263   }
00264 
00265 #ifdef GECODE_HAS_SET_VARS
00266 
00272 
00273   const char* set_assign_name[] = {
00274     "SET_ASSIGN_MIN_INC",
00275     "SET_ASSIGN_MIN_EXC",
00276     "SET_ASSIGN_MED_INC",
00277     "SET_ASSIGN_MED_EXC",
00278     "SET_ASSIGN_MAX_INC",
00279     "SET_ASSIGN_MAX_EXC",
00280     "SET_ASSIGN_RND_INC",
00281     "SET_ASSIGN_RND_EXC",
00282     "SET_ASSIGN"
00283   };
00285   const int n_set_assign =
00286     sizeof(set_assign_name)/sizeof(const char*);
00288   int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
00289     Gecode::SetVarUnknownRanges r(x);
00290     return r.min();
00291   }
00293 
00294   SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
00295     : Base("Set::Assign::"+s), arity(a), dom(d) {
00296   }
00297 
00298   bool
00299   SetTest::run(void) {
00300     using namespace Gecode;
00301     SetTestSpace* root = new SetTestSpace(arity,dom);
00302     post(*root, root->x);
00303     (void) root->status();
00304 
00305     for (int val = n_int_assign; val--; ) {
00306       SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone());
00307       Gecode::Search::Options o;
00308       o.a_d = Base::rand(10);
00309       o.c_d = Base::rand(10);
00310 
00311       Rnd r(1);
00312 
00313       SetAssign sa;
00314       switch (val) {
00315       case 0: sa = SET_ASSIGN_MIN_INC(); break;
00316       case 1: sa = SET_ASSIGN_MIN_EXC(); break;
00317       case 2: sa = SET_ASSIGN_MED_INC(); break;
00318       case 3: sa = SET_ASSIGN_MED_EXC(); break;
00319       case 4: sa = SET_ASSIGN_MAX_INC(); break;
00320       case 5: sa = SET_ASSIGN_MAX_EXC(); break;
00321       case 6: sa = SET_ASSIGN_RND_INC(r); break;
00322       case 7: sa = SET_ASSIGN_RND_EXC(r); break;
00323       case 8: sa = SET_ASSIGN(&set_val); break;
00324       }
00325 
00326       assign(*clone, clone->x, sa);
00327       Gecode::DFS<SetTestSpace> e_s(clone, o);
00328       delete clone;
00329 
00330       // Find number of solutions
00331       int solutions = 0;
00332       while (Space* s = e_s.next()) {
00333         delete s; solutions++;
00334       }
00335       if (solutions != 1) {
00336         std::cout << "FAILURE" << std::endl
00337                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00338                   << "\t" << set_assign_name[val] << std::endl;
00339         delete root;
00340         return false;
00341       }
00342     }
00343     delete root;
00344     return true;
00345   }
00346 
00347 #endif
00348 
00349 #ifdef GECODE_HAS_FLOAT_VARS
00350 
00356 
00357   const char* float_assign_name[] = {
00358     "FLOAT_ASSIGN_MIN",
00359     "FLOAT_ASSIGN_MAX",
00360     "FLOAT_ASSIGN_RND",
00361     "FLOAT_ASSIGN"
00362   };
00364   const int n_float_assign =
00365     sizeof(float_assign_name)/sizeof(const char*);
00367   Gecode::FloatNumBranch float_val(const Gecode::Space&,
00368                                    Gecode::FloatVar x, int) {
00369     Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
00370     return nl;
00371   }
00373 
00374   FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
00375     : Base("Float::Assign::"+s), arity(a), dom(d) {
00376   }
00377 
00378   bool
00379   FloatTest::run(void) {
00380     using namespace Gecode;
00381     FloatTestSpace* root = new FloatTestSpace(arity,dom);
00382     post(*root, root->x);
00383     (void) root->status();
00384 
00385     for (int val = n_float_assign; val--; ) {
00386       FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone());
00387       Gecode::Search::Options o;
00388       o.a_d = Base::rand(10);
00389       o.c_d = Base::rand(10);
00390 
00391       Rnd r(1);
00392 
00393       FloatAssign fa;
00394       switch (val) {
00395       case 0: fa = FLOAT_ASSIGN_MIN(); break;
00396       case 1: fa = FLOAT_ASSIGN_MAX(); break;
00397       case 2: fa = FLOAT_ASSIGN_RND(r); break;
00398       case 3: fa = FLOAT_ASSIGN(&float_val); break;
00399       }
00400 
00401       assign(*clone, clone->x, fa);
00402       Gecode::DFS<FloatTestSpace> e_s(clone, o);
00403       delete clone;
00404 
00405       // Find number of solutions
00406       int solutions = 0;
00407       while (Space* s = e_s.next()) {
00408         delete s; solutions++;
00409       }
00410       if (solutions != 1) {
00411         std::cout << "FAILURE" << std::endl
00412                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00413                   << "\t" << float_assign_name[val] << std::endl;
00414         delete root;
00415         return false;
00416       }
00417     }
00418     delete root;
00419     return true;
00420   }
00421 
00422 #endif
00423 
00424 }}
00425 
00426 // STATISTICS: test-branch