在计算之前找到未知的“正确”值

时间:2013-10-29 12:25:11

标签: java algorithm

问题如下我有一个大或小的数字(可以是一个),我需要调整这个数字并通过一个caluclation。鉴于计算结果,它必须至少在第5个小数点上输出一定值。

所以我需要创建一个采用这个起始值的方法,尝试增加或减少它给定当前结果,直到我得到正确的结果。我做了一些尝试没有成功。

这是一个根本不会低调的例子,但它暗示了我的意思......(这只是一个小规模的测试案例)

   public class Test {
        public static void main(String[]args)
        {
            double ran = 100 + (int)(Math.random() * 100000.999999999);
            int count = 0;              
            double tmpPay = 3666.545;
            double top = tmpPay;
            double low = 0;             

            while ( tmpPay != ran )
            {
                if ( tmpPay > ran)
                {
                    if( low == 0)
                    {
                            tmpPay = top / 2;
                            top = tmpPay;
                    }
                    else
                    {
                        tmpPay = tmpPay + ((top - low) / 2);
                        top = tmpPay;
                    }        
                }           

                if (tmpPay  < ran)
                {
                    tmpPay = top * 1.5;
                    low = top;
                    top = tmpPay;                   
                }
            }
            System.out.println(" VAlue of RAN: " +ran + "----VALUE OF tmpPay: " + tmpPay + "---------- COUNTER: " + count);         
}

示例2使用更清晰的描述。这是我的解决方案..

guessingValue = firstImput;

while (amortization > tmpPV)
{
    guessingValue -= (decimal)1;
    //guessingVlue -- > blackbox
    amortization = blackboxResults;
}
 while (amortization < tmpPV)
{
    guessingValue += (decimal)0.00001;
    //guessingVlue -- > blackbox
    amortization = blackboxResults;
}

}

4 个答案:

答案 0 :(得分:1)

一种方法是将您的问题定义为local optimization task并使用本地优化程序(例如Brent的方法或来自Apache commons的Nelder Mead Simplex)。

这里的目标函数是所需值与黑匣子之间的距离。

答案 1 :(得分:1)

如果我理解正确,你有一个函数g(x)和一个值K,你想要找到x0使得g(x0)= K. 这相当于找到函数f(x)= g(x) - K的根,因为f(x0)== f(x0) - K == K - K == 0.

一个简单的算法是Newton's method

答案 2 :(得分:1)

如果尝试运行该程序,它将很容易处于无限循环,因为while条件(对于双值比较)几乎不能相等。 例如 有两个值如下:

double value1 = 3666.545 ;

double value2 = 3666.54500001 ;

value1 == value2为false。

即使这种价值观也不相同。

您最好定义偏差范围。

例如,如果| value1 - value2 | &LT; 0.005,然后打破while条件并打印随机数信息。

答案 3 :(得分:1)

正如我在上面的评论中已经提到的,你不应该使用内置运算符来比较双打。这是您的代码无法正常工作的主要原因。第二个是在else子句中tmpPay = tmpPay +((top-low)/ 2);而不是tmpPay = tmpPay - ((top-low)/ 2);

完整的固定代码如下:

public class Test {
    private static final double EPSILON = 0.00001;
    public static boolean isEqual( double a, double b){

        return (Math.abs(a - b) < EPSILON);

    }


    public static void main(String[]args)
    {

        double ran = 100 + (int)(Math.random() * 100000.999999999);
        int count = 0;              
        double tmpPay = 3666.545;
        double top = tmpPay;
        double low = 0;             

        while ( !isEqual(tmpPay, ran))
        {
            if ( tmpPay > ran)
            {
                if( isEqual(low, 0.0))
                {
                        tmpPay = top / 2;
                        top = tmpPay;
                }
                else
                {
                    tmpPay = tmpPay - ((top - low) / 2);
                    top = tmpPay;
                }        
            }           

            if (tmpPay  < ran)
            {
                tmpPay = top * 1.5;
                low = top;
                top = tmpPay;                   
            }
            System.out.println("RAN:"+ran+" tmpPay:"+tmpPay+" top:"+top+" low:"+low+" counter:"+count);
            count++;
        }
        System.out.println(" VAlue of RAN: " +ran + "----VALUE OF tmpPay: " + tmpPay + "---------- COUNTER: " + count);




    }
}
相关问题