File: linear.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 (386 lines) | stat: -rwxr-xr-x 13,952 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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2005
 *
 *  Last modified:
 *     $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $
 *     $Revision: 10684 $
 *
 *  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 linear constraints
   namespace Linear {

     /// Check whether \a has only one coefficients
     bool one(const Gecode::IntArgs& a) {
      for (int i=a.size(); i--; )
        if (a[i] != 1)
          return false;
      return true;
    }

     /**
      * \defgroup TaskTestIntLinear Linear constraints
      * \ingroup TaskTestInt
      */
     //@{
     /// %Test linear relation over integer variables
     class IntInt : public Test {
     protected:
       /// Coefficients
       Gecode::IntArgs a;
       /// Integer relation type to propagate
       Gecode::IntRelType irt;
     public:
       /// Create and register test
       IntInt(const std::string& s, const Gecode::IntSet& d,
              const Gecode::IntArgs& a0, Gecode::IntRelType irt0,
              Gecode::IntConLevel icl=Gecode::ICL_BND)
         : Test("Linear::Int::Int::"+
                str(irt0)+"::"+str(icl)+"::"+s+"::"+str(a0.size()),
                a0.size(),d,icl != Gecode::ICL_DOM,icl),
         a(a0), irt(irt0) {}
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double e = 0.0;
         for (int i=0; i<x.size(); i++)
           e += a[i]*x[i];
         return cmp(e, irt, static_cast<double>(0));
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         if (one(a))
           Gecode::linear(home, x, irt, 0, icl);
         else
           Gecode::linear(home, a, x, irt, 0, icl);
       }
       /// Post reified constraint on \a x for \a b
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
                         Gecode::BoolVar b) {
         if (one(a))
           Gecode::linear(home, x, irt, 0, b, icl);
         else
           Gecode::linear(home, a, x, irt, 0, b, icl);
       }
     };

     /// %Test linear relation over integer variables
     class IntVar : public Test {
     protected:
       /// Coefficients
       Gecode::IntArgs a;
       /// Integer relation type to propagate
       Gecode::IntRelType irt;
     public:
       /// Create and register test
       IntVar(const std::string& s, const Gecode::IntSet& d,
              const Gecode::IntArgs& a0, Gecode::IntRelType irt0,
              Gecode::IntConLevel icl=Gecode::ICL_BND)
         : Test("Linear::Int::Var::"+
                str(irt0)+"::"+str(icl)+"::"+s+"::"+str(a0.size()),
                a0.size()+1,d,icl != Gecode::ICL_DOM,icl),
           a(a0), irt(irt0) {}
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double e = 0.0;
         for (int i=0; i<a.size(); i++)
           e += a[i]*x[i];
         return cmp(e, irt, static_cast<double>(x[a.size()]));
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         int n = a.size();
         Gecode::IntVarArgs y(n);
         for (int i=n; i--; )
           y[i] = x[i];
         if (one(a))
           Gecode::linear(home, y, irt, x[n], icl);
         else
           Gecode::linear(home, a, y, irt, x[n], icl);
       }
       /// Post reified constraint on \a x for \a b
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
                         Gecode::BoolVar b) {
         int n = a.size();
         Gecode::IntVarArgs y(n);
         for (int i=n; i--; )
           y[i] = x[i];
         if (one(a))
           Gecode::linear(home, y, irt, x[n], b, icl);
         else
           Gecode::linear(home, a, y, irt, x[n], b, icl);
       }
     };

     /// %Test linear relation over Boolean variables equal to constant
     class BoolInt : public Test {
     protected:
       /// Coefficients
       Gecode::IntArgs a;
       /// Integer relation type to propagate
       Gecode::IntRelType irt;
       /// Righthand-side constant
       int c;
     public:
       /// Create and register test
       BoolInt(const std::string& s, const Gecode::IntArgs& a0,
               Gecode::IntRelType irt0, int c0)
         : Test("Linear::Bool::Int::"+
                str(irt0)+"::"+s+"::"+str(a0.size())+"::"+str(c0),
                a0.size(),0,1,true,Gecode::ICL_DEF),
           a(a0), irt(irt0), c(c0) {
       }
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         double e = 0.0;
         for (int i=0; i<x.size(); i++)
           e += a[i]*x[i];
         return cmp(e, irt, static_cast<double>(c));
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         Gecode::BoolVarArgs y(x.size());
         for (int i=x.size(); i--; )
           y[i]=Gecode::channel(home,x[i]);
         if (one(a))
           Gecode::linear(home, y, irt, c, Gecode::ICL_DEF);
         else
           Gecode::linear(home, a, y, irt, c, Gecode::ICL_DEF);
       }
       /// Post reified constraint on \a x for \a b
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
                         Gecode::BoolVar b) {
         Gecode::BoolVarArgs y(x.size());
         for (int i=x.size(); i--; )
           y[i]=Gecode::channel(home,x[i]);
         if (one(a))
           Gecode::linear(home, y, irt, c, b, Gecode::ICL_DEF);
         else
           Gecode::linear(home, a, y, irt, c, b, Gecode::ICL_DEF);
       }
     };

     /// %Test linear relation over Boolean variables equal to integer variable
     class BoolVar : public Test {
     protected:
       /// Coefficients
       Gecode::IntArgs a;
       /// Integer relation type to propagate
       Gecode::IntRelType irt;
     public:
       /// Create and register test
       BoolVar(const std::string& s,
               int min, int max, const Gecode::IntArgs& a0,
               Gecode::IntRelType irt0)
         : Test("Linear::Bool::Var::"+str(irt0)+"::"+s,a0.size()+1,
                min,max,true),
           a(a0), irt(irt0) {}
       /// %Test whether \a x is solution
       virtual bool solution(const Assignment& x) const {
         int n=x.size()-1;
         for (int i=0; i<n; i++)
           if ((x[i] < 0) || (x[i] > 1))
             return false;
         double e = 0.0;
         for (int i=0; i<n; i++)
           e += a[i]*x[i];
         return cmp(e, irt, static_cast<double>(x[n]));
       }
       /// %Test whether \a x is to be ignore
       virtual bool ignore(const Assignment& x) const {
         for (int i=x.size()-1; i--; )
           if ((x[i] < 0) || (x[i] > 1))
             return true;
         return false;
       }
       /// Post constraint on \a x
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
         int n=x.size()-1;
         Gecode::BoolVarArgs y(n);
         for (int i=n; i--; )
           y[i]=Gecode::channel(home,x[i]);
         if (one(a))
           Gecode::linear(home, y, irt, x[n]);
         else
           Gecode::linear(home, a, y, irt, x[n]);
       }
       /// Post reified constraint on \a x for \a b
       virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
                         Gecode::BoolVar b) {
         int n=x.size()-1;
         Gecode::BoolVarArgs y(n);
         for (int i=n; i--; )
           y[i]=Gecode::channel(home,x[i]);
         if (one(a))
           Gecode::linear(home, y, irt, x[n], b);
         else
           Gecode::linear(home, a, y, irt, x[n], b);
       }
     };

     /// Help class to create and register tests
     class Create {
     public:
       /// Perform creation and registration
       Create(void) {
         using namespace Gecode;
         {
           IntSet d1(-2,2);
           const int dv2[] = {-4,-1,0,1,4};
           IntSet d2(dv2,5);

           IntArgs a1(1, 0);

           for (IntRelTypes irts; irts(); ++irts) {
             (void) new IntInt("11",d1,a1,irts.irt());
             (void) new IntVar("11",d1,a1,irts.irt());
             (void) new IntInt("21",d2,a1,irts.irt());
             (void) new IntVar("21",d2,a1,irts.irt());
           }
           (void) new IntInt("11",d1,a1,IRT_EQ,ICL_DOM);
           (void) new IntVar("11",d1,a1,IRT_EQ,ICL_DOM);
           (void) new IntInt("21",d2,a1,IRT_EQ,ICL_DOM);
           (void) new IntVar("21",d2,a1,IRT_EQ,ICL_DOM);

           const int av2[5] = {1,1,1,1,1};
           const int av3[5] = {1,-1,-1,1,-1};
           const int av4[5] = {2,3,5,7,11};
           const int av5[5] = {-2,3,-5,7,-11};

           for (int i=1; i<=5; i++) {
             IntArgs a2(i, av2);
             IntArgs a3(i, av3);
             IntArgs a4(i, av4);
             IntArgs a5(i, av5);
             for (IntRelTypes irts; irts(); ++irts) {
               (void) new IntInt("12",d1,a2,irts.irt());
               (void) new IntInt("13",d1,a3,irts.irt());
               (void) new IntInt("14",d1,a4,irts.irt());
               (void) new IntInt("15",d1,a5,irts.irt());
               (void) new IntInt("22",d2,a2,irts.irt());
               (void) new IntInt("23",d2,a3,irts.irt());
               (void) new IntInt("24",d2,a4,irts.irt());
               (void) new IntInt("25",d2,a5,irts.irt());
               if (i < 5) {
                 (void) new IntVar("12",d1,a2,irts.irt());
                 (void) new IntVar("13",d1,a3,irts.irt());
                 (void) new IntVar("14",d1,a4,irts.irt());
                 (void) new IntVar("15",d1,a5,irts.irt());
                 (void) new IntVar("22",d2,a2,irts.irt());
                 (void) new IntVar("23",d2,a3,irts.irt());
                 (void) new IntVar("24",d2,a4,irts.irt());
                 (void) new IntVar("25",d2,a5,irts.irt());
               }
             }
             (void) new IntInt("12",d1,a2,IRT_EQ,ICL_DOM);
             (void) new IntInt("13",d1,a3,IRT_EQ,ICL_DOM);
             (void) new IntInt("14",d1,a4,IRT_EQ,ICL_DOM);
             (void) new IntInt("15",d1,a5,IRT_EQ,ICL_DOM);
             (void) new IntInt("22",d2,a2,IRT_EQ,ICL_DOM);
             (void) new IntInt("23",d2,a3,IRT_EQ,ICL_DOM);
             (void) new IntInt("24",d2,a4,IRT_EQ,ICL_DOM);
             (void) new IntInt("25",d2,a5,IRT_EQ,ICL_DOM);
             if (i < 4) {
               (void) new IntVar("12",d1,a2,IRT_EQ,ICL_DOM);
               (void) new IntVar("13",d1,a3,IRT_EQ,ICL_DOM);
               (void) new IntVar("14",d1,a4,IRT_EQ,ICL_DOM);
               (void) new IntVar("15",d1,a5,IRT_EQ,ICL_DOM);
             }
           }
         }
         {
           const int av1[10] = { 
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1
           };
           const int av2[10] = {
             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
           };

           for (int i=1; i<=10; i += 3) {
             IntArgs a1(i, av1);
             IntArgs a2(i, av2);
             for (int c=0; c<=6; c++)
               for (IntRelTypes irts; irts(); ++irts) {
                 (void) new BoolInt("1",a1,irts.irt(),c);
                 (void) new BoolInt("2",a2,irts.irt(),-c);
               }
           }

           IntArgs a3(5, 1,2,3,4,5);
           IntArgs a4(5, -1,-2,-3,-4,-5);
           IntArgs a5(5, -1,-2,1,2,4);

           for (IntRelTypes irts; irts(); ++irts) {
             for (int c=0; c<=16; c++) {
               (void) new BoolInt("3",a3,irts.irt(),c);
               (void) new BoolInt("4",a4,irts.irt(),-c);
               (void) new BoolInt("5",a5,irts.irt(),c);
               (void) new BoolInt("6",a5,irts.irt(),-c);
             }
           }

           for (int i=1; i<=5; i += 2) {
             IntArgs a1(i, av1);
             IntArgs a2(i, av2);
             for (IntRelTypes irts; irts(); ++irts) {
               (void) new BoolVar("1::"+Test::str(i),0,5,a1,irts.irt());
               (void) new BoolVar("2::"+Test::str(i),-5,0,a2,irts.irt());
             }
           }

           IntArgs a6(4, 1,2,3,4);
           IntArgs a7(4, -1,-2,-3,-4);
           IntArgs a8(4, -1,-2,1,2);
           IntArgs a9(6, -1,-2,1,2,-3,3);

           for (IntRelTypes irts; irts(); ++irts) {
             (void) new BoolVar("6",0,10,a6,irts.irt());
             (void) new BoolVar("7",-10,0,a7,irts.irt());
             (void) new BoolVar("8",-3,3,a8,irts.irt());
             (void) new BoolVar("9",-3,3,a9,irts.irt());
           }

         }
       }
     };

     Create c;
     //@}

   }
}}

// STATISTICS: test-int