Paste Search Dynamic
Recent pastes
deepcopy
1. # -*- coding: utf-8 -*-
2. """
3. Created on Tue Nov 10 21:00:07 2020
4.
5. @author: Germain François
6. """
7.
8. import numpy as np
9. from copy import deepcopy
10. import matplotlib.pyplot as plt
11. from scipy.sparse.linalg import inv
12.
13. x,y = 40, 40
14. N_T = 40
15. N_O = 40
16.
17. left_prob = 0.2
18. right_prob = 0.2
19. top_prob = 0.1
20. bottom_prob = 0.5
21.
22. M = np.zeros(x*y)
23.
24. for i in range(N_T):
25.     pos = np.random.randint(0,x*y)
26.     p = pos
27.     while M[pos % x*y] != 0:
28.         pos += 1
29.     M[pos% (x*y)] = 1
30.
31. for i in range(N_O):
32.     pos = np.random.randint(0,x*y)
33.     p = pos
34.     while M[pos % (x*y)] != 0:
35.         pos += 1
36.     M[pos % (x*y)] = -1
37.
38. M = M.reshape(x,y)
39.
40.
41. plt.imshow(M)
42. plt.show()
43.
44.
45.
46.
47. ix = np.zeros((x,y))
48. for k, (i,j) in enumerate(zip(*np.where(M == 0))):
49.     ix[i,j] = k
50.
51. for k, (i,j) in enumerate(zip(*np.where(M == 1))):
52.     ix[i,j] = k
53.
54. END = 0
55. L = []
56. Ps = []
57. for END in range(N_T):
58.     print(END)
59.     P = np.eye(x*y)
60.     A = np.zeros(x*y)
61.
62.
63.     #for i,j in zip(*np.where(M == 0)):
64.     for i in range(x):
65.         for j in range(y):
66.
67.             node = x*i+j# int(ix[i,j])
68.             if M[i][j] == 0:
69.                 # top
70.                 if i == 0 or M[i-1,j] == -1: # top of the grid or obstacle
71.                     P[node,node] -= top_prob
72.                 elif M[i-1,j] == 1: # end node
73.                     if ix[i-1,j] == END:
74.                         A[node] += top_prob
75.                 else: # free to move
76.                     top_node = x * (i-1) + j # int(ix[i-1,j])
77.                     P[node,top_node] -= top_prob
78.
79.
80.                 # bottom
81.                 if i == x-1 or M[i+1,j] == -1:
82.                     P[node,node] -= bottom_prob
83.                 elif M[i+1,j] == 1: # end node
84.                     if ix[i+1,j] == END:
85.                         A[node] += bottom_prob
86.                 else: # free to move
87.                     bottom_node = x * (i+1) + j # int(ix[i+1,j])
88.                     P[node,bottom_node] -= bottom_prob
89.
90.                 # left
91.                 if j == 0 or M[i,j-1] == -1:
92.                     P[node,node] -= left_prob
93.                 elif M[i,j-1] == 1: # end node
94.                     if ix[i,j-1] == END:
95.                         A[node] += left_prob
96.                 else: # free to move
97.                     left_node = x * i + j - 1 # int(ix[i,j-1])
98.                     P[node,left_node] -= left_prob
99.
100.
101.                 # right
102.                 if j == y-1 or M[i,j+1] == -1:
103.                     P[node,node] -= right_prob
104.                 elif M[i,j+1] == 1: # end node
105.                     if ix[i,j+1] == END:
106.                         A[node] += right_prob
107.                 else: # free to move
108.                     right_node = x*i + j+1 # int(ix[i,j+1])
109.                     P[node,right_node] -= right_prob
110.
111.     R = np.linalg.inv(P).dot(A)
112.     L.append(R)
113.
114.     K = deepcopy(R.reshape(x,y))
115.     K[np.where(M==-1)]=-1
116.     plt.imshow(K)
117.     plt.show()
118.
119.
120. RES = [1/(x*y-N_O - N_T) * r.sum() for r in L]
121. print(RES)
122. print(sum(RES))
Parsed in 0.028 seconds