File: rel-op.cpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (497 lines) | stat: -rwxr-xr-x 15,249 bytes parent folder | download | duplicates (4)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Guido Tack <tack@gecode.org>
 *
 *  Copyright:
 *     Guido Tack, 2005
 *
 *  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/set.hh"

using namespace Gecode;

namespace Test { namespace Set {

  /// %Tests for relation/operation constraints
  namespace RelOp {

    /**
      * \defgroup TaskTestSetRelOp Relation/operation constraints
      * \ingroup TaskTestSet
      */
    //@{

    static IntSet ds_22(-2,2);
    static IntSet ds_12(-1,2);

    /// %Test for ternary relation constraint
    class Rel : public SetTest {
    private:
      Gecode::SetOpType  sot;
      Gecode::SetRelType srt;
      int share;

      template<class I, class J>
      bool
      sol(I& i, J& j) const {
        switch (srt) {
        case SRT_EQ: return Iter::Ranges::equal(i,j);
        case SRT_NQ: return !Iter::Ranges::equal(i,j);
        case SRT_SUB: return Iter::Ranges::subset(i,j);
        case SRT_SUP: return Iter::Ranges::subset(j,i);
        case SRT_DISJ:
          {
            Gecode::Iter::Ranges::Inter<I,J> inter(i,j);
            return !inter();
          }
        case SRT_CMPL:
          {
            Gecode::Set::RangesCompl<J> jc(j);
            return Iter::Ranges::equal(i,jc);
          }
        default: GECODE_NEVER;
        }
        return false;
      }

    public:
      /// Create and register test
      Rel(Gecode::SetOpType sot0, Gecode::SetRelType srt0, int share0=0)
        : SetTest("RelOp::"+str(sot0)+"::"+str(srt0)+"::S"+str(share0),
                  share0 == 0 ? 3 : 2,ds_22,false)
        , sot(sot0), srt(srt0), share(share0) {}
      /// %Test whether \a x is solution
      bool solution(const SetAssignment& x) const {
        int a=0, b=0, c=0;
        switch (share) {
          case 0: a=x[0]; b=x[1]; c=x[2]; break;
          case 1: a=x[0]; b=x[0]; c=x[0]; break;
          case 2: a=x[0]; b=x[0]; c=x[1]; break;
          case 3: a=x[0]; b=x[1]; c=x[0]; break;
          case 4: a=x[0]; b=x[1]; c=x[1]; break;
        }
        CountableSetRanges xr0(x.lub, a);
        CountableSetRanges xr1(x.lub, b);
        CountableSetRanges xr2(x.lub, c);
        switch (sot) {
        case SOT_UNION:
          {
            Iter::Ranges::Union<CountableSetRanges, CountableSetRanges>
              u(xr0,xr1);
            return sol(u,xr2);
          }
          break;
        case SOT_DUNION:
          {
            Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges>
              inter(xr0,xr1);
            if (inter())
              return false;
            Iter::Ranges::Union<CountableSetRanges, CountableSetRanges>
              u(xr0,xr1);
            return sol(u,xr2);
          }
          break;
        case SOT_INTER:
          {
            Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges>
              u(xr0,xr1);
            return sol(u,xr2);
          }
          break;
        case SOT_MINUS:
          {
            Iter::Ranges::Diff<CountableSetRanges, CountableSetRanges>
              u(xr0,xr1);
            return sol(u,xr2);
          }
          break;
        default: GECODE_NEVER;
        }
        GECODE_NEVER;
        return false;
      }
      /// Post constraint on \a x
      void post(Space& home, SetVarArray& x, IntVarArray&) {
        SetVar a,b,c;
        switch (share) {
          case 0: a=x[0]; b=x[1]; c=x[2]; break;
          case 1: a=x[0]; b=x[0]; c=x[0]; break;
          case 2: a=x[0]; b=x[0]; c=x[1]; break;
          case 3: a=x[0]; b=x[1]; c=x[0]; break;
          case 4: a=x[0]; b=x[1]; c=x[1]; break;
        }
        Gecode::rel(home, a, sot, b, srt, c);
      }
    };

    /// Help class to create and register tests
    class Create {
    public:
      /// Perform creation and registration
      Create(void) {
        using namespace Gecode;
        for (SetRelTypes srts; srts(); ++srts) {
          for (SetOpTypes sots; sots(); ++sots) {
            for (int i=0; i<=4; i++) {
              (void) new Rel(sots.sot(),srts.srt(),i);
            }
          }
        }
      }
    };

    Create c;

