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 #include <limits>
28 
29 namespace ns3
30 {
31 
32 NS_OBJECT_ENSURE_REGISTERED(SatMarkovModel);
33 NS_LOG_COMPONENT_DEFINE("SatMarkovModel");
34 
35 TypeId
37 {
38  static TypeId tid =
39  TypeId("ns3::SatMarkovModel").SetParent<Object>().AddConstructor<SatMarkovModel>();
40  return tid;
41 }
42 
44  : m_probabilities(new double[3 * 3]),
45  m_numOfStates(3),
46  m_currentState(0)
47 {
48  NS_LOG_FUNCTION(this);
49 
50  NS_FATAL_ERROR("SatMarkovModel::SatMarkovModel - Constructor not in use");
51 }
52 
53 SatMarkovModel::SatMarkovModel(uint32_t numOfStates, uint32_t initialState)
54  : m_probabilities(new double[numOfStates * numOfStates]),
55  m_numOfStates(numOfStates),
56  m_currentState(initialState)
57 {
58  NS_LOG_FUNCTION(this << numOfStates);
59 
60  NS_LOG_INFO("Creating Markov model for " << numOfStates
61  << " states, initial state: " << m_currentState);
62 
63  for (uint32_t i = 0; i < m_numOfStates; ++i)
64  {
65  for (uint32_t j = 0; j < m_numOfStates; ++j)
66  {
67  m_probabilities[i * m_numOfStates + j] = 1;
68  }
69  }
70 }
71 
73 {
74  NS_LOG_FUNCTION(this);
75 
76  Reset();
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION(this);
83 
84  Reset();
85  Object::DoDispose();
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION(this);
92 
93  if (m_probabilities != nullptr)
94  {
95  delete[] m_probabilities;
96  m_probabilities = nullptr;
97  }
98 }
99 
100 uint32_t
102 {
103  NS_LOG_FUNCTION(this);
104 
105  return m_currentState;
106 }
107 
108 void
109 SatMarkovModel::SetState(uint32_t newState)
110 {
111  NS_LOG_FUNCTION(this << newState);
112 
113  if (newState >= m_numOfStates)
114  {
115  NS_FATAL_ERROR("SatMarkovModel::SetState - Invalid state");
116  }
117 
118  m_currentState = newState;
119 }
120 
121 uint32_t
123 {
124  NS_LOG_FUNCTION(this);
125 
126  NS_LOG_INFO("Doing transition, current state: " << m_currentState);
127 
128  double total = 0;
129  for (uint32_t i = 0; i < m_numOfStates; ++i)
130  {
132  }
133 
134  if ((fabs(total - 1.0) > std::numeric_limits<double>::epsilon()))
135  {
136  NS_FATAL_ERROR("SatMarkovModel::DoTransition - Probability sum does not match");
137  }
138 
139  double r = total * (std::rand() / double(RAND_MAX));
140 
141  NS_LOG_INFO("Random value: " << r);
142 
143  double acc = 0.0;
144  for (uint32_t i = 0; i < m_numOfStates; ++i)
145  {
147 
148  NS_LOG_INFO("State " << i << " accumulated value: " << acc);
149 
150  if (r <= acc)
151  {
152  NS_LOG_INFO("Transition done, new state: " << i);
153  m_currentState = i;
154  return i;
155  }
156  }
157  NS_LOG_INFO("Transition done, new state: " << m_numOfStates - 1);
159  return m_numOfStates - 1;
160 }
161 
162 void
163 SatMarkovModel::SetProbability(uint32_t from, uint32_t to, double probability)
164 {
165  NS_LOG_FUNCTION(this << from << " " << to << " " << probability);
166 
167  NS_LOG_INFO("Setting probability, from: " << from << " to: " << to
168  << " probability: " << probability);
169  m_probabilities[from * m_numOfStates + to] = probability;
170 }
171 
172 } // 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.