bpp-phyl3 3.0.0
DecompositionReward.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#include <typeinfo>
6#include <vector>
7
10
11using namespace bpp;
12using namespace std;
13
14/******************************************************************************/
15
16DecompositionReward::DecompositionReward(
17 std::shared_ptr<const SubstitutionModelInterface> model,
18 std::shared_ptr<const AlphabetIndex1> alphIndex) :
19 AbstractReward(alphIndex),
21 rewards_(nbStates_, nbStates_),
22 currentLength_(-1.)
23{
24 // Check compatibility between model and alphabet Index:
25 if (typeid(model->getAlphabet()) != typeid(alphIndex_->getAlphabet()))
26 throw Exception("DecompositionReward (constructor): alphabets do not match between alphabet index and model.");
27
28 // Initialize the B matrice. This is done once for all,
29 // unless the number of states changes:
30
33
36}
37
39 const StateMapInterface& stateMap,
40 std::shared_ptr<const AlphabetIndex1> alphIndex) :
41 AbstractReward(alphIndex),
42 DecompositionMethods(stateMap),
43 rewards_(nbStates_, nbStates_),
44 currentLength_(-1.)
45{}
46
47/******************************************************************************/
48
50{
52}
53
54/******************************************************************************/
55
57{
58 vector<int> supportedStates = model_->getAlphabetStates();
59 for (size_t j = 0; j < nbStates_; ++j)
60 {
61 bMatrices_[0](j, j) = getAlphabetIndex()->getIndex(supportedStates[j]);
62 }
63}
64
65/******************************************************************************/
66
67void DecompositionReward::computeRewards_(double length) const
68{
70
71 // Now we must divide by pijt:
72 RowMatrix<double> P = model_->getPij_t(length);
73 for (size_t j = 0; j < nbStates_; j++)
74 {
75 for (size_t k = 0; k < nbStates_; k++)
76 {
77 rewards_(j, k) /= P(j, k);
78 if (std::isnan(rewards_(j, k)) || std::isnan(-rewards_(j, k)) || std::isinf(rewards_(j, k)))
79 rewards_(j, k) = 0.;
80 }
81 }
82}
83
84/******************************************************************************/
85
87{
88 if (!model_)
89 throw Exception("DecompositionReward::getAllRewards: model not defined.");
90
91 if (length < 0)
92 throw Exception("DecompositionReward::getAllRewards. Negative branch length: " + TextTools::toString(length) + ".");
93 if (length != currentLength_)
94 {
95 computeRewards_(length);
96 currentLength_ = length;
97 }
98 return new RowMatrix<double>(rewards_);
99}
100
101/******************************************************************************/
102
103void DecompositionReward::storeAllRewards(double length, Eigen::MatrixXd& mat) const
104{
105 if (!model_)
106 throw Exception("DecompositionReward::storeAllRewards: model not defined.");
107
108 if (length < 0)
109 throw Exception("DecompositionReward::storeAllRewards. Negative branch length: " + TextTools::toString(length) + ".");
110
111 if (length != currentLength_)
112 {
113 computeRewards_(length);
114 currentLength_ = length;
115 }
116
117 mat.resize(Eigen::Index(nbStates_), Eigen::Index(nbStates_));
118
119 for (size_t j = 0; j < nbStates_; j++)
120 {
121 for (size_t k = 0; k < nbStates_; k++)
122 {
123 mat(Eigen::Index(j), Eigen::Index(k)) = rewards_(j, k);
124 }
125 }
126}
127
128/******************************************************************************/
129
130double DecompositionReward::getReward(size_t initialState, size_t finalState, double length) const
131{
132 if (length < 0)
133 throw Exception("DecompositionReward::getRewards. Negative branch length: " + TextTools::toString(length) + ".");
134 if (length != currentLength_)
135 {
136 computeRewards_(length);
137 currentLength_ = length;
138 }
139 return rewards_(initialState, finalState);
140}
141
142/******************************************************************************/
143
145 std::shared_ptr<const SubstitutionModelInterface> model)
146{
148
149 if (!model)
150 return;
151
152 // Check compatibility between model and substitution register:
153 if (typeid(model->getAlphabet()) != typeid(alphIndex_->getAlphabet()))
154 throw Exception("DecompositionReward::setSubstitutionModel: alphabets do not match between alphabet index and model.");
155
156
157 initRewards_();
158
161
162 // Recompute rewards:
163
165}
166
167/******************************************************************************/
168
170{
171 if (!model_)
172 return;
173
174 // Check compatibility between model and substitution register:
175 if (typeid(model_->getAlphabet()) != typeid(alphIndex_->getAlphabet()))
176 throw Exception("DecompositionReward::AlphabetIndexHasChanged: alphabets do not match between alphbaet index and model.");
177
180
181 // Recompute rewards:
182 if (currentLength_ > 0)
184}
Basic implementation of the the Reward interface.
Definition: Reward.h:124
std::shared_ptr< const AlphabetIndex1 > alphIndex_
Definition: Reward.h:126
std::shared_ptr< const AlphabetIndex1 > getAlphabetIndex() const
Definition: Reward.h:159
Methods useful for analytical substitution count and rewards using the eigen decomposition method.
void computeExpectations(RowMatrix< double > &mapping, double length) const
void setSubstitutionModel(std::shared_ptr< const SubstitutionModelInterface > model)
Set the substitution model.
std::shared_ptr< const SubstitutionModelInterface > model_
std::vector< RowMatrix< double > > bMatrices_
computation matrices
void storeAllRewards(double length, Eigen::MatrixXd &mat) const override
Store the rewards on a branch, for each initial and final states, and given the branch length.
Matrix< double > * getAllRewards(double length) const override
Get the rewards on a branch, for each initial and final states, and given the branch length.
void computeRewards_(double length) const
DecompositionReward(std::shared_ptr< const SubstitutionModelInterface > model, std::shared_ptr< const AlphabetIndex1 > alphIndex)
void alphabetIndexHasChanged() override
void setSubstitutionModel(std::shared_ptr< const SubstitutionModelInterface > model) override
Set the substitution model.
RowMatrix< double > rewards_
double getReward(size_t initialState, size_t finalState, double length) const override
Get the reward of susbstitutions on a branch, given the initial and final states, and the branch leng...
void resize(size_t nRows, size_t nCols)
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
std::string toString(T t)
bool isinf(const double &d)
Defines the basic types of data flow nodes.