使用递归函数调用优化excel

时间:2017-05-02 11:16:13

标签: excel vba excel-vba

我有一张旧的excel表,其中有几个(50+)输入参数和几个基于这些输入的中间计算值,然后产生最终的计算输出值。现在我想使用这个excel表找到最佳情况的粗略估计,如果我在给定范围内改变这些输入参数的4(可能更多)。所以我给了所有这些命名的范围:

inputArray = Split("Do_heatloop,Fin_addendum,t_fin,pitch_fin", ",")

我在列中放置了我想要优化的参数的范围,请参见下面的截图。

enter image description here

所以我的想法是使用递归方法来解决这个问题,但它没有正确地执行嵌套for循环,我缺少什么?

我的主要分:

Sub optimalize()
    Dim inputType As String
    Dim inputArray() As String
    Dim maxValue As Double
    Dim temp, temp2, i, lower, higher As Double
    Dim tempFormula As String
    maxValue = 0


    inputArray = Split("Do_heatloop,Fin_addendum,t_fin,pitch_fin", ",")


    Call CustomLoop(inputArray, 0)
 end sub

我的递归函数:

Function CustomLoop(inputArray() As String, nextIndex As Integer) As Double
    Dim inputType As String
    Dim maxValue As Double
    Dim temp, temp2, i, lower, higher As Double
    Dim tempFormula As String
    Dim numberItems As Integer

    maxValue = 0
    numberItems = 5

    inputType = inputArray(nextIndex)

    'Orginal parameter value
    tempFormula = Range(inputType).Formula

    'lower
    lower = Range(inputType).Offset(RowOffSet:=0, ColumnOffset:=2).Value 'Range lower boundary defined one column to right
    'higher
    higher = Range(inputType).Offset(RowOffSet:=0, ColumnOffset:=3).Value 'Range upper boundary defined two column to right


    For i = lower To higher Step (higher - lower) / (numberItems - 1)
        'change the input parameters
        Range(inputType).Value = i
        'change the output
        temp2 = Range("Output").Value
        If temp2 >= maxValue Then
            maxValue = temp2
            'Optimum value for that parameter stored 4 columns to the right
            Range(inputType).Offset(RowOffSet:=0, ColumnOffset:=4).Value = Range(inputType).Value
        End If
       Debug.Print inputType & " Value:" & i & " Output:" & Round(temp2, 2) & "W/kg Max value:" & Round(maxValue, 2) & "W/kg"
        If nextIndex < UBound(inputArray) Then
            nextIndex = nextIndex + 1
             Call CustomLoop(inputArray(), nextIndex) 'Range("Output").Value
        End If
    Next i

    Range(inputType).Formula = tempFormula



    CustomLoop = maxValue
End Function

输出结果为:

Do_heatloop Value:6 Output:0.62W/kg Max value:0.62W/kg
Fin_addendum Value:0 Output:0.62W/kg Max value:0.62W/kg
t_fin Value:0.2 Output:0.61W/kg Max value:0.61W/kg
pitch_fin Value:1 Output:1.21W/kg Max value:1.21W/kg
pitch_fin Value:25.75 Output:0.14W/kg Max value:1.21W/kg
pitch_fin Value:50.5 Output:0.13W/kg Max value:1.21W/kg
pitch_fin Value:75.25 Output:0.12W/kg Max value:1.21W/kg
pitch_fin Value:100 Output:0.12W/kg Max value:1.21W/kg
t_fin Value:0.275 Output:0.62W/kg Max value:0.62W/kg
t_fin Value:0.35 Output:0.62W/kg Max value:0.62W/kg
t_fin Value:0.425 Output:0.63W/kg Max value:0.63W/kg
Fin_addendum Value:15 Output:7.9W/kg Max value:7.9W/kg
Fin_addendum Value:30 Output:14.6W/kg Max value:14.6W/kg
Fin_addendum Value:45 Output:20.71W/kg Max value:20.71W/kg
Fin_addendum Value:60 Output:26.23W/kg Max value:26.23W/kg
Do_heatloop Value:13.25 Output:0.68W/kg Max value:0.68W/kg
Do_heatloop Value:20.5 Output:0.59W/kg Max value:0.68W/kg
Do_heatloop Value:27.75 Output:0.49W/kg Max value:0.68W/kg

但我希望:

Do_heatloop Value:6 Output:0.62W/kg Max value:0.62W/kg
Fin_addendum Value:0 Output:0.62W/kg Max value:0.62W/kg
t_fin Value:0.2 Output:0.61W/kg Max value:0.61W/kg
pitch_fin Value:1 Output:1.21W/kg Max value:1.21W/kg
pitch_fin Value:25.75 Output:0.14W/kg Max value:1.21W/kg
pitch_fin Value:50.5 Output:0.13W/kg Max value:1.21W/kg
pitch_fin Value:75.25 Output:0.12W/kg Max value:1.21W/kg
pitch_fin Value:100 Output:0.12W/kg Max value:1.21W/kg
t_fin Value:0.275 Output:0.62W/kg Max value:0.62W/kg
pitch_fin Value:1 Output:1.21W/kg Max value:1.21W/kg
pitch_fin Value:25.75 Output:0.14W/kg Max value:1.21W/kg
pitch_fin Value:50.5 Output:0.13W/kg Max value:1.21W/kg
pitch_fin Value:75.25 Output:0.12W/kg Max value:1.21W/kg
pitch_fin Value:100 Output:0.12W/kg Max value:1.21W/kg
.......................................................

找到最佳解决方案可能有更好的方法,但这似乎是一种快速而肮脏的方法来找到它而不是收集所有的公式,(有些在实验中)试图结合到一个巨大的长方程然后导出它对于所有输入参数,要求它等于零。

0 个答案:

没有答案