Generated on Fri Mar 20 15:56:21 2015 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: 2013-05-29 13:53:43 +0200 (Wed, 29 May 2013) $ by $Author: schulte $
00015  *     $Revision: 13672 $
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   int bool_val(const Gecode::Space&, Gecode::BoolVar x, int) {
00154     return x.min();
00155   }
00157 
00158   IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
00159     : Base("Int::Assign::"+s), arity(a), dom(d) {
00160   }
00161 
00162   bool
00163   IntTest::run(void) {
00164     using namespace Gecode;
00165     IntTestSpace* root = new IntTestSpace(arity,dom);
00166     post(*root, root->x);
00167     (void) root->status();
00168 
00169     for (int val = 0; val<n_int_assign; val++) {
00170       IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone(false));
00171       Gecode::Search::Options o;
00172       o.a_d = Base::rand(10);
00173       o.c_d = Base::rand(10);
00174 
00175       Rnd r(1);
00176       IntAssign ia;
00177       switch (val) {
00178       case 0: ia = INT_ASSIGN_MIN(); break;
00179       case 1: ia = INT_ASSIGN_MED(); break;
00180       case 2: ia = INT_ASSIGN_MAX(); break;
00181       case 3: ia = INT_ASSIGN_RND(r); break;
00182       case 4: ia = INT_ASSIGN(&int_val); break;
00183       }
00184 
00185       assign(*clone, clone->x, ia);
00186       Gecode::DFS<IntTestSpace> e_s(clone, o);
00187       delete clone;
00188 
00189       // Find number of solutions
00190       int solutions = 0;
00191       while (Space* s = e_s.next()) {
00192         delete s; solutions++;
00193       }
00194       if (solutions != 1) {
00195         std::cout << "FAILURE" << std::endl
00196                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00197                   << "\t" << int_assign_name[val] << std::endl;
00198         delete root;
00199         return false;
00200       }
00201     }
00202     delete root;
00203     return true;
00204   }
00205 
00206   BoolTest::BoolTest(const std::string& s, int a)
00207     : Base("Bool::Assign::"+s), arity(a) {
00208   }
00209 
00210   bool
00211   BoolTest::run(void) {
00212     using namespace Gecode;
00213     BoolTestSpace* root = new BoolTestSpace(arity);
00214     post(*root, root->x);
00215     (void) root->status();
00216 
00217     for (int val = n_int_assign; val--; ) {
00218       BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone(false));
00219       Gecode::Search::Options o;
00220       o.a_d = Base::rand(10);
00221       o.c_d = Base::rand(10);
00222       Rnd r(1);
00223       IntAssign ia;
00224       switch (val) {
00225       case 0: ia = INT_ASSIGN_MIN(); break;
00226       case 1: ia = INT_ASSIGN_MED(); break;
00227       case 2: ia = INT_ASSIGN_MAX(); break;
00228       case 3: ia = INT_ASSIGN_RND(r); break;
00229       case 4: ia = INT_ASSIGN(&bool_val); break;
00230       }
00231 
00232       assign(*clone, clone->x, ia);
00233       Gecode::DFS<BoolTestSpace> e_s(clone, o);
00234       delete clone;
00235 
00236       // Find number of solutions
00237       int solutions = 0;
00238       while (Space* s = e_s.next()) {
00239         delete s; solutions++;
00240       }
00241       if (solutions != 1) {
00242         std::cout << "FAILURE" << std::endl
00243                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00244                   << "\t" << int_assign_name[val] << std::endl;
00245         delete root;
00246         return false;
00247       }
00248     }
00249     delete root;
00250     return true;
00251   }
00252 
00253 #ifdef GECODE_HAS_SET_VARS
00254 
00260 
00261   const char* set_assign_name[] = {
00262     "SET_ASSIGN_MIN_INC",
00263     "SET_ASSIGN_MIN_EXC",
00264     "SET_ASSIGN_MED_INC",
00265     "SET_ASSIGN_MED_EXC",
00266     "SET_ASSIGN_MAX_INC",
00267     "SET_ASSIGN_MAX_EXC",
00268     "SET_ASSIGN_RND_INC",
00269     "SET_ASSIGN_RND_EXC",
00270     "SET_ASSIGN"
00271   };
00273   const int n_set_assign =
00274     sizeof(set_assign_name)/sizeof(const char*);
00276   int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
00277     Gecode::SetVarUnknownRanges r(x);
00278     return r.min();
00279   }
00281 
00282   SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
00283     : Base("Set::Assign::"+s), arity(a), dom(d) {
00284   }
00285 
00286   bool
00287   SetTest::run(void) {
00288     using namespace Gecode;
00289     SetTestSpace* root = new SetTestSpace(arity,dom);
00290     post(*root, root->x);
00291     (void) root->status();
00292 
00293     for (int val = n_int_assign; val--; ) {
00294       SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone(false));
00295       Gecode::Search::Options o;
00296       o.a_d = Base::rand(10);
00297       o.c_d = Base::rand(10);
00298 
00299       Rnd r(1);
00300 
00301       SetAssign sa;
00302       switch (val) {
00303       case 0: sa = SET_ASSIGN_MIN_INC(); break;
00304       case 1: sa = SET_ASSIGN_MIN_EXC(); break;
00305       case 2: sa = SET_ASSIGN_MED_INC(); break;
00306       case 3: sa = SET_ASSIGN_MED_EXC(); break;
00307       case 4: sa = SET_ASSIGN_MAX_INC(); break;
00308       case 5: sa = SET_ASSIGN_MAX_EXC(); break;
00309       case 6: sa = SET_ASSIGN_RND_INC(r); break;
00310       case 7: sa = SET_ASSIGN_RND_EXC(r); break;
00311       case 8: sa = SET_ASSIGN(&set_val); break;
00312       }
00313           
00314       assign(*clone, clone->x, sa);
00315       Gecode::DFS<SetTestSpace> e_s(clone, o);
00316       delete clone;
00317 
00318       // Find number of solutions
00319       int solutions = 0;
00320       while (Space* s = e_s.next()) {
00321         delete s; solutions++;
00322       }
00323       if (solutions != 1) {
00324         std::cout << "FAILURE" << std::endl
00325                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00326                   << "\t" << set_assign_name[val] << std::endl;
00327         delete root;
00328         return false;
00329       }
00330     }
00331     delete root;
00332     return true;
00333   }
00334 
00335 #endif
00336 
00337 #ifdef GECODE_HAS_FLOAT_VARS
00338 
00344 
00345   const char* float_assign_name[] = {
00346     "FLOAT_ASSIGN_MIN",
00347     "FLOAT_ASSIGN_MAX",
00348     "FLOAT_ASSIGN_RND",
00349     "FLOAT_ASSIGN"
00350   };
00352   const int n_float_assign =
00353     sizeof(float_assign_name)/sizeof(const char*);
00355   Gecode::FloatNumBranch float_val(const Gecode::Space&, 
00356                                    Gecode::FloatVar x, int) {
00357     Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
00358     return nl;
00359   }
00361 
00362   FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
00363     : Base("Float::Assign::"+s), arity(a), dom(d) {
00364   }
00365 
00366   bool
00367   FloatTest::run(void) {
00368     using namespace Gecode;
00369     FloatTestSpace* root = new FloatTestSpace(arity,dom);
00370     post(*root, root->x);
00371     (void) root->status();
00372 
00373     for (int val = n_float_assign; val--; ) {
00374       FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone(false));
00375       Gecode::Search::Options o;
00376       o.a_d = Base::rand(10);
00377       o.c_d = Base::rand(10);
00378 
00379       Rnd r(1);
00380 
00381       FloatAssign fa;
00382       switch (val) {
00383       case 0: fa = FLOAT_ASSIGN_MIN(); break;
00384       case 1: fa = FLOAT_ASSIGN_MAX(); break;
00385       case 2: fa = FLOAT_ASSIGN_RND(r); break;
00386       case 3: fa = FLOAT_ASSIGN(&float_val); break;
00387       }
00388           
00389       assign(*clone, clone->x, fa);
00390       Gecode::DFS<FloatTestSpace> e_s(clone, o);
00391       delete clone;
00392 
00393       // Find number of solutions
00394       int solutions = 0;
00395       while (Space* s = e_s.next()) {
00396         delete s; solutions++;
00397       }
00398       if (solutions != 1) {
00399         std::cout << "FAILURE" << std::endl
00400                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00401                   << "\t" << float_assign_name[val] << std::endl;
00402         delete root;
00403         return false;
00404       }
00405     }
00406     delete root;
00407     return true;
00408   }
00409 
00410 #endif
00411 
00412 }}
00413 
00414 // STATISTICS: test-branch