如图,我想让这个程序运行100次,并把100次定位的结果存到列表中以进行数据分析,请大神帮忙看看怎么弄!
我当前的代码只能打印出一百个相同的结果,不知道为什么!
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
import numpy as np
from math import *
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from numpy import *
from numpy.linalg import inv, qr
Noise = 1 #噪声方差
a = random.random(3)
r = np.zeros(3)
for i in range(0,3):
r[i]=2*a[i]-1 #随机生成一个-1到1的数
def opt_location(range1, anchors): #range1是真实测得的toa
def con():
# 约束条件 分为eq 和ineq
# eq表示 函数结果等于0 ; ineq 表示 表达式大于等于0
x1min, x1max, x2min, x2max = -5, 15, -5, 15
cons = ({'type': 'ineq', 'fun': lambda x: x[0] - x1min}, #x[0]-x1min >= 0
{'type': 'ineq', 'fun': lambda x: -x[0] + x1max},
{'type': 'ineq', 'fun': lambda x: x[1] - x2min},
{'type': 'ineq', 'fun': lambda x: -x[1] + x2max})
return cons #
def cost(pos):#***************
ref = np.sqrt(np.sum((anchors-pos.reshape(1,2))**2, axis=1)) #anchors为四个基站的位置,pos为要优化的位置
ref0 = ref[1:] - ref[0]
ref1 = ref[2:] - ref[1]
ref2 = ref[3:] - ref[2] #tdoa
Ri_0 = range1[1:] - range1[0] + 3*Noise*r[0]
Ri_1 = range1[2:] - range1[1] + 2*Noise*r[1]
Ri_2 = range1[3:] - range1[2] + Noise*r[2] #真实测得的tdoa
return np.sum((Ri_0 - ref0)**2) + np.sum((Ri_1 - ref1)**2) + np.sum((Ri_2 - ref2)**2) #目标函数的目的是要求所估计位置和各基站的tdoa
#和真实测得的tdoa相差最小
x0 = np.array([5, 5])
res = minimize(cost, x0, constraints=con()).x
return res
def test():
#pos = np.array([0,4])
z = opt_location(range1, anchors)
return z
anchors = np.array([[0,0],[0,10],[10,10],[10,0]])
#range = np.array([7.07106781,7.07106781,7.07106781,7.07106781])
rangetoa = np.array([0.00000002358654336 , 0.00000002358654336 ,0.00000002358654336 ,0.00000002358654336]) #移动台为5 5的toa
#rangetoa = np.array([0.00000001886923469 , 0.00000002405364897,0.00000002830385204 ,0.00000002405364897])#移动台为4 4 的toa
rangetoa = np.array([0.00000002540348465, 0.00000001415192602,0.00000002540348465 ,0.00000003302116071])#移动台为3 7 的toa
#rangetoa = np.array([0.00000002028991179, 0.00000001375319997,0.00000003285225341 ,0.00000003608047346]) #移动台为1 6的toa
range1 = rangetoa * 299792458
position = np.zeros((100,2),dtype = float)
for i in range (0,100):
position[i] = test()
print(position)