Generated on Sat May 25 2013 18:00:41 for Gecode by doxygen 1.8.3.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  * Contributing authors:
7  * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
8  *
9  * Copyright:
10  * Christian Schulte, 2008
11  * Vincent Barichard, 2012
12  *
13  * Last modified:
14  * $Date: 2012-10-02 15:49:50 +0200 (Tue, 02 Oct 2012) $ by $Author: schulte $
15  * $Revision: 13123 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 #include "test/assign.hh"
43 
44 #include <gecode/search.hh>
45 
46 namespace Test { namespace Assign {
47 
49  class IntTestSpace : public Gecode::Space {
50  public:
55  : x(*this, n, d) {}
57  IntTestSpace(bool share, IntTestSpace& s)
58  : Gecode::Space(share,s) {
59  x.update(*this, share, s.x);
60  }
62  virtual Gecode::Space* copy(bool share) {
63  return new IntTestSpace(share,*this);
64  }
65  };
66 
68  class BoolTestSpace : public Gecode::Space {
69  public:
74  : x(*this, n, 0, 1) {}
76  BoolTestSpace(bool share, BoolTestSpace& s)
77  : Gecode::Space(share,s) {
78  x.update(*this, share, s.x);
79  }
81  virtual Gecode::Space* copy(bool share) {
82  return new BoolTestSpace(share,*this);
83  }
84  };
85 
86 #ifdef GECODE_HAS_SET_VARS
87 
89  class SetTestSpace : public Gecode::Space {
90  public:
95  : x(*this, n, Gecode::IntSet::empty, d) {}
97  SetTestSpace(bool share, SetTestSpace& s)
98  : Gecode::Space(share,s) {
99  x.update(*this, share, s.x);
100  }
102  virtual Gecode::Space* copy(bool share) {
103  return new SetTestSpace(share,*this);
104  }
105  };
106 
107 #endif
108 
109 #ifdef GECODE_HAS_FLOAT_VARS
110 
112  class FloatTestSpace : public Gecode::Space {
113  public:
118  : x(*this, n, d.min(), d.max()) {}
121  : Gecode::Space(share,s) {
122  x.update(*this, share, s.x);
123  }
125  virtual Gecode::Space* copy(bool share) {
126  return new FloatTestSpace(share,*this);
127  }
128  };
129 
130 #endif
131 
137 
138  const char* int_assign_name[] = {
139  "INT_ASSIGN_MIN",
140  "INT_ASSIGN_MED",
141  "INT_ASSIGN_MAX",
142  "INT_ASSIGN_RND",
143  "INT_ASSIGN"
144  };
146  const int n_int_assign =
147  sizeof(int_assign_name)/sizeof(const char*);
149  int int_val(const Gecode::Space&, Gecode::IntVar x, int) {
150  return x.min();
151  }
154  return x.min();
155  }
157 
158  IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
159  : Base("Int::Assign::"+s), arity(a), dom(d) {
160  }
161 
162  bool
163  IntTest::run(void) {
164  using namespace Gecode;
165  IntTestSpace* root = new IntTestSpace(arity,dom);
166  post(*root, root->x);
167  (void) root->status();
168 
169  for (int val = 0; val<n_int_assign; val++) {
170  IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone(false));
172  o.a_d = Base::rand(10);
173  o.c_d = Base::rand(10);
174 
175  Rnd r(1);
176  IntAssign ia;
177  switch (val) {
178  case 0: ia = INT_ASSIGN_MIN(); break;
179  case 1: ia = INT_ASSIGN_MED(); break;
180  case 2: ia = INT_ASSIGN_MAX(); break;
181  case 3: ia = INT_ASSIGN_RND(r); break;
182  case 4: ia = INT_ASSIGN(&int_val); break;
183  }
184 
185  assign(*clone, clone->x, ia);
186  Gecode::DFS<IntTestSpace> e_s(clone, o);
187  delete clone;
188 
189  // Find number of solutions
190  int solutions = 0;
191  while (Space* s = e_s.next()) {
192  delete s; solutions++;
193  }
194  if (solutions != 1) {
195  std::cout << "FAILURE" << std::endl
196  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
197  << "\t" << int_assign_name[val] << std::endl;
198  delete root;
199  return false;
200  }
201  }
202  delete root;
203  return true;
204  }
205 
206  BoolTest::BoolTest(const std::string& s, int a)
207  : Base("Bool::Assign::"+s), arity(a) {
208  }
209 
210  bool
212  using namespace Gecode;
213  BoolTestSpace* root = new BoolTestSpace(arity);
214  post(*root, root->x);
215  (void) root->status();
216 
217  for (int val = n_int_assign; val--; ) {
218  BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone(false));
220  o.a_d = Base::rand(10);
221  o.c_d = Base::rand(10);
222  Rnd r(1);
223  IntAssign ia;
224  switch (val) {
225  case 0: ia = INT_ASSIGN_MIN(); break;
226  case 1: ia = INT_ASSIGN_MED(); break;
227  case 2: ia = INT_ASSIGN_MAX(); break;
228  case 3: ia = INT_ASSIGN_RND(r); break;
229  case 4: ia = INT_ASSIGN(&bool_val); break;
230  }
231 
232  assign(*clone, clone->x, ia);
233  Gecode::DFS<BoolTestSpace> e_s(clone, o);
234  delete clone;
235 
236  // Find number of solutions
237  int solutions = 0;
238  while (Space* s = e_s.next()) {
239  delete s; solutions++;
240  }
241  if (solutions != 1) {
242  std::cout << "FAILURE" << std::endl
243  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
244  << "\t" << int_assign_name[val] << std::endl;
245  delete root;
246  return false;
247  }
248  }
249  delete root;
250  return true;
251  }
252 
253 #ifdef GECODE_HAS_SET_VARS
254 
260 
261  const char* set_assign_name[] = {
262  "SET_ASSIGN_MIN_INC",
263  "SET_ASSIGN_MIN_EXC",
264  "SET_ASSIGN_MED_INC",
265  "SET_ASSIGN_MED_EXC",
266  "SET_ASSIGN_MAX_INC",
267  "SET_ASSIGN_MAX_EXC",
268  "SET_ASSIGN_RND_INC",
269  "SET_ASSIGN_RND_EXC",
270  "SET_ASSIGN"
271  };
273  const int n_set_assign =
274  sizeof(set_assign_name)/sizeof(const char*);
276  int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
278  return r.min();
279  }
281 
282  SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
283  : Base("Set::Assign::"+s), arity(a), dom(d) {
284  }
285 
286  bool
287  SetTest::run(void) {
288  using namespace Gecode;
289  SetTestSpace* root = new SetTestSpace(arity,dom);
290  post(*root, root->x);
291  (void) root->status();
292 
293  for (int val = n_int_assign; val--; ) {
294  SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone(false));
296  o.a_d = Base::rand(10);
297  o.c_d = Base::rand(10);
298 
299  Rnd r(1);
300 
301  SetAssign sa;
302  switch (val) {
303  case 0: sa = SET_ASSIGN_MIN_INC(); break;
304  case 1: sa = SET_ASSIGN_MIN_EXC(); break;
305  case 2: sa = SET_ASSIGN_MED_INC(); break;
306  case 3: sa = SET_ASSIGN_MED_EXC(); break;
307  case 4: sa = SET_ASSIGN_MAX_INC(); break;
308  case 5: sa = SET_ASSIGN_MAX_EXC(); break;
309  case 6: sa = SET_ASSIGN_RND_INC(r); break;
310  case 7: sa = SET_ASSIGN_RND_EXC(r); break;
311  case 8: sa = SET_ASSIGN(&set_val); break;
312  }
313 
314  assign(*clone, clone->x, sa);
315  Gecode::DFS<SetTestSpace> e_s(clone, o);
316  delete clone;
317 
318  // Find number of solutions
319  int solutions = 0;
320  while (Space* s = e_s.next()) {
321  delete s; solutions++;
322  }
323  if (solutions != 1) {
324  std::cout << "FAILURE" << std::endl
325  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
326  << "\t" << set_assign_name[val] << std::endl;
327  delete root;
328  return false;
329  }
330  }
331  delete root;
332  return true;
333  }
334 
335 #endif
336 
337 #ifdef GECODE_HAS_FLOAT_VARS
338 
344 
345  const char* float_assign_name[] = {
346  "FLOAT_ASSIGN_MIN",
347  "FLOAT_ASSIGN_MAX",
348  "FLOAT_ASSIGN_RND",
349  "FLOAT_ASSIGN"
350  };
352  const int n_float_assign =
353  sizeof(float_assign_name)/sizeof(const char*);
356  return x.min();
357  }
359 
360  FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
361  : Base("Float::Assign::"+s), arity(a), dom(d) {
362  }
363 
364  bool
366  using namespace Gecode;
367  FloatTestSpace* root = new FloatTestSpace(arity,dom);
368  post(*root, root->x);
369  (void) root->status();
370 
371  for (int val = n_float_assign; val--; ) {
372  FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone(false));
374  o.a_d = Base::rand(10);
375  o.c_d = Base::rand(10);
376 
377  Rnd r(1);
378 
379  FloatAssign fa;
380  switch (val) {
381  case 0: fa = FLOAT_ASSIGN_MIN(); break;
382  case 1: fa = FLOAT_ASSIGN_MAX(); break;
383  case 2: fa = FLOAT_ASSIGN_RND(r); break;
384  case 3: fa = FLOAT_ASSIGN(&float_val); break;
385  }
386 
387  assign(*clone, clone->x, fa);
388  Gecode::DFS<FloatTestSpace> e_s(clone, o);
389  delete clone;
390 
391  // Find number of solutions
392  int solutions = 0;
393  while (Space* s = e_s.next()) {
394  delete s; solutions++;
395  }
396  if (solutions != 1) {
397  std::cout << "FAILURE" << std::endl
398  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
399  << "\t" << float_assign_name[val] << std::endl;
400  delete root;
401  return false;
402  }
403  }
404  delete root;
405  return true;
406  }
407 
408 #endif
409 
410 }}
411 
412 // STATISTICS: test-branch