Python 2.7:计算多条多项式曲线的坐标集下的曲线下面积(每次运行函数时,结果不会重置为0)

时间:2018-02-15 11:52:14

标签: python python-2.7 numpy polynomials

我有一个很长的功能,所以请耐心等待...我基本上试图根据输入的组合找到2或3个多项式曲线下的总面积。

类似this StackO帖子,但涉及多个多项式曲线。

在整个比赛中,驾驶员有各种轮胎组合和限制长度(在轮胎上花费的时间)。

EG。让我们说一场比赛有10圈。[' Super soft',' Super soft',' Super soft',' Super soft',& #39; Soft'," Soft"," Soft"," Soft"," Soft",Soft",& #34;柔软],意味着在软胎上使用了超级软胎,6圈(第2次)4圈(第1次)。

我有一个df,其中包含每个场景的坐标集(例如,Tire:Super soft,Stint:1)。现在,我想根据这些系数的总和计算总时间。下面的函数会产生一个结果,但结果与我的手动求和不符。

def calc_optimal_(tyre_to_use_list, name, year, tyre_degrad_models): 

    timings = []
    tyre_strategy = []
    freqs = []
    strategy = []

    # Filter tyre degradation models for the race
    model = tyre_degrad_models[tyre_degrad_models['name'] == name].reset_index(drop=True)
    #model = model[model["coeffs new"] != "Did not run a full stint on this tyre during the race"]

    # Generate tyre permutations of Super soft and Soft
     combis_all = tyre_combis(tyre_to_use_list, name , year)

    # Ensures that each tyre name appears once
    if len(tyre_to_use_list) == 2:
        combis = list(filter(lambda x: len(set(x)) > 1, combis_all))

    elif len(tyre_to_use_list) == 3:
        combis = list(filter(lambda x: len(set(x)) > 2, combis_all))

    # Delete integer at end of tyre combination if there are multiple names of the same combination
    def remove_int(x):
        return x.translate(None, digits).rstrip(" ")

    combis_new = [map(remove_int, combis[x]) for x in range(len(combis))]

    tyre_to_use_list = map(remove_int, tyre_to_use_list)

    # THIS IS THE PART WHERE I AM STUCK AT. Function to calculate total race time if tyre combination was used
def calc_time_per_combi(x):

    freq = [len(list(group)) for key, group in groupby(combis[x])]

    timing = []
    summed = 0 

    f1 = model[(model['tyre'] == combis_new[x][freq[0]]) & (model['stint'] == 0+1)]['coeffs centered'].reset_index(drop=True)[0]
    f1 = f1[:freq[0]]
    t1 = simps(f1, dx=5)
    timing.append(t1)
    print timing

    if len(freq) > 1:
        f2 = model[(model['tyre'] == combis_new[x][freq[1]]) & (model['stint'] == 1+1)]['coeffs centered'].reset_index(drop=True)[0]     
        if f2.any():
            f2 = f2[0:freq[1]]
            t2 = simps(f2, dx=5)
            timing.append(t2)
            print timing

    if len(freq) > 2:
        f3 = model[(model['tyre'] == combis_new[x][freq[2]]) & (model['stint'] == 2+1)]['coeffs centered'].reset_index(drop=True)[0]
        if f3.any():
            f3 = f3[0:freq[2]]
            t3 = simps(f3, dx=5)
            timing.append(t3)
            print timing

    summed = sum([int(i) for i in timing])
    del timing[ 0:len(timing) ]
    return summed 
    # Create dataframe of results of all combinations
    # Loop through all the possible tyre combinations and calculate the total race time for each combi
     for x in range(len(combis)):
        c = calc_time_per_combi(x)
        timings.append(c)
        tyre_strategy.append(combis[x])
        freq = [len(list(group)) for key, group in groupby(combis[x])]
        freqs.append(freq)

    df = pd.DataFrame({"tyre strategy": combis,
                            "tyre freq": freqs,
                            "timings": timings}).sort_values("timings", ascending=True)

     return df

当我使用calc_optimal_timeadv1(['Super soft 1', 'Super soft 2'], "Australian Grand Prix", 2016, all_centered, show_table=True)执行该函数时,"print timing"语句输出此信息,表明列表仍然存储旧结果,每次运行该函数时都不会重置为0,尽管我在return语句之前放置了"del timing[ 0:len(timing) ]"语句。

[2499256.5371356499]
[2499256.5371356499, 0.0]
[2352207.5371378958]
[2352207.5371378958, -3366.5472821174626]
[2211012.4066790882]
[2211012.4066790882, -13373.456908395456]

tyre_degrad_models dataframe

{'coeffs new': {0: array([  0.00000000e+00,   2.25236029e+02,   3.61353708e+02,
           4.08353035e+02,   3.66234012e+02,   2.34996638e+02,
           1.46409138e+01,  -2.94833161e+02,  -6.93425587e+02,
          -1.18113636e+03,  -1.75796549e+03,  -2.42391297e+03,
          -3.17897880e+03,  -4.02316298e+03,  -4.95646551e+03,
          -5.97888639e+03,  -7.09042562e+03,  -8.29108320e+03,
          -9.58085914e+03,  -1.09597534e+04,  -1.24277661e+04,
          -1.39848970e+04,  -1.56311464e+04,  -1.73665141e+04,
          -1.91910001e+04,  -2.11046045e+04,  -2.31073272e+04,
          -2.51991683e+04,  -2.73801278e+04,  -2.96502056e+04,
          -3.20094017e+04,  -3.44577162e+04,  -3.69951490e+04,
          -3.96217002e+04,  -4.23373698e+04,  -4.51421577e+04,
          -4.80360639e+04,  -5.10190885e+04,  -5.40912315e+04,
          -5.72524928e+04,  -6.05028724e+04,  -6.38423704e+04,
          -6.72709868e+04,  -7.07887215e+04,  -7.43955745e+04,
          -7.80915459e+04,  -8.18766357e+04,  -8.57508438e+04,
          -8.97141702e+04,  -9.37666150e+04,  -9.79081782e+04,
          -1.02138860e+05,  -1.06458660e+05,  -1.10867578e+05,
          -1.15365614e+05,  -1.19952769e+05,  -1.24629042e+05]),
  1: array([  0.00000000e+00,   9.38647679e+01,   1.67183908e+02,
           2.19957421e+02,   2.52185306e+02,   2.63867564e+02,
           2.55004193e+02,   2.25595196e+02,   1.75640571e+02,
           1.05140318e+02,   1.40944372e+01,  -9.74970708e+01,
          -2.29634206e+02,  -3.82316970e+02,  -5.55545360e+02,
          -7.49319379e+02,  -9.63639025e+02,  -1.19850430e+03,
          -1.45391520e+03,  -1.72987173e+03,  -2.02637388e+03,
          -2.34342167e+03,  -2.68101508e+03,  -3.03915412e+03,
          -3.41783879e+03,  -3.81706908e+03,  -4.23684500e+03,
          -4.67716655e+03,  -5.13803373e+03,  -5.61944653e+03,
          -6.12140497e+03,  -6.64390903e+03,  -7.18695871e+03,
          -7.75055403e+03,  -8.33469497e+03,  -8.93938154e+03,
          -9.56461374e+03,  -1.02103916e+04,  -1.08767150e+04,
          -1.15635841e+04,  -1.22709988e+04,  -1.29989591e+04,
          -1.37474651e+04,  -1.45165167e+04,  -1.53061139e+04,
          -1.61162568e+04,  -1.69469452e+04,  -1.77981793e+04,
          -1.86699591e+04,  -1.95622844e+04,  -2.04751554e+04,
          -2.14085720e+04,  -2.23625343e+04,  -2.33370421e+04,
          -2.43320956e+04,  -2.53476947e+04,  -2.63838395e+04])},
 'name': {0: 'Australian Grand Prix', 1: 'Australian Grand Prix'},
 'stint': {0: 1.0, 1: 2.0},
 'tyre': {0: u'Super soft', 1: u'Super soft'},
 'year': {0: 2016, 1: 2016}}

0 个答案:

没有答案