9#ifndef __BASE_HISTORY_H_INCLUDED__
10#define __BASE_HISTORY_H_INCLUDED__
12#include "../utils/random/DiscreteDistribution.hpp"
39template <
typename Space,
typename DemographicPolicy,
typename MemoryPolicy>
class HistoryBase
43 using coord_type = Space;
45 using time_type =
unsigned int;
47 using memory_policy = MemoryPolicy;
49 using demographic_policy = DemographicPolicy;
51 using value_type =
typename demographic_policy::value_type;
53 using flow_type =
typename memory_policy::template flow_type<coord_type, time_type, value_type>;
55 using pop_sizes_type =
typename memory_policy::template pop_sizes_type<coord_type, time_type, value_type>;
66 : m_nb_generations(
nb_generations), m_sizes(std::make_unique<pop_sizes_type>()),
67 m_flows(std::make_unique<flow_type>())
69 m_sizes->set(x, 0, N);
76 return m_sizes->definition_space(t);
83 auto &
pop_size(coord_type
const &x, time_type
const &t)
85 return m_sizes->operator()(x, t);
92 auto N_ref = std::cref(this->m_sizes);
93 return [N_ref](coord_type
const &x, time_type t) {
return N_ref.get()->get(x, t); };
100 return m_sizes->get(x, t);
105 void set_pop_size(coord_type
const &x, time_type
const &t, value_type N)
107 m_sizes->set(x, t, N);
114 return m_nb_generations;
124 template <
typename Generator> coord_type
backward_kernel(coord_type
const &x, time_type t, Generator &gen)
const
127 assert(m_flows->flow_to_is_defined(x, t));
128 auto k = make_backward_distribution(x, t);
134 std::unique_ptr<flow_type> m_flows;
137 unsigned int m_nb_generations;
138 std::unique_ptr<pop_sizes_type> m_sizes;
142 auto make_backward_distribution(coord_type
const &x, time_type
const &t)
const
144 std::vector<double> weights;
145 std::vector<coord_type> support;
146 weights.reserve(m_flows->flow_to(x, t).size());
147 support.reserve(m_flows->flow_to(x, t).size());
148 for (
auto const &it : m_flows->flow_to(x, t))
150 support.push_back(it.first);
151 weights.push_back(
static_cast<double>(it.second));
153 return discrete_distribution_type(std::move(support), std::move(weights));
Base class for simulating and recording spatial population history, encpasulating backward migration ...
Definition HistoryBase.hpp:40
auto & pop_size(coord_type const &x, time_type const &t)
Population size at deme x at time t.
Definition HistoryBase.hpp:83
auto get_functor_N() const noexcept
Read-only functor.
Definition HistoryBase.hpp:90
void set_pop_size(coord_type const &x, time_type const &t, value_type N)
Used in derived class History to increment migrants.
Definition HistoryBase.hpp:105
HistoryBase(coord_type const &x, value_type N, unsigned int nb_generations)
Constructor initializing the demographic database.
Definition HistoryBase.hpp:65
coord_type backward_kernel(coord_type const &x, time_type t, Generator &gen) const
Samples a coordinate from the backward-in-time transition matrix.
Definition HistoryBase.hpp:124
auto distribution_area(time_type t) const
Retrieve demes where N > 0 at time t.
Definition HistoryBase.hpp:74
auto get_pop_size(coord_type const &x, time_type const &t)
Read-only.
Definition HistoryBase.hpp:98
unsigned int const & nb_generations() const
Last time recorded in the foward-in-time database history.
Definition HistoryBase.hpp:112
Sampling (non) arithmetic values in discrete probability distribution.
Definition DiscreteDistribution.hpp:29
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21