Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
recorder.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, 2016
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode {
35
42
43 TE_INIT = 1 << 0,
44 TE_PRUNE = 1 << 1,
45 TE_FIX = 1 << 2,
46 TE_FAIL = 1 << 3,
47 TE_DONE = 1 << 4,
49
50 TE_PROPAGATE = 1 << 5,
51 TE_COMMIT = 1 << 6,
52 TE_POST = 1 << 7
53 };
54
59 template<class View>
61 public:
69 class Slack {
70 template<class ViewForTraceRecorder> friend class ViewTraceRecorder;
71 protected:
78 public:
80 SlackValue initial(void) const;
82 SlackValue previous(void) const;
84 SlackValue current(void) const;
85 };
86 protected:
88 class Idx : public Advisor {
89 protected:
91 int _idx;
92 public:
94 Idx(Space& home, Propagator& p, Council<Idx>& c, int i);
96 Idx(Space& home, Idx& a);
98 int idx(void) const;
99 };
109 int te;
113 Slack s;
116 public:
121 virtual Propagator* copy(Space& home);
123 virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
125 virtual void reschedule(Space& home);
127 virtual ExecStatus advise(Space& home, Advisor& a, const Delta& d);
131 virtual size_t dispose(Space& home);
136
137
138 const typename View::VarType operator [](int i) const;
140 int size(void) const;
142 const Slack& slack(void) const;
144 };
145
154 class TraceRecorder : public Propagator {
155 public:
159 int te;
164 public:
166 TraceRecorder(Home home, TraceFilter tf, int te, Tracer& t);
168 virtual Propagator* copy(Space& home);
170 virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
172 virtual void reschedule(Space& home);
174 virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
176 virtual size_t dispose(Space& home);
178 static ExecStatus post(Home home, TraceFilter tf, int te, Tracer& t);
180
181
182 const TraceFilter& filter(void) const;
184 int events(void) const;
186 Tracer& tracer(void) const;
188 };
189
190
191 /*
192 * Functions for trace support
193 *
194 */
195 template<class View>
196 forceinline const typename View::VarType
198 const typename View::VarType x(n[i].varimp());
199 return x;
200 }
201 template<class View>
202 forceinline int
204 return n.size();
205 }
206 template<class View>
209 return s;
210 }
211
212
213 /*
214 * Functions for access to slack
215 *
216 */
217 template<class View>
220 return i;
221 }
222 template<class View>
225 return p;
226 }
227 template<class View>
230 return c;
231 }
232
233
234 /*
235 * Advisor for tracer
236 *
237 */
238
239 template<class View>
242 Council<Idx>& c, int i)
243 : Advisor(home,p,c), _idx(i) {}
244 template<class View>
247 : Advisor(home,a), _idx(a._idx) {
248 }
249 template<class View>
250 forceinline int
252 return _idx;
253 }
254
255
256 /*
257 * Posting of tracer propagator
258 *
259 */
260 template<class View>
263 TraceFilter tf0, int te0,
265 : Propagator(home), o(home,x.size()), n(x), c(home),
266 tf(tf0), te(te0), t(t0) {
267 home.notice(*this, AP_VIEW_TRACE);
268 home.notice(*this, AP_DISPOSE);
269 for (int i=0; i<n.size(); i++) {
270 o[i] = TraceView(home,n[i]);
271 if (!n[i].assigned())
272 n[i].subscribe(home,*new (home) Idx(home,*this,c,i));
273 }
274 View::schedule(home,*this,ME_GEN_ASSIGNED);
275 s.i = TraceView::slack(n[0]);
276 for (int i=1; i<n.size(); i++)
277 s.i += TraceView::slack(n[i]);
278 s.p = s.i;
279 if ((te & TE_INIT) != 0)
280 t._init(home,*this);
281 }
282
283
284 template<class View>
288 if ((x.size() > 0) &&
289 (te & (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE)))
290 (void) new (home) ViewTraceRecorder(home,x,tf,te,t);
291 return ES_OK;
292 }
293
294
295 /*
296 * Propagation for trace recorder
297 *
298 */
299 template<class View>
302 : Propagator(home,p), tf(p.tf), te(p.te), t(p.t), s(p.s) {
303 o.update(home, p.o);
304 n.update(home, p.n);
305 c.update(home, p.c);
306 }
307
308 template<class View>
311 return new (home) ViewTraceRecorder(home, *this);
312 }
313
314 template<class View>
315 inline size_t
317 home.ignore(*this, AP_VIEW_TRACE);
318 home.ignore(*this, AP_DISPOSE);
319 tf.~TraceFilter();
320 // Cancel remaining advisors
321 for (Advisors<Idx> as(c); as(); ++as)
322 n[as.advisor().idx()].cancel(home,as.advisor());
323 c.dispose(home);
324 (void) Propagator::dispose(home);
325 return sizeof(*this);
326 }
327
328 template<class View>
331 return PropCost::record();
332 }
333
334 template<class View>
335 void
337 View::schedule(home,*this,ME_GEN_ASSIGNED);
338 }
339
340 template<class View>
343 Idx& a = static_cast<Idx&>(_a);
344 int i = a.idx();
345 if (((te & TE_PRUNE) != 0) && !disabled() && tf(a(home)) ) {
346 TraceDelta td(o[i],n[i],d);
347 t._prune(home,*this,a(home),i,td);
348 }
349 o[i].prune(home,n[i],d);
350 if (n[a.idx()].assigned())
351 a.dispose(home,c);
352 return ES_NOFIX;
353 }
354
355 template<class View>
358 s.c = TraceView::slack(n[0]);
359 for (int i=1; i<n.size(); i++)
360 s.c += TraceView::slack(n[i]);
361 if (home.failed() && ((te & TE_FAIL) != 0) && !disabled()) {
362 t._fail(home,*this);
363 return ES_FIX;
364 }
365 if ((te & TE_FIX) != 0)
366 t._fix(home,*this);
367 s.p = s.c;
368 if (c.empty()) {
369 if ((te & TE_DONE) != 0)
370 t._done(home,*this);
371 return home.ES_SUBSUMED(*this);
372 }
373 return ES_FIX;
374 }
375
376
377
378 /*
379 * Functions for trace support
380 *
381 */
384 return tf;
385 }
386 forceinline int
388 return te;
389 }
392 return t;
393 }
394
395
396 /*
397 * Trace recorder propagator
398 *
399 */
402 Tracer& t0)
403 : Propagator(home), tf(tf0), te(te0), t(t0) {
404 home.notice(*this, AP_DISPOSE);
405 home.notice(*this, AP_TRACE);
406 }
407
410 if (te & (TE_PROPAGATE | TE_COMMIT | TE_POST))
411 (void) new (home) TraceRecorder(home,tf,te,t);
412 return ES_OK;
413 }
414
417 : Propagator(home,p), tf(p.tf), te(p.te), t(p.t) {
418 }
419
420}
421
422// STATISTICS: kernel-trace
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition core.hpp:3256
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
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3223
Propagation cost.
Definition core.hpp:486
static PropCost record(void)
For recording information (no propagation allowed)
Definition core.hpp:4772
Base-class for propagators.
Definition core.hpp:1066
friend class Space
Definition core.hpp:1068
friend class Advisor
Definition core.hpp:1070
friend class Council
Definition core.hpp:1071
bool disabled(void) const
Whether propagator is currently disabled.
Definition core.hpp:3483
ModEventDelta med
A set of modification events (used during propagation)
Definition core.hpp:1077
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3505
Trace filters.
Definition filter.hpp:133
int te
Which events to trace.
Definition recorder.hpp:159
static ExecStatus post(Home home, TraceFilter tf, int te, Tracer &t)
Post propagator.
Definition recorder.hpp:409
Tracer & t
The actual tracer.
Definition recorder.hpp:161
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition recorder.cpp:62
int events(void) const
Which events to trace.
Definition recorder.hpp:387
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition recorder.cpp:39
const TraceFilter & filter(void) const
Return trace filter.
Definition recorder.hpp:383
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last)
Definition recorder.cpp:53
TraceRecorder(Space &home, TraceRecorder &p)
Constructor for cloning p.
Definition recorder.hpp:416
TraceFilter tf
The trace filter.
Definition recorder.hpp:157
Tracer & tracer(void) const
Return tracer.
Definition recorder.hpp:391
virtual void reschedule(Space &home)
Schedule function.
Definition recorder.cpp:58
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition recorder.cpp:44
Traits for tracing.
Definition traits.hpp:49
View arrays.
Definition array.hpp:253
Advisor with index information.
Definition recorder.hpp:88
int idx(void) const
Get index of view.
Definition recorder.hpp:251
Idx(Space &home, Propagator &p, Council< Idx > &c, int i)
Constructor for creation.
Definition recorder.hpp:241
int _idx
Index information.
Definition recorder.hpp:91
Collection of slack values.
Definition recorder.hpp:69
SlackValue p
Slack value at previous event (fixpoint or init)
Definition recorder.hpp:75
SlackValue initial(void) const
Return initial slack value.
Definition recorder.hpp:219
SlackValue c
Current slack value.
Definition recorder.hpp:77
SlackValue i
The initial slack value.
Definition recorder.hpp:73
SlackValue current(void) const
Return current slack value.
Definition recorder.hpp:229
SlackValue previous(void) const
Return previous slack value.
Definition recorder.hpp:224
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition recorder.hpp:357
ViewTracer< Float::FloatView > & t
Definition recorder.hpp:111
static ExecStatus post(Home home, ViewArray< View > &x, TraceFilter tf, int te, ViewTracer< View > &t)
Post recorder propagator.
Definition recorder.hpp:286
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last)
Definition recorder.hpp:330
virtual void reschedule(Space &home)
Schedule function.
Definition recorder.hpp:336
ViewTraceRecorder(Space &home, ViewTraceRecorder &p)
Constructor for cloning p.
Definition recorder.hpp:301
TraceTraits< Float::FloatView >::TraceView TraceView
Definition recorder.hpp:63
const View::VarType operator[](int i) const
Return variable being traced at position i.
Definition recorder.hpp:197
int size(void) const
Return number of variables being traced.
Definition recorder.hpp:203
TraceTraits< Float::FloatView >::SlackValue SlackValue
Definition recorder.hpp:67
const Slack & slack(void) const
Provide access to slack information.
Definition recorder.hpp:208
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition recorder.hpp:342
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition recorder.hpp:310
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition recorder.hpp:316
TraceTraits< Float::FloatView >::TraceDelta TraceDelta
Definition recorder.hpp:65
ViewTraceRecorder(Home home, ViewArray< View > &x, TraceFilter tf, int te, ViewTracer< View > &t)
Constructor for creation.
Definition recorder.hpp:262
Tracer that process view trace information.
Definition tracer.hpp:51
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3570
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4081
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
bool failed(void) const
Check whether space is failed.
Definition core.hpp:4051
@ AP_VIEW_TRACE
Definition core.hpp:573
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
@ AP_TRACE
Definition core.hpp:578
TraceEvent
Which events to trace.
Definition recorder.hpp:40
@ TE_INIT
Trace init events.
Definition recorder.hpp:43
@ TE_POST
Trace propagator posting.
Definition recorder.hpp:52
@ TE_COMMIT
Trace commit operations by branchers.
Definition recorder.hpp:51
@ TE_PRUNE
Trace prune events.
Definition recorder.hpp:44
@ TE_PROPAGATE
Trace propagator executions.
Definition recorder.hpp:50
@ TE_FIX
Trace fixpoint events.
Definition recorder.hpp:45
@ TE_FAIL
Trace fail events.
Definition recorder.hpp:46
@ TE_DONE
Trace done events.
Definition recorder.hpp:47
Gecode toplevel namespace
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
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition core.hpp:69
Post propagator for SetVar x
Definition set.hh:773
#define forceinline
Definition config.hpp:194