Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
42namespace Test { namespace Assign {
43
45 class IntTestSpace : public Gecode::Space {
46 public:
51 : x(*this, n, d) {}
52
54 : Gecode::Space(s) {
55 x.update(*this, s.x);
56 }
57
58 virtual Gecode::Space* copy(void) {
59 return new IntTestSpace(*this);
60 }
61 };
62
65 public:
70 : x(*this, n, 0, 1) {}
71
73 : Gecode::Space(s) {
74 x.update(*this, s.x);
75 }
76
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) {}
92
94 : Gecode::Space(s) {
95 x.update(*this, s.x);
96 }
97
98 virtual Gecode::Space* copy(void) {
99 return new SetTestSpace(*this);
100 }
101 };
102
103#endif
104
105#ifdef GECODE_HAS_FLOAT_VARS
106
109 public:
114 : x(*this, n, d.min(), d.max()) {}
115
117 : Gecode::Space(s) {
118 x.update(*this, s.x);
119 }
120
121 virtual Gecode::Space* copy(void) {
122 return new FloatTestSpace(*this);
123 }
124 };
125
126#endif
127
133
134 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 };
141
142 const int n_int_assign =
143 sizeof(int_assign_name)/sizeof(const char*);
146 return x.min();
147 }
148
149
155
156 const char* bool_assign_name[] = {
157 "BOOL_ASSIGN_MIN",
158 "BOOL_ASSIGN_MAX",
159 "BOOL_ASSIGN_RND",
160 "BOOL_ASSIGN"
161 };
162
163 const int n_bool_assign =
164 sizeof(bool_assign_name)/sizeof(const char*);
167 return x.min();
168 }
169
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);
200 Gecode::DFS<IntTestSpace> e_s(clone, o);
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());
233 Gecode::Search::Options o;
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);
247 Gecode::DFS<BoolTestSpace> e_s(clone, o);
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
275 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 };
286
287 const int n_set_assign =
288 sizeof(set_assign_name)/sizeof(const char*);
292 return r.min();
293 }
294
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);
330 Gecode::DFS<SetTestSpace> e_s(clone, o);
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
360 const char* float_assign_name[] = {
361 "FLOAT_ASSIGN_MIN",
362 "FLOAT_ASSIGN_MAX",
363 "FLOAT_ASSIGN_RND",
364 "FLOAT_ASSIGN"
365 };
366
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 }
375
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);
406 Gecode::DFS<FloatTestSpace> e_s(clone, o);
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
Boolean variable array.
Definition int.hh:820
Boolean integer variables.
Definition int.hh:515
Depth-first search engine.
Definition search.hh:1036
Which values to select for assignment.
Definition float.hh:1880
Value description class for branching.
Definition float.hh:1468
bool l
Whether to try the lower or upper half first.
Definition float.hh:1473
FloatNum n
The middle value for branching.
Definition float.hh:1471
Float value type.
Definition float.hh:334
Float variable array.
Definition float.hh:1033
Float variables.
Definition float.hh:870
Which values to select for assignment.
Definition int.hh:4986
Integer sets.
Definition int.hh:174
Integer variable array.
Definition int.hh:772
Integer variables.
Definition int.hh:371
Random number generator.
Definition rnd.hpp:42
Search engine options
Definition search.hh:746
unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition search.hh:753
unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance)
Definition search.hh:755
Which value to select for assignment.
Definition set.hh:1523
Set variable array
Definition set.hh:573
Iterator for the unknown ranges of a set variable.
Definition set.hh:337
Set variables
Definition set.hh:127
Computation spaces.
Definition core.hpp:1744
const char * bool_assign_name[]
Names for integer assignments.
Definition assign.cpp:156
BoolTestSpace(int n)
Initialize test space.
Definition assign.cpp:69
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:77
const int n_bool_assign
Number of integer value selections.
Definition assign.cpp:163
Gecode::BoolVarArray x
Variables to be tested.
Definition assign.cpp:67
BoolTestSpace(BoolTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:72
int bool_val(const Gecode::Space &, Gecode::BoolVar x, int)
Test function for branch value function.
Definition assign.cpp:166
virtual bool run(void)
Perform test.
BoolTest(const std::string &s, int a)
Construct and register test.
int arity
Number of variables.
Definition assign.hh:83
virtual void post(Gecode::Space &home, Gecode::BoolVarArray &x)=0
Post assignment on variables x.
Gecode::FloatNumBranch float_val(const Gecode::Space &, Gecode::FloatVar x, int)
Test function for branch value function.
Definition assign.cpp:370
Gecode::FloatVarArray x
Variables to be tested.
Definition assign.cpp:111
const int n_float_assign
Number of float value selections.
Definition assign.cpp:367
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:121
FloatTestSpace(FloatTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:116
const char * float_assign_name[]
Names for float assignments.
Definition assign.cpp:360
FloatTestSpace(int n, const Gecode::FloatVal &d)
Initialize test space.
Definition assign.cpp:113
FloatTest(const std::string &s, int a, const Gecode::FloatVal &d)
Construct and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)=0
Post assignment on variables x.
int arity
Number of variables.
Definition assign.hh:125
virtual bool run(void)
Perform test.
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:58
IntTestSpace(int n, Gecode::IntSet &d)
Initialize test space.
Definition assign.cpp:50
IntTestSpace(IntTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:53
const char * int_assign_name[]
Names for integer assignments.
Definition assign.cpp:134
int int_val(const Gecode::Space &, Gecode::IntVar x, int)
Test function for branch value function.
Definition assign.cpp:145
Gecode::IntVarArray x
Variables to be tested.
Definition assign.cpp:48
const int n_int_assign
Number of integer value selections.
Definition assign.cpp:142
virtual bool run(void)
Perform test.
IntTest(const std::string &s, int a, const Gecode::IntSet &d)
Construct and register test.
int arity
Number of variables.
Definition assign.hh:64
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)=0
Post assignment on variables x.
const int n_set_assign
Number of set value selections.
Definition assign.cpp:287
SetTestSpace(int n, const Gecode::IntSet &d)
Initialize test space.
Definition assign.cpp:90
int set_val(const Gecode::Space &, Gecode::SetVar x, int)
Test function for branch value function.
Definition assign.cpp:290
Gecode::SetVarArray x
Variables to be tested.
Definition assign.cpp:88
SetTestSpace(SetTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:93
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:98
const char * set_assign_name[]
Names for integer assignments.
Definition assign.cpp:275
int arity
Number of variables.
Definition assign.hh:102
virtual bool run(void)
Perform test.
virtual void post(Gecode::Space &home, Gecode::SetVarArray &x)=0
Post assignment on variables x.
SetTest(const std::string &s, int a, const Gecode::IntSet &d)
Construct and register test.
Base class for all tests to be run
Definition test.hh:103
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
void assign(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatAssign vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Assign all x with variable selection vars and value selection vals.
Definition branch.cpp:111
Space(void)
Default constructor.
Definition core.cpp:115
const bool clone
Whether engines create a clone when being initialized.
Definition search.hh:108
Gecode toplevel namespace
IntAssign INT_ASSIGN(IntBranchVal v, IntBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c.
Definition assign.hpp:75
IntAssign INT_ASSIGN_RND(Rnd r)
Select random value.
Definition assign.hpp:70
BoolAssign BOOL_ASSIGN_MAX(void)
Select largest value.
Definition assign.hpp:105
Exclude random element SetAssign SET_ASSIGN_RND_EXC(Rnd r)
Definition assign.hpp:90
SetAssign SET_ASSIGN_MED_EXC(void)
Definition assign.hpp:70
SetAssign SET_ASSIGN_MED_INC(void)
Definition assign.hpp:65
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
FloatAssign FLOAT_ASSIGN_MAX(void)
Select median value of the upper part.
Definition assign.hpp:60
Include largest element SetAssign SET_ASSIGN_MAX_INC(void)
Definition assign.hpp:75
BoolAssign BOOL_ASSIGN_RND(Rnd r)
Select random value.
Definition assign.hpp:110
Include random element SetAssign SET_ASSIGN_RND_INC(Rnd r)
Definition assign.hpp:85
IntAssign INT_ASSIGN_MED(void)
Select greatest value not greater than the median.
Definition assign.hpp:60
BoolAssign BOOL_ASSIGN(BoolBranchVal v, BoolBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c.
Definition assign.hpp:115
IntAssign INT_ASSIGN_MIN(void)
Select smallest value.
Definition assign.hpp:55
Exclude smallest element SetAssign SET_ASSIGN_MIN_EXC(void)
Definition assign.hpp:60
Include smallest element SetAssign SET_ASSIGN_MIN_INC(void)
Definition assign.hpp:55
SetAssign SET_ASSIGN(SetBranchVal v, SetBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c.
Definition assign.hpp:95
Exclude largest element 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
BoolAssign BOOL_ASSIGN_MIN(void)
Select smallest value.
Definition assign.hpp:100
IntAssign INT_ASSIGN_MAX(void)
Select largest value.
Definition assign.hpp:65
FloatAssign FLOAT_ASSIGN_MIN(void)
Select median value of the lower part.
Definition assign.hpp:55
FloatAssign FLOAT_ASSIGN(FloatBranchVal v, FloatBranchCommit c=nullptr)
Definition assign.hpp:70
Tests for assignments.
Definition assign.cpp:42
int solutions(TestSpace *c, Gecode::Search::Options &o, int maxNbSol=-1)
Find number of solutions.
Definition branch.cpp:412
Gecode::FloatVal a(-8, 5)
General test support.
Definition afc.cpp:39
Region r
Definition region.cpp:65
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56