1#ifndef VIENNASHE_MODELS_MARKOVCHAIN_CHAIN_HPP
2#define VIENNASHE_MODELS_MARKOVCHAIN_CHAIN_HPP
53 std::string
name()
const {
return name_; }
93 typedef std::map<index_type, rate_ptr_type > rate_map_type;
95 typedef std::map<index_type, rate_map_type > transition_map_type;
98 typedef std::map< index_type, state_ptr_type > state_map_type;
105 if(states_.count(st.
id()) <= 0)
107 chainmap_[st.
id()] = rate_map_type();
116 if(states_.count(from.
id()) <= 0) states_[from.
id()] = from.
clone();
117 if(states_.count(to.
id()) <= 0) states_[to.
id()] = to.
clone();
123 return this->
rate(from.
id(), to.
id());
129 transition_map_type::const_iterator chainmap_it = chainmap_.find(from);
130 if (chainmap_it == chainmap_.end())
131 throw std::runtime_error(
"Unknown from-state");
132 rate_map_type
const & frommap = chainmap_it->second;
133 rate_map_type::const_iterator frommap_it = frommap.find(to);
134 if (frommap_it == frommap.end())
135 throw std::runtime_error(
"Unknown to-state");
136 return *(frommap_it->second);
153 transition_map_type::const_iterator chainmap_it = chainmap_.find(from);
154 if (chainmap_it == chainmap_.end())
155 throw std::runtime_error(
"Unknown from-state");
156 rate_map_type
const & frommap = chainmap_it->second;
157 rate_map_type::const_iterator frommap_it = frommap.find(to);
158 if (frommap_it == frommap.end())
160 return frommap_it->second->value();
166 std::vector<index_type> ids(chainmap_.size());
169 for (transition_map_type::const_iterator cit = chainmap_.begin();
170 cit != chainmap_.end(); ++cit)
173 ids[i++] = cit->first;
177 for (transition_map_type::const_iterator cit = chainmap_.begin();
178 cit != chainmap_.end(); ++cit)
184 for (rate_map_type::const_iterator rit = cit->second.begin();
185 rit != cit->second.end(); ++rit)
207 template <
typename MatrixT >
210 if (rate_matrix.size1() != this->size1())
212 if (rate_matrix.size2() != this->size2())
215 for (transition_map_type::const_iterator cit = chainmap_.begin();
216 cit != chainmap_.end(); ++cit)
218 for (rate_map_type::const_iterator rit = cit->second.begin();
219 rit != cit->second.end(); ++rit)
221 rate_matrix(cit->first, rit->first) = rit->second->value();
230 for (state_map_type::const_iterator sit = states_.begin();
231 sit != states_.end(); ++sit)
237 for (state_map_type::const_iterator sit = states_.begin();
238 sit != states_.end(); ++sit)
251 if (states_.count(idx) <= 0)
252 throw std::runtime_error(
"State not registered!");
253 return *(states_[idx]);
257 state_map_type::const_iterator states_it = states_.find(idx);
258 if (states_it == states_.end())
259 throw std::runtime_error(
"Unknown state");
260 return *(states_it->second);
266 for (transition_map_type::const_iterator cit = chainmap_.begin(); cit != chainmap_.end(); ++cit)
267 for (rate_map_type::const_iterator rit = cit->second.begin(); rit != cit->second.end(); ++rit)
271 for (state_map_type::const_iterator sit = states_.begin(); sit != states_.end(); ++sit)
279 if (chainmap_.count(from) > 0)
281 rate_map_type & frommap = chainmap_[from];
282 frommap[to] = r.
clone();
286 rate_map_type frommap;
287 chainmap_[from] = frommap;
288 delete chainmap_[from][to];
289 chainmap_[from][to] = r.
clone();
294 transition_map_type chainmap_;
295 state_map_type states_;
306 typedef std::vector<double> VectorType;
308 typedef MatrixType::size_type size_type;
310 size_type num_rows = c.
size1();
312 MatrixType A(num_rows, num_rows);
313 VectorType b(num_rows);
314 VectorType x(num_rows);
317 for (size_type i = 0; i < num_rows; ++i)
320 for (size_type j = 0; j < num_rows; ++j)
325 for (size_type i = 0; i < num_rows; ++i)
330 for (size_type j = 0; j < num_rows; ++j)
332 const double kij = c.
get_rate(i, j);
339 for (size_type j = 0; j < num_rows; ++j)
340 A(num_rows-1, j) = 1.0;
358 for (size_type i = 0; i < num_rows; ++i)
363 if (total - 1e-4 > 1.0 || total + 1e-4 < 0.0)
366 for (size_type i = 0; i < num_rows; ++i)
386 typedef std::vector<double> VectorType;
388 typedef MatrixType::size_type size_type;
390 const size_type num_rows = c.
size1();
391 const double rtime = 1.0/dt;
393 MatrixType A(num_rows, num_rows);
394 VectorType b(num_rows);
395 VectorType x(num_rows);
398 for (size_type i = 0; i < num_rows; ++i)
404 for (size_type j = 0; j < num_rows; ++j)
409 for (size_type i = 0; i < num_rows; ++i)
416 for (size_type j = 0; j < num_rows; ++j)
418 const double kij = c.
get_rate(i, j);
425 for (size_type j = 0; j < num_rows; ++j)
426 A(num_rows-1, j) = 1.0;
444 for (size_type i = 0; i < num_rows; ++i)
449 if (total - 1e-4 > 1.0 || total + 1e-4 < 0.0)
452 for (size_type i = 0; i < num_rows; ++i)
Exception for the case that an invalid value (depends on the method called) is encountered.
Implementation of Markov-Chains.
index_type size2() const
Returns the column size of the rate-matrix.
void add_rate(state_base const &from, state_base const &to, rate_base const &r)
Adds a rate between to states. Additionally adds the given states if necessary.
void print_occupancies() const
Prints the occupancies of each state in the chain onto screen (log::info). Usefull for debugging.
void print_rate_matrix() const
Prints the rate matrix onto screen (log::info). Usefull for debugging.
state_base & get_state(index_type idx)
void add_rate(index_type from, index_type to, rate_base const &r)
double get_rate(index_type from, index_type to) const
Returns the acutal rate (double value!) between to states identified by their resp....
rate_base const & rate(state_base const &from, state_base const &to) const
Returns a constant reference to a rate between the given states.
state_base const & get_state(index_type idx) const
state_base::index_type index_type
double get_rate(state_base const &from, state_base const &to) const
Returns the actual rate (double value!) between to states.
void get_rate_matrix(MatrixT &rate_matrix) const
Fills the given rate_matrix.
bool has_state(index_type idx) const
Returns true if a state identified by its id is in the chain.
index_type size1() const
Returns the row size of the rate-matrix.
void add_state(state_base const &st)
Adds a single state to the chain.
rate_base const & rate(index_type from, index_type to) const
Returns a constant reference to a rate between to states identified by their resp....
Thrown whenever an invalid parameter to a model is given.
Thrown whenever a markov chain model finds an invalid or non-existing state.
Exception for the case that the evaluation of a model fails.
The basic concept of a state in Markov-Chains.
std::string name() const
Returns the name of the state for convenience and debugability.
virtual state_base * clone() const
Basic interface to get a clone (uses new operator!) of the current object for storage....
state_base(index_type id, std::string const &name)
index_type id() const
Returns the unique id of the state.
void occupancy(double o)
Sets the states occupancy.
double occupancy() const
Returns the occupancy f or p of the state. It has to be a number in [0,1].
state_base(state_base const &s)
Provides the exceptions used in the main viennashe namespace.
Implementation of various utilities related to linear algebra.
A logging facility providing fine-grained control over logging in ViennaSHE.
Contains exceptions for markov-models in viennashe::models.
logger< true > info()
Used to log infos. The logging level is logINFO.
void set_chain_to_equilibrium_expectation(chain &c)
Solves the time averaged set of equilibrium (dt->infty) equations for the given Markov-Chain and sets...
void solve_for_expectation_occupancies(chain &c, double dt)
Solves the time averaged set of equations (including time derivatives) for the occupancies f_i in a g...
std::vector< double > solve(viennashe::math::sparse_matrix< double > &A, std::vector< double > const &b, linear_solver_config const &config)
Public interface for solving a system of linear equations represented using a sparse matrix.
The main ViennaSHE namespace. All functionality resides inside this namespace.
Contains the basic reaction rate interface.
Forward declarations for a generic solver layer, providing bindings to a native Gauss solver,...
The basic rate interface.
virtual rate_base * clone() const =0