Generated on Sat Jul 28 2018 17:17:52 for Gecode by doxygen 1.8.14
trace-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  * Last modified:
10  * $Date: 2017-03-17 23:04:57 +0100 (Fri, 17 Mar 2017) $ by $Author: schulte $
11  * $Revision: 15597 $
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 {
39 
44  enum TraceEvent {
46 
47  TE_INIT = 1 << 0,
48  TE_PRUNE = 1 << 1,
49  TE_FIX = 1 << 2,
50  TE_FAIL = 1 << 3,
51  TE_DONE = 1 << 4,
52 
53  TE_PROPAGATE = 1 << 5,
55  TE_COMMIT = 1 << 6
56  };
57 
62  template<class View>
63  class ViewTraceRecorder : public Propagator {
64  public:
72  class Slack {
73  template<class ViewForTraceRecorder> friend class ViewTraceRecorder;
74  protected:
81  public:
83  SlackValue initial(void) const;
85  SlackValue previous(void) const;
87  SlackValue current(void) const;
88  };
89  protected:
91  class Idx : public Advisor {
92  protected:
94  int _idx;
95  public:
97  Idx(Space& home, Propagator& p, Council<Idx>& c, int i);
99  Idx(Space& home, bool share, Idx& a);
101  int idx(void) const;
102  };
112  int te;
118  ViewTraceRecorder(Space& home, bool share, ViewTraceRecorder& p);
119  public:
124  virtual Propagator* copy(Space& home, bool share);
126  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
128  virtual void reschedule(Space& home);
130  virtual ExecStatus advise(Space& home, Advisor& a, const Delta& d);
132  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
134  virtual size_t dispose(Space& home);
136  static ExecStatus post(Home home, ViewArray<View>& x,
139 
140  const typename View::VarType operator [](int i) const;
143  int size(void) const;
145  const Slack& slack(void) const;
147  };
148 
157  class TraceRecorder : public Propagator {
158  public:
162  int te;
166  TraceRecorder(Space& home, bool share, TraceRecorder& p);
167  public:
169  TraceRecorder(Home home, TraceFilter tf, int te, Tracer& t);
171  virtual Propagator* copy(Space& home, bool share);
173  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
175  virtual void reschedule(Space& home);
177  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
179  virtual size_t dispose(Space& home);
181  static ExecStatus post(Home home, TraceFilter tf, int te, Tracer& t);
183 
184  const TraceFilter& filter(void) const;
187  int events(void) const;
189  Tracer& tracer(void) const;
191  };
192 
193 
194  /*
195  * Functions for trace support
196  *
197  */
198  template<class View>
199  forceinline const typename View::VarType
201  const typename View::VarType x(n[i].varimp());
202  return x;
203  }
204  template<class View>
205  forceinline int
207  return n.size();
208  }
209  template<class View>
212  return s;
213  }
214 
215 
216  /*
217  * Functions for access to slack
218  *
219  */
220  template<class View>
223  return i;
224  }
225  template<class View>
228  return p;
229  }
230  template<class View>
233  return c;
234  }
235 
236 
237  /*
238  * Advisor for tracer
239  *
240  */
241 
242  template<class View>
245  Council<Idx>& c, int i)
246  : Advisor(home,p,c), _idx(i) {}
247  template<class View>
250  : Advisor(home,share,a), _idx(a._idx) {
251  }
252  template<class View>
253  forceinline int
255  return _idx;
256  }
257 
258 
259  /*
260  * Posting of tracer propagator
261  *
262  */
263  template<class View>
266  TraceFilter tf0, int te0,
267  ViewTracer<View>& t0)
268  : Propagator(home), o(home,x.size()), n(x), c(home),
269  tf(tf0), te(te0), t(t0) {
270  home.notice(*this, AP_VIEW_TRACE);
271  home.notice(*this, AP_DISPOSE);
272  for (int i=n.size(); i--; ) {
273  o[i] = TraceView(home,n[i]);
274  if (!n[i].assigned())
275  n[i].subscribe(home,*new (home) Idx(home,*this,c,i));
276  }
277  View::schedule(home,*this,ME_GEN_ASSIGNED);
278  s.i = TraceView::slack(n[n.size()-1]);
279  for (int i=n.size()-1; i--; )
280  s.i += TraceView::slack(n[i]);
281  s.p = s.i;
282  if ((te & TE_INIT) != 0)
283  t._init(home,*this);
284  }
285 
286 
287  template<class View>
290  TraceFilter tf, int te, ViewTracer<View>& t) {
291  if ((x.size() > 0) &&
292  (te & (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE)))
293  (void) new (home) ViewTraceRecorder(home,x,tf,te,t);
294  return ES_OK;
295  }
296 
297 
298  /*
299  * Propagation for trace recorder
300  *
301  */
302  template<class View>
306  : Propagator(home,share,p), te(p.te), t(p.t), s(p.s) {
307  o.update(home, share, p.o);
308  n.update(home, share, p.n);
309  c.update(home, share, p.c);
310  tf.update(home, share, p.tf);
311  }
312 
313  template<class View>
314  Propagator*
316  return new (home) ViewTraceRecorder(home, share, *this);
317  }
318 
319  template<class View>
320  inline size_t
322  home.ignore(*this, AP_VIEW_TRACE);
323  home.ignore(*this, AP_DISPOSE);
324  tf.~TraceFilter();
325  // Cancel remaining advisors
326  for (Advisors<Idx> as(c); as(); ++as)
327  n[as.advisor().idx()].cancel(home,as.advisor());
328  c.dispose(home);
329  (void) Propagator::dispose(home);
330  return sizeof(*this);
331  }
332 
333  template<class View>
334  PropCost
336  return PropCost::record();
337  }
338 
339  template<class View>
340  void
342  View::schedule(home,*this,ME_GEN_ASSIGNED);
343  }
344 
345  template<class View>
346  ExecStatus
348  Idx& a = static_cast<Idx&>(_a);
349  int i = a.idx();
350  if (((te & TE_PRUNE) != 0) && !disabled() && tf(a(home)) ) {
351  TraceDelta td(o[i],n[i],d);
352  t._prune(home,*this,a(home),i,td);
353  }
354  o[i].prune(home,n[i],d);
355  if (n[a.idx()].assigned())
356  a.dispose(home,c);
357  return ES_NOFIX;
358  }
359 
360  template<class View>
361  ExecStatus
363  s.c = TraceView::slack(n[n.size()-1]);
364  for (int i=n.size()-1; i--; )
365  s.c += TraceView::slack(n[i]);
366  if (home.failed() && ((te & TE_FAIL) != 0) && !disabled()) {
367  t._fail(home,*this);
368  return ES_FIX;
369  }
370  if ((te & TE_FIX) != 0)
371  t._fix(home,*this);
372  s.p = s.c;
373  if (c.empty()) {
374  if ((te & TE_DONE) != 0)
375  t._done(home,*this);
376  return home.ES_SUBSUMED(*this);
377  }
378  return ES_FIX;
379  }
380 
381 
382 
383  /*
384  * Functions for trace support
385  *
386  */
387  forceinline const TraceFilter&
388  TraceRecorder::filter(void) const {
389  return tf;
390  }
391  forceinline int
392  TraceRecorder::events(void) const {
393  return te;
394  }
396  TraceRecorder::tracer(void) const {
397  return t;
398  }
399 
400 
401  /*
402  * Trace recorder propagator
403  *
404  */
407  Tracer& t0)
408  : Propagator(home), tf(tf0), te(te0), t(t0) {
409  home.notice(*this, AP_DISPOSE);
410  home.notice(*this, AP_TRACE);
411  }
412 
415  if (te & (TE_PROPAGATE | TE_COMMIT))
416  (void) new (home) TraceRecorder(home,tf,te,t);
417  return ES_OK;
418  }
419 
422  TraceRecorder& p)
423  : Propagator(home,share,p), te(p.te), t(p.t) {
424  tf.update(home, share, p.tf);
425  }
426 
427 }
428 
429 // STATISTICS: kernel-trace
SlackValue i
The initial slack value.
Council of advisors
Definition: core.hpp:232
Advisor with index information.
void update(Space &, bool share, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1387
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last)
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Tracer that process view trace information.
Definition: tracer.hpp:55
virtual void reschedule(Space &home)
Schedule function.
Collection of slack values.
Tracer & t
The actual tracer.
ViewArray< View > n
Original views (new information)
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
Actor must always be disposed.
Definition: core.hpp:630
const View::VarType operator[](int i) const
Return variable being traced at position i.
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition: core.hpp:149
TraceTraits< View >::SlackValue SlackValue
The corresponding slack value type.
Base-class for propagators.
Definition: core.hpp:1092
TraceFilter tf
The trace filter.
Base-class for advisors.
Definition: core.hpp:1294
static PropCost record(void)
For recording information (no propagation allowed)
Definition: core.hpp:4810
Class to iterate over advisors of a council.
Definition: core.hpp:233
Propagation has computed fixpoint.
Definition: core.hpp:545
Trace init events.
static ExecStatus post(Home home, TraceFilter tf, int te, Tracer &t)
Post propagator.
Computation spaces.
Definition: core.hpp:1748
ViewTraceRecorder(Space &home, bool share, ViewTraceRecorder &p)
Constructor for cloning p.
Trace commit operations by branchers.
TraceRecorder(Space &home, bool share, TraceRecorder &p)
Constructor for cloning p.
const TraceFilter & filter(void) const
Return trace filter.
Gecode::IntSet d(v, 7)
TraceTraits< View >::TraceDelta TraceDelta
The corresponding trace delta type.
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
Gecode::FloatVal c(-8, 8)
Single _a(2, 3)
Trace prune events.
virtual void reschedule(Space &home)
Schedule function.
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
Tracer & tracer(void) const
Return tracer.
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Slack s
Slack information.
int _idx
Index information.
TraceFilter tf
The trace filter.
Propagator for recording trace information.
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:4095
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:1103
static ExecStatus post(Home home, ViewArray< View > &x, TraceFilter tf, int te, ViewTracer< View > &t)
Post recorder propagator.
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
TraceTraits< View >::TraceView TraceView
The corresponding duplicate view type.
Trace filters.
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
SlackValue previous(void) const
Return previous slack value.
Traits for tracing.
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3321
int te
Which events to trace.
Propagator for recording view trace information.
void update(Space &home, bool share, SharedHandle &sh)
Updating during cloning.
Definition: core.hpp:3127
Trace done events.
ViewTracer< View > & t
The actual tracer.
int size(void) const
Return number of variables being traced.
SlackValue c
Current slack value.
Generic domain change information to be supplied to advisors.
Definition: core.hpp:281
int te
Which events to trace.
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4141
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3354
Propagation cost.
Definition: core.hpp:554
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Idx(Space &home, Propagator &p, Council< Idx > &c, int i)
Constructor for creation.
ExecStatus
Definition: core.hpp:540
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last)
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:173
Trace fail events.
Post propagator for SetVar x
Definition: set.hh:784
Trace propagator executions.
Execution is okay.
Definition: core.hpp:544
Tracer.
Definition: tracer.hpp:153
ViewArray< TraceView > o
Duplicate views (old information)
Trace fixpoint events.
Propagation has not computed fixpoint.
Definition: core.hpp:543
SlackValue current(void) const
Return current slack value.
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Gecode toplevel namespace
int idx(void) const
Get index of view.
int events(void) const
Which events to trace.
virtual size_t dispose(Space &home)
Delete propagator and return its size.
const Slack & slack(void) const
Provide access to slack information.
SlackValue initial(void) const
Return initial slack value.
SlackValue p
Slack value at previous event (fixpoint or init)
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
Home class for posting propagators
Definition: core.hpp:922
Council< Idx > c
The advisor council.
virtual Propagator * copy(Space &home, bool share)
Copy propagator during cloning.
virtual Propagator * copy(Space &home, bool share)
Copy propagator during cloning.
TraceEvent
Which events to trace.