Generated on Tue Apr 18 10:22:15 2017 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  *  Last modified:
00014  *     $Date: 2017-02-17 09:01:39 +0100 (Fri, 17 Feb 2017) $ by $Author: schulte $
00015  *     $Revision: 15441 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 #include "test/assign.hh"
00043 
00044 #include <gecode/search.hh>
00045 
00046 namespace Test { namespace Assign {
00047 
00049   class IntTestSpace : public Gecode::Space {
00050   public:
00052     Gecode::IntVarArray x;
00054     IntTestSpace(int n, Gecode::IntSet& d)
00055       : x(*this, n, d) {}
00057     IntTestSpace(bool share, IntTestSpace& s)
00058       : Gecode::Space(share,s) {
00059       x.update(*this, share, s.x);
00060     }
00062     virtual Gecode::Space* copy(bool share) {
00063       return new IntTestSpace(share,*this);
00064     }
00065   };
00066 
00068   class BoolTestSpace : public Gecode::Space {
00069   public:
00071     Gecode::BoolVarArray x;
00073     BoolTestSpace(int n)
00074       : x(*this, n, 0, 1) {}
00076     BoolTestSpace(bool share, BoolTestSpace& s)
00077       : Gecode::Space(share,s) {
00078       x.update(*this, share, s.x);
00079     }
00081     virtual Gecode::Space* copy(bool share) {
00082       return new BoolTestSpace(share,*this);
00083     }
00084   };
00085 
00086 #ifdef GECODE_HAS_SET_VARS
00087 
00089   class SetTestSpace : public Gecode::Space {
00090   public:
00092     Gecode::SetVarArray x;
00094     SetTestSpace(int n, const Gecode::IntSet& d)
00095       : x(*this, n, Gecode::IntSet::empty, d) {}
00097     SetTestSpace(bool share, SetTestSpace& s)
00098       : Gecode::Space(share,s) {
00099       x.update(*this, share, s.x);
00100     }
00102     virtual Gecode::Space* copy(bool share) {
00103       return new SetTestSpace(share,*this);
00104     }
00105   };
00106 
00107 #endif
00108 
00109 #ifdef GECODE_HAS_FLOAT_VARS
00110 
00112   class FloatTestSpace : public Gecode::Space {
00113   public:
00115     Gecode::FloatVarArray x;
00117     FloatTestSpace(int n, const Gecode::FloatVal& d)
00118       : x(*this, n, d.min(), d.max()) {}
00120     FloatTestSpace(bool share, FloatTestSpace& s)
00121       : Gecode::Space(share,s) {
00122       x.update(*this, share, s.x);
00123     }
00125     virtual Gecode::Space* copy(bool share) {
00126       return new FloatTestSpace(share,*this);
00127     }
00128   };
00129 
00130 #endif
00131 
00137 
00138   const char* int_assign_name[] = {
00139     "INT_ASSIGN_MIN",
00140     "INT_ASSIGN_MED",
00141     "INT_ASSIGN_MAX",
00142     "INT_ASSIGN_RND",
00143     "INT_ASSIGN"
00144   };
00146   const int n_int_assign =
00147     sizeof(int_assign_name)/sizeof(const char*);
00149   int int_val(const Gecode::Space&, Gecode::IntVar x, int) {
00150     return x.min();
00151   }
00153 
00159 
00160   const char* bool_assign_name[] = {
00161     "BOOL_ASSIGN_MIN",
00162     "BOOL_ASSIGN_MAX",
00163     "BOOL_ASSIGN_RND",
00164     "BOOL_ASSIGN"
00165   };
00167   const int n_bool_assign =
00168     sizeof(bool_assign_name)/sizeof(const char*);
00170   int bool_val(const Gecode::Space&, Gecode::BoolVar x, int) {
00171     return x.min();
00172   }
00174 
00175   IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
00176     : Base("Int::Assign::"+s), arity(a), dom(d) {
00177   }
00178 
00179   bool
00180   IntTest::run(void) {
00181     using namespace Gecode;
00182     IntTestSpace* root = new IntTestSpace(arity,dom);
00183     post(*root, root->x);
00184     (void) root->status();
00185 
00186     for (int val = 0; val<n_int_assign; val++) {
00187       IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone(false));
00188       Gecode::Search::Options o;
00189       o.a_d = Base::rand(10);
00190       o.c_d = Base::rand(10);
00191 
00192       Rnd r(1);
00193       IntAssign ia;
00194       switch (val) {
00195       case 0: ia = INT_ASSIGN_MIN(); break;
00196       case 1: ia = INT_ASSIGN_MED(); break;
00197       case 2: ia = INT_ASSIGN_MAX(); break;
00198       case 3: ia = INT_ASSIGN_RND(r); break;
00199       case 4: ia = INT_ASSIGN(&int_val); break;
00200       }
00201 
00202       assign(*clone, clone->x, ia);
00203       Gecode::DFS<IntTestSpace> e_s(clone, o);
00204       delete clone;
00205 
00206       // Find number of solutions
00207       int solutions = 0;
00208       while (Space* s = e_s.next()) {
00209         delete s; solutions++;
00210       }
00211       if (solutions != 1) {
00212         std::cout << "FAILURE" << std::endl
00213                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00214                   << "\t" << int_assign_name[val] << std::endl;
00215         delete root;
00216         return false;
00217       }
00218     }
00219     delete root;
00220     return true;
00221   }
00222 
00223   BoolTest::BoolTest(const std::string& s, int a)
00224     : Base("Bool::Assign::"+s), arity(a) {
00225   }
00226 
00227   bool
00228   BoolTest::run(void) {
00229     using namespace Gecode;
00230     BoolTestSpace* root = new BoolTestSpace(arity);
00231     post(*root, root->x);
00232     (void) root->status();
00233 
00234     for (int val = n_bool_assign; val--; ) {
00235       BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone(false));
00236       Gecode::Search::Options o;
00237       o.a_d = Base::rand(10);
00238       o.c_d = Base::rand(10);
00239       Rnd r(1);
00240       BoolAssign ia;
00241       switch (val) {
00242       case 0: ia = BOOL_ASSIGN_MIN(); break;
00243       case 1: ia = BOOL_ASSIGN_MAX(); break;
00244       case 2: ia = BOOL_ASSIGN_RND(r); break;
00245       case 3: ia = BOOL_ASSIGN(&bool_val); break;
00246       }
00247 
00248       assign(*clone, clone->x, ia);
00249       Gecode::DFS<BoolTestSpace> e_s(clone, o);
00250       delete clone;
00251 
00252       // Find number of solutions
00253       int solutions = 0;
00254       while (Space* s = e_s.next()) {
00255         delete s; solutions++;
00256       }
00257       if (solutions != 1) {
00258         std::cout << "FAILURE" << std::endl
00259                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00260                   << "\t" << int_assign_name[val] << std::endl;
00261         delete root;
00262         return false;
00263       }
00264     }
00265     delete root;
00266     return true;
00267   }
00268 
00269 #ifdef GECODE_HAS_SET_VARS
00270 
00276 
00277   const char* set_assign_name[] = {
00278     "SET_ASSIGN_MIN_INC",
00279     "SET_ASSIGN_MIN_EXC",
00280     "SET_ASSIGN_MED_INC",
00281     "SET_ASSIGN_MED_EXC",
00282     "SET_ASSIGN_MAX_INC",
00283     "SET_ASSIGN_MAX_EXC",
00284     "SET_ASSIGN_RND_INC",
00285     "SET_ASSIGN_RND_EXC",
00286     "SET_ASSIGN"
00287   };
00289   const int n_set_assign =
00290     sizeof(set_assign_name)/sizeof(const char*);
00292   int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
00293     Gecode::SetVarUnknownRanges r(x);
00294     return r.min();
00295   }
00297 
00298   SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
00299     : Base("Set::Assign::"+s), arity(a), dom(d) {
00300   }
00301 
00302   bool
00303   SetTest::run(void) {
00304     using namespace Gecode;
00305     SetTestSpace* root = new SetTestSpace(arity,dom);
00306     post(*root, root->x);
00307     (void) root->status();
00308 
00309     for (int val = n_int_assign; val--; ) {
00310       SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone(false));
00311       Gecode::Search::Options o;
00312       o.a_d = Base::rand(10);
00313       o.c_d = Base::rand(10);
00314 
00315       Rnd r(1);
00316 
00317       SetAssign sa;
00318       switch (val) {
00319       case 0: sa = SET_ASSIGN_MIN_INC(); break;
00320       case 1: sa = SET_ASSIGN_MIN_EXC(); break;
00321       case 2: sa = SET_ASSIGN_MED_INC(); break;
00322       case 3: sa = SET_ASSIGN_MED_EXC(); break;
00323       case 4: sa = SET_ASSIGN_MAX_INC(); break;
00324       case 5: sa = SET_ASSIGN_MAX_EXC(); break;
00325       case 6: sa = SET_ASSIGN_RND_INC(r); break;
00326       case 7: sa = SET_ASSIGN_RND_EXC(r); break;
00327       case 8: sa = SET_ASSIGN(&set_val); break;
00328       }
00329 
00330       assign(*clone, clone->x, sa);
00331       Gecode::DFS<SetTestSpace> e_s(clone, o);
00332       delete clone;
00333 
00334       // Find number of solutions
00335       int solutions = 0;
00336       while (Space* s = e_s.next()) {
00337         delete s; solutions++;
00338       }
00339       if (solutions != 1) {
00340         std::cout << "FAILURE" << std::endl
00341                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00342                   << "\t" << set_assign_name[val] << std::endl;
00343         delete root;
00344         return false;
00345       }
00346     }
00347     delete root;
00348     return true;
00349   }
00350 
00351 #endif
00352 
00353 #ifdef GECODE_HAS_FLOAT_VARS
00354 
00360 
00361   const char* float_assign_name[] = {
00362     "FLOAT_ASSIGN_MIN",
00363     "FLOAT_ASSIGN_MAX",
00364     "FLOAT_ASSIGN_RND",
00365     "FLOAT_ASSIGN"
00366   };
00368   const int n_float_assign =
00369     sizeof(float_assign_name)/sizeof(const char*);
00371   Gecode::FloatNumBranch float_val(const Gecode::Space&,
00372                                    Gecode::FloatVar x, int) {
00373     Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
00374     return nl;
00375   }
00377 
00378   FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
00379     : Base("Float::Assign::"+s), arity(a), dom(d) {
00380   }
00381 
00382   bool
00383   FloatTest::run(void) {
00384     using namespace Gecode;
00385     FloatTestSpace* root = new FloatTestSpace(arity,dom);
00386     post(*root, root->x);
00387     (void) root->status();
00388 
00389     for (int val = n_float_assign; val--; ) {
00390       FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone(false));
00391       Gecode::Search::Options o;
00392       o.a_d = Base::rand(10);
00393       o.c_d = Base::rand(10);
00394 
00395       Rnd r(1);
00396 
00397       FloatAssign fa;
00398       switch (val) {
00399       case 0: fa = FLOAT_ASSIGN_MIN(); break;
00400       case 1: fa = FLOAT_ASSIGN_MAX(); break;
00401       case 2: fa = FLOAT_ASSIGN_RND(r); break;
00402       case 3: fa = FLOAT_ASSIGN(&float_val); break;
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