Generated on Thu Mar 16 2017 03:24:22 for Gecode by doxygen 1.8.13
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: 2016-07-01 13:53:19 +0200 (Fri, 01 Jul 2016) $ by $Author: schulte $
11  * $Revision: 15138 $
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 {
45  TE_INIT = 1 << 0,
46  TE_PRUNE = 1 << 1,
47  TE_FIX = 1 << 2,
48  TE_DONE = 1 << 3
49  };
50 
55  template<class View>
56  class TraceRecorder : public Propagator {
57  public:
65  class Slack {
66  template<class ViewForTraceRecorder> friend class TraceRecorder;
67  protected:
69  SlackValue i;
71  SlackValue p;
73  SlackValue c;
74  public:
76  SlackValue initial(void) const;
78  SlackValue previous(void) const;
80  SlackValue current(void) const;
81  };
82  protected:
84  class Idx : public Advisor {
85  protected:
87  int _idx;
88  public:
90  Idx(Space& home, Propagator& p, Council<Idx>& c, int i);
92  Idx(Space& home, bool share, Idx& a);
94  int idx(void) const;
95  };
105  int te;
111  TraceRecorder(Space& home, bool share, TraceRecorder& p);
112  public:
115  TraceFilter tf, int te, Tracer<View>& t);
117  virtual Propagator* copy(Space& home, bool share);
119  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
121  virtual void reschedule(Space& home);
123  virtual ExecStatus advise(Space& home, Advisor& a, const Delta& d);
125  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
127  virtual size_t dispose(Space& home);
129  static ExecStatus post(Home home, ViewArray<View>& x,
130  TraceFilter tf, int te, Tracer<View>& t);
132 
133  const typename View::VarType operator [](int i) const;
136  int size(void) const;
138  const Slack& slack(void) const;
140  };
141 
142  /*
143  * Functions for trace support
144  *
145  */
146  template<class View>
147  forceinline const typename View::VarType
149  const typename View::VarType x(n[i].varimp());
150  return x;
151  }
152  template<class View>
153  forceinline int
155  return n.size();
156  }
157  template<class View>
160  return s;
161  }
162 
163 
164  /*
165  * Functions for access to slack
166  *
167  */
168  template<class View>
171  return i;
172  }
173  template<class View>
176  return p;
177  }
178  template<class View>
181  return c;
182  }
183 
184 
185  /*
186  * Advisor for tracer
187  *
188  */
189 
190  template<class View>
193  Council<Idx>& c, int i)
194  : Advisor(home,p,c), _idx(i) {}
195  template<class View>
198  : Advisor(home,share,a), _idx(a._idx) {
199  }
200  template<class View>
201  forceinline int
203  return _idx;
204  }
205 
206 
207  /*
208  * Posting of tracer propagator
209  *
210  */
211  template<class View>
214  TraceFilter tf0, int te0,
215  Tracer<View>& t0)
216  : Propagator(home), o(home,x.size()), n(x), c(home),
217  tf(tf0), te(te0), t(t0) {
218  home.notice(*this, AP_DISPOSE);
219  for (int i=n.size(); i--; ) {
220  o[i] = TraceView(home,n[i]);
221  if (!n[i].assigned())
222  n[i].subscribe(home,*new (home) Idx(home,*this,c,i));
223  }
224  View::schedule(home,*this,ME_GEN_ASSIGNED);
225  s.i = TraceView::slack(n[n.size()-1]);
226  for (int i=n.size()-1; i--; )
227  s.i += TraceView::slack(n[i]);
228  s.p = s.i;
229  if ((te & TE_INIT) != 0)
230  t._init(home,*this);
231  }
232 
233 
234  template<class View>
237  TraceFilter tf, int te, Tracer<View>& t) {
238  if (x.size() > 0)
239  (void) new (home) TraceRecorder(home,x,tf,te,t);
240  return ES_OK;
241  }
242 
243 
244  /*
245  * Propagation for trace collector
246  *
247  */
248  template<class View>
251  TraceRecorder& p)
252  : Propagator(home,share,p), te(p.te), t(p.t), s(p.s) {
253  o.update(home, share, p.o);
254  n.update(home, share, p.n);
255  c.update(home, share, p.c);
256  tf.update(home, share, p.tf);
257  }
258 
259  template<class View>
260  Propagator*
261  TraceRecorder<View>::copy(Space& home, bool share) {
262  return new (home) TraceRecorder(home, share, *this);
263  }
264 
265  template<class View>
266  inline size_t
268  home.ignore(*this,AP_DISPOSE);
269  tf.~TraceFilter();
270  // Cancel remaining advisors
271  for (Advisors<Idx> as(c); as(); ++as)
272  n[as.advisor().idx()].cancel(home,as.advisor());
273  c.dispose(home);
274  (void) Propagator::dispose(home);
275  return sizeof(*this);
276  }
277 
278  template<class View>
279  PropCost
281  return PropCost::record();
282  }
283 
284  template<class View>
285  void
287  View::schedule(home,*this,ME_GEN_ASSIGNED);
288  }
289 
290  template<class View>
291  ExecStatus
293  Idx& a = static_cast<Idx&>(_a);
294  int i = a.idx();
295  if (((te & TE_PRUNE) != 0) && !disabled() && tf(a(home)) ) {
296  TraceDelta td(o[i],n[i],d);
297  t._prune(home,*this,a(home),i,td);
298  }
299  o[i].prune(home,n[i],d);
300  if (n[a.idx()].assigned())
301  a.dispose(home,c);
302  return ES_NOFIX;
303  }
304 
305  template<class View>
306  ExecStatus
308  s.c = TraceView::slack(n[n.size()-1]);
309  for (int i=n.size()-1; i--; )
310  s.c += TraceView::slack(n[i]);
311  if ((te & TE_FIX) != 0)
312  t._fix(home,*this);
313  s.p = s.c;
314  if (c.empty()) {
315  if ((te & TE_DONE) != 0)
316  t._done(home,*this);
317  return home.ES_SUBSUMED(*this);
318  }
319  return ES_FIX;
320  }
321 
322 }
323 
324 // STATISTICS: kernel-other
SlackValue p
Slack value at previous event (fixpoint or init)
Council of advisors
Definition: core.hpp:228
void update(Space &, bool share, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1387
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
SlackValue c
Current slack value.
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last)
Collection of slack values.
int _idx
Index information.
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3484
const Slack & slack(void) const
Provide access to slack information.
Actor must always be disposed.
Definition: core.hpp:626
SlackValue initial(void) const
Return initial slack value.
static ExecStatus post(Home home, ViewArray< View > &x, TraceFilter tf, int te, Tracer< View > &t)
Post activity recorder propagator.
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition: core.hpp:149
Base-class for propagators.
Definition: core.hpp:1012
Tracer< View > & t
The actual tracer.
Base-class for advisors.
Definition: core.hpp:1212
static PropCost record(void)
For recording information (no propagation allowed)
Definition: core.hpp:4546
Class to iterate over advisors of a council.
Definition: core.hpp:229
int idx(void) const
Get index of view.
int size(void) const
Return number of variables being traced.
Propagation has computed fixpoint.
Definition: core.hpp:541
Trace init events.
Computation spaces.
Definition: core.hpp:1672
Advisor with index information.
Gecode::IntSet d(v, 7)
TraceTraits< View >::SlackValue SlackValue
The corresponding slack value type.
const View::VarType operator[](int i) const
Return variable being traced at position i.
Single _a(2, 3)
Trace prune events.
struct Gecode::@554::NNF::@60::@62 a
For atomic nodes.
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
TraceFilter tf
The trace filter.
Propagator for recording trace information.
ViewArray< View > n
Original views (new information)
ViewArray< TraceView > o
Duplicate views (old information)
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:1022
Trace filters.
bool disabled(void) const
Whether propagator is currently disabled.
Definition: core.hpp:3412
TraceTraits< View >::TraceView TraceView
The corresponding duplicate view type.
Traits for tracing.
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3253
Idx(Space &home, Propagator &p, Council< Idx > &c, int i)
Constructor for creation.
SlackValue previous(void) const
Return previous slack value.
void update(Space &home, bool share, SharedHandle &sh)
Updating during cloning.
Definition: core.hpp:3042
Trace done events.
virtual Propagator * copy(Space &home, bool share)
Copy propagator during cloning.
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Generic domain change information to be supplied to advisors.
Definition: core.hpp:277
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.cpp:167
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3281
void dispose(Space &home, Council< A > &c)
Dispose the advisor.
Definition: core.hpp:3783
Propagation cost.
Definition: core.hpp:550
ExecStatus
Definition: core.hpp:536
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:173
virtual void reschedule(Space &home)
Schedule function.
int te
Which events to trace.
Execution is okay.
Definition: core.hpp:540
Tracer that process trace information.
Definition: tracer.hpp:55
Trace fixpoint events.
Slack s
Slack information.
Propagation has not computed fixpoint.
Definition: core.hpp:539
TraceTraits< View >::TraceDelta TraceDelta
The corresponding trace delta type.
Gecode toplevel namespace
SlackValue current(void) const
Return current slack value.
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
Home class for posting propagators
Definition: core.hpp:905
SlackValue i
The initial slack value.
Council< Idx > c
The advisor council.
virtual size_t dispose(Space &home)
Delete propagator and return its size.
TraceEvent
Which events to trace.
TraceRecorder(Space &home, bool share, TraceRecorder &p)
Constructor for cloning p.