File: no-overlap.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 (303 lines) | stat: -rwxr-xr-x 10,236 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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2011
 *
 *  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/int.hh"

#include <gecode/minimodel.hh>
#include <climits>

namespace Test { namespace Int {

  /// %Tests for no-overlap constraint
  namespace NoOverlap {

    /**
     * \defgroup TaskTestIntNoOverlap No-overlap constraints
     * \ingroup TaskTestInt
     */
    //@{
    /// %Test for no-overlap with integer dimensions (rectangles)
    class Int2 : public Test {
    protected:
      /// Width
      Gecode::IntArgs w;
      /// Height
      Gecode::IntArgs h;
    public:
      /// Create and register test with maximal coordinate value \a m
      Int2(int m, const Gecode::IntArgs& w0, const Gecode::IntArgs& h0)
        : Test("NoOverlap::Int::2::"+str(m)+"::"+str(w0)+"::"+str(h0),
               2*w0.size(), 0, m-1),
          w(w0), h(h0) {
      }
      /// %Test whether \a xy is solution
      virtual bool solution(const Assignment& xy) const {
        int n = xy.size() / 2;
        for (int i=0; i<n; i++) {
          int xi=xy[2*i+0], yi=xy[2*i+1];
          for (int j=i+1; j<n; j++) {
            int xj=xy[2*j+0], yj=xy[2*j+1];
            if (!((xi + w[i] <= xj) || (xj + w[j] <= xi) ||
                  (yi + h[i] <= yj) || (yj + h[j] <= yi)))
              return false;
          }
        }
        return true;
      }
      /// Post constraint on \a xy
      virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
        using namespace Gecode;
        int n = xy.size() / 2;
        IntVarArgs x(n), y(n);
        for (int i=0; i<n; i++) {
          x[i]=xy[2*i+0]; y[i]=xy[2*i+1];
        }
        nooverlap(home, x, w, y, h);
      }
    };
    /// %Test for no-overlap with optional rectangles
    class IntOpt2 : public Test {
    protected:
      /// Width
      Gecode::IntArgs w;
      /// Height
      Gecode::IntArgs h;
    public:
      /// Create and register test with maximal value \a m and \a n rectangles
      IntOpt2(int m, const Gecode::IntArgs& w0, const Gecode::IntArgs& h0)
        : Test("NoOverlap::Int::Opt::2::"+str(m)+"::"+str(w0)+"::"+str(h0),
               3*w0.size(), 0, m-1), w(w0), h(h0) {}
      /// %Test whether \a xyo is solution
      virtual bool solution(const Assignment& xyo) const {
        int n = xyo.size() / 3;
        for (int i=0; i<n; i++) {
          int xi=xyo[3*i+0], yi=xyo[3*i+1];
          int oi=xyo[3*i+2];
          for (int j=i+1; j<n; j++) {
            int xj=xyo[3*j+0], yj=xyo[3*j+1];
            int oj=xyo[3*j+2];
            if ((oi > 0) && (oj > 0) &&
                !((xi + w[i] <= xj) || (xj + w[j] <= xi) ||
                  (yi + h[i] <= yj) || (yj + h[j] <= yi)))
              return false;
          }
        }
        return true;
      }
      /// Post constraint on \a xwyho
      virtual void post(Gecode::Space& home, Gecode::IntVarArray& xyo) {
        using namespace Gecode;
        int n = xyo.size() / 3;
        IntVarArgs x(n), y(n);
        BoolVarArgs o(n);
        for (int i=0; i<n; i++) {
          x[i]=xyo[3*i+0]; y[i]=xyo[3*i+1];
          o[i]=expr(home, xyo[3*i+2] > 0);
        }
        nooverlap(home, x, w, y, h, o);
      }
    };

    /// %Test for no-overlap with variable dimensions (rectangles)
    class Var2 : public Test {
    public:
      /// Create and register test with maximal value \a m and \a n rectangles
      Var2(int m, int n)
        : Test("NoOverlap::Var::2::"+str(m)+"::"+str(n), 4*n, 0, m) {}
      /// %Test whether \a xwyh is solution
      virtual bool solution(const Assignment& xwyh) const {
        int n = xwyh.size() / 4;
        for (int i=0; i<n; i++) {
          int xi=xwyh[4*i+0], yi=xwyh[4*i+2];
          int wi=xwyh[4*i+1], hi=xwyh[4*i+3];
          for (int j=i+1; j<n; j++) {
            int xj=xwyh[4*j+0], yj=xwyh[4*j+2];
            int wj=xwyh[4*j+1], hj=xwyh[4*j+3];
            if (!((xi + wi <= xj) || (xj + wj <= xi) ||
                  (yi + hi <= yj) || (yj + hj <= yi)))
              return false;
          }
        }
        return true;
      }
      /// Post constraint on \a xwyh
      virtual void post(Gecode::Space& home, Gecode::IntVarArray& xwyh) {
        using namespace Gecode;
        int n = xwyh.size() / 4;
        IntVarArgs x0(n), w(n), x1(n), y0(n), h(n), y1(n);
        for (int i=0; i<n; i++) {
          x0[i]=xwyh[4*i+0]; w[i]=xwyh[4*i+1];
          x1[i]=expr(home, x0[i] + w[i]);
          y0[i]=xwyh[4*i+2]; h[i]=xwyh[4*i+3];
          y1[i]=expr(home, y0[i] + h[i]);
        }
        nooverlap(home, x0, w, x1, y0, h, y1);
      }
    };

