1+ import numpy as np
2+ import matplotlib .pyplot as plt
3+ from pathlib import Path
4+ from sklearn .mixture import GaussianMixture
5+
6+ from utils import *
7+
8+ path = Path .home () / "cnn_coverage"
9+ robots = np .load (str (path / "results/elle_pos.npy" ))
10+ robots_std = np .load (str (path / "results/stdelle_pos.npy" ))
11+ print ("Robots shape: " , robots .shape )
12+
13+ ROBOTS_NUM = robots .shape [1 ]
14+ TARGETS_NUM = 2
15+ PARTICLES_NUM = 500
16+ AREA_W = 30.0
17+
18+ targets = np .zeros ((TARGETS_NUM , 1 , 2 ))
19+ targets [0 , 0 , 0 ] = 0.0
20+ targets [0 , 0 , 1 ] = 12.5
21+ targets [1 , 0 , 0 ] = 8.0
22+ targets [1 , 0 , 1 ] = 7.5
23+
24+ STD_DEV = 3.0
25+ samples = np .zeros ((TARGETS_NUM , PARTICLES_NUM , 2 ))
26+ for k in range (TARGETS_NUM ):
27+ for i in range (PARTICLES_NUM ):
28+ samples [k , i , :] = targets [k , 0 , :] + STD_DEV * np .random .randn (1 , 2 )
29+
30+
31+
32+ # Fit GMM
33+ samples = samples .reshape ((TARGETS_NUM * PARTICLES_NUM , 2 ))
34+ print (samples .shape )
35+ gmm = GaussianMixture (n_components = TARGETS_NUM , covariance_type = 'full' , max_iter = 1000 )
36+ gmm .fit (samples )
37+
38+ means = gmm .means_
39+ covariances = gmm .covariances_
40+ mix = gmm .weights_
41+
42+ # print(f"Means: {means}")
43+ # print(f"Covs: {covariances}")
44+ # print(f"Mix: {mix}")
45+
46+
47+ ## -------- Generate decentralized probability grid ---------
48+ GRID_STEPS = 64
49+ s = AREA_W / GRID_STEPS # step
50+
51+ xg = np .linspace (- 0.5 * AREA_W , 0.5 * AREA_W , GRID_STEPS )
52+ yg = np .linspace (- 0.5 * AREA_W , 0.5 * AREA_W , GRID_STEPS )
53+ Xg , Yg = np .meshgrid (xg , yg )
54+ # Xg.shape
55+ # print(Xg.shape)
56+
57+ Z = gmm_pdf (Xg , Yg , means , covariances , mix )
58+ Z = Z .reshape (GRID_STEPS , GRID_STEPS )
59+ Zmax = np .max (Z )
60+ Z = Z / Zmax
61+ print ("Z min: " , Z .min ())
62+ print ("Z max: " , Z .max ())
63+
64+
65+ obstacles = np .array ([5.0 , - 5.0 ])
66+ OBS_W = 20.0
67+ BW = 5
68+
69+ fig , ax = plt .subplots (1 , 1 , figsize = (8 ,8 ))
70+ ax .pcolormesh (Xg , Yg , Z , cmap = "Reds" , vmin = 0.01 , alpha = 0.5 )
71+ # ax.plot([obstacles[0]-0.5*OBS_W, obstacles[0]+0.5*OBS_W], [obstacles[1]-0.5*OBS_W, obstacles[1]-0.5*OBS_W], c='tab:blue')
72+ # ax.plot([obstacles[0]+0.5*OBS_W, obstacles[0]+0.5*OBS_W], [obstacles[1]-0.5*OBS_W, obstacles[1]+0.5*OBS_W], c='tab:blue')
73+ # ax.plot([obstacles[0]+0.5*OBS_W, obstacles[0]-0.5*OBS_W], [obstacles[1]+0.5*OBS_W, obstacles[1]+0.5*OBS_W], c='tab:blue')
74+ # ax.plot([obstacles[0]-0.5*OBS_W, obstacles[0]-0.5*OBS_W], [obstacles[1]+0.5*OBS_W, obstacles[1]-0.5*OBS_W], c='tab:blue')
75+ ax .plot ([- 0.5 * AREA_W , obstacles [0 ]- 0.5 * OBS_W ], [- 0.5 * AREA_W , - 0.5 * AREA_W ], c = 'k' , lw = BW )
76+ ax .plot ([obstacles [0 ]- 0.5 * OBS_W , obstacles [0 ]- 0.5 * OBS_W ], [- 0.5 * AREA_W , obstacles [1 ]+ 0.5 * OBS_W ], c = 'k' , lw = BW )
77+ ax .plot ([obstacles [0 ]- 0.5 * OBS_W , 0.5 * AREA_W ], [obstacles [1 ]+ 0.5 * OBS_W , obstacles [1 ]+ 0.5 * OBS_W ], c = 'k' , lw = BW )
78+ ax .plot ([0.5 * AREA_W , 0.5 * AREA_W ], [obstacles [1 ]+ 0.5 * OBS_W , 0.5 * AREA_W ], c = 'k' , lw = BW )
79+ ax .plot ([0.5 * AREA_W , - 0.5 * AREA_W ], [0.5 * AREA_W , 0.5 * AREA_W ], c = 'k' , lw = BW )
80+ ax .plot ([- 0.5 * AREA_W , - 0.5 * AREA_W ], [0.5 * AREA_W , - 0.5 * AREA_W ], c = 'k' , lw = BW )
81+
82+
83+ ax .set_xticks ([])
84+ ax .set_yticks ([])
85+ # ax.plot([0.0, 2*v[0]], [0.0, 2*v[1]], c="tab:blue", linewidth=5)
86+
87+ for i in range (ROBOTS_NUM ):
88+ ax .plot (robots [:, i , 0 ], robots [:, i , 1 ], c = 'tab:blue' , lw = 3 )
89+ ax .scatter (robots [0 , i , 0 ], robots [0 , i , 1 ], facecolors = 'none' , edgecolors = 'tab:blue' , s = 90 )
90+ ax .scatter (robots [- 1 , i , 0 ], robots [- 1 , i , 1 ], c = 'tab:blue' , s = 90 )
91+
92+ plt .show ()
93+
94+
95+ fig , ax = plt .subplots (1 , 1 , figsize = (8 ,8 ))
96+ ax .pcolormesh (Xg , Yg , Z , cmap = "Reds" , vmin = 0.01 , alpha = 0.5 )
97+ # ax.plot([obstacles[0]-0.5*OBS_W, obstacles[0]+0.5*OBS_W], [obstacles[1]-0.5*OBS_W, obstacles[1]-0.5*OBS_W], c='tab:blue')
98+ # ax.plot([obstacles[0]+0.5*OBS_W, obstacles[0]+0.5*OBS_W], [obstacles[1]-0.5*OBS_W, obstacles[1]+0.5*OBS_W], c='tab:blue')
99+ # ax.plot([obstacles[0]+0.5*OBS_W, obstacles[0]-0.5*OBS_W], [obstacles[1]+0.5*OBS_W, obstacles[1]+0.5*OBS_W], c='tab:blue')
100+ # ax.plot([obstacles[0]-0.5*OBS_W, obstacles[0]-0.5*OBS_W], [obstacles[1]+0.5*OBS_W, obstacles[1]-0.5*OBS_W], c='tab:blue')
101+ ax .plot ([- 0.5 * AREA_W , obstacles [0 ]- 0.5 * OBS_W ], [- 0.5 * AREA_W , - 0.5 * AREA_W ], c = 'k' , lw = BW )
102+ ax .plot ([obstacles [0 ]- 0.5 * OBS_W , obstacles [0 ]- 0.5 * OBS_W ], [- 0.5 * AREA_W , obstacles [1 ]+ 0.5 * OBS_W ], c = 'k' , lw = BW )
103+ ax .plot ([obstacles [0 ]- 0.5 * OBS_W , 0.5 * AREA_W ], [obstacles [1 ]+ 0.5 * OBS_W , obstacles [1 ]+ 0.5 * OBS_W ], c = 'k' , lw = BW )
104+ ax .plot ([0.5 * AREA_W , 0.5 * AREA_W ], [obstacles [1 ]+ 0.5 * OBS_W , 0.5 * AREA_W ], c = 'k' , lw = BW )
105+ ax .plot ([0.5 * AREA_W , - 0.5 * AREA_W ], [0.5 * AREA_W , 0.5 * AREA_W ], c = 'k' , lw = BW )
106+ ax .plot ([- 0.5 * AREA_W , - 0.5 * AREA_W ], [0.5 * AREA_W , - 0.5 * AREA_W ], c = 'k' , lw = BW )
107+
108+
109+ ax .set_xticks ([])
110+ ax .set_yticks ([])
111+ # ax.plot([0.0, 2*v[0]], [0.0, 2*v[1]], c="tab:blue", linewidth=5)
112+
113+ for i in range (ROBOTS_NUM ):
114+ ax .plot (robots_std [:, i , 0 ], robots_std [:, i , 1 ], c = 'tab:blue' , lw = 3 )
115+ ax .scatter (robots_std [0 , i , 0 ], robots_std [0 , i , 1 ], facecolors = 'none' , edgecolors = 'tab:blue' , s = 90 )
116+ ax .scatter (robots_std [- 1 , i , 0 ], robots_std [- 1 , i , 1 ], c = 'tab:blue' , s = 90 )
117+
118+ th = np .arange (0 , 2 * np .pi + np .pi / 20 , np .pi / 20 )
119+ R = 2.5
120+ xc = obstacles [0 ]- 0.5 * OBS_W + R * np .cos (th )
121+ yc = obstacles [1 ]+ 0.5 * OBS_W + R * np .sin (th )
122+ # ax.plot(xc, yc, lw=7, c='r')
123+
124+ plt .show ()
125+
126+
127+ """
128+ figpath = Path.home() / "cnn_coverage/pics"
129+ plt.savefig(figpath/"l-results.png")
130+ plt.show()
131+ """
0 commit comments