    /// %Test for n-ary partition constraint
    class RelN : public SetTest {
    private:
      Gecode::SetOpType sot;
      int n;
      int shared;
      bool withConst;
      IntSet is;
    public:
      /// Create and register test
      RelN(Gecode::SetOpType sot0, int n0, int shared0, bool withConst0)
        : SetTest("RelOp::N::"+str(sot0)+"::"+str(n0)+"::S"+str(shared0)+
                  "::C"+str(withConst0 ? 1 : 0),
                  shared0 == 0 ? n0+1 : (shared0 <= 2 ? 3 : 2),ds_12,false)
        , sot(sot0), n(n0), shared(shared0), withConst(withConst0)
        , is(0,1) {
      }
      /// %Test whether \a x is solution
      bool solution(const SetAssignment& x) const {
        int realN = shared == 0 ? n : 3;

        CountableSetRanges* isrs = new CountableSetRanges[realN];

        switch (shared) {
        case 0:
          for (int i=realN; i--; )
            isrs[i].init(x.lub, x[i]);
          break;
        case 1:
          isrs[0].init(x.lub, x[0]);
          isrs[1].init(x.lub, x[0]);
          isrs[2].init(x.lub, x[1]);
          break;
        case 2:
          isrs[0].init(x.lub, x[0]);
          isrs[1].init(x.lub, x[1]);
          isrs[2].init(x.lub, x[2]);
          break;
        case 3:
          isrs[0].init(x.lub, x[0]);
          isrs[1].init(x.lub, x[1]);
          isrs[2].init(x.lub, x[0]);
          break;
        default:
          GECODE_NEVER;
        }

        int result = shared == 0 ? x.size() - 1 : (shared <= 2 ? 2 : 0);
        CountableSetRanges xnr(x.lub, x[result]);

        switch (sot) {
        case SOT_DUNION:
          {
            if (shared == 1 && (isrs[0]() || isrs[1]())) {
              delete[] isrs; return false;
            }
            if (shared == 3 && (isrs[0]() || isrs[2]())) {
              delete[] isrs; return false;
            }
            unsigned int cardSum = 0;
            if (shared == 1 || shared == 3) {
              CountableSetRanges x1r(x.lub, x[1]);
              cardSum = Iter::Ranges::size(x1r);
            } else {
              for (int i=0; i<realN; i++) {
                CountableSetRanges xir(x.lub, x[i]);
                cardSum += Iter::Ranges::size(xir);
              }
            }
            if (withConst)
              cardSum += 2;
            CountableSetRanges xnr2(x.lub, x[result]);
            if (cardSum != Iter::Ranges::size(xnr2)) {
              delete[] isrs; return false;
            }
          }
          // fall through to union case
        case SOT_UNION:
          {
            bool eq;
            if (withConst) {
              Region r;
              Iter::Ranges::NaryUnion u(r, isrs, realN);
              IntSetRanges isr(is);
              Iter::Ranges::Union<IntSetRanges,
                Iter::Ranges::NaryUnion> uu(isr, u);
              eq = Iter::Ranges::equal(uu, xnr);
            } else {
              Region r;
              Iter::Ranges::NaryUnion u(r, isrs, realN);
              eq = Iter::Ranges::equal(u, xnr);
            }
            delete [] isrs;
            return eq;
          }
        case SOT_INTER:
          {
            if (withConst) {
              bool eq;
              {
                Region r;
                Iter::Ranges::NaryInter u(r, isrs, realN);
                IntSetRanges isr(is);
                Iter::Ranges::Inter<IntSetRanges,
                  Iter::Ranges::NaryInter> uu(isr, u);
                eq = (realN == 0 ? Iter::Ranges::equal(isr, xnr) :
                           Iter::Ranges::equal(uu, xnr));
                delete [] isrs;
              }
              return eq;
            } else {
              if (realN == 0) {
                bool ret =
                  Iter::Ranges::size(xnr) ==  Gecode::Set::Limits::card;
                delete [] isrs;
                return ret;
              } else {
                bool eq;
                {
                  Region r;
                  Iter::Ranges::NaryInter u(r,isrs, realN);
                  eq = Iter::Ranges::equal(u, xnr);
                }
                delete [] isrs;
                return eq;
              }
            }
          }
        default:
          GECODE_NEVER;
        }
        GECODE_NEVER;
        return false;
      }
      /// Post constraint on \a x
      void post(Space& home, SetVarArray& x, IntVarArray&) {
        int size = shared == 0 ? x.size()-1 : 3;
        SetVarArgs xs(size);
        SetVar xn;
        switch (shared) {
        case 0:
          for (int i=x.size()-1; i--;)
            xs[i]=x[i];
          xn = x[x.size()-1];
          break;
        case 1:
          xs[0] = x[0]; xs[1] = x[0]; xs[2] = x[1]; xn = x[2];
          break;
        case 2:
          xs[0] = x[0]; xs[1] = x[1]; xs[2] = x[2]; xn = x[2];
          break;
        case 3:
          xs[0] = x[0]; xs[1] = x[1]; xs[2] = x[0]; xn = x[0];
          break;
        default:
          GECODE_NEVER;
          break;
        }
        if (!withConst)
          Gecode::rel(home, sot, xs, xn);
        else
          Gecode::rel(home, sot, xs, is, xn);
      }
    };

