Generated on for Gecode by doxygen 1.17.0
path.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.dev>
5 *
6 * Contributing authors:
7 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
8 *
9 * Copyright:
10 * Christian Schulte, 2003
11 * Mikael Zayenz Lagerkvist, 2026
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
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 Search { namespace Seq {
39
40 /*
41 * Edge for recomputation
42 *
43 */
44 template<class Tracer>
45 forceinline
47
48 template<class Tracer>
49 forceinline
51 : _space(c), _alt(0), _choice(s->choice()), _nid(nid) {}
52
53 template<class Tracer>
54 forceinline Space*
56 return _space;
57 }
58 template<class Tracer>
59 forceinline void
63
64 template<class Tracer>
65 forceinline unsigned int
67 return _alt;
68 }
69 template<class Tracer>
70 forceinline unsigned int
72 return std::min(_alt,_choice->alternatives()-1);
73 }
74 template<class Tracer>
75 forceinline bool
77 return _alt == 0;
78 }
79 template<class Tracer>
80 forceinline bool
82 return _alt+1 >= _choice->alternatives();
83 }
84 template<class Tracer>
85 forceinline bool
87 return _alt >= _choice->alternatives();
88 }
89 template<class Tracer>
90 forceinline void
93 }
94
95 template<class Tracer>
96 forceinline const Choice*
98 return _choice;
99 }
100
101 template<class Tracer>
102 forceinline unsigned int
104 return _nid;
106
107 template<class Tracer>
108 forceinline void
110 delete _space;
111 delete _choice;
112 }
113
114
115
116 /*
117 * Depth-first stack with recomputation
118 *
119 */
121 template<class Tracer>
122 forceinline
123 Path<Tracer>::Path(unsigned int l)
124 : ds(heap), _ngdl(l) {}
125
126 template<class Tracer>
127 forceinline unsigned int
128 Path<Tracer>::ngdl(void) const {
129 return _ngdl;
131
132 template<class Tracer>
133 forceinline void
134 Path<Tracer>::ngdl(unsigned int l) {
135 _ngdl = l;
137
138 template<class Tracer>
139 forceinline const Choice*
140 Path<Tracer>::push(Worker& stat, Space* s, Space* c, unsigned int nid) {
141 if (!ds.empty() && ds.top().lao()) {
142 // Topmost stack entry was LAO -> reuse
143 ds.pop().dispose();
144 }
145 Edge sn(s,c,nid);
146 ds.push(sn);
147 stat.stack_depth(static_cast<unsigned long int>(ds.entries()));
148 return sn.choice();
150
151 template<class Tracer>
152 forceinline void
154 while (!ds.empty())
155 if (ds.top().rightmost()) {
156 ds.pop().dispose();
157 } else {
158 ds.top().next();
159 return;
160 }
161 }
162
163 template<class Tracer>
164 forceinline typename Path<Tracer>::Edge&
165 Path<Tracer>::top(void) const {
166 assert(!ds.empty());
167 return ds.top();
168 }
169
170 template<class Tracer>
171 forceinline bool
173 return ds.empty();
174 }
175
176 template<class Tracer>
177 forceinline void
178 Path<Tracer>::commit(Space* s, int i) const {
179 const Edge& n = ds[i];
180 s->commit(*n.choice(),n.alt());
181 }
182
183 template<class Tracer>
184 forceinline int
185 Path<Tracer>::lc(void) const {
186 int l = ds.entries()-1;
187 while (ds[l].space() == nullptr)
188 l--;
189 return l;
190 }
191
192 template<class Tracer>
193 forceinline int
195 return ds.entries();
196 }
197
198 template<class Tracer>
199 forceinline void
201 assert((ds[l].space() == nullptr) || ds[l].space()->failed());
202 int n = ds.entries();
203 if (t) {
204 for (int i=l; i<n; i++) {
205 Path<Tracer>::Edge& top = ds.top();
206 unsigned int fa = (i != l) ? top.alt() + 1 : top.alt();
207 for (unsigned int a = fa; a < top.choice()->alternatives(); a++) {
208 SearchTracer::EdgeInfo ei(t.wid(),top.nid(),a);
209 t.skip(ei);
210 }
211 ds.pop().dispose();
212 }
213 } else {
214 for (int i=l; i<n; i++)
215 ds.pop().dispose();
216 }
217 assert(ds.entries() == l);
218 }
219
220 template<class Tracer>
221 inline void
223 while (!ds.empty())
224 ds.pop().dispose();
225 }
226
227 template<class Tracer>
228 forceinline Space*
229 Path<Tracer>::recompute(unsigned int& d, unsigned int a_d, Worker& stat,
230 Tracer& t) {
231 assert(!ds.empty());
232 // Recompute space according to path
233 // Also say distance to copy (d == 0) requires immediate copying
234
235 // Check for LAO
236 if ((ds.top().space() != nullptr) && ds.top().rightmost()) {
237 Space* s = ds.top().space();
238 s->commit(*ds.top().choice(),ds.top().alt());
239 assert(ds.entries()-1 == lc());
240 ds.top().space(nullptr);
241 // Mark as reusable
242 if (static_cast<unsigned int>(ds.entries()) > ngdl())
243 ds.top().next();
244 d = 0;
245 return s;
246 }
247 // General case for recomputation
248 int l = lc(); // Position of last clone
249 int n = ds.entries(); // Number of stack entries
250 // New distance, if no adaptive recomputation
251 d = static_cast<unsigned int>(n - l);
252
253 Space* s = ds[l].space()->clone(); // Last clone
254
255 if (d < a_d) {
256 // No adaptive recomputation
257 for (int i=l; i<n; i++)
258 commit(s,i);
259 } else {
260 int m = l + static_cast<int>(d >> 1); // Middle between copy and top
261 int i = l; // To iterate over all entries
262 // Recompute up to middle
263 for (; i<m; i++ )
264 commit(s,i);
265 // Skip over all rightmost branches
266 for (; (i<n) && ds[i].rightmost(); i++)
267 commit(s,i);
268 // Is there any point to make a copy?
269 if (i<n-1) {
270 // Propagate to fixpoint
271 SpaceStatus ss = s->status(stat);
272 /*
273 * Again, the space might already propagate to failure (due to
274 * weakly monotonic propagators).
275 */
276 if (ss == SS_FAILED) {
277 // s must be deleted as it is not on the stack
278 delete s;
279 stat.fail++;
280 unwind(i,t);
281 return nullptr;
282 }
283 ds[i].space(s->clone());
284 d = static_cast<unsigned int>(n-i);
285 }
286 // Finally do the remaining commits
287 for (; i<n; i++)
288 commit(s,i);
289 }
290 return s;
291 }
292
293 template<class Tracer>
294 forceinline Space*
295 Path<Tracer>::recompute(unsigned int& d, unsigned int a_d, Worker& stat,
296 const Space* best, int& mark,
297 Tracer& t) {
298 assert(!ds.empty());
299 // Recompute space according to path
300 // Also say distance to copy (d == 0) requires immediate copying
301
302 // Check for LAO
303 if ((ds.top().space() != nullptr) && ds.top().rightmost()) {
304 Space* s = ds.top().space();
305 s->commit(*ds.top().choice(),ds.top().alt());
306 assert(ds.entries()-1 == lc());
307 if ((best != nullptr) && (mark > ds.entries()-1)) {
308 mark = ds.entries()-1;
309 s->constrain(*best);
310 }
311 ds.top().space(nullptr);
312 // Mark as reusable
313 if (static_cast<unsigned int>(ds.entries()) > ngdl())
314 ds.top().next();
315 d = 0;
316 return s;
317 }
318 // General case for recomputation
319 int l = lc(); // Position of last clone
320 int n = ds.entries(); // Number of stack entries
321 // New distance, if no adaptive recomputation
322 d = static_cast<unsigned int>(n - l);
323
324 Space* s = ds[l].space(); // Last clone
325
326 if ((best != nullptr) && (l < mark)) {
327 mark = l;
328 s->constrain(*best);
329 // The space on the stack could be failed now as an additional
330 // constraint might have been added.
331 if (s->status(stat) == SS_FAILED) {
332 // s does not need deletion as it is on the stack (unwind does this)
333 stat.fail++;
334 unwind(l,t);
335 return nullptr;
336 }
337 // It is important to replace the space on the stack with the
338 // copy: a copy might be much smaller due to flushed caches
339 // of propagators
340 Space* c = s->clone();
341 ds[l].space(c);
342 } else {
343 s = s->clone();
344 }
345
346 if (d < a_d) {
347 // No adaptive recomputation
348 for (int i=l; i<n; i++)
349 commit(s,i);
350 } else {
351 int m = l + static_cast<int>(d >> 1); // Middle between copy and top
352 int i = l; // To iterate over all entries
353 // Recompute up to middle
354 for (; i<m; i++ )
355 commit(s,i);
356 // Skip over all rightmost branches
357 for (; (i<n) && ds[i].rightmost(); i++)
358 commit(s,i);
359 // Is there any point to make a copy?
360 if (i<n-1) {
361 // Propagate to fixpoint
362 SpaceStatus ss = s->status(stat);
363 /*
364 * Again, the space might already propagate to failure
365 *
366 * This can be for two reasons:
367 * - constrain is true, so we fail
368 * - the space has weakly monotonic propagators
369 */
370 if (ss == SS_FAILED) {
371 // s must be deleted as it is not on the stack
372 delete s;
373 stat.fail++;
374 unwind(i,t);
375 return nullptr;
376 }
377 ds[i].space(s->clone());
378 d = static_cast<unsigned int>(n-i);
379 }
380 // Finally do the remaining commits
381 for (; i<n; i++)
382 commit(s,i);
383 }
384 return s;
385 }
386
387 template<class Tracer>
388 void
391 }
392
393}}}
394
395// STATISTICS: search-seq
Choice for performing commit
Definition core.hpp:1423
std::atomic< unsigned long int > n
Number of no-goods.
Definition core.hpp:1602
static ExecStatus post(Space &home, const Path &p)
Post propagator for path p.
Definition nogoods.hpp:90
Search tree edge for recomputation
Definition path.hh:70
Space * space(void) const
Return space for edge.
Definition path.hpp:55
Space * _space
Space corresponding to this edge (might be nullptr).
Definition path.hh:73
unsigned int alt(void) const
Return number for alternatives.
Definition path.hpp:66
Edge(void)
Default constructor.
Definition path.hpp:46
bool rightmost(void) const
Test whether current alternative is rightmost.
Definition path.hpp:81
void next(void)
Move to next alternative.
Definition path.hpp:91
bool leftmost(void) const
Test whether current alternative is leftmost.
Definition path.hpp:76
bool lao(void) const
Test whether current alternative was LAO.
Definition path.hpp:86
void dispose(void)
Free memory for edge.
Definition path.hpp:109
unsigned int truealt(void) const
Return true number for alternatives (excluding lao optimization).
Definition path.hpp:71
ID _nid
Node identifier.
Definition path.hh:79
unsigned int nid(void) const
Return node identifier.
Definition path.hpp:103
const Choice * _choice
Choice.
Definition path.hh:77
const Choice * choice(void) const
Return choice.
Definition path.hpp:97
unsigned int _alt
Current alternative.
Definition path.hh:75
unsigned int _ngdl
Depth limit for no-good generation.
Definition path.hh:117
void unwind(int l, Tracer &t)
Unwind the stack up to position l (after failure).
Definition path.hpp:200
bool empty(void) const
Test whether path is empty.
Definition path.hpp:172
int entries(void) const
Return number of entries on stack.
Definition path.hpp:194
int lc(void) const
Return position on stack of last copy.
Definition path.hpp:185
void next(void)
Generate path for next node.
Definition path.hpp:153
virtual void post(Space &home) const
Post no-goods.
Definition path.hpp:389
void commit(Space *s, int i) const
Commit space s as described by stack entry at position i.
Definition path.hpp:178
unsigned int ngdl(void) const
Return no-good depth limit.
Definition path.hpp:128
Support::DynamicStack< Edge, Heap > ds
Stack to store edge information.
Definition path.hh:115
Path(unsigned int l)
Initialize with no-good depth limit l.
Definition path.hpp:123
Edge & top(void) const
Provide access to topmost edge.
Definition path.hpp:165
void reset(void)
Reset stack.
Definition path.hpp:222
const Choice * push(Worker &stat, Space *s, Space *c, unsigned int nid)
Push space c (a clone of s or nullptr).
Definition path.hpp:140
Space * recompute(unsigned int &d, unsigned int a_d, Worker &s, Tracer &t)
Recompute space according to path.
Definition path.hpp:229
unsigned long long int fail
Number of failed nodes in search tree.
Definition search.hh:154
Search worker statistics
Definition worker.hh:44
void stack_depth(unsigned long int d)
Record stack depth d.
Definition worker.hh:100
Computation spaces.
Definition core.hpp:1775
Heap heap
The single global heap.
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition macros.hpp:103
virtual void constrain(const Space &best)
Constrain function for best solution search.
Space * clone(void) const
Clone space.
Definition core.hpp:3312
SpaceStatus status(StatusStatistics &stat)
Query space status.
void commit(const Choice &c, unsigned int a)
Commit choice c for alternative a.
Definition core.hpp:3326
SpaceStatus
Space status
Definition core.hpp:1714
@ SS_FAILED
Space is failed
Definition core.hpp:1715
Search engines
Gecode toplevel namespace