How to use nested for loops for brute forcing combinations - Python

三世轮回 提交于 2019-12-11 17:56:28

问题


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)


来源:https://stackoverflow.com/questions/53881221/how-to-use-nested-for-loops-for-brute-forcing-combinations-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!