Generated on Thu Feb 14 2013 20:59:32 for Gecode by doxygen 1.8.3.1
dom.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2005
8  *
9  * Last modified:
10  * $Date: 2010-08-24 02:53:25 +1000 (Tue, 24 Aug 2010) $ by $Author: tack $
11  * $Revision: 11358 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include "test/int.hh"
39 
40 namespace Test { namespace Int {
41 
43  namespace Dom {
44 
50 
51  class DomInt : public Test {
52  public:
54  DomInt(int n) : Test("Dom::Int::"+str(n),n,-4,4,n == 1) {}
56  virtual bool solution(const Assignment& x) const {
57  for (int i=x.size(); i--; )
58  if (x[i] != -2)
59  return false;
60  return true;
61  }
63  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
64  if (x.size() == 1)
65  Gecode::dom(home, x[0], -2);
66  else
67  Gecode::dom(home, x, -2);
68  }
70  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
72  assert(x.size() == 1);
73  Gecode::dom(home, x[0], -2, b);
74  }
75  };
76 
77 
79  class DomRange : public Test {
80  public:
82  DomRange(int n) : Test("Dom::Range::"+str(n),n,-4,4,n == 1) {}
84  virtual bool solution(const Assignment& x) const {
85  for (int i=x.size(); i--; )
86  if ((x[i] < -2) || (x[i] > 2))
87  return false;
88  return true;
89  }
91  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
92  if (x.size() == 1)
93  Gecode::dom(home, x[0], -2, 2);
94  else
95  Gecode::dom(home, x, -2, 2);
96  }
98  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
100  assert(x.size() == 1);
101  Gecode::dom(home, x[0], -2, 2, b);
102  }
103  };
104 
106  class DomRangeEmpty : public Test {
107  public:
109  DomRangeEmpty(void) : Test("Dom::Range::Empty",1,-4,4,true) {}
111  virtual bool solution(const Assignment&) const {
112  return false;
113  }
115  virtual void post(Gecode::Space& home, Gecode::IntVarArray&) {
116  home.fail();
117  }
119  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
120  Gecode::BoolVar b) {
121  Gecode::dom(home, x[0], 3, 2, b);
122  }
123  };
124 
125 
126  const int r[4][2] = {
127  {-4,-3},{-1,-1},{1,1},{3,5}
128  };
129  Gecode::IntSet d(r,4);
130 
132  class DomDom : public Test {
133  public:
135  DomDom(int n) : Test("Dom::Dom::"+str(n),n,-6,6,n == 1) {}
137  virtual bool solution(const Assignment& x) const {
138  for (int i=x.size(); i--; )
139  if (!(((x[i] >= -4) && (x[i] <= -3)) ||
140  ((x[i] >= -1) && (x[i] <= -1)) ||
141  ((x[i] >= 1) && (x[i] <= 1)) ||
142  ((x[i] >= 3) && (x[i] <= 5))))
143  return false;
144  return true;
145  }
147  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
148  if (x.size() == 1)
149  Gecode::dom(home, x[0], d);
150  else
151  Gecode::dom(home, x, d);
152  }
154  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
155  Gecode::BoolVar b) {
156  assert(x.size() == 1);
157  Gecode::dom(home, x[0], d, b);
158  }
159  };
160 
161  DomInt di1(1);
162  DomInt di3(3);
163  DomRange dr1(1);
164  DomRange dr3(3);
165  DomDom dd1(1);
166  DomDom dd3(3);
169 
170  }
171 }}
172 
173 // STATISTICS: test-int
174