    /// Help class to create and register tests
    class CreateN {
    public:
      /// Perform creation and registration
      CreateN(void) {
        for (int wc=0; wc<=1; wc++) {
          for (int i=0; i<=3; i++) {
            (void) new RelN(Gecode::SOT_UNION, i, 0, wc);
            (void) new RelN(Gecode::SOT_DUNION, i, 0, wc);
            (void) new RelN(Gecode::SOT_INTER, i, 0, wc);

            if (i>0) {
              (void) new RelN(Gecode::SOT_UNION, 0, i, wc);
              (void) new RelN(Gecode::SOT_DUNION, 0, i, wc);
              (void) new RelN(Gecode::SOT_INTER, 0, i, wc);
            }
          }
        }
      }
    };

    CreateN cn;

    /// %Test for n-ary partition constraint
    class RelIntN : public SetTest {
    private:
      Gecode::SetOpType sot;
      int n;
      bool withConst;
      IntSet is;
    public:
      /// Create and register test
      RelIntN(Gecode::SetOpType sot0, int n0, bool withConst0)
        : SetTest("RelOp::IntN::"+str(sot0)+"::"+str(n0)+
                  "::C"+str(withConst0 ? 1 : 0),
                  1,ds_12,false,n0)
        , sot(sot0), n(n0), withConst(withConst0)
        , is(0,1) {
      }
      /// %Test whether \a x is solution
      bool solution(const SetAssignment& x) const {
        int* isrs = new int[n];
        for (int i=0; i<n; i++)
          isrs[i] = x.ints()[i];

        IntSet iss(isrs,n);
        CountableSetRanges xnr(x.lub, x[0]);

        switch (sot) {
        case SOT_DUNION:
          {
            IntSetRanges issr(iss);
            unsigned int cardSum = Iter::Ranges::size(issr);
            if (cardSum != static_cast<unsigned int>(n)) {
              delete[] isrs;
              return false;
            }
            if (withConst) {
              IntSetRanges isr(is);
              cardSum += Iter::Ranges::size(isr);
            }
            CountableSetRanges xnr2(x.lub, x[0]);
            if (cardSum != Iter::Ranges::size(xnr2)) {
              delete[] isrs;
              return false;
            }
          }
          // fall through to union case
        case SOT_UNION:
          {
            if (withConst) {
              IntSetRanges issr(iss);
              IntSetRanges isr(is);
              Iter::Ranges::Union<IntSetRanges,IntSetRanges > uu(isr, issr);
              delete[] isrs;
              return Iter::Ranges::equal(uu, xnr);
            } else {
              IntSetRanges issr(iss);
              delete[] isrs;
              return Iter::Ranges::equal(issr, xnr);
            }
          }
        case SOT_INTER:
          {
            bool allEqual = true;
            for (int i=1; i<n; i++) {
              if (isrs[i] != isrs[0]) {
                allEqual = false;
                break;
              }
            }
            if (withConst) {
              if (allEqual) {
                if (n == 0) {
                  IntSetRanges isr(is);
                  delete[] isrs;
                  return Iter::Ranges::equal(isr, xnr);
                } else {
                  Iter::Ranges::Singleton s(isrs[0],isrs[0]);
                  IntSetRanges isr(is);
                  Iter::Ranges::Inter<IntSetRanges,Iter::Ranges::Singleton>
                    uu(isr, s);
                  delete[] isrs;
                  return Iter::Ranges::equal(uu, xnr);
                }
              } else {
                delete[] isrs;
                return Iter::Ranges::size(xnr) == 0;
              }
            } else {
              if (allEqual) {
                if (n == 0) {
                  delete[] isrs;
                  return Iter::Ranges::size(xnr) == Gecode::Set::Limits::card;
                } else {
                  Iter::Ranges::Singleton s(isrs[0],isrs[0]);
                  delete[] isrs;
                  return Iter::Ranges::equal(s, xnr);
                }
              } else {
                delete[] isrs;
                return Iter::Ranges::size(xnr) == 0;
              }
            }
          }
        default:
          GECODE_NEVER;
        }
        GECODE_NEVER;
        return false;
      }
      /// Post constraint on \a x
      void post(Space& home, SetVarArray& x, IntVarArray& y) {
        if (!withConst)
          Gecode::rel(home, sot, y, x[0]);
        else
          Gecode::rel(home, sot, y, is, x[0]);
      }
    };

    /// Help class to create and register tests
    class CreateIntN {
    public:
      /// Perform creation and registration
      CreateIntN(void) {
        for (int wc=0; wc<=1; wc++) {
          for (int i=0; i<=3; i++) {
            (void) new RelIntN(Gecode::SOT_UNION, i, wc);
            (void) new RelIntN(Gecode::SOT_DUNION, i, wc);
            (void) new RelIntN(Gecode::SOT_INTER, i, wc);
          }
        }
      }
    };

    CreateIntN intcn;

    //@}

}}}

// STATISTICS: test-set