satellite-markov-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Frans Laakso <frans.laakso@magister.fi>
19  */
20 
21 #include "satellite-markov-model.h"
22 
23 #include <ns3/log.h>
24 #include <ns3/simulator.h>
25 
26 #include <cstdlib>
27 
28 namespace ns3
29 {
30 
31 NS_OBJECT_ENSURE_REGISTERED(SatMarkovModel);
32 NS_LOG_COMPONENT_DEFINE("SatMarkovModel");
33 
34 TypeId
36 {
37  static TypeId tid =
38  TypeId("ns3::SatMarkovModel").SetParent<Object>().AddConstructor<SatMarkovModel>();
39  return tid;
40 }
41 
43  : m_probabilities(new double[3 * 3]),
44  m_numOfStates(3),
45  m_currentState(0)
46 {
47  NS_LOG_FUNCTION(this);
48 
49  NS_FATAL_ERROR("SatMarkovModel::SatMarkovModel - Constructor not in use");
50 }
51 
52 SatMarkovModel::SatMarkovModel(uint32_t numOfStates, uint32_t initialState)
53  : m_probabilities(new double[numOfStates * numOfStates]),
54  m_numOfStates(numOfStates),
55  m_currentState(initialState)
56 {
57  NS_LOG_FUNCTION(this << numOfStates);
58 
59  NS_LOG_INFO("Creating Markov model for " << numOfStates
60  << " states, initial state: " << m_currentState);
61 
62  for (uint32_t i = 0; i < m_numOfStates; ++i)
63  {
64  for (uint32_t j = 0; j < m_numOfStates; ++j)
65  {
66  m_probabilities[i * m_numOfStates + j] = 1;
67  }
68  }
69 }
70 
72 {
73  NS_LOG_FUNCTION(this);
74 
75  Reset();
76 }
77 
78 void
80 {
81  NS_LOG_FUNCTION(this);
82 
83  Reset();
84  Object::DoDispose();
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION(this);
91 
92  if (m_probabilities != NULL)
93  {
94  delete[] m_probabilities;
95  m_probabilities = NULL;
96  }
97 }
98 
99 uint32_t
101 {
102  NS_LOG_FUNCTION(this);
103 
104  return m_currentState;
105 }
106 
107 void
108 SatMarkovModel::SetState(uint32_t newState)
109 {
110  NS_LOG_FUNCTION(this << newState);
111 
112  if (newState >= m_numOfStates)
113  {
114  NS_FATAL_ERROR("SatMarkovModel::SetState - Invalid state");
115  }
116 
117  m_currentState = newState;
118 }
119 
120 uint32_t
122 {
123  NS_LOG_FUNCTION(this);
124 
125  NS_LOG_INFO("Doing transition, current state: " << m_currentState);
126 
127  double total = 0;
128  for (uint32_t i = 0; i < m_numOfStates; ++i)
129  {
131  }
132 
133  if ((fabs(total - 1.0) > std::numeric_limits<double>::epsilon()))
134  {
135  NS_FATAL_ERROR("SatMarkovModel::DoTransition - Probability sum does not match");
136  }
137 
138  double r = total * (std::rand() / double(RAND_MAX));
139 
140  NS_LOG_INFO("Random value: " << r);
141 
142  double acc = 0.0;
143  for (uint32_t i = 0; i < m_numOfStates; ++i)
144  {
146 
147  NS_LOG_INFO("State " << i << " accumulated value: " << acc);
148 
149  if (r <= acc)
150  {
151  NS_LOG_INFO("Transition done, new state: " << i);
152  m_currentState = i;
153  return i;
154  }
155  }
156  NS_LOG_INFO("Transition done, new state: " << m_numOfStates - 1);
158  return m_numOfStates - 1;
159 }
160 
161 void
162 SatMarkovModel::SetProbability(uint32_t from, uint32_t to, double probability)
163 {
164  NS_LOG_FUNCTION(this << from << " " << to << " " << probability);
165 
166  NS_LOG_INFO("Setting probability, from: " << from << " to: " << to
167  << " probability: " << probability);
168  m_probabilities[from * m_numOfStates + to] = probability;
169 }
170 
171 } // namespace ns3
uint32_t m_numOfStates
Number of states.
static TypeId GetTypeId(void)
NS-3 function for type id.
void Reset()
Clear used variables.
void SetProbability(uint32_t from, uint32_t to, double probability)
Function for setting the probability values.
uint32_t GetState() const
Function for returning the current state.
void DoDispose()
Do needed dispose actions.
double * m_probabilities
Markov state change probabilities.
uint32_t m_currentState
Current state.
uint32_t DoTransition()
Function for evaluating the state change.
void SetState(uint32_t newState)
Function for setting the state.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.