BUPT丶云卷云舒 2021-07-03 13:23 采纳率: 100%
浏览 236
已结题

如何让一个函数循环执行100次生成不同的结果并存入列表?

如图,我想让这个程序运行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)
  • 写回答

2条回答 默认 最新

  • 宁缺灬 2021-07-03 15:56
    关注

    好吧,换了python37可以跑,老版本需要手动引入。
    你的r作为全局变量,赋值只操作了一次,后续每次test送入的range1,anchors都是不会变动值的变量,全是固定值的变量,结果当然都一样。。。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月30日

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?