File: assign.cpp

package info (click to toggle)
gecode 6.1.0-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 26,372 kB
  • sloc: cpp: 328,292; perl: 2,048; makefile: 1,793; sh: 214
file content (426 lines) | stat: -rw-r--r-- 11,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Contributing authors:
 *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
 *
 *  Copyright:
 *     Christian Schulte, 2008
 *     Vincent Barichard, 2012
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "test/assign.hh"

#include <gecode/search.hh>

namespace Test { namespace Assign {

  /// Space for executing integer tests
  class IntTestSpace : public Gecode::Space {
  public:
    /// Variables to be tested
    Gecode::IntVarArray x;
    /// Initialize test space
    IntTestSpace(int n, Gecode::IntSet& d)
      : x(*this, n, d) {}
    /// Constructor for cloning \a s
    IntTestSpace(IntTestSpace& s)
      : Gecode::Space(s) {
      x.update(*this, s.x);
    }
    /// Copy space during cloning
    virtual Gecode::Space* copy(void) {
      return new IntTestSpace(*this);
    }
  };

  /// Space for executing Boolean tests
  class BoolTestSpace : public Gecode::Space {
  public:
    /// Variables to be tested
    Gecode::BoolVarArray x;
    /// Initialize test space
    BoolTestSpace(int n)
      : x(*this, n, 0, 1) {}
    /// Constructor for cloning \a s
    BoolTestSpace(BoolTestSpace& s)
      : Gecode::Space(s) {
      x.update(*this, s.x);
    }
    /// Copy space during cloning
    virtual Gecode::Space* copy(void) {
      return new BoolTestSpace(*this);
    }
  };

#ifdef GECODE_HAS_SET_VARS

  /// Space for executing Boolean tests
  class SetTestSpace : public Gecode::Space {
  public:
    /// Variables to be tested
    Gecode::SetVarArray x;
    /// Initialize test space
    SetTestSpace(int n, const Gecode::IntSet& d)
      : x(*this, n, Gecode::IntSet::empty, d) {}
    /// Constructor for cloning \a s
    SetTestSpace(SetTestSpace& s)
      : Gecode::Space(s) {
      x.update(*this, s.x);
    }
    /// Copy space during cloning
    virtual Gecode::Space* copy(void) {
      return new SetTestSpace(*this);
    }
  };

#endif

#ifdef GECODE_HAS_FLOAT_VARS

  /// Space for executing Boolean tests
  class FloatTestSpace : public Gecode::Space {
  public:
    /// Variables to be tested
    Gecode::FloatVarArray x;
    /// Initialize test space
    FloatTestSpace(int n, const Gecode::FloatVal& d)
      : x(*this, n, d.min(), d.max()) {}
    /// Constructor for cloning \a s
    FloatTestSpace(FloatTestSpace& s)
      : Gecode::Space(s) {
      x.update(*this, s.x);
    }
    /// Copy space during cloning
    virtual Gecode::Space* copy(void) {
      return new FloatTestSpace(*this);
    }
  };

#endif

  /** \name Collection of possible arguments for integer assignments
   *
   * \relates IntTestSpace
   */
  //@{
  /// Names for integer assignments
  const char* int_assign_name[] = {
    "INT_ASSIGN_MIN",
    "INT_ASSIGN_MED",
    "INT_ASSIGN_MAX",
    "INT_ASSIGN_RND",
    "INT_ASSIGN"
  };
  /// Number of integer value selections
  const int n_int_assign =
    sizeof(int_assign_name)/sizeof(const char*);
  /// Test function for branch value function
  int int_val(const Gecode::Space&, Gecode::IntVar x, int) {
    return x.min();
  }
  //@}

