Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class state
  10. {
  11. public:
  12. string id;
  13. double *q2;
  14. };
  15.  
  16. class CTMC
  17. {
  18. public:
  19. state * Q;
  20. int states;
  21. CTMC(int nos) {
  22. Q = new state[nos];
  23. states = nos;
  24. for (int i = 0; i < nos; i++)
  25. {
  26.  
  27. Q[i].id = i;
  28. Q[i].q2 = new double[nos];
  29. double sum = -1;
  30. for (int j = 0; j < nos; j++)
  31. {
  32. cout << "enter value for Q " << i + 1 << j + 1 << " : " ;
  33. cin >> Q[i].q2[j];
  34. if (i == j)
  35. Q[i].q2[j] = 0;
  36. sum += Q[i].q2[j];
  37.  
  38.  
  39. }
  40. cout << endl;
  41. cout << "-------------------------" << endl;
  42. if (sum == -1)
  43. {
  44. cout << "invalid inputs, try again!"<<endl;
  45. i -= 1;
  46. }
  47. }
  48. }
  49.  
  50.  
  51. double V(int n) {
  52. double v = 0;
  53. for (int i = 0; i < states; i++)
  54. {
  55. v += Q[n-1].q2[i];
  56. }
  57. return v;
  58. }
  59.  
  60. double Exp(int n) {
  61.  
  62. double ex;
  63. ex = 1 / V(n);
  64. return ex;
  65.  
  66. }
  67.  
  68. double P(int i, int j)
  69. {
  70. double p;
  71. p = Q[i-1].q2[j-1] / V(i);
  72. return p;
  73. }
  74.  
  75. double less(int i, int min)
  76. {
  77. double less;
  78. less = 1 - (exp(-V(i)*min));
  79. return less;
  80.  
  81. }
  82.  
  83. double more(int i, int min)
  84. {
  85. double more;
  86. more = 1 - less(i, min);
  87. return more;
  88.  
  89. }
  90.  
  91. double lessToj (int i, int min , int j )
  92. {
  93. double less;
  94. less = (1 - (exp(-V(i)*min)) ) * P(i,j);
  95. return less;
  96. }
  97.  
  98. double more(int i, int min, int j)
  99. {
  100. double more;
  101. more = (1 - (1 - (exp(-V(i)*min)))) * P(i,j);
  102. return more;
  103.  
  104. }
  105.  
  106. double T(int i, int vis)
  107. {
  108. return (Exp(i)* vis);
  109. }
  110.  
  111. };
  112.  
  113. int main()
  114. {
  115. int nos; //Number of states
  116. int i, j , min , visits; //inset indices for matrix and minutes
  117. cout << "Enter the number of states : ";
  118. cin >> nos;
  119. cout << endl;
  120. CTMC k(nos);
  121.  
  122. cout << "enter i and j : ";
  123. cin >> i >> j;
  124.  
  125. cout << "V" << i << " : " << k.V(i) <<endl;
  126. cout << "Exp[" << i << "] : " << k.Exp(i)<<endl;
  127. cout << "P " << i<<j << " : " << k.P(i, j)<<endl;
  128. cout << "Enter # of mins : ";
  129. cin >> min;
  130. cout << "P(T<="<<min << ") : " << k.less(i, min) << endl;
  131. cout << "P(T>=" << min << ") : " << k.more(i, min) << endl;
  132. cout << "Enter # of visits : ";
  133. cin >> visits;
  134. cout << "T" << 2 << " : " << k.T(i, visits) << endl;
  135. cout << endl;
  136.  
  137. system("pause");
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement