![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 5, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 #ifndef WNS_MARKOVCHAIN_MARKOVBASE_HPP 00028 #define WNS_MARKOVCHAIN_MARKOVBASE_HPP 00029 00030 #include <WNS/logger/Logger.hpp> 00031 00032 #include <boost/numeric/ublas/matrix.hpp> 00033 #include <boost/numeric/ublas/io.hpp> 00034 00035 namespace wns { namespace markovchain { 00036 00040 typedef boost::numeric::ublas::matrix<double> TransitionMatrixType; 00041 00051 template<class T> 00052 class MarkovBase 00053 { 00054 public: 00055 00056 friend std::ostream& 00057 operator <<(std::ostream& os, const MarkovBase& markov) 00058 { 00059 os << "TransitionMatrix="; 00060 os << markov.transitionsMatrix; 00061 return os; 00062 } 00063 00069 MarkovBase(int _numberOfChains) : 00070 numberOfStates(0), 00071 numberOfChains(_numberOfChains), 00072 startStates(std::vector<int>(numberOfChains,0)), 00073 actualStates(std::vector<int>(numberOfChains)), 00074 nextStates(std::vector<int>(numberOfChains)), 00075 nextTransitionTime(std::vector<double>(numberOfChains,0.0)), 00076 logger(std::string("WNS"), std::string("MarkovModel")) 00077 { 00078 for (int chainNumber = 0; chainNumber<numberOfChains; chainNumber++) { 00079 startStates[chainNumber]=0; // initial value 00080 } 00081 actualStates = startStates; 00082 MESSAGE_SINGLE(VERBOSE, logger, "MarkovBase(C=" <<_numberOfChains<< ") called"); 00083 } 00084 00099 MarkovBase(int nos, 00100 int noc, 00101 std::vector<T> states, 00102 TransitionMatrixType matrix, 00103 std::vector<int> sStates) : 00104 numberOfStates(nos), 00105 numberOfChains(noc), 00106 vectorOfStates(states), 00107 transitionsMatrix(matrix), 00108 startStates(sStates), 00109 actualStates(std::vector<int>(numberOfChains)), 00110 nextStates(std::vector<int>(numberOfChains)), 00111 nextTransitionTime(std::vector<double>(numberOfChains,0.0)), 00112 logger(std::string("Mrkv"), std::string("MarkovModel")) 00113 { 00114 actualStates = startStates; 00115 MESSAGE_SINGLE(VERBOSE,logger, "MarkovBase(C=" << noc << ",...) called"); 00116 } 00117 00122 virtual 00123 ~MarkovBase() 00124 { 00125 MESSAGE_SINGLE(VERBOSE, logger, "~MarkovBase() called"); 00126 } 00127 00132 void 00133 setNumberOfStates(int _numberOfStates) 00134 { 00135 numberOfStates = _numberOfStates; 00136 prepareTransitionMatrixAndVectorOfStates(); 00137 } 00138 00142 const std::vector<T>& 00143 getStates() const 00144 { 00145 assure(numberOfStates > 0, "numberOfStates uninitialized"); 00146 return vectorOfStates; 00147 } 00148 00152 int 00153 getActualStateIndex(int actualchain) const 00154 { 00155 return actualStates[actualchain]; 00156 } 00157 00161 double 00162 getStateProbability(int stateIndex) const 00163 { 00164 assure(!stateProbabilities.empty(),"stateProbabilities is empty"); 00165 assure(stateProbabilities[0]>=0.0, "calculateStateProbabilities() has not been called before"); 00166 return stateProbabilities[stateIndex]; 00167 } 00168 00169 const std::vector<double>& getStateProbabilities() const 00170 { 00171 return stateProbabilities; 00172 } 00173 00174 00178 const T* 00179 getStateContent(int index) const 00180 { 00181 assure(!vectorOfStates.empty(),"vectorOfStates is empty"); 00182 return &(vectorOfStates[index]); 00183 } 00187 void 00188 setStartStates(std::vector<int> states) 00189 { 00190 startStates = states; 00191 } 00192 00193 00197 void 00198 setTransitionMatrixElement(int row, int col, double value) 00199 { 00200 assure((row >= 0) && (row < numberOfStates), "wrong dimension"); 00201 assure((col >= 0) && (col < numberOfStates), "wrong dimension"); 00202 transitionsMatrix(row, col) = value; 00203 } 00204 00208 double 00209 getTransitionMatrixElement(int row, int col) const 00210 { 00211 assure((row >= 0) && (row < numberOfStates), "wrong dimension"); 00212 assure((col >= 0) && (col < numberOfStates), "wrong dimension"); 00213 return transitionsMatrix(row, col); 00214 } 00215 00216 // wish: double& operator[](int row, int col) 00217 00221 void 00222 setTransitionMatrix(const TransitionMatrixType& matrix) 00223 { 00224 transitionsMatrix = matrix; // copy 00225 } 00226 00230 int 00231 readNumberOfStates (std::istream& in) 00232 { 00233 std::string line; 00234 int nos = -1; 00235 while (in.good() && nos < 0) { 00236 std::getline(in, line); 00237 int comment_pos = line.find_first_not_of(" \t"); 00238 if (line[comment_pos] != '#') { 00239 std::string ident; 00240 std::size_t pos_eq = line.find("=", 0); 00241 if (pos_eq != std::string::npos) { 00242 line.replace(pos_eq, 1, " "); 00243 std::istringstream instream(line); 00244 instream >> ident; 00245 instream >> nos; 00246 numberOfStates = nos; 00247 } 00248 } 00249 } 00250 prepareTransitionMatrixAndVectorOfStates(); 00251 return nos; 00252 } 00253 00258 void 00259 readTransitionsFromFile(std::istream& in) 00260 { 00261 std::string line; 00262 assure((numberOfStates > 0), "numberOfStates in not defined"); 00263 int row = 0; 00264 while (in.good() && row < numberOfStates) { 00265 std::getline(in, line); 00266 int comment_pos = line.find_first_not_of(" \t"); 00267 if (line[comment_pos] != '#') { 00268 std::istringstream instream(line); 00269 double value; 00270 for (int col = 0; col < numberOfStates; col++) { 00271 instream >> value; 00272 transitionsMatrix(row, col) = value; 00273 } 00274 row++; 00275 } 00276 } 00277 MESSAGE_BEGIN(VERBOSE, MarkovBase<T>::logger, m, "transitionMatrix=\n"); 00278 m << transitionsMatrix; 00279 MESSAGE_END(); 00280 } 00281 00282 protected: 00288 virtual void 00289 calculateStateProbabilities() // = 0 problematic because of MarkovBaseTest 00290 { 00291 assure(0, "cannot calculateStateProbabilities in MarkovBase"); 00292 } 00293 00297 int numberOfStates; 00298 00302 int numberOfChains; 00303 00307 std::vector<T> vectorOfStates; 00308 00312 TransitionMatrixType transitionsMatrix; 00313 00318 std::vector<int> startStates; 00319 00324 std::vector<int> actualStates; 00325 00330 std::vector<int> nextStates; 00331 00336 std::vector<double> nextTransitionTime; 00337 00342 std::vector<double> stateProbabilities; 00343 00347 wns::logger::Logger logger; 00348 00349 private: 00350 friend class MarkovBaseTest; 00351 00357 void 00358 prepareTransitionMatrixAndVectorOfStates() 00359 { 00360 assure(numberOfStates > 0, "numberOfStates uninitialized"); 00361 transitionsMatrix = TransitionMatrixType(numberOfStates, numberOfStates); 00362 vectorOfStates = std::vector<T>(numberOfStates); 00363 stateProbabilities = std::vector<double>(numberOfStates,-1.0); 00364 } 00365 00366 };//MarkovBase 00367 00368 } // markovchain 00369 } // wns 00370 00371 #endif //WNS_MARKOVCHAIN_MARKOVBASE_HPP 00372 00373 00374
1.5.5