【问题标题】:Is there a way to calculate all possible combinations of multiple arrays?有没有办法计算多个数组的所有可能组合?
【发布时间】:2019-03-27 04:09:44
【问题描述】:

我试图找到四个不同参数(kc、tauI、tauC、tauD)的值,以优化控制器的性能。我希望我的程序在给定数字范围内的合理时间范围内测试尽可能多的组合。我需要帮助来弄清楚如何尝试给定范围内的所有参数组合。

我尝试过使用 itertools.combinations,但它似乎不适用于数组。这与其说是一个语法问题,不如说是一个使用哪种方法来解决这个问题的问题。

    kc = np.linspace (0.1 , 1, 10)
    tauI = np.linspace (0.1 , 1, 10)
    tauD = np.linspace (0.1 , 1, 10)
    tauC = np.linspace (0.1 , 1, 10)

    def performance(kc, tauI, tauD, tauC):

        # defining s
        s = control.tf([1, 0], [0, 1])

        # defining all combinations of parameters to test for controller 
        performance
        possible_combinations = itertools.combinations([kc, tauI, tauD, tauC])

        index = 0
        best_performance = 1000

        for i in possible_combinations:
            kc = possible_combinations[index][0]
            tauI = possible_combinations[index][1]
            tauD = possible_combinations[index][2]
            tauC = possible_combinations[index][3]

            # defining the transfer functions
            Gp = 1/(s**2 + s + 1)
            Gd = (s + 1)/(s**2 + s + 1)
            Gc = Kc * (1 + 1/(tauI*s) + (tauD * s)/(tauC * s + 1))

            # defining the system
            sys_D = Gd/(1 + Gp * Gc)
            sys_U = Gc/(1 + Gp * Gc)

            # calculate the performance

            total_performance = 0.

            # loop through csv files and calculate performance
            for filename in all_files:
                # import disturbance from csv
                T_i = pd.read_csv(filename, header = 0)
                T_i = T_i.values.reshape(1,60)
                # calculate output response
                Y = Y_func(sys_D, time_array, T_i)
                # calculate input response
                U = U_func(sys_U, time_array, Y)
                # calculate performance for this csv file
                perfect = perf(Y, U)
                # add the performance for this csv to the total performance
                total_performance += perfect

            # calculate the average performance
            average_perf = total_performance/len(all_files)

            # check if the performance for these parameters were better than 
            # previously run tests
            if average_perf < best_performance:
                best_performance = average_perf
                kept_kc = kc
                kept_tauI = tauI
                kept_tauD = tauD
                kept_tauC = tauC

【问题讨论】:

  • 你到底想要什么
  • 看起来你想要一个cartesian product
  • 看看 scipy.optimize.brute,它基于网格搜索优化功能。

标签: python numpy optimization combinations


【解决方案1】:

这将生成您似乎需要的东西。我不确定这是否比将代码放在 4 个 for 循环中更好。

kc = np.linspace (0.1 , 1, 10)
tauI = np.linspace (0.1 , 1, 10)
tauD = np.linspace (0.1 , 1, 10)
tauC = np.linspace (0.1 , 1, 10)

def possible_combinations():               
    for k in kc:                
        for ti in tauI:         
            for td in tauD:     
                for tc in tauC:   
                    yield k, ti, td, tc

for a,b,c,d in possible_combinations():    
    print( a, b, c, d )          

itertools.combinations 对一个列表中的 n 项进行所有组合。

from itertools import combinations  
for i, j in combinations([1,2,3,4], 2):   
    print(i,j)

out: 1 2
     1 3
     1 4
     2 3
     2 4
     3 4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 2015-11-10
    • 2021-12-15
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多