Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
single.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christopher Mears <Chris.Mears@monash.edu>
5 *
6 * Contributing authors:
7 * Christian Schulte <schulte@gecode.org>
8 *
9 * Copyright:
10 * Christopher Mears, 2011
11 * Christian Schulte, 2011
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
38namespace Gecode { namespace Int { namespace Precede {
39
41 template<class View>
42 forceinline bool
43 assigned(View x, int v) {
44 return x.assigned() && (x.val() == v);
45 }
46
47 template<class View>
50 Council<Index>& c, int i0)
51 : Advisor(home,p,c), i(i0) {}
52
53 template<class View>
56 : Advisor(home,a), i(a.i) {}
57
58
59 template<class View>
62 int n = x.size();
63 while ((alpha < n) && !x[alpha].in(s))
64 GECODE_ME_CHECK(x[alpha++].nq(home, t));
65 if (alpha < n)
66 GECODE_ME_CHECK(x[alpha].nq(home, t));
67 return ES_OK;
68 }
69
70 template<class View>
73 int n = x.size();
74 do {
75 beta++;
76 } while ((beta < n) && !x[beta].in(s));
77 if (beta > gamma)
78 GECODE_ME_CHECK(x[alpha].eq(home, s));
79 return ES_OK;
80 }
81
82 template<class View>
85 int s0, int t0, int b, int g)
86 : NaryPropagator<View, PC_INT_NONE>(home,x0),
87 c(home), s(s0), t(t0), alpha(0), beta(b), gamma(g) {
88 for (int i=0; i<x.size(); i++)
89 if (!x[i].assigned())
90 x[i].subscribe(home,*new (home) Index(home,*this,c,i));
91 View::schedule(home, *this, ME_INT_BND);
92 }
93
94 template<class View>
95 inline ExecStatus
97 {
98 int alpha = 0;
99 while ((alpha < x.size()) && !x[alpha].in(s))
100 GECODE_ME_CHECK(x[alpha++].nq(home,t));
101 x.drop_fst(alpha);
102 if (x.size() == 0)
103 return ES_OK;
104 }
105 // alpha has been normalized to 0
106 int beta = 0, gamma = 0;
107 GECODE_ME_CHECK(x[0].nq(home,t));
108 do {
109 gamma++;
110 } while ((gamma < x.size()) && !assigned(x[gamma],t));
111 do {
112 beta++;
113 } while ((beta < x.size()) && !x[beta].in(s));
114 if (beta > gamma) {
115 GECODE_ME_CHECK(x[0].eq(home, s));
116 return ES_OK;
117 }
118 if (gamma < x.size())
119 x.drop_lst(gamma);
120 (void) new (home) Single<View>(home, x, s, t, beta, gamma);
121 return ES_OK;
122 }
123
124
125
126 template<class View>
129 : NaryPropagator<View,PC_INT_NONE>(home, p),
130 s(p.s), t(p.t),
131 alpha(p.alpha), beta(p.beta), gamma(p.gamma) {
132 c.update(home, p.c);
133 }
134 template<class View>
137 // Try to eliminate assigned views at the beginning
138 if (alpha > 0) {
139 int i = 0;
140 while ((i < alpha) && x[i].assigned())
141 i++;
142 x.drop_fst(i);
143 for (Advisors<Index> as(c); as(); ++as)
144 as.advisor().i -= i;
145 alpha -= i; beta -= i; gamma -= i;
146 }
147 // Try to eliminate assigned views at the end
148 if (gamma < x.size()) {
149 int i = x.size()-1;
150 while ((i > gamma) && x[i].assigned())
151 i--;
152 x.drop_lst(i);
153 }
154 return new (home) Single<View>(home, *this);
155 }
156
157
158 template<class View>
159 inline size_t
161 // Cancel remaining advisors
162 for (Advisors<Index> as(c); as(); ++as)
163 x[as.advisor().i].cancel(home,as.advisor());
164 c.dispose(home);
166 return sizeof(*this);
167 }
168
169 template<class View>
171 Single<View>::cost(const Space&, const ModEventDelta&) const {
172 return PropCost::linear(PropCost::LO, x.size());
173 }
174
175 template<class View>
176 void
178 View::schedule(home, *this, ME_INT_BND);
179 }
180
181 template<class View>
183 Single<View>::advise(Space& home, Advisor& a0, const Delta& d) {
184 Index& a(static_cast<Index&>(a0));
185 int i = a.i;
186 // Check for gamma
187 if ((beta <= gamma) && (i < gamma) && assigned(x[i],t))
188 gamma = i;
189 if (x[i].assigned()) {
190 a.dispose(home,c);
191 if (c.empty())
192 return ES_NOFIX;
193 } else if ((i < alpha) || (i > gamma)) {
194 x[i].cancel(home,a);
195 a.dispose(home,c);
196 return (c.empty()) ? ES_NOFIX : ES_FIX;
197 }
198 if (beta > gamma)
199 return ES_NOFIX;
200 if ((alpha == i) || (beta == i)) {
201 if (x[i].any(d) && !x[i].in(s))
202 return ES_NOFIX;
203 if ((x[i].min(d) <= s) && (s <= x[i].max(d)))
204 return ES_NOFIX;
205 }
206 return ES_FIX;
207 }
208
209 template<class View>
212 int n = x.size();
213 if (beta > gamma) {
214 GECODE_ME_CHECK(x[alpha].eq(home, s));
215 return home.ES_SUBSUMED(*this);
216 }
217 if ((alpha < n) && !x[alpha].in(s)) {
218 alpha++;
219 while (alpha < beta)
220 GECODE_ME_CHECK(x[alpha++].nq(home, t));
222 beta = alpha;
223 if (alpha < n)
225 } else if ((beta < n) && !x[beta].in(s)) {
227 }
228
229 return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX;
230 }
231
232}}}
233
234// STATISTICS: int-prop
Advisor(Space &home, Propagator &p, Council< A > &c)
Constructor for creation.
Definition core.hpp:3838
friend class Council
Definition core.hpp:1296
Class to iterate over advisors of a council.
Definition core.hpp:1268
Generic domain change information to be supplied to advisors.
Definition core.hpp:204
Home class for posting propagators
Definition core.hpp:856
Advisors for views (by position in array)
Definition precede.hh:65
Index(Space &home, Propagator &p, Council< Index > &c, int i)
Create index advisor.
Definition single.hpp:49
int i
The position of the view in the view array.
Definition precede.hh:68
Single value precedence propagator.
Definition precede.hh:61
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition single.hpp:183
int s
The value s must precede t.
Definition precede.hh:77
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition single.hpp:211
ExecStatus updateAlpha(Space &home)
Update the alpha pointer.
Definition single.hpp:61
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition single.hpp:136
Council< Index > c
The advisor council.
Definition precede.hh:75
virtual void reschedule(Space &home)
Schedule function.
Definition single.hpp:177
Single(Home home, ViewArray< View > &x, int s, int t, int beta, int gamma)
Constructor for posting.
Definition single.hpp:84
int alpha
Pointers updated during propagation.
Definition precede.hh:79
ExecStatus updateBeta(Space &home)
Update the beta pointer.
Definition single.hpp:72
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition single.hpp:160
virtual PropCost cost(const Space &, const ModEventDelta &) const
Cost function.
Definition single.hpp:171
static ExecStatus post(Home home, ViewArray< View > &x, int s, int t)
Post propagator that s precedes t in x.
Definition single.hpp:96
n-ary propagator
Definition pattern.hpp:142
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition pattern.hpp:513
NaryPropagator(Space &home, NaryPropagator &p)
Propagation cost.
Definition core.hpp:486
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition core.hpp:4803
Base-class for propagators.
Definition core.hpp:1066
friend class Space
Definition core.hpp:1068
friend class Advisor
Definition core.hpp:1070
Computation spaces.
Definition core.hpp:1744
View arrays.
Definition array.hpp:253
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3570
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
Value precedence propagators.
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition single.hpp:43
Finite domain integers.
const Gecode::ModEvent ME_INT_BND
Domain operation has changed the minimum or maximum of the domain.
Definition var-type.hpp:65
const Gecode::PropCond PC_INT_NONE
Propagation condition to be ignored (convenience)
Definition var-type.hpp:74
Gecode toplevel namespace
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar x
Definition set.hh:773
#define forceinline
Definition config.hpp:194