Marmote Core
The project aims at realizing the prototype of a software environment dedicated to modeling with Markov chains.
markovChain.h
1 /* Marmote is free software: you can redistribute it and/or modify
2 it under the terms of the GNU General Public License as published by
3 the Free Software Foundation, either version 3 of the License, or
4 (at your option) any later version.
5 
6 Marmote is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU General Public License for more details.
10 
11 You should have received a copy of the GNU General Public License
12 along with Marmote. If not, see <http://www.gnu.org/licenses/>.
13 
14 Copyright 2015 Alain Jean-Marie, Jean-Michel Fourneau, Jean-Marc Vincent, Issam Rabhi */
15 
16 #ifndef MARKOVCHAIN_H
17 #define MARKOVCHAIN_H
18 
19 #include "marmoteConstants.h"
20 #include "Distribution/Distribution.h"
21 #include "Distribution/discreteDistribution.h"
22 #include "simulationResult.h"
23 #include "transitionStructure/transitionStructure.h"
24 #include <string>
25 #include <iostream>
26 
27 #include <vector>
28 //#include <RInside.h>
29 
30 
31 #ifdef WITH_R
32 #include <RInside.h>
33 #endif
34 
35 
36 using namespace std;
37 
43 {
44 
45  protected:
46 
47  timeType _type;
50  // someType _reward; to be defined later
51  // int _initState;
53  bool _debug;
55  bool _isAbstract;
57  string* _abstract;
58  string _format;
59  string _modelName;
61 #ifdef WITH_R
62  static RInside* _Rmotor;
63 #else
64  typedef void* RInside;
65  typedef void* SEXP;
66 #endif
67 
68 public:
69  // constructors
70 
78  markovChain(int sz, timeType t);
79 
80 
89 
106  markovChain(string format, string param[], int nbreParam, string modelName,
107  bool isAbstract);
108 
114  virtual ~markovChain();
115 
116 public:
117  // accessors
118 
123  int stateSpaceSize() { return _stateSpaceSize; }
129  transitionStructure* generator() { return _generator; }
135  void setInitDistribution( discreteDistribution* d ) { if ( _initDistribution != NULL ){ delete(_initDistribution); }; _initDistribution = d; }
141  void setGenerator( transitionStructure* tr ) { _generator = tr; }
142 
150  RInside* Rmotor();
151 
152 
158  Distribution* read();
159 
165  void setFormat(string format){_format=format;}
166 
172 void setModelName(string modelName){_modelName=modelName;}
173 
179  void setAbstractNbre(int abstractNbre){_abstractNbre=abstractNbre;}
180 
187  void setAbstract(string abstract[]);
188 
196  int abstractNbre(){return _abstractNbre;}
197 
204  string modelName(){ if ( _modelName.size() == 0 ) fprintf( stderr, "Warning: empty model name.\n" ); return _modelName;}
210  string format(){return _format;}
211 
217  void abstract()
218  {
219 
220  for(int i=0; i<_abstractNbre;i++)
221  {
222  cout<<i<<":"<<_abstract[i];
223  }
224 
225  }
226 
227  public:
228  // solution methods
242  virtual simulationResult* simulateChain( double tMax,
243  bool Stats, bool Traj,
244  bool withIncrements, bool Print );
245 
256  virtual simulationResult* simulateChainDT( int tMax,
257  bool stats, bool traj,
258  bool trace );
270  virtual simulationResult* simulateChainCT( double tMax,
271  bool stats, bool traj,
272  bool withIncrements,
273  bool trace );
288  simulationResult* simulatePSI( int tMax,
289  bool stats, bool Traj,
290  bool Print );
304  Distribution* stationaryDistribution_iterative( string method,
305  int tmax,
306  double precision,
307  string initDistribType,
308  discreteDistribution* initDistrib,
309  bool progress );
317  Distribution* stationaryDistribution( bool progress );
325  virtual Distribution* stationaryDistributionCT( bool progress );
336  virtual Distribution* stationaryDistributionCT_embedding( int tMax, double epsilon,
337  discreteDistribution *iDis, bool progress );
348  virtual Distribution* stationaryDistribution_power( int tMax, double epsilon,
349  discreteDistribution *iDis, bool progress );
350 
351 
352 
359  void NCDProperty(double epsilon);
360 
366 void BandIMSUB(std::string modelName="modelName");
367 
373  void Vincent();
374 
380  void RowVincent();
381 
388  void Absorbing();
389 
396 void ProdFundSW(std::string modelName="modelName");
397 
403 void RowSum(std::string modelName="modelName");
404 
412  Distribution* stationaryDistributionGthLD();
413 
420  Distribution* stationaryDistributionSOR();
421 
428  Distribution* transientDistributionR( int fromState, double t );
429 
436  Distribution* transientDistributionDT( int fromState, int t );
437 
445  Distribution* stationaryDistributionR();
446 
447 
459  simulationResult* simulateChainR( double tMax,
460  bool stats, bool traj, bool trace );
461 
462 
471  double transitionProbability(int stateFrom, int stateTo);
472 
480  std::vector<int> absorbingStates();
481 
490  std::vector< std::vector<int> > recurrentClasses();
491 
500  std::vector< std::vector<int> > communicatingClasses();
501 
509  bool isirreducible();
510 
520  bool isaccessible(int stateFrom, int stateTo);
521 
531  simulationResult* stationaryDistributionSample( int nbSamples );
532 
541  Distribution* hittingTimeDistribution( int iState, bool *hitSetIndicator );
542 
554  int* simulateHittingTime(int iState, bool *hittingSet,
555  int nbSamples, int tMax );
562  double* averageHittingTime( bool *hitSetIndicator );
563 
571  double* averageHittingTimeDT( bool *hitSetIndicator );
572 
580  double* averageHittingTimeDT_iterative( bool *hitSetIndicator );
581 
582  public:
583 
589  virtual markovChain* copy();
590 
591 
599  virtual markovChain* uniformize();
600 
609  virtual markovChain* embed();
610 
619  void setSizeType(const string path);
620  // virtual void makeMarkovChain();
621 
630  int charVectorElt2State(SEXP elt, std::string function );
631 
632 
633 public:
634 
641  virtual void write( FILE *out, bool withReward );
642 
650  virtual void write( string format, string modelName);
651 
658  void write( string format ) { write( format, modelName() ); }
659 
665  std::string toString(std::string format);
666 
667 };
668 
669 #endif // MARKOVCHAIN_H
Markov Chain class.
Definition: markovChain.h:42
string _format
format/language of the model
Definition: markovChain.h:58
void traj(const string path, const string file_out, int state, const int length)
PSI Trajectory Simulation.
Definition: psi_traj.cpp:86
string * _abstract
table of abstraction parameters
Definition: markovChain.h:57
bool _isAbstract
true if the object is "abstract", i.e. a pointer to some files
Definition: markovChain.h:55
transitionStructure * generator()
Read accessor to get the value of _generator which is a transitionStructure.
Definition: markovChain.h:129
void setAbstractNbre(int abstractNbre)
Utility to set the value of _abstractNbre.
Definition: markovChain.h:179
int _stateSpaceSize
size of the state space (should be a pointer on the state space itself)
Definition: markovChain.h:48
string modelName()
Utility to get _modelName.
Definition: markovChain.h:204
int stateSpaceSize()
Read accessor to get the number of states in the state space of the Markov chain. ...
Definition: markovChain.h:123
bool _debug
internal debugging indicator
Definition: markovChain.h:53
string _modelName
name of the model
Definition: markovChain.h:59
void setInitDistribution(discreteDistribution *d)
Write accessor to set the value of _initDistribution which is a discreteDistribution.
Definition: markovChain.h:135
Utilization of the Standard Template Library.
A class for representing probability distributions.
Definition: Distribution.h:44
int _abstractNbre
number of abstraction parameters
Definition: markovChain.h:56
string format()
Utility to get _format.
Definition: markovChain.h:210
void setModelName(string modelName)
Utility to set the value of _modelName.
Definition: markovChain.h:172
void write(string format)
Method for writing Markov chains in files with various formats. This version uses the given model nam...
Definition: markovChain.h:658
void setFormat(string format)
Utility to set the value of _format.
Definition: markovChain.h:165
transitionStructure * _generator
transition structure of the chain
Definition: markovChain.h:49
void * RInside
recast nonexisting RInside type to void
Definition: markovChain.h:64
Abstract class for transition structures. These are structures which describe transitions to one stat...
Definition: transitionStructure.h:33
discreteDistribution * _initDistribution
initial distribution of the process
Definition: markovChain.h:52
void * SEXP
recast nonexisting SEXP type to void
Definition: markovChain.h:65
timeType _type
time type: discrete or continuous
Definition: markovChain.h:47
The general discrete distribution with finite support.
Definition: discreteDistribution.h:25
int abstractNbre()
Utility to get _abstractNbre.
Definition: markovChain.h:196
The class for transmitting (Monte Carlo) simulation results between objects. Simulation results may b...
Definition: simulationResult.h:33
void setGenerator(transitionStructure *tr)
Write accessor to set the value of _generator which is a transitionStructure.
Definition: markovChain.h:141