    /// %Test for no-overlap with optional rectangles
    class VarOpt2 : public Test {
    public:
      /// Create and register test with maximal value \a m and \a n rectangles
      VarOpt2(int m, int n)
        : Test("NoOverlap::Var::Opt::2::"+str(m)+"::"+str(n), 5*n, 0, m) {
        testfix = false;
      }
      /// %Test whether \a xwyho is solution
      virtual bool solution(const Assignment& xwyho) const {
        int n = xwyho.size() / 5;
        for (int i=0; i<n; i++) {
          int xi=xwyho[5*i+0], yi=xwyho[5*i+2];
          int wi=xwyho[5*i+1], hi=xwyho[5*i+3];
          int oi=xwyho[5*i+4];
          for (int j=i+1; j<n; j++) {
            int xj=xwyho[5*j+0], yj=xwyho[5*j+2];
            int wj=xwyho[5*j+1], hj=xwyho[5*j+3];
            int oj=xwyho[5*j+4];
            if ((oi > 0) && (oj > 0) &&
                !((xi + wi <= xj) || (xj + wj <= xi) ||
                  (yi + hi <= yj) || (yj + hj <= yi)))
              return false;
          }
        }
        return true;
      }
      /// Post constraint on \a xwyho
      virtual void post(Gecode::Space& home, Gecode::IntVarArray& xwyho) {
        using namespace Gecode;
        int n = xwyho.size() / 5;
        IntVarArgs x0(n), w(n), x1(n), y0(n), h(n), y1(n);
        BoolVarArgs o(n);
        for (int i=0; i<n; i++) {
          x0[i]=xwyho[5*i+0]; w[i]=xwyho[5*i+1];
          x1[i]=expr(home, x0[i] + w[i]);
          y0[i]=xwyho[5*i+2]; h[i]=xwyho[5*i+3];
          y1[i]=expr(home, y0[i] + h[i]);
          o[i]=expr(home, xwyho[5*i+4] > 0);
        }
        nooverlap(home, x0, w, x1, y0, h, y1, o);
      }
    };

     /// %Test for no-overlap with optional rectangles and shared variables
    class VarOptShared2 : public Test {
    public:
      /// Create and register test with maximal value \a m and \a n rectangles
      VarOptShared2(int m, int n)
        : Test("NoOverlap::Var::Opt::Shared::2::"+
               str(m)+"::"+str(n), 2*n+2, 0, m) {
        testfix = false;
      }
      /// %Test whether \a xwyho is solution
      virtual bool solution(const Assignment& xwyho) const {
        int n = (xwyho.size() - 2) / 2;
        for (int i=0; i<n; i++) {
          int xi=xwyho[2*i+0], yi=xwyho[2*i+0];
          int wi=xwyho[2*i+1], hi=xwyho[2*i+1];
          int oi=xwyho[2*n + (i % 2)];
          for (int j=i+1; j<n; j++) {
            int xj=xwyho[2*j+0], yj=xwyho[2*j+0];
            int wj=xwyho[2*j+1], hj=xwyho[2*j+1];
            int oj=xwyho[2*n + (j % 2)];
            if ((oi > 0) && (oj > 0) &&
                !((xi + wi <= xj) || (xj + wj <= xi) ||
                  (yi + hi <= yj) || (yj + hj <= yi)))
              return false;
          }
        }
        return true;
      }
      /// Post constraint on \a xwyho
      virtual void post(Gecode::Space& home, Gecode::IntVarArray& xwyho) {
        using namespace Gecode;
        int n = (xwyho.size() - 2) / 2;
        IntVarArgs x0(n), w(n), x1(n), y0(n), h(n), y1(n);
        BoolVarArgs o(n);
        for (int i=0; i<n; i++) {
          x0[i]=xwyho[2*i+0]; w[i]=xwyho[2*i+1];
          x1[i]=expr(home, x0[i] + w[i]);
          y0[i]=xwyho[2*i+0]; h[i]=xwyho[2*i+1];
          y1[i]=expr(home, y0[i] + h[i]);
          o[i]=expr(home, xwyho[2*n + (i % 2)] > 0);
        }
        nooverlap(home, x0, w, x1, y0, h, y1, o);
      }
    };


    /// Help class to create and register tests
    class Create {
    public:
      /// Perform creation and registration
      Create(void) {
        using namespace Gecode;

        IntArgs s1({2,1,1});
        IntArgs s2({1,2,3,4});
        IntArgs s3({4,3,2,1});
        IntArgs s4({1,1,1,1});

        for (int m=2; m<3; m++) {
          (void) new Int2(m, s1, s1);
          (void) new Int2(m, s2, s2);
          (void) new Int2(m, s3, s3);
          (void) new Int2(m, s2, s3);
          (void) new Int2(m, s4, s4);
          (void) new Int2(m, s4, s2);
          (void) new IntOpt2(m, s2, s3);
          (void) new IntOpt2(m, s4, s3);
        }

        (void) new Var2(2, 2);
        (void) new Var2(3, 2);
        (void) new Var2(1, 3);
        (void) new VarOpt2(2, 2);
        (void) new VarOpt2(3, 2);
        (void) new VarOptShared2(2, 2);
        (void) new VarOptShared2(3, 2);
        (void) new VarOptShared2(4, 2);

      }
    };

    Create c;

    //@}

  }

}}


// STATISTICS: test-int