Generated on Sat Feb 7 2015 02:01:25 for Gecode by doxygen 1.8.9.1
opt.hpp
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: 2012-10-22 21:13:52 +0200 (Mon, 22 Oct 2012) $ by $Author: schulte $
11  * $Revision: 13163 $
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 namespace Gecode { namespace Int { namespace NoOverlap {
39 
40  template<class Box>
42  OptProp<Box>::OptProp(Home home, Box* b, int n, int m0)
43  : Base<Box>(home,b,n), m(m0) {
44  for (int i=m; i--; )
45  b[n+i].subscribe(home, *this);
46  }
47 
48  template<class Box>
50  OptProp<Box>::post(Home home, Box* b, int n) {
51  // Partition into mandatory and optional boxes
52  if (n > 1) {
53  int p = Base<Box>::partition(b, 0, n);
54  (void) new (home) OptProp<Box>(home,b,p,n-p);
55  }
56  return ES_OK;
57  }
58 
59  template<class Box>
60  forceinline size_t
62  for (int i=m; i--; )
63  b[n+i].cancel(home, *this);
64  (void) Base<Box>::dispose(home);
65  return sizeof(*this);
66  }
67 
68 
69  template<class Box>
72  : Base<Box>(home, shared, p, p.n + p.m), m(p.m) {}
73 
74  template<class Box>
75  Actor*
76  OptProp<Box>::copy(Space& home, bool share) {
77  return new (home) OptProp<Box>(home,share,*this);
78  }
79 
80  template<class Box>
81  ExecStatus
83  Region r(home);
84 
85  if (BoolView::me(med) == ME_BOOL_VAL) {
86  // Eliminate excluded boxes
87  for (int i=m; i--; )
88  if (b[n+i].excluded()) {
89  b[n+i].cancel(home,*this);
90  b[n+i] = b[n+(--m)];
91  }
92  // Reconsider optional boxes
93  if (m > 0) {
94  int p = Base<Box>::partition(b+n, 0, m);
95  n += p; m -= p;
96  }
97  }
98 
99  // Number of disjoint boxes
100  int* db = r.alloc<int>(n);
101  for (int i=n; i--; )
102  db[i] = n-1;
103 
104  // Number of boxes to be eliminated
105  int e = 0;
106  for (int i=n; i--; ) {
107  assert(b[i].mandatory());
108  for (int j=i; j--; )
109  if (b[i].nooverlap(b[j])) {
110  assert(db[i] > 0); assert(db[j] > 0);
111  if (--db[i] == 0) e++;
112  if (--db[j] == 0) e++;
113  continue;
114  } else {
115  GECODE_ES_CHECK(b[i].nooverlap(home,b[j]));
116  }
117  }
118 
119  if (m == 0) {
120  if (e == n)
121  return home.ES_SUBSUMED(*this);
122  int i = n-1;
123  while (e > 0) {
124  // Eliminate boxes that do not overlap
125  while (db[i] > 0)
126  i--;
127  b[i].cancel(home, *this);
128  b[i] = b[--n]; b[n] = b[n+m];
129  e--; i--;
130  }
131  if (n < 2)
132  return home.ES_SUBSUMED(*this);
133  }
134 
135  // Check whether some optional boxes must be excluded
136  for (int i=m; i--; ) {
137  if (b[n+i].optional()) {
138  for (int j=n; j--; )
139  if (b[n+i].overlap(b[j])) {
140  GECODE_ES_CHECK(b[n+i].exclude(home));
141  b[n+i].cancel(home,*this);
142  b[n+i] = b[n+(--m)];
143  break;
144  }
145  } else {
146  // This might be the case if the same Boolean view occurs
147  // several times and has already been excluded
148  assert(b[n+i].excluded());
149  b[n+i].cancel(home,*this);
150  b[n+i] = b[n+(--m)];
151  }
152  }
153 
154  return ES_NOFIX;
155  }
156 
157 }}}
158 
159 // STATISTICS: int-prop
160 
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: opt.hpp:76
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:84
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
bool overlap(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:502
virtual size_t dispose(Space &home)
Destructor.
Definition: opt.hpp:61
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: opt.hpp:82
Handle to region.
Definition: region.hpp:61
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
int m
Number of optional boxes: b[n] ... b[n+m-1].
Definition: no-overlap.hh:293
ModEvent exclude(Space &home, View &x, int s)
Prune view x to exclude all values from s.
Definition: set-op.hpp:141
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
void subscribe(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:74
OptProp(Home home, Box *b, int n, int m)
Constructor for posting.
Definition: opt.hpp:42
No-overlap propagator for optional boxes.
Definition: no-overlap.hh:288
static ExecStatus post(Home home, Box *b, int n)
Post propagator for boxes b.
Definition: opt.hpp:50
ExecStatus
Definition: core.hpp:523
static ModEvent me(const ModEventDelta &med)
Return modification event for view type in med.
#define forceinline
Definition: config.hpp:132
Base class for no-overlap propagator.
Definition: no-overlap.hh:230
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
static int partition(Box *b, int i, int n)
Partition n boxes b starting at position i.
Definition: base.hpp:50
void nooverlap(Home home, const IntVarArgs &x, const IntArgs &w, const IntVarArgs &y, const IntArgs &h, IntConLevel)
Post propagator for rectangle packing.
Definition: no-overlap.cpp:55
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
bool optional(const BoolVarArgs &m)
Definition: no-overlap.cpp:45
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
const Gecode::ModEvent ME_BOOL_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:116