Example #1
0
 public nleqreport()
 {
     _innerobj = new nleq.nleqreport();
 }
Example #2
0
 public nleqreport(nleq.nleqreport obj)
 {
     _innerobj = obj;
 }
        public static bool testnleq(bool silent)
        {
            bool result = new bool();
            bool waserrors = new bool();
            bool basicserrors = new bool();
            bool converror = new bool();
            bool othererrors = new bool();
            int n = 0;
            double[] x = new double[0];
            int i = 0;
            int k = 0;
            double v = 0;
            double flast = 0;
            double[] xlast = new double[0];
            bool firstrep = new bool();
            int nfunc = 0;
            int njac = 0;
            int itcnt = 0;
            nleq.nleqstate state = new nleq.nleqstate();
            nleq.nleqreport rep = new nleq.nleqreport();
            int pass = 0;
            int passcount = 0;
            double epsf = 0;
            double stpmax = 0;
            int i_ = 0;

            waserrors = false;
            basicserrors = false;
            converror = false;
            othererrors = false;
            
            //
            // Basic tests
            //
            // Test with Himmelblau's function (M):
            // * ability to find correct result
            // * ability to work after soft restart (restart after finish)
            // * ability to work after hard restart (restart in the middle of optimization)
            //
            passcount = 100;
            for(pass=0; pass<=passcount-1; pass++)
            {
                
                //
                // Ability to find correct result
                //
                x = new double[2];
                x[0] = 20*math.randomreal()-10;
                x[1] = 20*math.randomreal()-10;
                nleq.nleqcreatelm(2, 2, x, state);
                epsf = 1.0E-9;
                nleq.nleqsetcond(state, epsf, 0);
                while( nleq.nleqiteration(state) )
                {
                    testfunchbm(state);
                }
                nleq.nleqresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    basicserrors = basicserrors | (double)(math.sqr(x[0]*x[0]+x[1]-11)+math.sqr(x[0]+x[1]*x[1]-7))>(double)(math.sqr(epsf));
                }
                else
                {
                    basicserrors = true;
                }
                
                //
                // Ability to work after soft restart
                //
                x = new double[2];
                x[0] = 20*math.randomreal()-10;
                x[1] = 20*math.randomreal()-10;
                nleq.nleqcreatelm(2, 2, x, state);
                epsf = 1.0E-9;
                nleq.nleqsetcond(state, epsf, 0);
                while( nleq.nleqiteration(state) )
                {
                    testfunchbm(state);
                }
                nleq.nleqresults(state, ref x, rep);
                x = new double[2];
                x[0] = 20*math.randomreal()-10;
                x[1] = 20*math.randomreal()-10;
                nleq.nleqrestartfrom(state, x);
                while( nleq.nleqiteration(state) )
                {
                    testfunchbm(state);
                }
                nleq.nleqresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    basicserrors = basicserrors | (double)(math.sqr(x[0]*x[0]+x[1]-11)+math.sqr(x[0]+x[1]*x[1]-7))>(double)(math.sqr(epsf));
                }
                else
                {
                    basicserrors = true;
                }
                
                //
                // Ability to work after hard restart:
                // * stopping condition: small F
                // * StpMax is so small that we need about 10000 iterations to
                //   find solution (steps are small)
                // * choose random K significantly less that 9999
                // * iterate for some time, then break, restart optimization
                //
                x = new double[2];
                x[0] = 100;
                x[1] = 100;
                nleq.nleqcreatelm(2, 2, x, state);
                epsf = 1.0E-9;
                nleq.nleqsetcond(state, epsf, 0);
                nleq.nleqsetstpmax(state, 0.01);
                k = 1+math.randominteger(100);
                for(i=0; i<=k-1; i++)
                {
                    if( !nleq.nleqiteration(state) )
                    {
                        break;
                    }
                    testfunchbm(state);
                }
                x = new double[2];
                x[0] = 20*math.randomreal()-10;
                x[1] = 20*math.randomreal()-10;
                nleq.nleqrestartfrom(state, x);
                while( nleq.nleqiteration(state) )
                {
                    testfunchbm(state);
                }
                nleq.nleqresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    basicserrors = basicserrors | (double)(math.sqr(x[0]*x[0]+x[1]-11)+math.sqr(x[0]+x[1]*x[1]-7))>(double)(math.sqr(epsf));
                }
                else
                {
                    basicserrors = true;
                }
            }
            
            //
            // Basic tests
            //
            // Test with Himmelblau's function (1):
            // * ability to find correct result
            //
            passcount = 100;
            for(pass=0; pass<=passcount-1; pass++)
            {
                
                //
                // Ability to find correct result
                //
                x = new double[2];
                x[0] = 20*math.randomreal()-10;
                x[1] = 20*math.randomreal()-10;
                nleq.nleqcreatelm(2, 1, x, state);
                epsf = 1.0E-9;
                nleq.nleqsetcond(state, epsf, 0);
                while( nleq.nleqiteration(state) )
                {
                    testfunchb1(state);
                }
                nleq.nleqresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    basicserrors = basicserrors | (double)(math.sqr(x[0]*x[0]+x[1]-11)+math.sqr(x[0]+x[1]*x[1]-7))>(double)(epsf);
                }
                else
                {
                    basicserrors = true;
                }
            }
            
            //
            // Basic tests
            //
            // Ability to detect situation when we can't find minimum
            //
            passcount = 100;
            for(pass=0; pass<=passcount-1; pass++)
            {
                x = new double[2];
                x[0] = 20*math.randomreal()-10;
                x[1] = 20*math.randomreal()-10;
                nleq.nleqcreatelm(2, 3, x, state);
                epsf = 1.0E-9;
                nleq.nleqsetcond(state, epsf, 0);
                while( nleq.nleqiteration(state) )
                {
                    testfuncshbm(state);
                }
                nleq.nleqresults(state, ref x, rep);
                basicserrors = basicserrors | rep.terminationtype!=-4;
            }
            
            //
            // Test correctness of intermediate reports and final report:
            // * first report is starting point
            // * function value decreases on subsequent reports
            // * function value is correctly reported
            // * last report is final point
            // * NFunc and NJac are compared with values counted directly
            // * IterationsCount is compared with value counter directly
            //
            n = 2;
            x = new double[n];
            xlast = new double[n];
            x[0] = 20*math.randomreal()-10;
            x[1] = 20*math.randomreal()-10;
            xlast[0] = math.maxrealnumber;
            xlast[1] = math.maxrealnumber;
            nleq.nleqcreatelm(n, 2, x, state);
            nleq.nleqsetcond(state, 1.0E-6, 0);
            nleq.nleqsetxrep(state, true);
            firstrep = true;
            flast = math.maxrealnumber;
            nfunc = 0;
            njac = 0;
            itcnt = 0;
            while( nleq.nleqiteration(state) )
            {
                if( state.xupdated )
                {
                    
                    //
                    // first report must be starting point
                    //
                    if( firstrep )
                    {
                        for(i=0; i<=n-1; i++)
                        {
                            othererrors = othererrors | (double)(state.x[i])!=(double)(x[i]);
                        }
                        firstrep = false;
                    }
                    
                    //
                    // function value must decrease
                    //
                    othererrors = othererrors | (double)(state.f)>(double)(flast);
                    
                    //
                    // check correctness of function value
                    //
                    v = math.sqr(state.x[0]*state.x[0]+state.x[1]-11)+math.sqr(state.x[0]+state.x[1]*state.x[1]-7);
                    othererrors = othererrors | (double)(Math.Abs(v-state.f)/Math.Max(v, 1))>(double)(100*math.machineepsilon);
                    
                    //
                    // update info and continue
                    //
                    for(i_=0; i_<=n-1;i_++)
                    {
                        xlast[i_] = state.x[i_];
                    }
                    flast = state.f;
                    itcnt = itcnt+1;
                    continue;
                }
                if( state.needf )
                {
                    nfunc = nfunc+1;
                }
                if( state.needfij )
                {
                    nfunc = nfunc+1;
                    njac = njac+1;
                }
                testfunchbm(state);
            }
            nleq.nleqresults(state, ref x, rep);
            if( rep.terminationtype>0 )
            {
                othererrors = (othererrors | (double)(xlast[0])!=(double)(x[0])) | (double)(xlast[1])!=(double)(x[1]);
                v = math.sqr(x[0]*x[0]+x[1]-11)+math.sqr(x[0]+x[1]*x[1]-7);
                othererrors = othererrors | (double)(Math.Abs(flast-v)/Math.Max(v, 1))>(double)(100*math.machineepsilon);
            }
            else
            {
                converror = true;
            }
            othererrors = othererrors | rep.nfunc!=nfunc;
            othererrors = othererrors | rep.njac!=njac;
            othererrors = othererrors | rep.iterationscount!=itcnt-1;
            
            //
            // Test ability to set limit on algorithm steps
            //
            x = new double[2];
            xlast = new double[2];
            x[0] = 20*math.randomreal()+20;
            x[1] = 20*math.randomreal()+20;
            xlast[0] = x[0];
            xlast[1] = x[1];
            stpmax = 0.1+0.1*math.randomreal();
            epsf = 1.0E-9;
            nleq.nleqcreatelm(2, 3, x, state);
            nleq.nleqsetstpmax(state, stpmax);
            nleq.nleqsetcond(state, epsf, 0);
            nleq.nleqsetxrep(state, true);
            while( nleq.nleqiteration(state) )
            {
                if( state.needf | state.needfij )
                {
                    testfunchbm(state);
                }
                if( (state.needf | state.needfij) | state.xupdated )
                {
                    othererrors = othererrors | (double)(Math.Sqrt(math.sqr(state.x[0]-xlast[0])+math.sqr(state.x[1]-xlast[1])))>(double)(1.00001*stpmax);
                }
                if( state.xupdated )
                {
                    xlast[0] = state.x[0];
                    xlast[1] = state.x[1];
                }
            }
            
            //
            // end
            //
            waserrors = (basicserrors | converror) | othererrors;
            if( !silent )
            {
                System.Console.Write("TESTING NLEQ SOLVER");
                System.Console.WriteLine();
                System.Console.Write("BASIC FUNCTIONALITY:                      ");
                if( basicserrors )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("CONVERGENCE:                              ");
                if( converror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("OTHER PROPERTIES:                         ");
                if( othererrors )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                if( waserrors )
                {
                    System.Console.Write("TEST FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("TEST PASSED");
                    System.Console.WriteLine();
                }
                System.Console.WriteLine();
                System.Console.WriteLine();
            }
            result = !waserrors;
            return result;
        }