-->

How to use nested for loops for brute forcing comb

2019-08-26 08:02发布

问题:

I wish to do the following: 1) Try a variety of input combinations to search for a best result 2) Reset all arrays as they were before each loop of the code

Every variable I am working with is in an array such as f[0,1,2,3,...]

The issue is likely in the resetting variables after each pass part, as the first pass works fine, but the residuals of the first pass cause the following iterations to break early..

Here is the pseudo for my method. So very simple, likely an issue with how Python handles data (object oriented)..

index_save = index
for input1 in [0.1,0.2,0.3,...]
    for input2 in [10,20,30,...]
        for input3 in [-0.1,-0.2,-0.3,...]
            index = index_save  #To reset the index and thus all arrays
            while True:
                index = index + 1
                f[index] = *Function of inputs*
                result = *Function of f and inputs*
                if condition_met = true
                    break
            if result > result_best
                result_best = result
                inputs_best = [input1,input2,input3]

回答1:

It turns out the answer to my question is as follows.

Using nested for loops to brute force combinations works (obviously). Using the method I outlined in the question works to do so. The part that requires care, is making sure you have successfully reset all variables on each pass. That means variables that are integers will have to be reset manually. This is in contrast to how I could reset all arrays simply by resetting the index.

integer_save = integer
index_save = index
for input in range
    index = index_save
    integer = integer_save
    index = index + 1
    array[index] = (physics functions based on input)
    integer = (physics functions based on input)