Generated on Sun Aug 26 2012 08:43:25 for Gecode by doxygen 1.8.1.1
assign.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2008
8  *
9  * Last modified:
10  * $Date: 2009-03-02 21:35:30 +1100 (Mon, 02 Mar 2009) $ by $Author: schulte $
11  * $Revision: 8325 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include "test/assign.hh"
39 
40 #include <gecode/search.hh>
41 
42 namespace Test { namespace Assign {
43 
45  class IntTestSpace : public Gecode::Space {
46  public:
51  : x(*this, n, d) {}
53  IntTestSpace(bool share, IntTestSpace& s)
54  : Gecode::Space(share,s) {
55  x.update(*this, share, s.x);
56  }
58  virtual Gecode::Space* copy(bool share) {
59  return new IntTestSpace(share,*this);
60  }
61  };
62 
64  class BoolTestSpace : public Gecode::Space {
65  public:
70  : x(*this, n, 0, 1) {}
72  BoolTestSpace(bool share, BoolTestSpace& s)
73  : Gecode::Space(share,s) {
74  x.update(*this, share, s.x);
75  }
77  virtual Gecode::Space* copy(bool share) {
78  return new BoolTestSpace(share,*this);
79  }
80  };
81 
82 #ifdef GECODE_HAS_SET_VARS
83 
85  class SetTestSpace : public Gecode::Space {
86  public:
90  SetTestSpace(int n, const Gecode::IntSet& d)
91  : x(*this, n, Gecode::IntSet::empty, d) {}
93  SetTestSpace(bool share, SetTestSpace& s)
94  : Gecode::Space(share,s) {
95  x.update(*this, share, s.x);
96  }
98  virtual Gecode::Space* copy(bool share) {
99  return new SetTestSpace(share,*this);
100  }
101  };
102 
103 #endif
104 
110 
111  const Gecode::IntAssign int_assign[] = {
116  };
118  const int n_int_assign =
119  sizeof(int_assign)/sizeof(Gecode::IntAssign);
121  const char* int_assign_name[] = {
122  "INT_ASSIGN_MIN",
123  "INT_ASSIGN_MED",
124  "INT_ASSIGN_MAX",
125  "INT_ASSIGN_RND"
126  };
128 
129  IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
130  : Base("Int::Assign::"+s), arity(a), dom(d) {
131  }
132 
133  bool
134  IntTest::run(void) {
135  using namespace Gecode;
136  IntTestSpace* root = new IntTestSpace(arity,dom);
137  post(*root, root->x);
138  (void) root->status();
139 
140  for (int val = n_int_assign; val--; ) {
141  IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone(false));
143  o.a_d = Base::rand(10);
144  o.c_d = Base::rand(10);
145  assign(*clone, clone->x, int_assign[val]);
146  Gecode::DFS<IntTestSpace> e_s(clone, o);
147  delete clone;
148 
149  // Find number of solutions
150  int solutions = 0;
151  while (Space* s = e_s.next()) {
152  delete s; solutions++;
153  }
154  if (solutions != 1) {
155  std::cout << "FAILURE" << std::endl
156  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
157  << "\t" << int_assign_name[val] << std::endl;
158  delete root;
159  return false;
160  }
161  }
162  delete root;
163  return true;
164  }
165 
166  BoolTest::BoolTest(const std::string& s, int a)
167  : Base("Bool::Assign::"+s), arity(a) {
168  }
169 
170  bool
172  using namespace Gecode;
173  BoolTestSpace* root = new BoolTestSpace(arity);
174  post(*root, root->x);
175  (void) root->status();
176 
177  for (int val = n_int_assign; val--; ) {
178  BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone(false));
180  o.a_d = Base::rand(10);
181  o.c_d = Base::rand(10);
182  assign(*clone, clone->x, int_assign[val]);
183  Gecode::DFS<BoolTestSpace> e_s(clone, o);
184  delete clone;
185 
186  // Find number of solutions
187  int solutions = 0;
188  while (Space* s = e_s.next()) {
189  delete s; solutions++;
190  }
191  if (solutions != 1) {
192  std::cout << "FAILURE" << std::endl
193  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
194  << "\t" << int_assign_name[val] << std::endl;
195  delete root;
196  return false;
197  }
198  }
199  delete root;
200  return true;
201  }
202 
203 #ifdef GECODE_HAS_SET_VARS
204 
210 
211  const Gecode::SetAssign set_assign[] = {
220  };
222  const int n_set_assign =
223  sizeof(set_assign)/sizeof(Gecode::SetAssign);
225  const char* set_assign_name[] = {
226  "SET_ASSIGN_MIN_INC",
227  "SET_ASSIGN_MIN_EXC",
228  "SET_ASSIGN_MED_INC",
229  "SET_ASSIGN_MED_EXC",
230  "SET_ASSIGN_MAX_INC",
231  "SET_ASSIGN_MAX_EXC",
232  "SET_ASSIGN_RND_INC",
233  "SET_ASSIGN_RND_EXC"
234  };
236 
237  SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
238  : Base("Set::Assign::"+s), arity(a), dom(d) {
239  }
240 
241  bool
242  SetTest::run(void) {
243  using namespace Gecode;
244  SetTestSpace* root = new SetTestSpace(arity,dom);
245  post(*root, root->x);
246  (void) root->status();
247 
248  for (int val = n_int_assign; val--; ) {
249  SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone(false));
251  o.a_d = Base::rand(10);
252  o.c_d = Base::rand(10);
253  assign(*clone, clone->x, set_assign[val]);
254  Gecode::DFS<SetTestSpace> e_s(clone, o);
255  delete clone;
256 
257  // Find number of solutions
258  int solutions = 0;
259  while (Space* s = e_s.next()) {
260  delete s; solutions++;
261  }
262  if (solutions != 1) {
263  std::cout << "FAILURE" << std::endl
264  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
265  << "\t" << set_assign_name[val] << std::endl;
266  delete root;
267  return false;
268  }
269  }
270  delete root;
271  return true;
272  }
273 
274 #endif
275 
276 }}
277 
278 // STATISTICS: test-branch