Generated on Sun Aug 26 2012 08:43:03 for Gecode by doxygen 1.8.1.1
no-overlap.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, 2011
8  *
9  * Last modified:
10  * $Date: 2011-08-10 18:38:22 +1000 (Wed, 10 Aug 2011) $ by $Author: schulte $
11  * $Revision: 12264 $
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 <gecode/int/no-overlap.hh>
39 
40 namespace Gecode {
41 
42  namespace Int { namespace NoOverlap {
43 
44  bool
46  for (int i=m.size(); i--; )
47  if (m[i].none())
48  return true;
49  return false;
50  }
51 
52  }}
53 
54  void
55  nooverlap(Home home,
56  const IntVarArgs& x, const IntArgs& w,
57  const IntVarArgs& y, const IntArgs& h,
58  IntConLevel) {
59  using namespace Int;
60  using namespace NoOverlap;
61  if (x.same(home) || y.same(home))
62  throw ArgumentSame("Int::nooverlap");
63  if ((x.size() != w.size()) || (x.size() != y.size()) ||
64  (x.size() != h.size()))
65  throw ArgumentSizeMismatch("Int::nooverlap");
66  for (int i=x.size(); i--; ) {
67  Limits::nonnegative(w[i],"Int::nooverlap");
68  Limits::nonnegative(h[i],"Int::nooverlap");
69  Limits::check(static_cast<double>(x[i].max()) + w[i],
70  "Int::nooverlap");
71  Limits::check(static_cast<double>(y[i].max()) + h[i],
72  "Int::nooverlap");
73  }
74  if (home.failed()) return;
75 
76  ManBox<FixDim,2>* b
77  = static_cast<Space&>(home).alloc<ManBox<FixDim,2> >(x.size());
78  for (int i=x.size(); i--; ) {
79  b[i][0] = FixDim(x[i],w[i]);
80  b[i][1] = FixDim(y[i],h[i]);
81  }
82 
84  }
85 
86  void
87  nooverlap(Home home,
88  const IntVarArgs& x, const IntArgs& w,
89  const IntVarArgs& y, const IntArgs& h,
90  const BoolVarArgs& m,
91  IntConLevel) {
92  using namespace Int;
93  using namespace NoOverlap;
94  if (x.same(home) || y.same(home) || m.same(home))
95  throw ArgumentSame("Int::nooverlap");
96  if ((x.size() != w.size()) || (x.size() != y.size()) ||
97  (x.size() != h.size()) || (x.size() != m.size()))
98  throw ArgumentSizeMismatch("Int::nooverlap");
99  for (int i=x.size(); i--; ) {
100  Limits::nonnegative(w[i],"Int::nooverlap");
101  Limits::nonnegative(h[i],"Int::nooverlap");
102  Limits::check(static_cast<double>(x[i].max()) + w[i],
103  "Int::nooverlap");
104  Limits::check(static_cast<double>(y[i].max()) + h[i],
105  "Int::nooverlap");
106  }
107  if (home.failed()) return;
108 
109  if (optional(m)) {
110  OptBox<FixDim,2>* b
111  = static_cast<Space&>(home).alloc<OptBox<FixDim,2> >(x.size());
112  for (int i=x.size(); i--; ) {
113  b[i][0] = FixDim(x[i],w[i]);
114  b[i][1] = FixDim(y[i],h[i]);
115  b[i].optional(m[i]);
116  }
118  } else {
119  ManBox<FixDim,2>* b
120  = static_cast<Space&>(home).alloc<ManBox<FixDim,2> >(x.size());
121  int n = 0;
122  for (int i=0; i<x.size(); i++)
123  if (m[i].one()) {
124  b[n][0] = FixDim(x[i],w[i]);
125  b[n][1] = FixDim(y[i],h[i]);
126  n++;
127  }
129  }
130  }
131 
132  void
134  const IntVarArgs& x0, const IntVarArgs& w, const IntVarArgs& x1,
135  const IntVarArgs& y0, const IntVarArgs& h, const IntVarArgs& y1,
136  IntConLevel) {
137  using namespace Int;
138  using namespace NoOverlap;
139  if ((x0.size() != w.size()) || (x0.size() != x1.size()) ||
140  (x0.size() != y0.size()) || (x0.size() != h.size()) ||
141  (x0.size() != y1.size()))
142  throw ArgumentSizeMismatch("Int::nooverlap");
143  if (x0.same(home) || w.same(home) || x1.same(home) ||
144  y0.same(home) || h.same(home) || y1.same(home))
145  throw ArgumentSame("Int::nooverlap");
146  if (home.failed()) return;
147 
148  for (int i=x0.size(); i--; ) {
149  GECODE_ME_FAIL(IntView(w[i]).gq(home,0));
150  GECODE_ME_FAIL(IntView(h[i]).gq(home,0));
151  }
152 
153  if (w.assigned() && h.assigned()) {
154  IntArgs wc(x0.size()), hc(x0.size());
155  for (int i=x0.size(); i--; ) {
156  wc[i] = w[i].val();
157  hc[i] = h[i].val();
158  }
159  nooverlap(home, x0, wc, y0, hc);
160  } else {
161  ManBox<FlexDim,2>* b
162  = static_cast<Space&>(home).alloc<ManBox<FlexDim,2> >(x0.size());
163  for (int i=x0.size(); i--; ) {
164  b[i][0] = FlexDim(x0[i],w[i],x1[i]);
165  b[i][1] = FlexDim(y0[i],h[i],y1[i]);
166  }
168  }
169  }
170 
171  void
173  const IntVarArgs& x0, const IntVarArgs& w, const IntVarArgs& x1,
174  const IntVarArgs& y0, const IntVarArgs& h, const IntVarArgs& y1,
175  const BoolVarArgs& m,
176  IntConLevel) {
177  using namespace Int;
178  using namespace NoOverlap;
179  if ((x0.size() != w.size()) || (x0.size() != x1.size()) ||
180  (x0.size() != y0.size()) || (x0.size() != h.size()) ||
181  (x0.size() != y1.size()) || (x0.size() != m.size()))
182  throw ArgumentSizeMismatch("Int::nooverlap");
183  if (x0.same(home) || w.same(home) || x1.same(home) ||
184  y0.same(home) || h.same(home) || y1.same(home) ||
185  m.same(home))
186  throw ArgumentSame("Int::nooverlap");
187  if (home.failed()) return;
188 
189  for (int i=x0.size(); i--; ) {
190  GECODE_ME_FAIL(IntView(w[i]).gq(home,0));
191  GECODE_ME_FAIL(IntView(h[i]).gq(home,0));
192  }
193 
194  if (w.assigned() && h.assigned()) {
195  IntArgs wc(x0.size()), hc(x0.size());
196  for (int i=x0.size(); i--; ) {
197  wc[i] = w[i].val();
198  hc[i] = h[i].val();
199  }
200  nooverlap(home, x0, wc, y0, hc, m);
201  } else if (optional(m)) {
202  OptBox<FlexDim,2>* b
203  = static_cast<Space&>(home).alloc<OptBox<FlexDim,2> >(x0.size());
204  for (int i=x0.size(); i--; ) {
205  b[i][0] = FlexDim(x0[i],w[i],x1[i]);
206  b[i][1] = FlexDim(y0[i],h[i],y1[i]);
207  b[i].optional(m[i]);
208  }
210  } else {
211  ManBox<FlexDim,2>* b
212  = static_cast<Space&>(home).alloc<ManBox<FlexDim,2> >(x0.size());
213  int n = 0;
214  for (int i=0; i<x0.size(); i++)
215  if (m[i].one()) {
216  b[n][0] = FlexDim(x0[i],w[i],x1[i]);
217  b[n][1] = FlexDim(y0[i],h[i],y1[i]);
218  n++;
219  }
221  }
222  }
223 
224 }
225 
226 // STATISTICS: int-post