  /** \name Collection of possible arguments for Boolean assignments
   *
   * \relates BoolTestSpace
   */
  //@{
  /// Names for integer assignments
  const char* bool_assign_name[] = {
    "BOOL_ASSIGN_MIN",
    "BOOL_ASSIGN_MAX",
    "BOOL_ASSIGN_RND",
    "BOOL_ASSIGN"
  };
  /// Number of integer value selections
  const int n_bool_assign =
    sizeof(bool_assign_name)/sizeof(const char*);
  /// Test function for branch value function
  int bool_val(const Gecode::Space&, Gecode::BoolVar x, int) {
    return x.min();
  }
  //@}

  IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
    : Base("Int::Assign::"+s), arity(a), dom(d) {
  }

  bool
  IntTest::run(void) {
    using namespace Gecode;
    IntTestSpace* root = new IntTestSpace(arity,dom);
    post(*root, root->x);
    (void) root->status();

    for (int val = 0; val<n_int_assign; val++) {
      IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone());
      Gecode::Search::Options o;
      o.a_d = Base::rand(10);
      o.c_d = Base::rand(10);

      Rnd r(1);
      IntAssign ia;
      switch (val) {
      case 0: ia = INT_ASSIGN_MIN(); break;
      case 1: ia = INT_ASSIGN_MED(); break;
      case 2: ia = INT_ASSIGN_MAX(); break;
      case 3: ia = INT_ASSIGN_RND(r); break;
      case 4: ia = INT_ASSIGN(&int_val); break;
      }

      assign(*clone, clone->x, ia);
      Gecode::DFS<IntTestSpace> e_s(clone, o);
      delete clone;

      // Find number of solutions
      int solutions = 0;
      while (Space* s = e_s.next()) {
        delete s; solutions++;
      }
      if (solutions != 1) {
        std::cout << "FAILURE" << std::endl
                  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
                  << "\t" << int_assign_name[val] << std::endl;
        delete root;
        return false;
      }
    }
    delete root;
    return true;
  }

  BoolTest::BoolTest(const std::string& s, int a)
    : Base("Bool::Assign::"+s), arity(a) {
  }

  bool
  BoolTest::run(void) {
    using namespace Gecode;
    BoolTestSpace* root = new BoolTestSpace(arity);
    post(*root, root->x);
    (void) root->status();

    for (int val = n_bool_assign; val--; ) {
      BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone());
      Gecode::Search::Options o;
      o.a_d = Base::rand(10);
      o.c_d = Base::rand(10);
      Rnd r(1);
      BoolAssign ia;
      switch (val) {
      case 0: ia = BOOL_ASSIGN_MIN(); break;
      case 1: ia = BOOL_ASSIGN_MAX(); break;
      case 2: ia = BOOL_ASSIGN_RND(r); break;
      case 3: ia = BOOL_ASSIGN(&bool_val); break;
      }

      assign(*clone, clone->x, ia);
      Gecode::DFS<BoolTestSpace> e_s(clone, o);
      delete clone;

      // Find number of solutions
      int solutions = 0;
      while (Space* s = e_s.next()) {
        delete s; solutions++;
      }
      if (solutions != 1) {
        std::cout << "FAILURE" << std::endl
                  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
                  << "\t" << int_assign_name[val] << std::endl;
        delete root;
        return false;
      }
    }
    delete root;
    return true;
  }

