File: mm-arithmetic.cpp

package info (click to toggle)
gecode 3.4.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,136 kB
  • ctags: 16,905
  • sloc: cpp: 124,581; perl: 2,004; makefile: 1,453; sh: 247
file content (368 lines) | stat: -rwxr-xr-x 12,228 bytes parent folder | download | duplicates (3)
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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2008
 *
 *  Last modified:
 *     $Date: 2010-07-15 22:33:36 +0200 (Thu, 15 Jul 2010) $ by $Author: tack $
 *     $Revision: 11206 $
 *
 *  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>

namespace Test { namespace Int {

  /// %Tests for minimal modelling constraints (arithmetic)
  namespace MiniModelArithmetic {

     /**
      * \defgroup TaskTestIntMiniModelArithmetic Minimal modelling constraints (arithmetic)
      * \ingroup TaskTestInt
      */
     //@{
     /// %Test for multiplication constraint
     class Mult : public Test {
     public:
       /// Create and register test
       Mult(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Mult::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double d0 = static_cast<double>(x[0]);
         double d1 = static_cast<double>(x[1]);
         double d2 = static_cast<double>(x[2]);
         return d0*d1 == d2;
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for division constraint
     class Div : public Test {
     public:
       /// Create and register test
       Div(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Div::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         return (x[1] != 0) && (x[0] / x[1] == x[2]);
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for division constraint
     class Mod : public Test {
     public:
       /// Create and register test
       Mod(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Mod::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         return (x[1] != 0) && (x[0] % x[1] == x[2]);
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for addition constraint
     class Plus : public Test {
     public:
       /// Create and register test
       Plus(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Plus::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double d0 = static_cast<double>(x[0]);
         double d1 = static_cast<double>(x[1]);
         double d2 = static_cast<double>(x[2]);
         return ((d0+d1 >= Gecode::Int::Limits::min) &&
                 (d0+d1 <= Gecode::Int::Limits::max) &&
                 (d0+d1 == d2));
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for subtraction constraint
     class Minus : public Test {
     public:
       /// Create and register test
       Minus(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Minus::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double d0 = static_cast<double>(x[0]);
         double d1 = static_cast<double>(x[1]);
         double d2 = static_cast<double>(x[2]);
         return ((d0-d1 >= Gecode::Int::Limits::min) &&
                 (d0-d1 <= Gecode::Int::Limits::max) &&
                 (d0-d1 == d2));
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for sqr constraint
     class Sqr : public Test {
     public:
       /// Create and register test
       Sqr(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Sqr::"+s,2,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double d0 = static_cast<double>(x[0]);
         double d1 = static_cast<double>(x[1]);
         return d0*d0 == d1;
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], ICL_DOM);
       }
     };

     /// %Test for sqrt constraint
     class Sqrt : public Test {
     public:
       /// Create and register test
       Sqrt(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Sqrt::"+s,2,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double d0 = static_cast<double>(x[0]);
         double d1 = static_cast<double>(x[1]);
         return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], ICL_DOM);
       }
     };

     /// %Test for absolute value constraint
     class Abs : public Test {
     public:
       /// Create and register test
       Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntConLevel icl)
         : Test("MiniModel::Abs::"+str(icl)+"::"+s,
                   2,d,false,icl) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double d0 = static_cast<double>(x[0]);
         double d1 = static_cast<double>(x[1]);
         return (d0<0.0 ? -d0 : d0) == d1;
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, abs(x[0]), icl), IRT_EQ, x[1], ICL_DOM);
       }
     };

     /// %Test for binary minimum constraint
     class Min : public Test {
     public:
       /// Create and register test
       Min(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Min::Bin::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         return std::min(x[0],x[1]) == x[2];
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for binary maximum constraint
     class Max : public Test {
     public:
       /// Create and register test
       Max(const std::string& s, const Gecode::IntSet& d)
         : Test("MiniModel::Max::Bin::"+s,3,d) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         return std::max(x[0],x[1]) == x[2];
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], ICL_DOM);
       }
     };

     /// %Test for n-ary minimmum constraint
     class MinNary : public Test {
     public:
       /// Create and register test
       MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         return std::min(std::min(x[0],x[1]), x[2]) == x[3];
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         IntVarArgs m(3);
         m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
         rel(home, expr(home, min(m)), IRT_EQ, x[3], ICL_DOM);
       }
     };

     /// %Test for n-ary maximum constraint
     class MaxNary : public Test {
     public:
       /// Create and register test
       MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
         testfix = false;
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         return std::max(std::max(x[0],x[1]), x[2]) == x[3];
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         using namespace Gecode;
         IntVarArgs m(3);
         m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
         rel(home, expr(home, max(m)), IRT_EQ, x[3], ICL_DOM);
       }
     };

     const int v1[7] = {
       Gecode::Int::Limits::min, Gecode::Int::Limits::min+1,
       -1,0,1,
       Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
     };
     const int v2[9] = {
       static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
       -4,-2,-1,0,1,2,4,
       static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
     };

     Gecode::IntSet d1(v1,7);
     Gecode::IntSet d2(v2,9);
     Gecode::IntSet d3(-8,8);

     Mult mult_max("A",d1);
     Mult mult_med("B",d2);
     Mult mult_min("C",d3);

     Div div_max("A",d1);
     Div div_med("B",d2);
     Div div_min("C",d3);

     Mod mod_max("A",d1);
     Mod mod_med("B",d2);
     Mod mod_min("C",d3);

     Plus plus_max("A",d1);
     Plus plus_med("B",d2);
     Plus plus_min("C",d3);

     Minus minus_max("A",d1);
     Minus minus_med("B",d2);
     Minus minus_min("C",d3);

     Sqr sqr_max("A",d1);
     Sqr sqr_med("B",d2);
     Sqr sqr_min("C",d3);

     Sqrt sqrt_max("A",d1);
     Sqrt sqrt_med("B",d2);
     Sqrt sqrt_min("C",d3);

     Abs abs_bnd_max("A",d1,Gecode::ICL_BND);
     Abs abs_bnd_med("B",d2,Gecode::ICL_BND);
     Abs abs_bnd_min("C",d3,Gecode::ICL_BND);
     Abs abs_dom_max("A",d1,Gecode::ICL_DOM);
     Abs abs_dom_med("B",d2,Gecode::ICL_DOM);
     Abs abs_dom_min("C",d3,Gecode::ICL_DOM);

     Min min_max("A",d1);
     Min min_med("B",d2);
     Min min_min("C",d3);

     Max max_max("A",d1);
     Max max_med("B",d2);
     Max max_min("C",d3);

     MinNary min_nary;
     MaxNary max_nary;

     //@}
   }

}}

// STATISTICS: test-minimodel