main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
gecode
set
distinct
atmostOne.cpp
Go to the documentation of this file.
1
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
/*
3
* Main authors:
4
* Guido Tack <tack@gecode.org>
5
*
6
* Copyright:
7
* Guido Tack, 2004
8
*
9
* Last modified:
10
* $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
11
* $Revision: 13068 $
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/set/distinct.hh
>
39
40
/*
41
* These propagators implement the scheme discussed in
42
*
43
* Andrew Sadler and Carment Gervet: Global Reasoning on Sets.
44
* FORMUL'01 workshop in conjunction with CP 2001.
45
*
46
* Todo: make the propagators incremental.
47
*/
48
49
namespace
Gecode
{
namespace
Set {
namespace
Distinct
{
50
51
/*
52
* "AtMostOneIntersection" propagator
53
*
54
*/
55
56
Actor
*
57
AtmostOne::copy
(
Space
& home,
bool
share) {
58
return
new
(home)
AtmostOne
(home,share,*
this
);
59
}
60
61
ExecStatus
62
AtmostOne::propagate
(
Space
& home,
const
ModEventDelta
&) {
63
Region
r
(home);
64
LubRanges<SetView>
* lubs =
r
.alloc<
LubRanges<SetView>
>(
x
.
size
());
65
for
(
int
i
=
x
.
size
();
i
--; ) {
66
lubs[
i
].
init
(
x
[
i
]);
67
}
68
Iter::Ranges::NaryUnion
bigT(
r
, lubs,
x
.
size
());
69
70
Iter::Ranges::ToValues<Iter::Ranges::NaryUnion>
71
as(bigT);
72
73
while
(as()) {
74
int
a
= as.
val
(); ++as;
75
76
// cardSa is the number of sets that contain a in the glb
77
int
cardSa = 0;
78
for
(
int
i
=
x
.
size
();
i
--;)
79
if
(
x
[
i
].contains(
a
))
80
cardSa++;
81
82
// bigTa is the union of all lubs that contain a
83
GLBndSet
bigTa(home);
84
for
(
int
i
=
x
.
size
();
i
--;) {
85
if
(!
x
[
i
].notContains(
a
)) {
86
LubRanges<SetView>
xilub(
x
[
i
]);
87
bigTa.
includeI
(home, xilub);
88
}
89
}
90
91
// maxa is the maximum number of sets that can contain a
92
int
maxa =
static_cast<
int
>
((bigTa.
size
() - 1) / (
c
- 1));
93
bigTa.
dispose
(home);
94
95
// Conditional Rule A:
96
// If more sets already contain a than allowed, fail.
97
if
(maxa < cardSa)
98
return
ES_FAILED
;
99
100
if
(maxa == cardSa) {
101
// Conditional Rule B:
102
// All a used up. All other sets (those that don't have a in their
103
// glb already) cannot contain a.
104
for
(
int
i
=
x
.
size
();
i
--;) {
105
if
(!
x
[
i
].contains(
a
)) {
106
GECODE_ME_CHECK
(
x
[
i
].
exclude
(home,
a
));
107
}
108
}
109
}
else
{
110
LubRanges<SetView>
* lubs2 =
r
.alloc<
LubRanges<SetView>
>(
x
.
size
());
111
for
(
int
i
=
x
.
size
();
i
--; ) {
112
lubs2[
i
].
init
(
x
[
i
]);
113
}
114
Iter::Ranges::NaryUnion
bigT2(
r
, lubs2,
x
.
size
());
115
116
GlbRanges<SetView>
* glbs =
r
.alloc<
GlbRanges<SetView>
>(cardSa);
117
int
count
= 0;
118
for
(
int
i
=
x
.
size
();
i
--; ) {
119
if
(
x
[
i
].contains(
a
)) {
120
glbs[
count
].
init
(
x
[
i
]);
121
count
++;
122
}
123
}
124
Iter::Ranges::NaryUnion
glbsa(
r
, glbs, cardSa);
125
Iter::Ranges::Diff
<
Iter::Ranges::NaryUnion
,
126
Iter::Ranges::NaryUnion
> deltaA(bigT2, glbsa);
127
Iter::Ranges::Cache
deltaAC(
r
,deltaA);
128
// deltaAC contains all elements that are not yet known to be
129
// in a set together with a.
130
// Formally: \cup_i lub(x_i) - \cup_i {glb(s_i) | a\in glb(s_i)}
131
132
133
if
(
Iter::Ranges::size
(deltaAC) ==
c
- 1) {
134
// Conditional Rule C:
135
// If deltaA has exactly c-1 elements, all sets that are not yet
136
// known to contain a cannot contain a if it is impossible that
137
// they contain all of deltaA. Or the other way around:
138
// If a is added to the glb of a set s, deltaA must be a subset of
139
// s, because otherwise s would share at least one more element
140
// with another set that has a in its lower bound.
141
// Weird, eh?
142
for
(
int
i
=
x
.
size
();
i
--; ) {
143
if
(!
x
[
i
].contains(
a
) && !
x
[
i
].notContains(
a
)) {
144
deltaAC.
reset
();
145
LubRanges<SetView>
xilub(
x
[
i
]);
146
if
(!
Iter::Ranges::subset
(deltaAC, xilub)) {
147
GECODE_ME_CHECK
(
x
[
i
].
exclude
(home,
a
));
148
}
149
}
150
}
151
}
152
153
}
154
155
}
156
157
return
ES_NOFIX
;
158
}
159
160
}}}
161
162
// STATISTICS: set-prop
Gecode::Int::Sequence::exclude
ModEvent exclude(Space &home, View &x, int s)
Prune view x to exclude all values from s.
Definition:
set-op.hpp:141
Gecode::Set::LubRanges< SetView >
Range iterator for least upper bound of set variable views
Definition:
set.hpp:205
Gecode::Set::Distinct::AtmostOne::AtmostOne
AtmostOne(Space &home, bool share, AtmostOne &p)
Constructor for cloning p.
Definition:
atmostOne.hpp:50
Gecode::Set::GLBndSet::includeI
bool includeI(Space &home, I &i)
Include the set represented by i in this set.
Definition:
integerset.hpp:300
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:78
Gecode::Set::Distinct::AtmostOne::copy
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition:
atmostOne.cpp:57
Test::Int::Basic::i
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::Space
Computation spaces.
Definition:
core.hpp:1748
Gecode::Actor
Base-class for both propagators and branchers.
Definition:
core.hpp:696
Gecode::Set::GlbRanges< SetView >
Range iterator for greatest lower bound of set variable views
Definition:
set.hpp:236
Gecode::Iter::Ranges::subset
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
Definition:
ranges-operations.hpp:101
Gecode::NaryPropagator< SetView, PC_SET_ANY >::x
ViewArray< SetView > x
Array of views.
Definition:
propagator.hpp:152
Gecode::Iter::Ranges::ToValues::val
int val(void) const
Return current value.
Definition:
ranges-values.hpp:133
Gecode::Set::LubRanges< SetView >::init
void init(const SetView &x)
Initialize with ranges for view x.
Definition:
set.hpp:226
Gecode::Set::GlbRanges< SetView >::init
void init(const SetView &x)
Initialize with ranges for view x.
Definition:
set.hpp:256
Gecode::Iter::Ranges::RangeListIter::reset
void reset(void)
Reset iterator to start.
Definition:
ranges-list.hpp:244
Gecode
Gecode toplevel namespace
a
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
Gecode::Set::BndSet::size
unsigned int size(void) const
Return size.
Definition:
integerset.hpp:97
Gecode::Iter::Ranges::ToValues
Value iterator from range iterator.
Definition:
ranges-values.hpp:46
Gecode::Region
Handle to region.
Definition:
region.hpp:61
Gecode::r
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition:
set.hh:784
Gecode::Iter::Ranges::Diff
Range iterator for computing set difference.
Definition:
ranges-diff.hpp:47
Gecode::Iter::Ranges::NaryUnion
Range iterator for union of iterators.
Definition:
ranges-union.hpp:78
Gecode::Set::BndSet::dispose
void dispose(Space &home)
Free memory used by this set.
Definition:
integerset.hpp:64
Gecode::Iter::Ranges::Cache
Range iterator cache
Definition:
ranges-cache.hpp:49
Gecode::Set::Rel::Distinct
Propagator for negated equality
Definition:
rel.hh:267
Gecode::Set::Distinct::AtmostOne::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition:
atmostOne.cpp:62
Gecode::count
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition:
count.cpp:44
Gecode::Set::Distinct::AtmostOne::c
unsigned int c
Cardinality of the sets.
Definition:
distinct.hh:59
Gecode::ViewArray::size
int size(void) const
Return size of array (number of elements)
Definition:
array.hpp:1215
Gecode::Set::GLBndSet
Growing sets of integers.
Definition:
var-imp.hpp:209
GECODE_ME_CHECK
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition:
macros.hpp:56
Gecode::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition:
core.hpp:542
Gecode::ModEventDelta
int ModEventDelta
Modification event deltas.
Definition:
core.hpp:169
Gecode::ES_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition:
core.hpp:543
Gecode::ExecStatus
ExecStatus
Definition:
core.hpp:540
distinct.hh