#ifdef GECODE_HAS_SET_VARS

  /** \name Collection of possible arguments for set assignments
   *
   * \relates SetTestSpace
   */
  //@{
  /// Names for integer assignments
  const char* set_assign_name[] = {
    "SET_ASSIGN_MIN_INC",
    "SET_ASSIGN_MIN_EXC",
    "SET_ASSIGN_MED_INC",
    "SET_ASSIGN_MED_EXC",
    "SET_ASSIGN_MAX_INC",
    "SET_ASSIGN_MAX_EXC",
    "SET_ASSIGN_RND_INC",
    "SET_ASSIGN_RND_EXC",
    "SET_ASSIGN"
  };
  /// Number of set value selections
  const int n_set_assign =
    sizeof(set_assign_name)/sizeof(const char*);
  /// Test function for branch value function
  int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
    Gecode::SetVarUnknownRanges r(x);
    return r.min();
  }
  //@}

  SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
    : Base("Set::Assign::"+s), arity(a), dom(d) {
  }

  bool
  SetTest::run(void) {
    using namespace Gecode;
    SetTestSpace* root = new SetTestSpace(arity,dom);
    post(*root, root->x);
    (void) root->status();

    for (int val = n_int_assign; val--; ) {
      SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone());
      Gecode::Search::Options o;
      o.a_d = Base::rand(10);
      o.c_d = Base::rand(10);

      Rnd r(1);

      SetAssign sa;
      switch (val) {
      case 0: sa = SET_ASSIGN_MIN_INC(); break;
      case 1: sa = SET_ASSIGN_MIN_EXC(); break;
      case 2: sa = SET_ASSIGN_MED_INC(); break;
      case 3: sa = SET_ASSIGN_MED_EXC(); break;
      case 4: sa = SET_ASSIGN_MAX_INC(); break;
      case 5: sa = SET_ASSIGN_MAX_EXC(); break;
      case 6: sa = SET_ASSIGN_RND_INC(r); break;
      case 7: sa = SET_ASSIGN_RND_EXC(r); break;
      case 8: sa = SET_ASSIGN(&set_val); break;
      }

      assign(*clone, clone->x, sa);
      Gecode::DFS<SetTestSpace> e_s(clone, o);
      delete clone;

      // Find number of solutions
      int solutions = 0;
      while (Space* s = e_s.next()) {
        delete s; solutions++;
      }
      if (solutions != 1) {
        std::cout << "FAILURE" << std::endl
                  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
                  << "\t" << set_assign_name[val] << std::endl;
        delete root;
        return false;
      }
    }
    delete root;
    return true;
  }

#endif

#ifdef GECODE_HAS_FLOAT_VARS

  /** \name Collection of possible arguments for float assignments
   *
   * \relates FloatTestSpace
   */
  //@{
  /// Names for float assignments
  const char* float_assign_name[] = {
    "FLOAT_ASSIGN_MIN",
    "FLOAT_ASSIGN_MAX",
    "FLOAT_ASSIGN_RND",
    "FLOAT_ASSIGN"
  };
  /// Number of float value selections
  const int n_float_assign =
    sizeof(float_assign_name)/sizeof(const char*);
  /// Test function for branch value function
  Gecode::FloatNumBranch float_val(const Gecode::Space&,
                                   Gecode::FloatVar x, int) {
    Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
    return nl;
  }
  //@}

  FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
    : Base("Float::Assign::"+s), arity(a), dom(d) {
  }

  bool
  FloatTest::run(void) {
    using namespace Gecode;
    FloatTestSpace* root = new FloatTestSpace(arity,dom);
    post(*root, root->x);
    (void) root->status();

    for (int val = n_float_assign; val--; ) {
      FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone());
      Gecode::Search::Options o;
      o.a_d = Base::rand(10);
      o.c_d = Base::rand(10);

      Rnd r(1);

      FloatAssign fa;
      switch (val) {
      case 0: fa = FLOAT_ASSIGN_MIN(); break;
      case 1: fa = FLOAT_ASSIGN_MAX(); break;
      case 2: fa = FLOAT_ASSIGN_RND(r); break;
      case 3: fa = FLOAT_ASSIGN(&float_val); break;
      }

      assign(*clone, clone->x, fa);
      Gecode::DFS<FloatTestSpace> e_s(clone, o);
      delete clone;

      // Find number of solutions
      int solutions = 0;
      while (Space* s = e_s.next()) {
        delete s; solutions++;
      }
      if (solutions != 1) {
        std::cout << "FAILURE" << std::endl
                  << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
                  << "\t" << float_assign_name[val] << std::endl;
        delete root;
        return false;
      }
    }
    delete root;
    return true;
  }

#endif

}}

// STATISTICS: test-branch