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 * Copyright: 00007 * Christian Schulte, 2008 00008 * 00009 * Last modified: 00010 * $Date: 2009-03-02 11:35:30 +0100 (Mon, 02 Mar 2009) $ by $Author: schulte $ 00011 * $Revision: 8325 $ 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(bool share, IntTestSpace& s) 00054 : Gecode::Space(share,s) { 00055 x.update(*this, share, s.x); 00056 } 00058 virtual Gecode::Space* copy(bool share) { 00059 return new IntTestSpace(share,*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(bool share, BoolTestSpace& s) 00073 : Gecode::Space(share,s) { 00074 x.update(*this, share, s.x); 00075 } 00077 virtual Gecode::Space* copy(bool share) { 00078 return new BoolTestSpace(share,*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(bool share, SetTestSpace& s) 00094 : Gecode::Space(share,s) { 00095 x.update(*this, share, s.x); 00096 } 00098 virtual Gecode::Space* copy(bool share) { 00099 return new SetTestSpace(share,*this); 00100 } 00101 }; 00102 00103 #endif 00104 00110 00111 const Gecode::IntAssign int_assign[] = { 00112 Gecode::INT_ASSIGN_MIN, 00113 Gecode::INT_ASSIGN_MED, 00114 Gecode::INT_ASSIGN_MAX, 00115 Gecode::INT_ASSIGN_RND 00116 }; 00118 const int n_int_assign = 00119 sizeof(int_assign)/sizeof(Gecode::IntAssign); 00121 const char* int_assign_name[] = { 00122 "INT_ASSIGN_MIN", 00123 "INT_ASSIGN_MED", 00124 "INT_ASSIGN_MAX", 00125 "INT_ASSIGN_RND" 00126 }; 00128 00129 IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d) 00130 : Base("Int::Assign::"+s), arity(a), dom(d) { 00131 } 00132 00133 bool 00134 IntTest::run(void) { 00135 using namespace Gecode; 00136 IntTestSpace* root = new IntTestSpace(arity,dom); 00137 post(*root, root->x); 00138 (void) root->status(); 00139 00140 for (int val = n_int_assign; val--; ) { 00141 IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone(false)); 00142 Gecode::Search::Options o; 00143 o.a_d = Base::rand(10); 00144 o.c_d = Base::rand(10); 00145 assign(*clone, clone->x, int_assign[val]); 00146 Gecode::DFS<IntTestSpace> e_s(clone, o); 00147 delete clone; 00148 00149 // Find number of solutions 00150 int solutions = 0; 00151 while (Space* s = e_s.next()) { 00152 delete s; solutions++; 00153 } 00154 if (solutions != 1) { 00155 std::cout << "FAILURE" << std::endl 00156 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 00157 << "\t" << int_assign_name[val] << std::endl; 00158 delete root; 00159 return false; 00160 } 00161 } 00162 delete root; 00163 return true; 00164 } 00165 00166 BoolTest::BoolTest(const std::string& s, int a) 00167 : Base("Bool::Assign::"+s), arity(a) { 00168 } 00169 00170 bool 00171 BoolTest::run(void) { 00172 using namespace Gecode; 00173 BoolTestSpace* root = new BoolTestSpace(arity); 00174 post(*root, root->x); 00175 (void) root->status(); 00176 00177 for (int val = n_int_assign; val--; ) { 00178 BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone(false)); 00179 Gecode::Search::Options o; 00180 o.a_d = Base::rand(10); 00181 o.c_d = Base::rand(10); 00182 assign(*clone, clone->x, int_assign[val]); 00183 Gecode::DFS<BoolTestSpace> e_s(clone, o); 00184 delete clone; 00185 00186 // Find number of solutions 00187 int solutions = 0; 00188 while (Space* s = e_s.next()) { 00189 delete s; solutions++; 00190 } 00191 if (solutions != 1) { 00192 std::cout << "FAILURE" << std::endl 00193 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 00194 << "\t" << int_assign_name[val] << std::endl; 00195 delete root; 00196 return false; 00197 } 00198 } 00199 delete root; 00200 return true; 00201 } 00202 00203 #ifdef GECODE_HAS_SET_VARS 00204 00210 00211 const Gecode::SetAssign set_assign[] = { 00212 Gecode::SET_ASSIGN_MIN_INC, 00213 Gecode::SET_ASSIGN_MIN_EXC, 00214 Gecode::SET_ASSIGN_MED_INC, 00215 Gecode::SET_ASSIGN_MED_EXC, 00216 Gecode::SET_ASSIGN_MAX_INC, 00217 Gecode::SET_ASSIGN_MAX_EXC, 00218 Gecode::SET_ASSIGN_RND_INC, 00219 Gecode::SET_ASSIGN_RND_EXC 00220 }; 00222 const int n_set_assign = 00223 sizeof(set_assign)/sizeof(Gecode::SetAssign); 00225 const char* set_assign_name[] = { 00226 "SET_ASSIGN_MIN_INC", 00227 "SET_ASSIGN_MIN_EXC", 00228 "SET_ASSIGN_MED_INC", 00229 "SET_ASSIGN_MED_EXC", 00230 "SET_ASSIGN_MAX_INC", 00231 "SET_ASSIGN_MAX_EXC", 00232 "SET_ASSIGN_RND_INC", 00233 "SET_ASSIGN_RND_EXC" 00234 }; 00236 00237 SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d) 00238 : Base("Set::Assign::"+s), arity(a), dom(d) { 00239 } 00240 00241 bool 00242 SetTest::run(void) { 00243 using namespace Gecode; 00244 SetTestSpace* root = new SetTestSpace(arity,dom); 00245 post(*root, root->x); 00246 (void) root->status(); 00247 00248 for (int val = n_int_assign; val--; ) { 00249 SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone(false)); 00250 Gecode::Search::Options o; 00251 o.a_d = Base::rand(10); 00252 o.c_d = Base::rand(10); 00253 assign(*clone, clone->x, set_assign[val]); 00254 Gecode::DFS<SetTestSpace> e_s(clone, o); 00255 delete clone; 00256 00257 // Find number of solutions 00258 int solutions = 0; 00259 while (Space* s = e_s.next()) { 00260 delete s; solutions++; 00261 } 00262 if (solutions != 1) { 00263 std::cout << "FAILURE" << std::endl 00264 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 00265 << "\t" << set_assign_name[val] << std::endl; 00266 delete root; 00267 return false; 00268 } 00269 } 00270 delete root; 00271 return true; 00272 } 00273 00274 #endif 00275 00276 }} 00277 00278 // STATISTICS: test-branch