1/* Copyright (c) 2012 Massachusetts Institute of Technology
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22#include "model/TransitionInfo.h"
23
24namespace DSENT
25{
26    TransitionInfo::TransitionInfo()
27        : m_number_transitions_00_(0.25), m_number_transitions_01_(0.25),
28        m_number_transitions_11_(0.25), m_frequency_multiplier_(1.0)
29    {
30        update();
31    }
32
33    TransitionInfo::TransitionInfo(double number_transitions_00_, double number_transitions_01_,
34            double number_transitions_11_)
35        : m_number_transitions_00_(number_transitions_00_), m_number_transitions_01_(number_transitions_01_),
36        m_number_transitions_11_(number_transitions_11_)
37    {
38        m_frequency_multiplier_ = m_number_transitions_00_ + 2.0 * m_number_transitions_01_ + m_number_transitions_11_;
39        update();
40    }
41
42    TransitionInfo::~TransitionInfo()
43    {}
44
45    void TransitionInfo::update()
46    {
47        ASSERT(m_number_transitions_00_ >= 0.0, "[Error] Number of 0->0 transitions (" +
48                (String)m_number_transitions_00_ + ") must be >= 0.0!");
49        ASSERT(m_number_transitions_01_ >= 0.0, "[Error] Number of 0->1 transitions (" +
50                (String)m_number_transitions_01_ + ") must be >= 0.0!");
51        ASSERT(m_number_transitions_11_ >= 0.0, "[Error] Number of 1->1 transitions (" + (String)m_number_transitions_11_ + ") must be >= 0.0!");
52
53        m_probability_1_ = (m_number_transitions_01_ + m_number_transitions_11_) / m_frequency_multiplier_;
54        m_probability_0_ = 1.0 - m_probability_1_;
55        return;
56    }
57
58    TransitionInfo TransitionInfo::scaleFrequencyMultiplier(double frequency_multiplier_) const
59    {
60        // A short cut if frequency_multiplier_ == m_frequency_multiplier_ to avoid excess calculations
61        if(frequency_multiplier_ == m_frequency_multiplier_)
62        {
63            return TransitionInfo(m_number_transitions_00_, m_number_transitions_01_, m_number_transitions_11_);
64        }
65        else if (frequency_multiplier_ > m_frequency_multiplier_)
66        {
67            double freq_ratio = frequency_multiplier_ / m_frequency_multiplier_;
68            double diff_num_trans_01 = m_number_transitions_01_ * (freq_ratio - 1.0);
69            double norm_num_trans_00 = m_number_transitions_00_ * freq_ratio + diff_num_trans_01;
70            double norm_num_trans_01 = m_number_transitions_01_;
71            double norm_num_trans_11 = m_number_transitions_11_ * freq_ratio + diff_num_trans_01;
72
73            return TransitionInfo(norm_num_trans_00, norm_num_trans_01, norm_num_trans_11);
74        }
75        else
76        {
77            ASSERT(false, "[Error] Cannot scale to frequency multiplier (" + (String) frequency_multiplier_ +
78                ") since current frequency multiplier (" + (String) m_frequency_multiplier_ + ") is bigger!");
79        }
80    }
81
82    void TransitionInfo::print(std::ostream& ost_) const
83    {
84        ost_ << "[" << m_number_transitions_00_ << ", " << m_number_transitions_01_ << ", " << m_number_transitions_11_ << "]";
85        ost_ << " [" << m_number_transitions_00_ / m_frequency_multiplier_;
86        ost_ << ", " << m_number_transitions_01_*2.0 / m_frequency_multiplier_;
87        ost_ << ", " << m_number_transitions_11_ / m_frequency_multiplier_ << "]" << endl;
88        return;
89    }
90} // namespace DSENT
91
92