Generated on Mon Aug 25 11:35:44 2008 for Gecode by doxygen 1.5.6

assign.cc

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  *  Copyright:
00007  *     Christian Schulte, 2008
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-27 05:39:03 +0100 (Wed, 27 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6320 $
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/kernel.hh"
00041 #include "gecode/int.hh"
00042 #include "gecode/search.hh"
00043 
00044 namespace Test { namespace Assign {
00045 
00047   class IntTestSpace : public Gecode::Space {
00048   public:
00050     Gecode::IntVarArray x;
00052     IntTestSpace(int n, Gecode::IntSet& d)
00053       : x(this, n, d) {}
00055     IntTestSpace(bool share, IntTestSpace& s) 
00056       : Gecode::Space(share,s) {
00057       x.update(this, share, s.x);
00058     }
00060     virtual Gecode::Space* copy(bool share) {
00061       return new IntTestSpace(share,*this);
00062     }
00063   };
00064 
00066   class BoolTestSpace : public Gecode::Space {
00067   public:
00069     Gecode::BoolVarArray x;
00071     BoolTestSpace(int n)
00072       : x(this, n, 0, 1) {}
00074     BoolTestSpace(bool share, BoolTestSpace& s) 
00075       : Gecode::Space(share,s) {
00076       x.update(this, share, s.x);
00077     }
00079     virtual Gecode::Space* copy(bool share) {
00080       return new BoolTestSpace(share,*this);
00081     }
00082   };
00083 
00084 
00090 
00091   const Gecode::IntAssign int_assign[] = {
00092     Gecode::INT_ASSIGN_MIN,
00093     Gecode::INT_ASSIGN_MED,
00094     Gecode::INT_ASSIGN_MAX
00095   };
00097   const int n_int_assign =
00098     sizeof(int_assign)/sizeof(Gecode::IntAssign);
00100   const char* int_assign_name[] = {
00101     "INT_ASSIGN_MIN",
00102     "INT_ASSIGN_MED",       
00103     "INT_ASSIGN_MAX"
00104   };
00106 
00107   IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
00108     : Base("Assign::Int::"+s), arity(a), dom(d) {
00109   }
00110 
00111   bool
00112   IntTest::run(void) {
00113     using namespace Gecode;
00114     IntTestSpace* root = new IntTestSpace(arity,dom);
00115     post(root, root->x);
00116 
00117     for (int val = n_int_assign; val--; ) {
00118       IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone(false));
00119       Gecode::Search::Options o;
00120       o.a_d = Base::rand(10);
00121       o.c_d = Base::rand(10);
00122       assign(clone, clone->x, int_assign[val]);
00123       Gecode::DFS<IntTestSpace> e_s(clone, o);
00124       delete clone;
00125         
00126       // Find number of solutions
00127       int solutions = 0;
00128       while (Space* s = e_s.next()) {
00129         delete s; solutions++;
00130       }
00131       if (solutions != 1) {
00132         std::cout << "FAILURE" << std::endl
00133                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00134                   << "\t" << int_assign_name[val] << std::endl;
00135         delete root;
00136         return false;
00137       }
00138     }
00139     delete root;
00140     return true;
00141   }
00142 
00143   BoolTest::BoolTest(const std::string& s, int a)
00144     : Base("Assign::Bool::"+s), arity(a) {
00145   }
00146 
00147   bool
00148   BoolTest::run(void) {
00149     using namespace Gecode;
00150     BoolTestSpace* root = new BoolTestSpace(arity);
00151     post(root, root->x);
00152 
00153     for (int val = n_int_assign; val--; ) {
00154       BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone(false));
00155       Gecode::Search::Options o;
00156       o.a_d = Base::rand(10);
00157       o.c_d = Base::rand(10);
00158       assign(clone, clone->x, int_assign[val]);
00159       Gecode::DFS<BoolTestSpace> e_s(clone, o);
00160       delete clone;
00161         
00162       // Find number of solutions
00163       int solutions = 0;
00164       while (Space* s = e_s.next()) {
00165         delete s; solutions++;
00166       }
00167       if (solutions != 1) {
00168         std::cout << "FAILURE" << std::endl
00169                   << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
00170                   << "\t" << int_assign_name[val] << std::endl;
00171         delete root;
00172         return false;
00173       }
00174     }
00175     delete root;
00176     return true;
00177   }
00178 
00179 }}
00180 
00181 // STATISTICS: test-branch