Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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$ by $Author$
11  * $Revision$
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:
76  SlackValue i;
78  SlackValue p;
80  SlackValue c;
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, Idx& a);
101  int idx(void) const;
102  };
112  int te;
119  public:
122  TraceFilter tf, int te, ViewTracer<View>& t);
124  virtual Propagator* copy(Space& home);
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,
137  TraceFilter tf, int te, ViewTracer<View>& t);
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;
167  public:
169  TraceRecorder(Home home, TraceFilter tf, int te, Tracer& t);
171  virtual Propagator* copy(Space& home);
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,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>
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>
305  : Propagator(home,p), tf(p.tf), te(p.te), t(p.t), s(p.s) {
306  o.update(home, p.o);
307  n.update(home, p.n);
308  c.update(home, p.c);
309  }
310 
311  template<class View>
312  Propagator*
314  return new (home) ViewTraceRecorder(home, *this);
315  }
316 
317  template<class View>
318  inline size_t
320  home.ignore(*this, AP_VIEW_TRACE);
321  home.ignore(*this, AP_DISPOSE);
322  tf.~TraceFilter();
323  // Cancel remaining advisors
324  for (Advisors<Idx> as(c); as(); ++as)
325  n[as.advisor().idx()].cancel(home,as.advisor());
326  c.dispose(home);
327  (void) Propagator::dispose(home);
328  return sizeof(*this);
329  }
330 
331  template<class View>
332  PropCost
334  return PropCost::record();
335  }
336 
337  template<class View>
338  void
340  View::schedule(home,*this,ME_GEN_ASSIGNED);
341  }
342 
343  template<class View>
344  ExecStatus
346  Idx& a = static_cast<Idx&>(_a);
347  int i = a.idx();
348  if (((te & TE_PRUNE) != 0) && !disabled() && tf(a(home)) ) {
349  TraceDelta td(o[i],n[i],d);
350  t._prune(home,*this,a(home),i,td);
351  }
352  o[i].prune(home,n[i],d);
353  if (n[a.idx()].assigned())
354  a.dispose(home,c);
355  return ES_NOFIX;
356  }
357 
358  template<class View>
359  ExecStatus
361  s.c = TraceView::slack(n[n.size()-1]);
362  for (int i=n.size()-1; i--; )
363  s.c += TraceView::slack(n[i]);
364  if (home.failed() && ((te & TE_FAIL) != 0) && !disabled()) {
365  t._fail(home,*this);
366  return ES_FIX;
367  }
368  if ((te & TE_FIX) != 0)
369  t._fix(home,*this);
370  s.p = s.c;
371  if (c.empty()) {
372  if ((te & TE_DONE) != 0)
373  t._done(home,*this);
374  return home.ES_SUBSUMED(*this);
375  }
376  return ES_FIX;
377  }
378 
379 
380 
381  /*
382  * Functions for trace support
383  *
384  */
385  forceinline const TraceFilter&
386  TraceRecorder::filter(void) const {
387  return tf;
388  }
389  forceinline int
390  TraceRecorder::events(void) const {
391  return te;
392  }
394  TraceRecorder::tracer(void) const {
395  return t;
396  }
397 
398 
399  /*
400  * Trace recorder propagator
401  *
402  */
405  Tracer& t0)
406  : Propagator(home), tf(tf0), te(te0), t(t0) {
407  home.notice(*this, AP_DISPOSE);
408  home.notice(*this, AP_TRACE);
409  }
410 
413  if (te & (TE_PROPAGATE | TE_COMMIT))
414  (void) new (home) TraceRecorder(home,tf,te,t);
415  return ES_OK;
416  }
417 
420  : Propagator(home,p), tf(p.tf), te(p.te), t(p.t) {
421  }
422 
423 }
424 
425 // STATISTICS: kernel-trace
SlackValue i
The initial slack value.
Definition: recorder.hpp:76
Council of advisors
Definition: core.hpp:156
Advisor with index information.
Definition: recorder.hpp:91
Tracer that process view trace information.
Definition: tracer.hpp:55
virtual void reschedule(Space &home)
Schedule function.
Definition: recorder.hpp:339
Collection of slack values.
Definition: recorder.hpp:72
Tracer & t
The actual tracer.
Definition: recorder.hpp:164
ViewArray< View > n
Original views (new information)
Definition: recorder.hpp:106
void update(Space &home, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1375
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
Actor must always be disposed.
Definition: core.hpp:554
const View::VarType operator[](int i) const
Return variable being traced at position i.
Definition: recorder.hpp:200
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition: core.hpp:71
TraceTraits< View >::SlackValue SlackValue
The corresponding slack value type.
Definition: recorder.hpp:70
Base-class for propagators.
Definition: core.hpp:1016
TraceFilter tf
The trace filter.
Definition: recorder.hpp:160
Base-class for advisors.
Definition: core.hpp:1218
static PropCost record(void)
For recording information (no propagation allowed)
Definition: core.hpp:4612
Class to iterate over advisors of a council.
Definition: core.hpp:157
TraceRecorder(Space &home, TraceRecorder &p)
Constructor for cloning p.
Definition: recorder.hpp:419
#define forceinline
Definition: config.hpp:182
Propagation has computed fixpoint.
Definition: core.hpp:469
Trace init events.
Definition: recorder.hpp:47
static ExecStatus post(Home home, TraceFilter tf, int te, Tracer &t)
Post propagator.
Definition: recorder.hpp:412
Computation spaces.
Definition: core.hpp:1668
Trace commit operations by branchers.
Definition: recorder.hpp:55
const TraceFilter & filter(void) const
Return trace filter.
Definition: recorder.hpp:386
Gecode::IntSet d(v, 7)
TraceTraits< View >::TraceDelta TraceDelta
The corresponding trace delta type.
Definition: recorder.hpp:68
Single _a(2, 3)
Trace prune events.
Definition: recorder.hpp:48
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.
Definition: recorder.hpp:394
Slack s
Slack information.
Definition: recorder.hpp:116
int _idx
Index information.
Definition: recorder.hpp:94
TraceFilter tf
The trace filter.
Definition: recorder.hpp:110
Propagator for recording trace information.
Definition: recorder.hpp:157
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3914
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:1027
static ExecStatus post(Home home, ViewArray< View > &x, TraceFilter tf, int te, ViewTracer< View > &t)
Post recorder propagator.
Definition: recorder.hpp:289
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition: recorder.hpp:345
TraceTraits< View >::TraceView TraceView
The corresponding duplicate view type.
Definition: recorder.hpp:66
Trace filters.
Definition: filter.hpp:137
bool disabled(void) const
Whether propagator is currently disabled.
Definition: core.hpp:3358
SlackValue previous(void) const
Return previous slack value.
Definition: recorder.hpp:227
Traits for tracing.
Definition: traits.hpp:53
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3139
int te
Which events to trace.
Definition: recorder.hpp:162
ViewTraceRecorder(Space &home, ViewTraceRecorder &p)
Constructor for cloning p.
Definition: recorder.hpp:304
Propagator for recording view trace information.
Definition: recorder.hpp:63
Trace done events.
Definition: recorder.hpp:51
ViewTracer< View > & t
The actual tracer.
Definition: recorder.hpp:114
int size(void) const
Return number of variables being traced.
Definition: recorder.hpp:206
SlackValue c
Current slack value.
Definition: recorder.hpp:80
Generic domain change information to be supplied to advisors.
Definition: core.hpp:205
int te
Which events to trace.
Definition: recorder.hpp:112
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:3944
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3172
void dispose(Space &home, Council< A > &c)
Dispose the advisor.
Definition: core.hpp:3734
Propagation cost.
Definition: core.hpp:478
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition: recorder.hpp:313
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: recorder.hpp:360
Idx(Space &home, Propagator &p, Council< Idx > &c, int i)
Constructor for creation.
Definition: recorder.hpp:244
ExecStatus
Definition: core.hpp:464
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last)
Definition: recorder.hpp:333
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
Trace fail events.
Definition: recorder.hpp:50
Post propagator for SetVar x
Definition: set.hh:769
Trace propagator executions.
Definition: recorder.hpp:54
Execution is okay.
Definition: core.hpp:468
Tracer.
Definition: tracer.hpp:153
ViewArray< TraceView > o
Duplicate views (old information)
Definition: recorder.hpp:104
Trace fixpoint events.
Definition: recorder.hpp:49
Propagation has not computed fixpoint.
Definition: core.hpp:467
SlackValue current(void) const
Return current slack value.
Definition: recorder.hpp:232
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: recorder.hpp:319
Gecode toplevel namespace
int idx(void) const
Get index of view.
Definition: recorder.hpp:254
int events(void) const
Which events to trace.
Definition: recorder.hpp:390
const Slack & slack(void) const
Provide access to slack information.
Definition: recorder.hpp:211
SlackValue initial(void) const
Return initial slack value.
Definition: recorder.hpp:222
SlackValue p
Slack value at previous event (fixpoint or init)
Definition: recorder.hpp:78
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1203
int ModEventDelta
Modification event deltas.
Definition: core.hpp:91
Home class for posting propagators
Definition: core.hpp:846
Council< Idx > c
The advisor council.
Definition: recorder.hpp:108
TraceEvent
Which events to trace.
Definition: recorder.hpp:44