main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Oct 22 2013 00:49:05 for Gecode by
doxygen
1.8.4
gecode
int
no-overlap
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>
41
forceinline
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>
49
ExecStatus
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
61
OptProp<Box>::dispose
(
Space
& home) {
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>
70
forceinline
71
OptProp<Box>::OptProp
(
Space
& home,
bool
shared
,
OptProp<Box>
&
p
)
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
82
OptProp<Box>::propagate
(
Space
& home,
const
ModEventDelta
& med) {
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