如何:根据不同的变量找到最佳结果(Brute-Force)

时间:2015-11-27 16:40:57

标签: java function math brute-force

我有以下变量:

int n = 0   //with an range from 10 - 200 and a increment of 1
int f = 0       //with an range from 0 - 7    and a increment of 1
double g = 0.0  //with an range from -10.0 - 10.0 and a increment of 0.001
double h = 0.0  //with an range from -10.0 - 10.0 and a increment of 0.001
double k = 0.0. //with an range from -5.0 - 5.0 and a increment of 0.1

现在我在Java中有以下程序结构:

private
int result = 0;

    forLoopNr1(){   

        //...
        //functions based on the variables: n 
        //...

        forLoopNr2(){   

        //...
        //functions based on the variables: f, g, h, k
        //...

        }
    }

    return result;
}

目前我手动更改变量并将它们视为常量。任何 给定范围内的变量的变化与给定的增量会影响结果。 问题是,我寻找最高的结果,不能尝试各种可能性。 (结果= n * f * g * h * k)并不是最好的结果是基于最高值。

对我而言,它看起来类似于密码锁。因此,Brute-Force算法可能是一条线索。

我需要的是一种改变变量的循环,以确保尝试所有可能性。 像:

private
int result = 0;

variableChangingLoop(){ 
    //changes n,f,g,h,k in order that every possibility is taken bevore loop is done

    forLoopNr1(){       

        //...
        //functions based on the variables: n 
        //...

        forLoopNr2(){   

        //...
        //functions based on the variables: f, g, h, k
        //...

        }
    }

    return result;
    }
}

我希望它清楚。我尽量保持这个例子的简单。

到目前为止我的解决方案:

   class Program
{
    static void Main(string[] args)
    {
        double g = -10.0;
        double h = -10.0;
        double k = -10.0;
        int f = 1;
        int n = 10;

        int counter = 0;

        try
        {
            while (n <= 200)
            {
                g = g + 0.1;

                if (g >= 10.0)
                {
                    g = -10.0;
                    h = h + 0.01;
                }

                if (h >= 10.0)
                {
                    h = -10.0;
                    k = k + 0.01;
                }

                if (k >= 10.0)
                {
                    k = -10.0;
                    f = f + 1;
                }

                if (f >= 7)
                {
                    f = 1;
                    n =  +1;
                }

                //here space for calculations and other functions

            }


        }

        catch
        {

        }
    }
}

我的问题是,有更好的方法吗?

0 个答案:

没有答案