Generated on Fri Jan 10 2020 11:38:25 for Gecode by doxygen 1.8.16
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  * 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) {}
54  : Gecode::Space(s) {
55  x.update(*this, s.x);
56  }
58  virtual Gecode::Space* copy(void) {
59  return new IntTestSpace(*this);
60  }
61  };
62 
64  class BoolTestSpace : public Gecode::Space {
65  public:
70  : x(*this, n, 0, 1) {}
73  : Gecode::Space(s) {
74  x.update(*this, s.x);
75  }
77  virtual Gecode::Space* copy(void) {
78  return new BoolTestSpace(*this);
79  }
80  };
81 
82 #ifdef GECODE_HAS_SET_VARS
83 
85  class SetTestSpace : public Gecode::Space {
86  public:
91  : x(*this, n, Gecode::IntSet::empty, d) {}
94  : Gecode::Space(s) {
95  x.update(*this, s.x);
96  }
98  virtual Gecode::Space* copy(void) {
99  return new SetTestSpace(*this);
100  }
101  };
102 
103 #endif
104 
105 #ifdef GECODE_HAS_FLOAT_VARS
106 
108  class FloatTestSpace : public Gecode::Space {
109  public:
114  : x(*this, n, d.min(), d.max()) {}
117  : Gecode::Space(s) {
118  x.update(*this, s.x);
119  }
121  virtual Gecode::Space* copy(void) {
122  return new FloatTestSpace(*this);
123  }
124  };
125 
126 #endif
127 
133  const char* int_assign_name[] = {
135  "INT_ASSIGN_MIN",
136  "INT_ASSIGN_MED",
137  "INT_ASSIGN_MAX",
138  "INT_ASSIGN_RND",
139  "INT_ASSIGN"
140  };
142  const int n_int_assign =
143  sizeof(int_assign_name)/sizeof(const char*);
145  int int_val(const Gecode::Space&, Gecode::IntVar x, int) {
146  return x.min();
147  }
149 
155  const char* bool_assign_name[] = {
157  "BOOL_ASSIGN_MIN",
158  "BOOL_ASSIGN_MAX",
159  "BOOL_ASSIGN_RND",
160  "BOOL_ASSIGN"
161  };
163  const int n_bool_assign =
164  sizeof(bool_assign_name)/sizeof(const char*);
167  return x.min();
168  }
170 
171  IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
172  : Base("Int::Assign::"+s), arity(a), dom(d) {
173  }
174 
175  bool
176  IntTest::run(void) {
177  using namespace Gecode;
178  IntTestSpace* root = new IntTestSpace(arity,dom);
179  post(*root, root->x);
180  (void) root->status();
181 
182  for (int val = 0; val<n_int_assign; val++) {
183  IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone());
185  o.a_d = Base::rand(10);
186  o.c_d = Base::rand(10);
187 
188  Rnd r(1);
189  IntAssign ia;
190  switch (val) {
191  case 0: ia = INT_ASSIGN_MIN(); break;
192  case 1: ia = INT_ASSIGN_MED(); break;
193  case 2: ia = INT_ASSIGN_MAX(); break;
194  case 3: ia = INT_ASSIGN_RND(r); break;
195  case 4: ia = INT_ASSIGN(&int_val); break;
196  default: GECODE_NEVER;
197  }
198 
199  assign(*clone, clone->x, ia);
201  delete clone;
202 
203  // Find number of solutions
204  int solutions = 0;
205  while (Space* s = e_s.next()) {
206  delete s; solutions++;
207  }
208  if (solutions != 1) {
209  std::cout << "FAILURE" << std::endl
210  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
211  << "\t" << int_assign_name[val] << std::endl;
212  delete root;
213  return false;
214  }
215  }
216  delete root;
217  return true;
218  }
219 
220  BoolTest::BoolTest(const std::string& s, int a)
221  : Base("Bool::Assign::"+s), arity(a) {
222  }
223 
224  bool
225  BoolTest::run(void) {
226  using namespace Gecode;
227  BoolTestSpace* root = new BoolTestSpace(arity);
228  post(*root, root->x);
229  (void) root->status();
230 
231  for (int val = n_bool_assign; val--; ) {
232  BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone());
234  o.a_d = Base::rand(10);
235  o.c_d = Base::rand(10);
236  Rnd r(1);
237  BoolAssign ia;
238  switch (val) {
239  case 0: ia = BOOL_ASSIGN_MIN(); break;
240  case 1: ia = BOOL_ASSIGN_MAX(); break;
241  case 2: ia = BOOL_ASSIGN_RND(r); break;
242  case 3: ia = BOOL_ASSIGN(&bool_val); break;
243  default: GECODE_NEVER;
244  }
245 
246  assign(*clone, clone->x, ia);
248  delete clone;
249 
250  // Find number of solutions
251  int solutions = 0;
252  while (Space* s = e_s.next()) {
253  delete s; solutions++;
254  }
255  if (solutions != 1) {
256  std::cout << "FAILURE" << std::endl
257  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
258  << "\t" << int_assign_name[val] << std::endl;
259  delete root;
260  return false;
261  }
262  }
263  delete root;
264  return true;
265  }
266 
267 #ifdef GECODE_HAS_SET_VARS
268 
274  const char* set_assign_name[] = {
276  "SET_ASSIGN_MIN_INC",
277  "SET_ASSIGN_MIN_EXC",
278  "SET_ASSIGN_MED_INC",
279  "SET_ASSIGN_MED_EXC",
280  "SET_ASSIGN_MAX_INC",
281  "SET_ASSIGN_MAX_EXC",
282  "SET_ASSIGN_RND_INC",
283  "SET_ASSIGN_RND_EXC",
284  "SET_ASSIGN"
285  };
287  const int n_set_assign =
288  sizeof(set_assign_name)/sizeof(const char*);
290  int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
292  return r.min();
293  }
295 
296  SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
297  : Base("Set::Assign::"+s), arity(a), dom(d) {
298  }
299 
300  bool
301  SetTest::run(void) {
302  using namespace Gecode;
303  SetTestSpace* root = new SetTestSpace(arity,dom);
304  post(*root, root->x);
305  (void) root->status();
306 
307  for (int val = n_set_assign; val--; ) {
308  SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone());
310  o.a_d = Base::rand(10);
311  o.c_d = Base::rand(10);
312 
313  Rnd r(1);
314 
315  SetAssign sa;
316  switch (val) {
317  case 0: sa = SET_ASSIGN_MIN_INC(); break;
318  case 1: sa = SET_ASSIGN_MIN_EXC(); break;
319  case 2: sa = SET_ASSIGN_MED_INC(); break;
320  case 3: sa = SET_ASSIGN_MED_EXC(); break;
321  case 4: sa = SET_ASSIGN_MAX_INC(); break;
322  case 5: sa = SET_ASSIGN_MAX_EXC(); break;
323  case 6: sa = SET_ASSIGN_RND_INC(r); break;
324  case 7: sa = SET_ASSIGN_RND_EXC(r); break;
325  case 8: sa = SET_ASSIGN(&set_val); break;
326  default: GECODE_NEVER;
327  }
328 
329  assign(*clone, clone->x, sa);
331  delete clone;
332 
333  // Find number of solutions
334  int solutions = 0;
335  while (Space* s = e_s.next()) {
336  delete s; solutions++;
337  }
338  if (solutions != 1) {
339  std::cout << "FAILURE" << std::endl
340  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
341  << "\t" << set_assign_name[val] << std::endl;
342  delete root;
343  return false;
344  }
345  }
346  delete root;
347  return true;
348  }
349 
350 #endif
351 
352 #ifdef GECODE_HAS_FLOAT_VARS
353 
359  const char* float_assign_name[] = {
361  "FLOAT_ASSIGN_MIN",
362  "FLOAT_ASSIGN_MAX",
363  "FLOAT_ASSIGN_RND",
364  "FLOAT_ASSIGN"
365  };
367  const int n_float_assign =
368  sizeof(float_assign_name)/sizeof(const char*);
371  Gecode::FloatVar x, int) {
372  Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
373  return nl;
374  }
376 
377  FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
378  : Base("Float::Assign::"+s), arity(a), dom(d) {
379  }
380 
381  bool
382  FloatTest::run(void) {
383  using namespace Gecode;
384  FloatTestSpace* root = new FloatTestSpace(arity,dom);
385  post(*root, root->x);
386  (void) root->status();
387 
388  for (int val = n_float_assign; val--; ) {
389  FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone());
391  o.a_d = Base::rand(10);
392  o.c_d = Base::rand(10);
393 
394  Rnd r(1);
395 
396  FloatAssign fa;
397  switch (val) {
398  case 0: fa = FLOAT_ASSIGN_MIN(); break;
399  case 1: fa = FLOAT_ASSIGN_MAX(); break;
400  case 2: fa = FLOAT_ASSIGN_RND(r); break;
401  case 3: fa = FLOAT_ASSIGN(&float_val); break;
402  default: GECODE_NEVER;
403  }
404 
405  assign(*clone, clone->x, fa);
407  delete clone;
408 
409  // Find number of solutions
410  int solutions = 0;
411  while (Space* s = e_s.next()) {
412  delete s; solutions++;
413  }
414  if (solutions != 1) {
415  std::cout << "FAILURE" << std::endl
416  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
417  << "\t" << float_assign_name[val] << std::endl;
418  delete root;
419  return false;
420  }
421  }
422  delete root;
423  return true;
424  }
425 
426 #endif
427 
428 }}
429 
430 // STATISTICS: test-branch
bool l
Whether to try the lower or upper half first.
Definition: float.hh:1467
Region r
Definition: region.cpp:65
Gecode::BoolVarArray x
Variables to be tested.
Definition: assign.cpp:67
BoolAssign BOOL_ASSIGN_RND(Rnd r)
Select random value.
Definition: assign.hpp:110
BoolAssign BOOL_ASSIGN_MIN(void)
Select smallest value.
Definition: assign.hpp:100
FloatAssign FLOAT_ASSIGN_MAX(void)
Select median value of the upper part.
Definition: assign.hpp:60
Gecode::IntSet dom
Domain of variables.
Definition: assign.hh:66
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition: assign.cpp:121
BoolTestSpace(int n)
Initialize test space.
Definition: assign.cpp:69
FloatTestSpace(FloatTestSpace &s)
Constructor for cloning s.
Definition: assign.cpp:116
virtual bool run(void)
Perform test.
Search engine options
Definition: search.hh:746
SetAssign SET_ASSIGN_MIN_INC(void)
Definition: assign.hpp:55
int arity
Number of variables.
Definition: assign.hh:83
IntTest(const std::string &s, int a, const Gecode::IntSet &d)
Construct and register test.
int solutions(TestSpace *c, Gecode::Search::Options &o, int maxNbSol=-1)
Find number of solutions.
Definition: branch.cpp:412
SetTestSpace(SetTestSpace &s)
Constructor for cloning s.
Definition: assign.cpp:93
FloatTestSpace(int n, const Gecode::FloatVal &d)
Initialize test space.
Definition: assign.cpp:113
IntTestSpace(int n, Gecode::IntSet &d)
Initialize test space.
Definition: assign.cpp:50
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition: assign.cpp:77
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition: assign.cpp:58
const FloatNum min
Smallest allowed float value.
Definition: float.hh:846
SetAssign SET_ASSIGN_MED_INC(void)
Definition: assign.hpp:65
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)=0
Post assignment on variables x.
Computation spaces.
Definition: core.hpp:1742
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)=0
Post assignment on variables x.
Depth-first search engine.
Definition: search.hh:1036
Gecode::SetVarArray x
Variables to be tested.
Definition: assign.cpp:88
int arity
Number of variables.
Definition: assign.hh:102
Integer variable array.
Definition: int.hh:763
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:134
Which value to select for assignment.
Definition: set.hh:1517
FloatTest(const std::string &s, int a, const Gecode::FloatVal &d)
Construct and register test.
const bool clone
Whether engines create a clone when being initialized.
Definition: search.hh:108
virtual bool run(void)
Perform test.
SetAssign SET_ASSIGN_MAX_EXC(void)
Definition: assign.hpp:80
FloatAssign FLOAT_ASSIGN_RND(Rnd r)
Select median value of a randomly chosen part.
Definition: assign.hpp:65
Gecode toplevel namespace
Base class for all tests to be run
Definition: test.hh:103
Integer sets.
Definition: int.hh:174
unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance)
Definition: search.hh:755
IntTestSpace(IntTestSpace &s)
Constructor for cloning s.
Definition: assign.cpp:53
virtual void post(Gecode::Space &home, Gecode::SetVarArray &x)=0
Post assignment on variables x.
Boolean variable array.
Definition: int.hh:808
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Which values to select for assignment.
Definition: int.hh:4971
int arity
Number of variables.
Definition: assign.hh:125
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition: assign.cpp:98
FloatAssign FLOAT_ASSIGN_MIN(void)
Select median value of the lower part.
Definition: assign.hpp:55
int bool_val(const Gecode::Space &, Gecode::BoolVar x, int)
Test function for branch value function.
Definition: assign.cpp:166
BoolTestSpace(BoolTestSpace &s)
Constructor for cloning s.
Definition: assign.cpp:72
SetAssign SET_ASSIGN_RND_INC(Rnd r)
Definition: assign.hpp:85
BoolTest(const std::string &s, int a)
Construct and register test.
Gecode::IntSet dom
Upper bound of variable domains.
Definition: assign.hh:104
SetAssign SET_ASSIGN_MED_EXC(void)
Definition: assign.hpp:70
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:40
Gecode::FloatVarArray x
Variables to be tested.
Definition: assign.cpp:111
Boolean integer variables.
Definition: int.hh:512
Gecode::FloatVal dom
Domain of variables.
Definition: assign.hh:127
Set variables
Definition: set.hh:127
SetAssign SET_ASSIGN_RND_EXC(Rnd r)
Definition: assign.hpp:90
Value description class for branching.
Definition: float.hh:1462
FloatNum n
The middle value for branching.
Definition: float.hh:1465
SetAssign SET_ASSIGN_MAX_INC(void)
Definition: assign.hpp:75
SetAssign SET_ASSIGN_MIN_EXC(void)
Definition: assign.hpp:60
void assign(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatAssign vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Assign all x with variable selection vars and value selection vals.
Definition: branch.cpp:111
Random number generator.
Definition: rnd.hpp:42
IntAssign INT_ASSIGN_MAX(void)
Select largest value.
Definition: assign.hpp:65
FloatAssign FLOAT_ASSIGN(FloatBranchVal v, FloatBranchCommit c)
Definition: assign.hpp:70
Integer variables.
Definition: int.hh:371
virtual bool run(void)
Perform test.
IntAssign INT_ASSIGN(IntBranchVal v, IntBranchCommit c)
Select value as defined by the value function v and commit function c.
Definition: assign.hpp:75
Space for executing integer tests.
Definition: assign.cpp:45
Gecode::FloatNumBranch float_val(const Gecode::Space &, Gecode::FloatVar x, int)
Test function for branch value function.
Definition: assign.cpp:370
IntAssign INT_ASSIGN_RND(Rnd r)
Select random value.
Definition: assign.hpp:70
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56
SetAssign SET_ASSIGN(SetBranchVal v, SetBranchCommit c)
Select value as defined by the value function v and commit function c.
Definition: assign.hpp:95
Space(void)
Default constructor.
Definition: core.cpp:115
Float value type.
Definition: float.hh:334
virtual void post(Gecode::Space &home, Gecode::BoolVarArray &x)=0
Post assignment on variables x.
Gecode::IntVarArray x
Variables to be tested.
Definition: assign.cpp:48
Float variables.
Definition: float.hh:870
virtual bool run(void)
Perform test.
Space for executing Boolean tests.
Definition: assign.cpp:108
Which values to select for assignment.
Definition: float.hh:1874
int set_val(const Gecode::Space &, Gecode::SetVar x, int)
Test function for branch value function.
Definition: assign.cpp:290
Gecode::IntSet d(v, 7)
int arity
Number of variables.
Definition: assign.hh:64
IntAssign INT_ASSIGN_MED(void)
Select greatest value not greater than the median.
Definition: assign.hpp:60
BoolAssign BOOL_ASSIGN_MAX(void)
Select largest value.
Definition: assign.hpp:105
SetTest(const std::string &s, int a, const Gecode::IntSet &d)
Construct and register test.
Float variable array.
Definition: float.hh:1030
SetTestSpace(int n, const Gecode::IntSet &d)
Initialize test space.
Definition: assign.cpp:90
General test support.
Definition: afc.cpp:39
Iterator for the unknown ranges of a set variable.
Definition: set.hh:334
Space for executing Boolean tests.
Definition: assign.cpp:64
Space for executing Boolean tests.
Definition: assign.cpp:85
Which values to select for assignment.
Definition: int.hh:5000
Set variable array
Definition: set.hh:570
int int_val(const Gecode::Space &, Gecode::IntVar x, int)
Test function for branch value function.
Definition: assign.cpp:145
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
BoolAssign BOOL_ASSIGN(BoolBranchVal v, BoolBranchCommit c)
Select value as defined by the value function v and commit function c.
Definition: assign.hpp:115
Base(const std::string &s)
Create and register test with name s.
Definition: test.cpp:59
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1013
IntAssign INT_ASSIGN_MIN(void)
Select smallest value.
Definition: assign.hpp:55