Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. rm(list=ls())
  2.  
  3. #set.seed(1000)
  4.  
  5. pareto = function(x_0,par) {
  6. return (x_0*((1-runif(1,0,1))^(-1/par)-1))
  7. }
  8.  
  9. x0 = 0.6
  10. p = 0.5
  11.  
  12. lambda1 = 2
  13. lambda2 = 3
  14. lambda3 = 1
  15. alpha1 = 0.5
  16. alpha2 = 0.1
  17. alpha3 = 0.3
  18.  
  19. N=1000
  20. mu=3
  21. lambda=9
  22. n_1=4
  23. c=2
  24.  
  25. m=1 # число прогонов
  26. P0=array(0,m)
  27.  
  28. #X - queue size (2 comp); T - nearest arrival (type 1,1,2) (2 servers in pool 1 and 1 server in pool 2), service completion on server 1,2 (4 comp)
  29. G =function(L){
  30. X=L$X
  31. T=L$T
  32. event=which.min(T)
  33. t=L$t+T[event]
  34. T=T-T[event]
  35. if(event>1){ # service completion
  36. X=X-1
  37. if (X<=(n_1-1))
  38. T[event]=Inf
  39. if (X>(n_1-1)) {
  40. if (runif(1)<p)
  41. T[event] = rexp(n=1,rate = lambda3)
  42. else
  43. T[event] = rexp(n=1, rate = lambda2)
  44. }
  45. }
  46. if(event==1 && X<(n_1+c)){ # arrival
  47. T[event]=rexp(n=1,rate=lambda) # time to next arrival
  48. if (X<=(n_1-1)) {
  49. i=which(T==Inf)[1]
  50. if (i>1) {
  51. if (runif(1)<p)
  52. T[i] = rexp(1,lambda3)
  53. else
  54. T[i] = rexp(1, lambda2)
  55. }
  56. }
  57. X=X+1
  58. }
  59. if (event==1 && X>=(n_1+c))
  60. T[event]=rexp(n=1,rate=lambda)
  61. return(list(X=X,T=T,t=t))
  62. }
  63.  
  64. for (k in 1:m) {
  65.  
  66. state=vector("list",N)
  67. res=rep(0,N)
  68. perf=rep(0,N)
  69. times=rep(0,N)
  70. beta = array(0,N) # массив моментов регенерации
  71. beta[1] = 1
  72. state[[1]]=G(list(X=0,T=c(0,rep(Inf,n_1)),t=0)) # initial state: arrival at time 0 of 1st class customer
  73. for(j in 2:N) {
  74. state[[j]]=G(state[[j-1]])
  75. if (state[[j-1]]$X == 0) {
  76. beta[which(beta == 0)[1]] = j
  77. }
  78. }
  79. times=diff(sapply(1:N,function(i)state[[i]]$t))
  80. last_time=state[[N]]$t
  81. P0[k]=sum(sapply(1:(N-1),function(i){ times[i]*as.numeric(state[[i]]$X[1]==0) }))/last_time
  82.  
  83. a = array(0,length(which(beta > 0))-1) # array of cycles lengths
  84. a = sapply(1:length(a), function(i) beta[i+1] - beta[i])
  85. len = length(a) # number of the regeneration cycles
  86. y = array(0, len) # y_i = sum of the number of customers in the i-th cycle
  87. for (i in 1:len) {
  88. y[i] = sum(sapply(beta[i]:(beta[i+1]-1), function(j){ state[[j]]$X }))
  89. }
  90. mean_y = mean(y)
  91. mean_a = mean(a)
  92. }
  93.  
  94. # для проверки правильности работы
  95. rho = lambda/mu
  96. p0_theor = 1/(1+rho+rho^2/2+rho^3/6+rho^4/24+rho^4*(rho/4+(rho/4)^2)/24)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement