81template <Space Space_, RealScalar Scalar_ =
double,
typename Allocator_ = DefaultAllocator>
94 using IndexBuffer = internal::Buffer<Index, Allocator_>;
95 using IndexAllocator =
typename IndexBuffer::allocator_type;
114 ,
parents(IndexAllocator(allocator))
120 Step(
Step const & other, Allocator_
const & allocator)
168 using StepAllocator = internal::ReBound<Allocator_, Step>;
169 using ListType = std::list<Step, StepAllocator>;
180 : m_steps(StepAllocator(allocator))
181 , m_lineage(IndexAllocator(allocator))
182 , m_traced(internal::bufferOf<
Scalar>(allocator))
187 : m_steps(StepAllocator(allocator))
188 , m_lineage(IndexAllocator(allocator))
189 , m_traced(internal::bufferOf<
Scalar>(allocator))
192 EMRYS_FILTER_ASSERT(
lag.steps > 0 &&
"a fixed-lag history keeps at least one step");
202 : m_steps(other.m_steps, StepAllocator(allocator))
203 , m_lineage(IndexAllocator(allocator))
204 , m_traced(internal::bufferOf<
Scalar>(allocator))
206 , m_resampleCount{other.m_resampleCount}
216 : m_steps(std::move(other.m_steps), StepAllocator(allocator))
217 , m_lineage(IndexAllocator(allocator))
218 , m_traced(internal::bufferOf<
Scalar>(allocator))
220 , m_resampleCount{other.m_resampleCount}
241 [[nodiscard]] std::optional<Index>
244 return m_lag > 0 ? std::optional<Index>{m_lag} : std::nullopt;
251 return Index(m_steps.size());
258 return m_steps.empty();
262 [[nodiscard]] Step
const &
265 EMRYS_FILTER_ASSERT(!
empty() &&
"the history is empty");
266 return m_steps.front();
270 [[nodiscard]] Step
const &
273 EMRYS_FILTER_ASSERT(!
empty() &&
"the history is empty");
274 return m_steps.back();
281 return m_steps.begin();
288 return m_steps.end();
298 bool const resampled = !
empty() && newResample(particles);
301 &&
"without a resample the particles must be those of the step before"
303 if (m_lag > 0 &&
size() == m_lag)
305 m_steps.splice(m_steps.end(), m_steps, m_steps.begin());
306 Step & step = m_steps.back();
312 m_steps.emplace_back(particles);
314 Step & step = m_steps.back();
318 std::span<Index const>
const ancestors = particles.
ancestors();
319 step.
parents.assign(ancestors.begin(), ancestors.end());
337 template <ExecutionPolicy P>
345 bool const resampled = newResample(latest);
347 (resampled || latest.
size() ==
back().particles.size())
348 &&
"without a resample the particles must be those of the newest step"
351 m_lineage.resize(std::size_t(count));
352 std::span<Index const>
const ancestors = latest.
ancestors();
353 internal::forEachIndex(policy, count, [&](
Index j) {
354 m_lineage[std::size_t(j)] = resampled ? ancestors[std::size_t(j)] : j;
356 Step const * next =
nullptr;
357 for (
auto step = m_steps.rbegin(); step != m_steps.rend(); ++step)
359 if (next !=
nullptr && next->resampled)
361 internal::forEachIndex(policy, count, [&](
Index j) {
362 Index const here = m_lineage[std::size_t(j)];
363 EMRYS_FILTER_ASSERT(here <
Index(next->parents.size()) &&
"a traced index is out of range");
364 m_lineage[std::size_t(j)] = next->parents[std::size_t(here)];
367 auto const states = step->particles.states();
368 m_traced.resize(std::size_t(states.rows() * count));
370 internal::gather(policy, std::span<Index const>{m_lineage}, states, traced);
371 step->smoothed.assign(traced, latest.
logWeights());
372 step->smoothed.clearBandwidth();
392 Index const resamples = particles.resampleCount() - m_resampleCount;
394 resamples >= 0 && resamples <= 1 &&
"at most one resample may fall between two records of the particles"
396 return resamples == 1;
400 IndexBuffer m_lineage;
401 internal::Buffer<Scalar, Allocator_> m_traced;
403 Index m_resampleCount = 0;
The allocator conventions of decisions.md [allocation]: DefaultAllocator, Capacity,...
Execution policies (decisions.md [execution-policies]): execution::sequential, the ExecutionPolicy co...
Lag, the number of steps a fixed-lag history keeps, which every history of the Smoothers layer takes ...
The macros every header includes first.
Mathematical constants and scalar functions that work in every scalar type the library accepts,...
The particle set: ParticleSet, the weighted samples, with resampling, upkeep of weights and particles...
The Space concept and what every continuous space shares (decisions.md [sizes-and-dimensions]): the t...
One recorded set of particles, its descent from the step before, and its smoothed set,...
Definition ParticleHistory.h:103
ParticleSetType particles
The particles as push() recorded them, before the step's prediction.
Definition ParticleHistory.h:151
Step(ParticleSetType const &recorded, Allocator_ const &allocator={})
A copy of recorded in storage from allocator, with the recorded particles as the smoothed ones until ...
Definition ParticleHistory.h:111
Step(Step const &other, Allocator_ const &allocator)
Copies other into storage from allocator.
Definition ParticleHistory.h:120
Step & operator=(Step &&)=default
Takes the other step's storage, as the members' do.
Step(Step &&other, Allocator_ const &allocator)
Moves other into storage from allocator, taking its storage when the allocators are equal.
Definition ParticleHistory.h:130
Allocator_ allocator_type
the allocator its sets and indices come from
Definition ParticleHistory.h:106
Step(Step &&)=default
moves the sets and the parents
IndexBuffer parents
If resampled, particle i here descends from particle parents[i] of the step before; otherwise from pa...
Definition ParticleHistory.h:161
bool resampled
whether a resample fell between the step before and this one
Definition ParticleHistory.h:163
ParticleSetType smoothed
for the step's , as of the last smooth(): the traced states with the latest weights; a copy of partic...
Definition ParticleHistory.h:156
Step & operator=(Step const &)=default
Copies into this step's storage.
Step(Step const &)=default
copies the sets and the parents
The record of a particle filter's forward pass that ancestor tracing runs backward over: the particle...
Definition ParticleHistory.h:83
ParticleHistory()=default
An empty history that keeps every step.
ParticleHistory(Allocator_ const &allocator)
An empty history that keeps every step, with storage from allocator.
Definition ParticleHistory.h:179
ParticleHistory & operator=(ParticleHistory &&other)=default
Takes other's steps and lag, as std::list's move assignment does.
ParticleHistory & operator=(ParticleHistory const &other)=default
Copies other's steps and lag.
ParticleSet< Space_, Scalar_, Allocator_ > ParticleSetType
what each step records
Definition ParticleHistory.h:88
Step const & back() const
Definition ParticleHistory.h:271
ParticleHistory(ParticleHistory const &other, Allocator_ const &allocator)
Copies other's steps into storage from allocator.
Definition ParticleHistory.h:201
std::optional< Index > lag() const
Definition ParticleHistory.h:242
bool empty() const
Definition ParticleHistory.h:256
const_iterator end() const
Definition ParticleHistory.h:286
Space_ SpaceType
the state space
Definition ParticleHistory.h:87
ParticleHistory(ParticleHistory &&other)=default
Takes other's steps.
typename ListType::const_iterator const_iterator
iterates the steps, oldest first
Definition ParticleHistory.h:173
void push(ParticleSetType const &particles)
Records particles, the filter's state before its next prediction, as the newest step,...
Definition ParticleHistory.h:296
void clear()
Drops every step, keeping the lag.
Definition ParticleHistory.h:379
void smooth(P &&policy, ParticleSetType const &latest)
smooth() under policy: each step's tracing and gather are for-eaches over the latest particles (see E...
Definition ParticleHistory.h:339
void smooth(ParticleSetType const &latest)
Fills every step's smoothed set by ancestor tracing, from the newest back, given latest,...
Definition ParticleHistory.h:329
ParticleHistory(ParticleHistory const &other)
Copies the steps into nodes from other's allocator (see Allocation).
Definition ParticleHistory.h:196
Allocator_ allocator_type
the allocator the steps come from
Definition ParticleHistory.h:89
ParticleHistory(Lag lag, Allocator_ const &allocator={})
An empty history that keeps the newest lag steps, with storage from allocator.
Definition ParticleHistory.h:186
const_iterator begin() const
Definition ParticleHistory.h:279
ParticleHistory(ParticleHistory &&other, Allocator_ const &allocator)
Moves other's steps into storage from allocator, taking its storage when the allocators are equal.
Definition ParticleHistory.h:215
Eigen::Index Index
Eigen's index type.
Definition ParticleHistory.h:90
Index size() const
Definition ParticleHistory.h:249
allocator_type get_allocator() const
Definition ParticleHistory.h:235
Scalar_ Scalar
the scalar type
Definition ParticleHistory.h:86
Step const & front() const
Definition ParticleHistory.h:263
A set of weighted samples: the empirical distribution.
Definition ParticleSet.h:279
Index resampleCount() const
Definition ParticleSet.h:1331
Index size() const
Definition ParticleSet.h:606
LogWeightsType logWeights() const
Definition ParticleSet.h:666
std::span< Index const > ancestors() const
Definition ParticleSet.h:1321
Eigen::Map< SampleSet< Space, Scalar > > SampleSetMap
A writable view of samples in Space, one per column, over memory it does not own, laid out as a Sampl...
Definition SampleSet.h:50
constexpr Sequential sequential
The default execution policy: plain loops on the calling thread, in index order (Sequential).
Definition Execution.h:128
The number of steps a fixed-lag history keeps.
Definition Lag.h:17