Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # W = 1
  5. # b = np.array([1., 0.])
  6. # M = 2
  7. # D_0 = np.matrix([[-8, 1], [1, -11]])
  8. # D_1 = np.matrix([[2, 5], [4, 6]])
  9.  
  10. def generateY_0(W, M, b):
  11. IB = np.kron(np.identity(W + 1), b)
  12. y_0 = np.matrix([[0 for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  13. y_0[0:W + 1, W + 1:(W + 1) * (M + 1)] = IB
  14.  
  15. return y_0
  16.  
  17.  
  18. def generateY_1(W, M, S, S_0, D_0):
  19. kron_sum = np.kron(D_0, np.identity(M)) + np.kron(np.identity(W + 1), S)
  20.  
  21. diag = kron_sum.diagonal()
  22. R = np.diagflat(np.absolute(diag))
  23. y_21 = np.dot(np.linalg.inv(R), np.kron(np.identity(W + 1), S_0))
  24. tmp = np.dot(np.linalg.inv(R), kron_sum)
  25. y_22 = tmp + np.identity(tmp.shape[0])
  26.  
  27. y_1 = np.matrix([[0. for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  28. y_1[W + 1: W + (W + 1) * (M + 1), 0: (W + 1)] = y_21
  29. y_1[W + 1:(W + 1) * (M + 1), W + 1:(W + 1) * (M + 1)] = y_22
  30. return y_1
  31.  
  32.  
  33. def generateY_2(M, W, S, D_0, D_1):
  34. y_2 = np.matrix([[0. for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  35. kron_sum = np.kron(D_0, np.identity(M)) + np.kron(np.identity(W + 1), S)
  36. diag = kron_sum.diagonal()
  37. R = np.diagflat(np.absolute(diag))
  38. y_22 = np.dot(np.linalg.inv(R), np.kron(D_1, np.identity(M)))
  39. y_2[W + 1:(W + 1) * (M + 1), W + 1:(W + 1) * (M + 1)] = y_22
  40. return y_2
  41.  
  42.  
  43. def solveEquation(W, M, Y_0, Y_1, Y_2, e_g):
  44. G_0 = np.identity((W + 1) * (M + 1))
  45. tmp1 = np.linalg.inv(np.identity((W + 1) * (M + 1)) - Y_1)
  46. tmp2 = Y_0 + np.dot(Y_2, np.dot(G_0, G_0))
  47. G_1 = np.dot(tmp1, tmp2)
  48. while np.linalg.norm(G_1 - G_0) > e_g:
  49. G_0 = G_1
  50. tmp1 = np.linalg.inv(np.identity((W + 1) * (M + 1)) - Y_1)
  51. tmp2 = Y_0 + np.dot(Y_2, np.dot(G_0, G_0))
  52. G_1 = np.dot(tmp1, tmp2)
  53.  
  54. return G_1
  55.  
  56.  
  57. def generateQii(W, M, S_0, D_0, D_1, b, alpha, S, i):
  58. q00 = np.matrix([[0. for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  59. q00[W + 1:(W + 1) * (M + 1), 0:W + 1] = np.kron(np.identity(W + 1), S_0)
  60. q00[0:(W + 1), 0:W + 1] = D_0 - i * alpha * np.identity(W + 1)
  61. q00[0:(W + 1), W + 1:(W + 1) * (M + 1)] = np.kron(D_1, b)
  62. kron_sum = np.kron(D_0, np.identity(M)) + np.kron(np.identity(W + 1), S)
  63. q00[W + 1:(W + 1) * (M + 1), W + 1:(W + 1) * (M + 1)] = kron_sum
  64. return q00
  65.  
  66.  
  67. def generateQiiPlus1(W, M, D_1):
  68. q01 = np.matrix([[0. for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  69. q01[W + 1:(W + 1) * (M + 1), W + 1:(W + 1) * (M + 1)] = np.kron(D_1, np.identity(M));
  70. return q01
  71.  
  72.  
  73. def generateQiiMin1(W, M, alpha, b, i):
  74. q10 = np.matrix([[0. for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  75. q10[0:(W + 1), W + 1:(W + 1) * (M + 1)] = np.kron(i * alpha * np.identity(W + 1), b)
  76. return q10
  77.  
  78.  
  79. def find_i0(W, M, S_0, D_0, D_1, b, alpha, S, G, e_g):
  80. i0 = 10
  81. G_i0 = np.dot(np.transpose(-generateQii(W, M, S_0, D_0, D_1, b, alpha, S, i0 + 1) - np.dot(generateQiiPlus1(W, M, D_1), G)), generateQiiMin1(W, M, alpha, b, i0 + 1))
  82. while np.linalg.norm(G_i0 - G) < e_g:
  83. i0 += 10
  84. G_i0 = np.dot(np.transpose(-generateQii(W, M, S_0, D_0, D_1, b, alpha, S, i + 1) - np.dot(generateQiiPlus1(W, M, D_1), G)), generateQiiMin1(W, M, alpha, b, i + 1))
  85. return i0
  86.  
  87.  
  88. def generateG_i(W, M, S_0, D_0, D_1, b, alpha, S, G_iPlus1, i):
  89. G_i = np.dot(np.linalg.inv(-generateQii(W, M, S_0, D_0, D_1, b, alpha, S, i + 1) - np.dot(generateQiiPlus1(W, M, D_1), G_iPlus1)), generateQiiMin1(W, M, alpha, b, i + 1))
  90. return G_i
  91.  
  92.  
  93. def generateQ(W, M, S_0, D_0, D_1, b, alpha, S, i, Gi):
  94. return generateQii(W, M, S_0, D_0, D_1, b, alpha, S, i) + np.dot(generateQiiPlus1(W, M, D_1), Gi)
  95.  
  96.  
  97. def generateF_i(W, M, D_1, i, Q, listF):
  98. return np.dot(np.dot(listF[i - 1], generateQiiPlus1(W, M, D_1)), np.linalg.inv(-Q))
  99.  
  100.  
  101. def solveP(W, M, listQ, listF):
  102. Q00 = -listQ[0]
  103. F = np.matrix([[0. for __ in range((W + 1) * (M + 1))] for _ in range((W + 1) * (M + 1))])
  104. for f in listF:
  105. F += f
  106. F = np.dot(F, np.ones((M + 1) * (W + 1)))
  107. Q00[0:1, 0:(W + 1) * (M + 1)] = F
  108. zero = np.zeros((W + 1) * (M + 1))
  109. zero[0] = 1
  110. return np.linalg.solve(Q00, zero)
  111.  
  112.  
  113. def criterion(D_0, D_1, S, beta, size):
  114. D_0 = np.array(D_0)
  115. D_1 = np.array(D_1)
  116. S = np.array(S)
  117. beta = np.array(beta)
  118. EPS = 10 ** (-5)
  119. nul = np.array([0] * size)
  120. eden = np.array([1] * size)
  121. # print(EPS)
  122. D = D_0 + D_1
  123. D = np.matrix.transpose(D)
  124. D[0] = [1] * size
  125. nul[0] = 1
  126. # print(D)
  127. Teta = np.linalg.solve(D, nul)
  128. print("Teta: ", Teta)
  129. lamb = np.dot(Teta, np.dot(D_1, eden))
  130. print("Lambda = ", lamb)
  131. # print("Inverse matrix S")
  132. # print(np.linalg.inv(S))
  133. # print(np.linalg.inv(S)*np.matrix.transpose(S))
  134. nym = np.dot(np.dot(beta, np.linalg.inv(S)), eden)
  135. nym = (-1) / nym
  136. print("Ny = ", nym)
  137. print("ro = ", lamb / nym)
  138. return (lamb / nym + EPS) < 1, lamb
  139.  
  140.  
  141. def checkNegative(matrix):
  142. for i in range(len(matrix) - 1):
  143. if matrix[i, i] > 0:
  144. return False
  145. return True
  146.  
  147.  
  148. def solution(i, alpha):
  149. print('Hello! Please, enter W')
  150. # W = int(input())
  151. M = 2
  152. W = 1
  153. D_0 = np.matrix([[0 for __ in range(W + 1)] for _ in range(W + 1)])
  154. D_1 = np.matrix([[0 for __ in range(W + 1)] for _ in range(W + 1)])
  155. D_0 = np.matrix([[-8, 1], [1, -11]])
  156. D_1 = i * np.matrix([[2, 5], [4, 6]])
  157.  
  158.  
  159. print('Please, enter matrix elements')
  160.  
  161. # for i in range(W + 1):
  162. # D_0[i] = [float(j) for j in input().split()]
  163. # for i in range(W + 1):
  164. # D_1[i] = [float(j) for j in input().split()]
  165.  
  166. print('Please, enter M')
  167. # M = int(input())
  168. print('Enter b')
  169. b = np.array([1, 0])
  170. # print('Enter S')
  171. # S = np.matrix([[0 for _ in range(M + 1)] for __ in range(M + 1)])
  172.  
  173. S = np.matrix([[-30., 30.], [0., -30.]])
  174. E = np.ones(W + 1)
  175. S_0_1 = np.dot(-S, E)
  176. S_0 = np.transpose(S_0_1)
  177.  
  178. # for i in range(M + 1):
  179. # S[i] = [float(i) for i in input().split()]
  180. # e_g, e_f = [float(i) for i in input().split()]
  181.  
  182. e_g = 10**-9
  183. e_f = 0.000001
  184.  
  185. condition, lamb = criterion(D_0, D_1, S, b, W + 1)
  186. print(condition)
  187.  
  188. Y_0 = generateY_0(W, M, b)
  189. Y_1 = generateY_1(W, M, S, S_0, D_0)
  190. Y_2 = generateY_2(M, W, S, D_0, D_1)
  191. G = solveEquation(W, M, Y_0, Y_1, Y_2, e_g)
  192.  
  193. Q00 = generateQii(W, M, S_0, D_0, D_1, b, alpha, S, 0)
  194. Q01 = generateQiiPlus1(W, M, D_1)
  195. Q10 = generateQiiMin1(W, M, alpha, b, 1)
  196.  
  197. I = find_i0(W, M, S_0, D_0, D_1, b, alpha, S, G, e_g)
  198. print("i0 = ", I)
  199.  
  200. i = I
  201. listG = [G]
  202. while i >= 0:
  203. listG = [generateG_i(W, M, S_0, D_0, D_1, b, alpha, S, listG[0], i)] + listG
  204. i -= 1
  205.  
  206. listQ = []
  207. i = I
  208. while i >= 0:
  209. listQ = [generateQ(W, M, S_0, D_0, D_1, b, alpha, S, i, listG[i])] + listQ
  210. i -= 1
  211.  
  212. listF = [np.identity((W + 1) * (M + 1))]
  213. i = 1
  214. while i < I + 1:
  215. listF.append(generateF_i(W, M, D_1, i, listQ[i], listF))
  216. if np.linalg.norm(listF[len(listF) - 1], np.inf) < e_f:
  217. break
  218. i += 1
  219.  
  220. if np.linalg.norm(listF[len(listF) - 1], np.inf) > e_f:
  221. while np.linalg.norm(listF[len(listF) - 1], np.inf) > e_f:
  222. listF.append(generateF_i(W, M, D_1, i, generateQ(W, M, S_0, D_0, D_1, b, alpha, S, i, G), listF))
  223. i += 1
  224. p0 = solveP(W, M, listQ, listF)
  225. p = []
  226. for f in listF:
  227. p.append(np.array(np.dot(p0, f)).flatten())
  228.  
  229. i = 0
  230. prob = []
  231. for Pi in p:
  232. prob.append(np.dot(Pi, np.transpose(np.ones((W + 1) * (M + 1)))))
  233. print("probability that in orbit ", i, "applications: ", np.dot(Pi, np.transpose(np.ones((W + 1) * (M + 1)))))
  234. i += 1
  235.  
  236. probSum = 0
  237. i = 1
  238. for pr in prob:
  239. probSum += i * pr
  240. i += 1
  241. return probSum, lamb
  242.  
  243.  
  244. if __name__ == '__main__':
  245. Leng = [[], [], []]
  246. Lamb = [[], [], []]
  247. q = 0
  248. for alpha in [5, 10, 20]:
  249. L = list()
  250. La = list()
  251. koef = 1
  252. for i in range(1, 11):
  253. koef += 0.01
  254. length, lamb = solution(koef, alpha)
  255. L.append(length)
  256. La.append(lamb)
  257. Leng[q] = L
  258. Lamb[q] = La
  259. q += 1
  260. plt.ylabel(r'$L$')
  261. plt.xlabel(r'$\lambda$')
  262. print('Leng', Leng[0])
  263. print('lam', Lamb[0])
  264. print('Leng', Leng[0])
  265. print('lam', Lamb[1])
  266. plt.plot(Lamb[0], Leng[0], label=r'$\gamma=1$', linestyle='-')
  267. plt.plot(Lamb[1], Leng[1], label=r'$\gamma=5$', linestyle='--')
  268. plt.plot(Lamb[2], Leng[2], label=r'$\gamma=30$', linestyle='-.')
  269. plt.legend(loc='best')
  270. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement