-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_for_algorithm.py
122 lines (88 loc) · 3.91 KB
/
test_for_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from calculator import V, Calculator
from convertions import dist
import numpy as np
import matplotlib.pyplot as plt
# define a field
L = 6
def establishScenario(n, dim):
rn = L * np.random.rand(dim, n)
#print rn
bn = L * np.random.rand(dim, 1)
doa = dist(rn, bn, 0)
toa = doa / V
noise = np.zeros(0)
for i in range(len(doa)):
noise = np.append(noise, np.random.randn()) * 1e-6
toa = toa + noise
ary = np.vstack((rn, toa))
return ary.T, bn
if __name__ == '__main__':
"""
(3, 2)
(4, 2)
(4, 3)
(5, 3)
"""
ary, bn = establishScenario(5, 2)
a_shape = np.shape(ary)
b_shape = np.shape(bn)
rechner = Calculator(ary)
est_ml = [rechner.ml()]
est_tls = [rechner.tls()]
est_ml_5 = [rechner.ml_5()]
n, dim = np.shape(ary)
if b_shape == (2,1):
print('the unknown is locating at: ', bn[0][0], bn[1][0], '\n')
if len(est_ml) == 2:
x0, y0 = est_ml[0][0], est_ml[0][1]
x1, y1 = est_ml[1][0], est_ml[1][1]
print('the first estimation of maximun likelihoos is: ', x0, y0)
print('the alternative estimation of maximun likelihoos is: ', x1, y1)
plt.plot(x0, y0, 'o', color='g')
plt.plot(x1, y1, 'o', color='g')
elif len(est_ml) == 1:
x0, y0 = est_ml[0][0], est_ml[0][1]
print('the estimation of maximun likelihoos is: ', x0, y0)
plt.plot(x0, y0, 'o', color='g')
x_tls, y_tls = est_tls[0][0], est_tls[0][1]
plt.plot(x_tls, y_tls, 'o', color='y')
print('the estimation of least square is: ', x_tls, y_tls)
plt.plot(bn[0], bn[1], 'x', color='r')
plt.plot(ary[:, 0], ary[:, 1], '^')
elif b_shape == (3,1):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.plot(bn[0], bn[1], bn[2],'x', color='r')
plt.plot(ary[:, 0], ary[:, 1], ary[:, 2], '^')
print('the unknown is locating at: ', bn[0][0], bn[1][0], bn[2][0])
if n == 4:
if len(est_ml) == 2:
x0, y0, z0 = est_ml[0][0], est_ml[0][1], est_ml[0][2]
x1, y1, z1 = est_ml[1][0], est_ml[1][1], est_ml[1][2]
print('the first estimation of maximun likelihoos is: ', x0, y0, z0)
print('the alternative estimation of maximun likelihoos is: ', x1, y1, z1)
ax.scatter(x0, y0, z0, color='g')
ax.scatter(x1, y1, z1, color='g')
elif len(est_ml) == 1:
x0, y0, z0 = est_ml[0][0], est_ml[0][1], est_ml[0][2]
print('the estimation of maximun likelihoos is: ', x0, y0, z0)
ax.scatter(x0, y0, z0, color='g')
x_ml_5, y_ml_5, z_ml_5 = est_ml_5[0][0], est_ml_5[0][1], est_ml_5[0][2]
print('the estimation of high dimention maximun likelihoos is: ', x_ml_5, y_ml_5, z_ml_5)
ax.scatter(x_ml_5, y_ml_5, z_ml_5, color='r')
elif n == 5:
if len(est_ml) == 2:
x0, y0, z0 = est_ml[0][0], est_ml[0][1], est_ml[0][2]
x1, y1, z1 = est_ml[1][0], est_ml[1][1], est_ml[1][2]
print('the first estimation of maximun likelihoos is: ', x0, y0, z0)
print('the alternative estimation of maximun likelihoos is: ', x1, y1, z1)
ax.scatter(x0, y0, z0, color='g')
ax.scatter(x1, y1, z1, color='g')
elif len(est_ml) == 1:
x0, y0, z0 = est_ml[0][0], est_ml[0][1], est_ml[0][2]
print('the estimation of maximun likelihoos is: ', x0, y0, z0)
ax.scatter(x0, y0, z0, color='g')
x_ml_5, y_ml_5, z_ml_5 = est_ml_5[0][0], est_ml_5[0][1], est_ml_5[0][2]
print('the estimation of high dimention maximun likelihoos is: ', x_ml_5, y_ml_5, z_ml_5)
ax.scatter(x_ml_5, y_ml_5, z_ml_5, color='black')
plt.show()