Guest User

Untitled

a guest
Nov 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import itertools
  2.  
  3. # Number of running programs
  4. progs = 5
  5.  
  6. # Number of facilities per node
  7. fac_num = (1, 1, 1, 1, 1)
  8.  
  9. # Number of nodes
  10. nodes = len(fac_num)
  11.  
  12. # Tuple with omega coefficients (probability of entering node).
  13. # Were calculated manually
  14. w = (0.5, 0.125, 0.125, 0.125, 0.125)
  15.  
  16. # Servicing intensity
  17. m1 = 100
  18. m2 = m1*0.2
  19. m3 = m1*0.2
  20. m4 = m1*0.2
  21. m5 = m1*0.2
  22.  
  23. # Tuple with servicing intensity coefficients
  24. m = (m1, m2, m3, m4, m5)
  25.  
  26. # Compose all possible permutations with repetitions
  27. # of 5 elements from 6 possible numbers (Cartesian product)
  28. states = itertools.product(range(progs + 1), repeat=nodes)
  29. # Filter out all permutations with total sum of tuple members
  30. # not equal to 5 to get all possible states
  31. states = set(filter(lambda pwr: sum(pwr) == progs, states))
  32.  
  33. # Calculate normalizing constant
  34. g = 0
  35. # Go through all states
  36. for state in states:
  37. # Form product for each state
  38. product = 1
  39. # Each product has number of members equal to number
  40. # of nodes
  41. for node in range(nodes):
  42. product *= (w[node] / m[node]) ** state[node]
  43. # Add calculated product to constant
  44. g += product
  45.  
  46. # Assign probability to states
  47. state_prob_map = {}
  48. for state in states:
  49. # Start calculating product from neutral number
  50. product = 1
  51. # Multiply it by (w/m)^n for each node
  52. for node in range(nodes):
  53. product *= (w[node] / m[node]) ** state[node]
  54. # Calculate probability
  55. probability = product/g
  56. # Add everything to the map
  57. state_prob_map[state] = probability
  58.  
  59. # List for average number of requests in node
  60. rq_total_node = []
  61. # Calculate expected value using our state map
  62. for node in range(nodes):
  63. sum = 0
  64. for state in state_prob_map:
  65. rq_num = state[node]
  66. prob = state_prob_map[state]
  67. sum += rq_num * prob
  68. rq_total_node.append(sum)
  69.  
  70. # List for average number of requests in queue in node
  71. rq_queued_node = []
  72. # Calculate expected value using our state map
  73. for node in range(nodes):
  74. sum = 0
  75. for state in state_prob_map:
  76. rq_num = state[node]
  77. facilities = fac_num[node]
  78. if rq_num > facilities:
  79. prob = state_prob_map[state]
  80. sum += (rq_num - facilities) * prob
  81. rq_queued_node.append(sum)
  82.  
  83. # List for average number of requests in service in node
  84. rq_serviced_node = []
  85. for node in range(nodes):
  86. sum = 0
  87. for state in state_prob_map:
  88. rq_num = state[node]
  89. facilities = fac_num[node]
  90. if rq_num <= facilities:
  91. prob = state_prob_map[state]
  92. sum += rq_num * prob
  93. else:
  94. prob = state_prob_map[state]
  95. sum += facilities * prob
  96. rq_serviced_node.append(sum)
  97.  
  98. for node in range(nodes):
  99. print("Node {}, total: {}, queued: {}, serviced: {}, diff: {}".format(node + 1, round(rq_total_node[node], 5), round(rq_queued_node[node], 5), round(rq_serviced_node[node], 5), round(rq_total_node[node] - rq_queued_node[node] - rq_serviced_node[node], 10)))
Add Comment
Please, Sign In to add comment