recorder.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2016
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode {
enum TraceEvent {
TE_INIT = 1 << 0,
TE_PRUNE = 1 << 1,
TE_FIX = 1 << 2,
TE_FAIL = 1 << 3,
TE_DONE = 1 << 4,
TE_PROPAGATE = 1 << 5,
TE_COMMIT = 1 << 6,
TE_POST = 1 << 7
};
template<class View>
class ViewTraceRecorder : public Propagator {
public:
typedef typename TraceTraits<View>::TraceView TraceView;
typedef typename TraceTraits<View>::TraceDelta TraceDelta;
typedef typename TraceTraits<View>::SlackValue SlackValue;
class Slack {
template<class ViewForTraceRecorder> friend class ViewTraceRecorder;
protected:
SlackValue i;
SlackValue p;
SlackValue c;
public:
SlackValue initial(void) const;
SlackValue previous(void) const;
SlackValue current(void) const;
};
protected:
class Idx : public Advisor {
protected:
int _idx;
public:
Idx(Space& home, Propagator& p, Council<Idx>& c, int i);
Idx(Space& home, Idx& a);
int idx(void) const;
};
ViewArray<TraceView> o;
ViewArray<View> n;
Council<Idx> c;
TraceFilter tf;
int te;
ViewTracer<View>& t;
Slack s;
ViewTraceRecorder(Space& home, ViewTraceRecorder& p);
public:
ViewTraceRecorder(Home home, ViewArray<View>& x,
TraceFilter tf, int te, ViewTracer<View>& t);
virtual Propagator* copy(Space& home);
virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
virtual void reschedule(Space& home);
virtual ExecStatus advise(Space& home, Advisor& a, const Delta& d);
virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
virtual size_t dispose(Space& home);
static ExecStatus post(Home home, ViewArray<View>& x,
TraceFilter tf, int te, ViewTracer<View>& t);
const typename View::VarType operator [](int i) const;
int size(void) const;
const Slack& slack(void) const;
};
class TraceRecorder : public Propagator {
public:
TraceFilter tf;
int te;
Tracer& t;
TraceRecorder(Space& home, TraceRecorder& p);
public:
TraceRecorder(Home home, TraceFilter tf, int te, Tracer& t);
virtual Propagator* copy(Space& home);
virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
virtual void reschedule(Space& home);
virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
virtual size_t dispose(Space& home);
static ExecStatus post(Home home, TraceFilter tf, int te, Tracer& t);
const TraceFilter& filter(void) const;
int events(void) const;
Tracer& tracer(void) const;
};
/*
* Functions for trace support
*
*/
template<class View>
forceinline const typename View::VarType
ViewTraceRecorder<View>::operator [](int i) const {
const typename View::VarType x(n[i].varimp());
return x;
}
template<class View>
forceinline int
ViewTraceRecorder<View>::size(void) const {
return n.size();
}
template<class View>
forceinline const typename ViewTraceRecorder<View>::Slack&
ViewTraceRecorder<View>::slack(void) const {
return s;
}
/*
* Functions for access to slack
*
*/
template<class View>
forceinline typename ViewTraceRecorder<View>::SlackValue
ViewTraceRecorder<View>::Slack::initial(void) const {
return i;
}
template<class View>
forceinline typename ViewTraceRecorder<View>::SlackValue
ViewTraceRecorder<View>::Slack::previous(void) const {
return p;
}
template<class View>
forceinline typename ViewTraceRecorder<View>::SlackValue
ViewTraceRecorder<View>::Slack::current(void) const {
return c;
}
/*
* Advisor for tracer
*
*/
template<class View>
forceinline
ViewTraceRecorder<View>::Idx::Idx(Space& home, Propagator& p,
Council<Idx>& c, int i)
: Advisor(home,p,c), _idx(i) {}
template<class View>
forceinline
ViewTraceRecorder<View>::Idx::Idx(Space& home, Idx& a)
: Advisor(home,a), _idx(a._idx) {
}
template<class View>
forceinline int
ViewTraceRecorder<View>::Idx::idx(void) const {
return _idx;
}
/*
* Posting of tracer propagator
*
*/
template<class View>
forceinline
ViewTraceRecorder<View>::ViewTraceRecorder(Home home, ViewArray<View>& x,
TraceFilter tf0, int te0,
ViewTracer<View>& t0)
: Propagator(home), o(home,x.size()), n(x), c(home),
tf(tf0), te(te0), t(t0) {
home.notice(*this, AP_VIEW_TRACE);
home.notice(*this, AP_DISPOSE);
for (int i=0; i<n.size(); i++) {
o[i] = TraceView(home,n[i]);
if (!n[i].assigned())
n[i].subscribe(home,*new (home) Idx(home,*this,c,i));
}
View::schedule(home,*this,ME_GEN_ASSIGNED);
s.i = TraceView::slack(n[0]);
for (int i=1; i<n.size(); i++)
s.i += TraceView::slack(n[i]);
s.p = s.i;
if ((te & TE_INIT) != 0)
t._init(home,*this);
}
template<class View>
forceinline ExecStatus
ViewTraceRecorder<View>::post(Home home, ViewArray<View>& x,
TraceFilter tf, int te, ViewTracer<View>& t) {
if ((x.size() > 0) &&
(te & (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE)))
(void) new (home) ViewTraceRecorder(home,x,tf,te,t);
return ES_OK;
}
/*
* Propagation for trace recorder
*
*/
template<class View>
forceinline
ViewTraceRecorder<View>::ViewTraceRecorder(Space& home, ViewTraceRecorder& p)
: Propagator(home,p), tf(p.tf), te(p.te), t(p.t), s(p.s) {
o.update(home, p.o);
n.update(home, p.n);
c.update(home, p.c);
}
template<class View>
Propagator*
ViewTraceRecorder<View>::copy(Space& home) {
return new (home) ViewTraceRecorder(home, *this);
}
template<class View>
inline size_t
ViewTraceRecorder<View>::dispose(Space& home) {
home.ignore(*this, AP_VIEW_TRACE);
home.ignore(*this, AP_DISPOSE);
tf.~TraceFilter();
// Cancel remaining advisors
for (Advisors<Idx> as(c); as(); ++as)
n[as.advisor().idx()].cancel(home,as.advisor());
c.dispose(home);
(void) Propagator::dispose(home);
return sizeof(*this);
}
template<class View>
PropCost
ViewTraceRecorder<View>::cost(const Space&, const ModEventDelta&) const {
return PropCost::record();
}
template<class View>
void
ViewTraceRecorder<View>::reschedule(Space& home) {
View::schedule(home,*this,ME_GEN_ASSIGNED);
}
template<class View>
ExecStatus
ViewTraceRecorder<View>::advise(Space& home, Advisor& _a, const Delta& d) {
Idx& a = static_cast<Idx&>(_a);
int i = a.idx();
if (((te & TE_PRUNE) != 0) && !disabled() && tf(a(home)) ) {
TraceDelta td(o[i],n[i],d);
t._prune(home,*this,a(home),i,td);
}
o[i].prune(home,n[i],d);
if (n[a.idx()].assigned())
a.dispose(home,c);
return ES_NOFIX;
}
template<class View>
ExecStatus
ViewTraceRecorder<View>::propagate(Space& home, const ModEventDelta&) {
s.c = TraceView::slack(n[0]);
for (int i=1; i<n.size(); i++)
s.c += TraceView::slack(n[i]);
if (home.failed() && ((te & TE_FAIL) != 0) && !disabled()) {
t._fail(home,*this);
return ES_FIX;
}
if ((te & TE_FIX) != 0)
t._fix(home,*this);
s.p = s.c;
if (c.empty()) {
if ((te & TE_DONE) != 0)
t._done(home,*this);
return home.ES_SUBSUMED(*this);
}
return ES_FIX;
}
/*
* Functions for trace support
*
*/
forceinline const TraceFilter&
TraceRecorder::filter(void) const {
return tf;
}
forceinline int
TraceRecorder::events(void) const {
return te;
}
forceinline Tracer&
TraceRecorder::tracer(void) const {
return t;
}
/*
* Trace recorder propagator
*
*/
forceinline
TraceRecorder::TraceRecorder(Home home, TraceFilter tf0, int te0,
Tracer& t0)
: Propagator(home), tf(tf0), te(te0), t(t0) {
home.notice(*this, AP_DISPOSE);
home.notice(*this, AP_TRACE);
}
forceinline ExecStatus
TraceRecorder::post(Home home, TraceFilter tf, int te, Tracer& t) {
if (te & (TE_PROPAGATE | TE_COMMIT | TE_POST))
(void) new (home) TraceRecorder(home,tf,te,t);
return ES_OK;
}
forceinline
TraceRecorder::TraceRecorder(Space& home, TraceRecorder& p)
: Propagator(home,p), tf(p.tf), te(p.te), t(p.t) {
}
}
// STATISTICS: kernel-trace
Gecode::ActorLink::Space
friend class Space
Definition core.hpp:595
Gecode::ActorLink::Propagator
friend class Propagator
Definition core.hpp:591
Gecode::Actor::dispose
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition core.hpp:3252
Gecode::Advisor::Advisor
Advisor(Space &home, Propagator &p, Council< A > &c)
Constructor for creation.
Definition core.hpp:3831
Gecode::Advisor::Council
friend class Council
Definition core.hpp:1294
Gecode::Advisors
Class to iterate over advisors of a council.
Definition core.hpp:1266
Gecode::Delta
Generic domain change information to be supplied to advisors.
Definition core.hpp:204
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Home::notice
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3219
Gecode::PropCost
Propagation cost.
Definition core.hpp:486
Gecode::PropCost::record
static PropCost record(void)
For recording information (no propagation allowed).
Definition core.hpp:4765
Gecode::Propagator
Base-class for propagators.
Definition core.hpp:1064
Gecode::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Propagator::Advisor
friend class Advisor
Definition core.hpp:1068
Gecode::Propagator::Council
friend class Council
Definition core.hpp:1069
Gecode::Propagator::disabled
bool disabled(void) const
Whether propagator is currently disabled.
Definition core.hpp:3476
Gecode::Propagator::med
ModEventDelta med
A set of modification events (used during propagation).
Definition core.hpp:1075
Gecode::Propagator::Propagator
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3498
Gecode::TraceFilter
Trace filters.
Definition filter.hpp:133
Gecode::TraceRecorder::te
int te
Which events to trace.
Definition recorder.hpp:159
Gecode::TraceRecorder::post
static ExecStatus post(Home home, TraceFilter tf, int te, Tracer &t)
Post propagator.
Definition recorder.hpp:409
Gecode::TraceRecorder::t
Tracer & t
The actual tracer.
Definition recorder.hpp:161
Gecode::TraceRecorder::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition recorder.cpp:62
Gecode::TraceRecorder::events
int events(void) const
Which events to trace.
Definition recorder.hpp:387
Gecode::TraceRecorder::copy
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition recorder.cpp:39
Gecode::TraceRecorder::filter
const TraceFilter & filter(void) const
Return trace filter.
Definition recorder.hpp:383
Gecode::TraceRecorder::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last).
Definition recorder.cpp:53
Gecode::TraceRecorder::TraceRecorder
TraceRecorder(Space &home, TraceRecorder &p)
Constructor for cloning p.
Definition recorder.hpp:416
Gecode::TraceRecorder::tf
TraceFilter tf
The trace filter.
Definition recorder.hpp:157
Gecode::TraceRecorder::tracer
Tracer & tracer(void) const
Return tracer.
Definition recorder.hpp:391
Gecode::TraceRecorder::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition recorder.cpp:58
Gecode::TraceRecorder::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition recorder.cpp:44
Gecode::TraceTraits
Traits for tracing.
Definition traits.hpp:49
Gecode::Tracer
Tracer.
Definition tracer.hpp:149
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::ViewArray::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1156
Gecode::ViewTraceRecorder::Idx
Advisor with index information.
Definition recorder.hpp:88
Gecode::ViewTraceRecorder::Idx::idx
int idx(void) const
Get index of view.
Definition recorder.hpp:251
Gecode::ViewTraceRecorder::Idx::Idx
Idx(Space &home, Propagator &p, Council< Idx > &c, int i)
Constructor for creation.
Definition recorder.hpp:241
Gecode::ViewTraceRecorder::Idx::_idx
int _idx
Index information.
Definition recorder.hpp:91
Gecode::ViewTraceRecorder::Slack
Collection of slack values.
Definition recorder.hpp:69
Gecode::ViewTraceRecorder::Slack::p
SlackValue p
Slack value at previous event (fixpoint or init).
Definition recorder.hpp:75
Gecode::ViewTraceRecorder::Slack::initial
SlackValue initial(void) const
Return initial slack value.
Definition recorder.hpp:219
Gecode::ViewTraceRecorder::Slack::ViewTraceRecorder
friend class ViewTraceRecorder
Definition recorder.hpp:70
Gecode::ViewTraceRecorder::Slack::c
SlackValue c
Current slack value.
Definition recorder.hpp:77
Gecode::ViewTraceRecorder::Slack::i
SlackValue i
The initial slack value.
Definition recorder.hpp:73
Gecode::ViewTraceRecorder::Slack::current
SlackValue current(void) const
Return current slack value.
Definition recorder.hpp:229
Gecode::ViewTraceRecorder::Slack::previous
SlackValue previous(void) const
Return previous slack value.
Definition recorder.hpp:224
Gecode::ViewTraceRecorder::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition recorder.hpp:357
Gecode::ViewTraceRecorder< Float::FloatView >::t
ViewTracer< Float::FloatView > & t
Definition recorder.hpp:111
Gecode::ViewTraceRecorder::post
static ExecStatus post(Home home, ViewArray< View > &x, TraceFilter tf, int te, ViewTracer< View > &t)
Post recorder propagator.
Definition recorder.hpp:286
Gecode::ViewTraceRecorder< Float::FloatView >::c
Council< Idx > c
Definition recorder.hpp:105
Gecode::ViewTraceRecorder::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last).
Definition recorder.hpp:330
Gecode::ViewTraceRecorder::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition recorder.hpp:336
Gecode::ViewTraceRecorder::ViewTraceRecorder
ViewTraceRecorder(Space &home, ViewTraceRecorder &p)
Constructor for cloning p.
Definition recorder.hpp:301
Gecode::ViewTraceRecorder< Float::FloatView >::TraceView
TraceTraits< Float::FloatView >::TraceView TraceView
Definition recorder.hpp:63
Gecode::ViewTraceRecorder< Float::FloatView >::n
ViewArray< Float::FloatView > n
Definition recorder.hpp:103
Gecode::ViewTraceRecorder::operator[]
const View::VarType operator[](int i) const
Return variable being traced at position i.
Definition recorder.hpp:197
Gecode::ViewTraceRecorder< Float::FloatView >::o
ViewArray< TraceView > o
Definition recorder.hpp:101
Gecode::ViewTraceRecorder::size
int size(void) const
Return number of variables being traced.
Definition recorder.hpp:203
Gecode::ViewTraceRecorder< Float::FloatView >::s
Slack s
Definition recorder.hpp:113
Gecode::ViewTraceRecorder< Float::FloatView >::SlackValue
TraceTraits< Float::FloatView >::SlackValue SlackValue
Definition recorder.hpp:67
Gecode::ViewTraceRecorder::slack
const Slack & slack(void) const
Provide access to slack information.
Definition recorder.hpp:208
Gecode::ViewTraceRecorder::advise
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition recorder.hpp:342
Gecode::ViewTraceRecorder::copy
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition recorder.hpp:310
Gecode::ViewTraceRecorder::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition recorder.hpp:316
Gecode::ViewTraceRecorder< Float::FloatView >::TraceDelta
TraceTraits< Float::FloatView >::TraceDelta TraceDelta
Definition recorder.hpp:65
Gecode::ViewTraceRecorder< Float::FloatView >::te
int te
Definition recorder.hpp:109
Gecode::ViewTraceRecorder::ViewTraceRecorder
ViewTraceRecorder(Home home, ViewArray< View > &x, TraceFilter tf, int te, ViewTracer< View > &t)
Constructor for creation.
Definition recorder.hpp:262
Gecode::ViewTraceRecorder< Float::FloatView >::tf
TraceFilter tf
Definition recorder.hpp:107
Gecode::ViewTracer
Tracer that process view trace information.
Definition tracer.hpp:51
Gecode::Space::ES_SUBSUMED
ExecStatus ES_SUBSUMED(Propagator &p)
Definition core.hpp:3563
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
Gecode::ModEventDelta
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
Gecode::Space::failed
bool failed(void) const
Check whether space is failed.
Definition core.hpp:4044
Gecode::AP_VIEW_TRACE
@ AP_VIEW_TRACE
Definition core.hpp:573
Gecode::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
Gecode::AP_TRACE
@ AP_TRACE
Definition core.hpp:578
Gecode::TraceEvent
TraceEvent
Which events to trace.
Definition recorder.hpp:40
Gecode::TE_INIT
@ TE_INIT
Trace init events.
Definition recorder.hpp:43
Gecode::TE_POST
@ TE_POST
Trace propagator posting.
Definition recorder.hpp:52
Gecode::TE_COMMIT
@ TE_COMMIT
Trace commit operations by branchers.
Definition recorder.hpp:51
Gecode::TE_PRUNE
@ TE_PRUNE
Trace prune events.
Definition recorder.hpp:44
Gecode::TE_PROPAGATE
@ TE_PROPAGATE
Trace propagator executions.
Definition recorder.hpp:50
Gecode::TE_FIX
@ TE_FIX
Trace fixpoint events.
Definition recorder.hpp:45
Gecode::TE_FAIL
@ TE_FAIL
Trace fail events.
Definition recorder.hpp:46
Gecode::TE_DONE
@ TE_DONE
Trace done events.
Definition recorder.hpp:47
Gecode
Gecode toplevel namespace
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::ES_FIX
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
Gecode::ES_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
Gecode::ME_GEN_ASSIGNED
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition core.hpp:69
forceinline
#define forceinline
Definition config.hpp:187