如何使用嵌套的for循环进行强行强制组合-Python [英] How to use nested for loops for brute forcing combinations - Python

查看:213
本文介绍了如何使用嵌套的for循环进行强行强制组合-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望执行以下操作: 1)尝试各种输入组合以搜索最佳结果 2)重置所有数组,使其与每次代码循环之前一样

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

我正在使用的每个变量都在一个数组中,例如f [0,1,2,3,...]

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..

这是我的方法的伪.非常简单,可能是Python如何处理数据(面向对象)的问题.

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]

推荐答案

事实证明,我的问题的答案如下.

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

使用嵌套的for循环进行强力组合是有效的(显然).使用我在问题中概述的方法可以做到这一点.需要注意的部分是确保每次通过都成功重置所有变量.这意味着整数变量将必须手动重置.这与我仅通过重置索引即可重置所有数组的方式形成对比.

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)

这篇关于如何使用嵌套的for循环进行强行强制组合-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