Exemple #1
0
 public minbleicreport()
 {
     _innerobj = new minbleic.minbleicreport();
 }
Exemple #2
0
 public minbleicreport(minbleic.minbleicreport obj)
 {
     _innerobj = obj;
 }
Exemple #3
0
 public mcpdstate()
 {
     states = new int[0];
     data = new double[0,0];
     ec = new double[0,0];
     bndl = new double[0,0];
     bndu = new double[0,0];
     c = new double[0,0];
     ct = new int[0];
     pw = new double[0];
     priorp = new double[0,0];
     bs = new minbleic.minbleicstate();
     br = new minbleic.minbleicreport();
     tmpp = new double[0];
     effectivew = new double[0];
     effectivebndl = new double[0];
     effectivebndu = new double[0];
     effectivec = new double[0,0];
     effectivect = new int[0];
     h = new double[0];
     p = new double[0,0];
 }
Exemple #4
0
 public override void init()
 {
     solver = new minbleic.minbleicstate();
     solverrep = new minbleic.minbleicreport();
     tmp0 = new double[0];
     tmp1 = new double[0];
     tmpi = new int[0];
 }
        /*************************************************************************
        This function tests preconditioning

        On failure sets Err to True (leaves it unchanged otherwise)
        *************************************************************************/
        private static void testpreconditioning(ref bool err)
        {
            int pass = 0;
            int n = 0;
            double[] x = new double[0];
            double[] x0 = new double[0];
            int i = 0;
            int k = 0;
            double[,] v = new double[0,0];
            double[,] c = new double[0,0];
            int[] ct = new int[0];
            double[] bl = new double[0];
            double[] bu = new double[0];
            double[] vd = new double[0];
            double[] d = new double[0];
            double[] units = new double[0];
            double[] s = new double[0];
            int cntb1 = 0;
            int cntb2 = 0;
            int cntg1 = 0;
            int cntg2 = 0;
            double epsg = 0;
            double[] diagh = new double[0];
            minbleic.minbleicstate state = new minbleic.minbleicstate();
            minbleic.minbleicreport rep = new minbleic.minbleicreport();
            int fkind = 0;
            int ckind = 0;
            int fk = 0;

            
            //
            // Preconditioner test 1.
            //
            // If
            // * B1 is default preconditioner with unit scale
            // * G1 is diagonal preconditioner based on approximate diagonal of Hessian matrix
            // * B2 is default preconditioner with non-unit scale S[i]=1/sqrt(h[i])
            // * G2 is scale-based preconditioner with non-unit scale S[i]=1/sqrt(h[i])
            // then B1 is worse than G1, B2 is worse than G2.
            // "Worse" means more iterations to converge.
            //
            // Test problem setup:
            // * f(x) = sum( ((i*i+1)^FK*x[i])^2, i=0..N-1)
            // * FK is either +1 or -1 (we try both to test different aspects of preconditioning)
            // * constraints:
            //   0) absent
            //   1) boundary only
            //   2) linear equality only
            //   3) combination of boundary and linear equality constraints
            //
            // N        - problem size
            // K        - number of repeated passes (should be large enough to average out random factors)
            //
            k = 30;
            epsg = 1.0E-10;
            for(n=5; n<=8; n++)
            {
                for(fkind=0; fkind<=1; fkind++)
                {
                    for(ckind=0; ckind<=3; ckind++)
                    {
                        fk = 1-2*fkind;
                        x = new double[n];
                        units = new double[n];
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 0;
                            units[i] = 1;
                        }
                        minbleic.minbleiccreate(n, x, state);
                        if( ckind==1 | ckind==3 )
                        {
                            bl = new double[n];
                            bu = new double[n];
                            for(i=0; i<=n-1; i++)
                            {
                                bl[i] = -1;
                                bu[i] = 1;
                            }
                            minbleic.minbleicsetbc(state, bl, bu);
                        }
                        if( ckind==2 | ckind==3 )
                        {
                            c = new double[1, n+1];
                            ct = new int[1];
                            ct[0] = math.randominteger(3)-1;
                            for(i=0; i<=n-1; i++)
                            {
                                c[0,i] = 2*math.randomreal()-1;
                            }
                            c[0,n] = 0;
                            minbleic.minbleicsetlc(state, c, ct, 1);
                        }
                        
                        //
                        // Test it with default preconditioner VS. perturbed diagonal preconditioner
                        //
                        minbleic.minbleicsetprecdefault(state);
                        minbleic.minbleicsetscale(state, units);
                        cntb1 = 0;
                        for(pass=0; pass<=k-1; pass++)
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = 2*math.randomreal()-1;
                            }
                            minbleic.minbleicrestartfrom(state, x);
                            while( minbleic.minbleiciteration(state) )
                            {
                                calciip2(state, n, fk);
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            cntb1 = cntb1+rep.inneriterationscount;
                            err = err | rep.terminationtype<=0;
                        }
                        diagh = new double[n];
                        for(i=0; i<=n-1; i++)
                        {
                            diagh[i] = 2*Math.Pow(i*i+1, 2*fk)*(0.8+0.4*math.randomreal());
                        }
                        minbleic.minbleicsetprecdiag(state, diagh);
                        minbleic.minbleicsetscale(state, units);
                        cntg1 = 0;
                        for(pass=0; pass<=k-1; pass++)
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = 2*math.randomreal()-1;
                            }
                            minbleic.minbleicrestartfrom(state, x);
                            while( minbleic.minbleiciteration(state) )
                            {
                                calciip2(state, n, fk);
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            cntg1 = cntg1+rep.inneriterationscount;
                            err = err | rep.terminationtype<=0;
                        }
                        err = err | cntb1<cntg1;
                        
                        //
                        // Test it with scale-based preconditioner
                        //
                        s = new double[n];
                        for(i=0; i<=n-1; i++)
                        {
                            s[i] = 1/Math.Sqrt(2*Math.Pow(i*i+1, 2*fk)*(0.8+0.4*math.randomreal()));
                        }
                        minbleic.minbleicsetprecdefault(state);
                        minbleic.minbleicsetscale(state, s);
                        cntb2 = 0;
                        for(pass=0; pass<=k-1; pass++)
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = 2*math.randomreal()-1;
                            }
                            minbleic.minbleicrestartfrom(state, x);
                            while( minbleic.minbleiciteration(state) )
                            {
                                calciip2(state, n, fk);
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            cntb2 = cntb2+rep.inneriterationscount;
                            err = err | rep.terminationtype<=0;
                        }
                        minbleic.minbleicsetprecscale(state);
                        minbleic.minbleicsetscale(state, s);
                        cntg2 = 0;
                        for(pass=0; pass<=k-1; pass++)
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = 2*math.randomreal()-1;
                            }
                            minbleic.minbleicrestartfrom(state, x);
                            while( minbleic.minbleiciteration(state) )
                            {
                                calciip2(state, n, fk);
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            cntg2 = cntg2+rep.inneriterationscount;
                            err = err | rep.terminationtype<=0;
                        }
                        err = err | cntb2<cntg2;
                    }
                }
            }
        }
        /*************************************************************************
        This function tests convergence properties.
        We solve several simple problems with different combinations of constraints

        On failure sets Err to True (leaves it unchanged otherwise)
        *************************************************************************/
        private static void testconv(ref bool err)
        {
            int passcount = 0;
            int pass = 0;
            double[] bl = new double[0];
            double[] bu = new double[0];
            double[] x = new double[0];
            double[,] c = new double[0,0];
            int[] ct = new int[0];
            minbleic.minbleicstate state = new minbleic.minbleicstate();
            double epsc = 0;
            double epsg = 0;
            double tol = 0;
            minbleic.minbleicreport rep = new minbleic.minbleicreport();

            epsc = 1.0E-4;
            epsg = 1.0E-8;
            tol = 0.001;
            passcount = 10;
            
            //
            // Three closely connected problems:
            // * 2-dimensional space
            // * octagonal area bounded by:
            //   * -1<=x<=+1
            //   * -1<=y<=+1
            //   * x+y<=1.5
            //   * x-y<=1.5
            //   * -x+y<=1.5
            //   * -x-y<=1.5
            // * several target functions:
            //   * f0=x+0.001*y, minimum at x=-1, y=-0.5
            //   * f1=(x+10)^2+y^2, minimum at x=-1, y=0
            //   * f2=(x+10)^2+(y-0.6)^2, minimum at x=-1, y=0.5
            //
            x = new double[2];
            bl = new double[2];
            bu = new double[2];
            c = new double[4, 3];
            ct = new int[4];
            bl[0] = -1;
            bl[1] = -1;
            bu[0] = 1;
            bu[1] = 1;
            c[0,0] = 1;
            c[0,1] = 1;
            c[0,2] = 1.5;
            ct[0] = -1;
            c[1,0] = 1;
            c[1,1] = -1;
            c[1,2] = 1.5;
            ct[1] = -1;
            c[2,0] = -1;
            c[2,1] = 1;
            c[2,2] = 1.5;
            ct[2] = -1;
            c[3,0] = -1;
            c[3,1] = -1;
            c[3,2] = 1.5;
            ct[3] = -1;
            for(pass=1; pass<=passcount; pass++)
            {
                
                //
                // f0
                //
                x[0] = 0.2*math.randomreal()-0.1;
                x[1] = 0.2*math.randomreal()-0.1;
                minbleic.minbleiccreate(2, x, state);
                minbleic.minbleicsetbc(state, bl, bu);
                minbleic.minbleicsetlc(state, c, ct, 4);
                minbleic.minbleicsetinnercond(state, epsg, 0, 0);
                minbleic.minbleicsetoutercond(state, epsc, epsc);
                while( minbleic.minbleiciteration(state) )
                {
                    if( state.needfg )
                    {
                        state.f = state.x[0]+0.001*state.x[1];
                        state.g[0] = 1;
                        state.g[1] = 0.001;
                    }
                }
                minbleic.minbleicresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    err = err | (double)(Math.Abs(x[0]+1))>(double)(tol);
                    err = err | (double)(Math.Abs(x[1]+0.5))>(double)(tol);
                }
                else
                {
                    err = true;
                }
                
                //
                // f1
                //
                x[0] = 0.2*math.randomreal()-0.1;
                x[1] = 0.2*math.randomreal()-0.1;
                minbleic.minbleiccreate(2, x, state);
                minbleic.minbleicsetbc(state, bl, bu);
                minbleic.minbleicsetlc(state, c, ct, 4);
                minbleic.minbleicsetinnercond(state, epsg, 0, 0);
                minbleic.minbleicsetoutercond(state, epsc, epsc);
                while( minbleic.minbleiciteration(state) )
                {
                    if( state.needfg )
                    {
                        state.f = math.sqr(state.x[0]+10)+math.sqr(state.x[1]);
                        state.g[0] = 2*(state.x[0]+10);
                        state.g[1] = 2*state.x[1];
                    }
                }
                minbleic.minbleicresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    err = err | (double)(Math.Abs(x[0]+1))>(double)(tol);
                    err = err | (double)(Math.Abs(x[1]))>(double)(tol);
                }
                else
                {
                    err = true;
                }
                
                //
                // f2
                //
                x[0] = 0.2*math.randomreal()-0.1;
                x[1] = 0.2*math.randomreal()-0.1;
                minbleic.minbleiccreate(2, x, state);
                minbleic.minbleicsetbc(state, bl, bu);
                minbleic.minbleicsetlc(state, c, ct, 4);
                minbleic.minbleicsetinnercond(state, epsg, 0, 0);
                minbleic.minbleicsetoutercond(state, epsc, epsc);
                while( minbleic.minbleiciteration(state) )
                {
                    if( state.needfg )
                    {
                        state.f = math.sqr(state.x[0]+10)+math.sqr(state.x[1]-0.6);
                        state.g[0] = 2*(state.x[0]+10);
                        state.g[1] = 2*(state.x[1]-0.6);
                    }
                }
                minbleic.minbleicresults(state, ref x, rep);
                if( rep.terminationtype>0 )
                {
                    err = err | (double)(Math.Abs(x[0]+1))>(double)(tol);
                    err = err | (double)(Math.Abs(x[1]-0.5))>(double)(tol);
                }
                else
                {
                    err = true;
                }
            }
        }
        /*************************************************************************
        This function additional properties.

        On failure sets Err to True (leaves it unchanged otherwise)
        *************************************************************************/
        private static void testother(ref bool err)
        {
            int passcount = 0;
            int pass = 0;
            int n = 0;
            int nmax = 0;
            int i = 0;
            double[] bl = new double[0];
            double[] bu = new double[0];
            double[] x = new double[0];
            double[] xf = new double[0];
            double[] xlast = new double[0];
            double[] a = new double[0];
            double[] s = new double[0];
            double[] h = new double[0];
            double[,] c = new double[0,0];
            int[] ct = new int[0];
            double fprev = 0;
            double xprev = 0;
            double stpmax = 0;
            double v = 0;
            int pkind = 0;
            int ckind = 0;
            int mkind = 0;
            double vc = 0;
            double vm = 0;
            minbleic.minbleicstate state = new minbleic.minbleicstate();
            double epsc = 0;
            double epsg = 0;
            double tmpeps = 0;
            minbleic.minbleicreport rep = new minbleic.minbleicreport();
            double diffstep = 0;
            int dkind = 0;
            bool wasf = new bool();
            bool wasfg = new bool();
            double r = 0;
            int i_ = 0;

            nmax = 5;
            epsc = 1.0E-4;
            epsg = 1.0E-8;
            passcount = 10;
            for(pass=1; pass<=passcount; pass++)
            {
                
                //
                // Test reports:
                // * first value must be starting point
                // * last value must be last point
                //
                n = 50;
                x = new double[n];
                xlast = new double[n];
                bl = new double[n];
                bu = new double[n];
                for(i=0; i<=n-1; i++)
                {
                    x[i] = 10;
                    bl[i] = 2*math.randomreal()-1;
                    bu[i] = Double.PositiveInfinity;
                }
                minbleic.minbleiccreate(n, x, state);
                minbleic.minbleicsetbc(state, bl, bu);
                minbleic.minbleicsetinnercond(state, 0, 0, 0);
                minbleic.minbleicsetmaxits(state, 10);
                minbleic.minbleicsetoutercond(state, 1.0E-64, 1.0E-64);
                minbleic.minbleicsetxrep(state, true);
                fprev = math.maxrealnumber;
                while( minbleic.minbleiciteration(state) )
                {
                    if( state.needfg )
                    {
                        state.f = 0;
                        for(i=0; i<=n-1; i++)
                        {
                            state.f = state.f+math.sqr((1+i)*state.x[i]);
                            state.g[i] = 2*(1+i)*state.x[i];
                        }
                    }
                    if( state.xupdated )
                    {
                        if( (double)(fprev)==(double)(math.maxrealnumber) )
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                err = err | (double)(state.x[i])!=(double)(x[i]);
                            }
                        }
                        fprev = state.f;
                        for(i_=0; i_<=n-1;i_++)
                        {
                            xlast[i_] = state.x[i_];
                        }
                    }
                }
                minbleic.minbleicresults(state, ref x, rep);
                for(i=0; i<=n-1; i++)
                {
                    err = err | (double)(x[i])!=(double)(xlast[i]);
                }
                
                //
                // Test differentiation vs. analytic gradient
                // (first one issues NeedF requests, second one issues NeedFG requests)
                //
                n = 50;
                diffstep = 1.0E-6;
                for(dkind=0; dkind<=1; dkind++)
                {
                    x = new double[n];
                    xlast = new double[n];
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 1;
                    }
                    if( dkind==0 )
                    {
                        minbleic.minbleiccreate(n, x, state);
                    }
                    if( dkind==1 )
                    {
                        minbleic.minbleiccreatef(n, x, diffstep, state);
                    }
                    minbleic.minbleicsetinnercond(state, 1.0E-10, 0, 0);
                    minbleic.minbleicsetoutercond(state, 1.0E-6, 1.0E-6);
                    wasf = false;
                    wasfg = false;
                    while( minbleic.minbleiciteration(state) )
                    {
                        if( state.needf | state.needfg )
                        {
                            state.f = 0;
                        }
                        for(i=0; i<=n-1; i++)
                        {
                            if( state.needf | state.needfg )
                            {
                                state.f = state.f+math.sqr((1+i)*state.x[i]);
                            }
                            if( state.needfg )
                            {
                                state.g[i] = 2*(1+i)*state.x[i];
                            }
                        }
                        wasf = wasf | state.needf;
                        wasfg = wasfg | state.needfg;
                    }
                    minbleic.minbleicresults(state, ref x, rep);
                    if( dkind==0 )
                    {
                        err = (err | wasf) | !wasfg;
                    }
                    if( dkind==1 )
                    {
                        err = (err | !wasf) | wasfg;
                    }
                }
                
                //
                // Test that numerical differentiation uses scaling.
                //
                // In order to test that we solve simple optimization
                // problem: min(x^2) with initial x equal to 0.0.
                //
                // We choose random DiffStep and S, then we check that
                // optimizer evaluates function at +-DiffStep*S only.
                //
                x = new double[1];
                s = new double[1];
                diffstep = math.randomreal()*1.0E-6;
                s[0] = Math.Exp(math.randomreal()*4-2);
                x[0] = 0;
                minbleic.minbleiccreatef(1, x, diffstep, state);
                minbleic.minbleicsetinnercond(state, 1.0E-6, 0, 0);
                minbleic.minbleicsetscale(state, s);
                v = 0;
                while( minbleic.minbleiciteration(state) )
                {
                    state.f = math.sqr(state.x[0]);
                    v = Math.Max(v, Math.Abs(state.x[0]));
                }
                minbleic.minbleicresults(state, ref x, rep);
                r = v/(s[0]*diffstep);
                err = err | (double)(Math.Abs(Math.Log(r)))>(double)(Math.Log(1+1000*math.machineepsilon));
                
                //
                // Test stpmax
                //
                n = 1;
                x = new double[n];
                bl = new double[n];
                bu = new double[n];
                x[0] = 100;
                bl[0] = 2*math.randomreal()-1;
                bu[0] = Double.PositiveInfinity;
                stpmax = 0.05+0.05*math.randomreal();
                minbleic.minbleiccreate(n, x, state);
                minbleic.minbleicsetbc(state, bl, bu);
                minbleic.minbleicsetinnercond(state, epsg, 0, 0);
                minbleic.minbleicsetoutercond(state, epsc, epsc);
                minbleic.minbleicsetxrep(state, true);
                minbleic.minbleicsetstpmax(state, stpmax);
                xprev = x[0];
                while( minbleic.minbleiciteration(state) )
                {
                    if( state.needfg )
                    {
                        state.f = Math.Exp(state.x[0])+Math.Exp(-state.x[0]);
                        state.g[0] = Math.Exp(state.x[0])-Math.Exp(-state.x[0]);
                        err = err | (double)(Math.Abs(state.x[0]-xprev))>(double)((1+Math.Sqrt(math.machineepsilon))*stpmax);
                    }
                    if( state.xupdated )
                    {
                        err = err | (double)(Math.Abs(state.x[0]-xprev))>(double)((1+Math.Sqrt(math.machineepsilon))*stpmax);
                        xprev = state.x[0];
                    }
                }
                
                //
                // Ability to solve problems with function which is unbounded from below
                //
                n = 1;
                x = new double[n];
                bl = new double[n];
                bu = new double[n];
                bl[0] = 4*math.randomreal()+1;
                bu[0] = bl[0]+1;
                x[0] = 0.5*(bl[0]+bu[0]);
                minbleic.minbleiccreate(n, x, state);
                minbleic.minbleicsetbc(state, bl, bu);
                minbleic.minbleicsetinnercond(state, epsg, 0, 0);
                minbleic.minbleicsetoutercond(state, epsc, epsc);
                while( minbleic.minbleiciteration(state) )
                {
                    if( state.needfg )
                    {
                        state.f = -(1.0E8*math.sqr(state.x[0]));
                        state.g[0] = -(2.0E8*state.x[0]);
                    }
                }
                minbleic.minbleicresults(state, ref x, rep);
                err = err | (double)(Math.Abs(x[0]-bu[0]))>(double)(epsc);
                
                //
                // Test correctness of the scaling:
                // * initial point is random point from [+1,+2]^N
                // * f(x) = SUM(A[i]*x[i]^4), C[i] is random from [0.01,100]
                // * function is EFFECTIVELY unconstrained; it has formal constraints,
                //   but they are inactive at the solution; we try different variants
                //   in order to explore different control paths of the optimizer:
                //   0) absense of constraints
                //   1) bound constraints -100000<=x[i]<=100000
                //   2) one linear constraint 0*x=0
                //   3) combination of (1) and (2)
                // * we use random scaling matrix
                // * we test different variants of the preconditioning:
                //   0) unit preconditioner
                //   1) random diagonal from [0.01,100]
                //   2) scale preconditioner
                // * we set very mild outer stopping conditions - OuterEpsX=1.0, but
                //   inner conditions are very stringent
                // * and we test that in the extremum inner stopping conditions are
                //   satisfied subject to the current scaling coefficients.
                //
                tmpeps = 1.0E-10;
                for(n=1; n<=10; n++)
                {
                    for(ckind=0; ckind<=3; ckind++)
                    {
                        for(pkind=0; pkind<=2; pkind++)
                        {
                            x = new double[n];
                            a = new double[n];
                            s = new double[n];
                            h = new double[n];
                            bl = new double[n];
                            bu = new double[n];
                            c = new double[1, n+1];
                            ct = new int[1];
                            ct[0] = 0;
                            c[0,n] = 0;
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = math.randomreal()+1;
                                bl[i] = -100000;
                                bu[i] = 100000;
                                c[0,i] = 0;
                                a[i] = Math.Exp(Math.Log(100)*(2*math.randomreal()-1));
                                s[i] = Math.Exp(Math.Log(100)*(2*math.randomreal()-1));
                                h[i] = Math.Exp(Math.Log(100)*(2*math.randomreal()-1));
                            }
                            minbleic.minbleiccreate(n, x, state);
                            if( ckind==1 | ckind==3 )
                            {
                                minbleic.minbleicsetbc(state, bl, bu);
                            }
                            if( ckind==2 | ckind==3 )
                            {
                                minbleic.minbleicsetlc(state, c, ct, 1);
                            }
                            if( pkind==1 )
                            {
                                minbleic.minbleicsetprecdiag(state, h);
                            }
                            if( pkind==2 )
                            {
                                minbleic.minbleicsetprecscale(state);
                            }
                            minbleic.minbleicsetinnercond(state, tmpeps, 0, 0);
                            minbleic.minbleicsetoutercond(state, 1.0, 1.0E-8);
                            minbleic.minbleicsetscale(state, s);
                            while( minbleic.minbleiciteration(state) )
                            {
                                if( state.needfg )
                                {
                                    state.f = 0;
                                    for(i=0; i<=n-1; i++)
                                    {
                                        state.f = state.f+a[i]*Math.Pow(state.x[i], 4);
                                        state.g[i] = 4*a[i]*Math.Pow(state.x[i], 3);
                                    }
                                }
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            if( rep.terminationtype<=0 )
                            {
                                err = true;
                                return;
                            }
                            v = 0;
                            for(i=0; i<=n-1; i++)
                            {
                                v = v+math.sqr(s[i]*4*a[i]*Math.Pow(x[i], 3));
                            }
                            v = Math.Sqrt(v);
                            err = err | (double)(v)>(double)(tmpeps);
                        }
                    }
                }
                
                //
                // Check correctness of the "trimming".
                //
                // Trimming is a technique which is used to help algorithm
                // cope with unbounded functions. In order to check this
                // technique we will try to solve following optimization
                // problem:
                //
                //     min f(x) subject to no constraints on X
                //            { 1/(1-x) + 1/(1+x) + c*x, if -0.999999<x<0.999999
                //     f(x) = {
                //            { M, if x<=-0.999999 or x>=0.999999
                //
                // where c is either 1.0 or 1.0E+6, M is either 1.0E8, 1.0E20 or +INF
                // (we try different combinations)
                //
                for(ckind=0; ckind<=1; ckind++)
                {
                    for(mkind=0; mkind<=2; mkind++)
                    {
                        
                        //
                        // Choose c and M
                        //
                        if( ckind==0 )
                        {
                            vc = 1.0;
                        }
                        if( ckind==1 )
                        {
                            vc = 1.0E+6;
                        }
                        if( mkind==0 )
                        {
                            vm = 1.0E+8;
                        }
                        if( mkind==1 )
                        {
                            vm = 1.0E+20;
                        }
                        if( mkind==2 )
                        {
                            vm = Double.PositiveInfinity;
                        }
                        
                        //
                        // Create optimizer, solve optimization problem
                        //
                        epsg = 1.0E-6*vc;
                        x = new double[1];
                        x[0] = 0.0;
                        minbleic.minbleiccreate(1, x, state);
                        minbleic.minbleicsetinnercond(state, epsg, 0, 0);
                        minbleic.minbleicsetoutercond(state, 1.0E-6, 1.0E-6);
                        while( minbleic.minbleiciteration(state) )
                        {
                            if( state.needfg )
                            {
                                if( (double)(-0.999999)<(double)(state.x[0]) & (double)(state.x[0])<(double)(0.999999) )
                                {
                                    state.f = 1/(1-state.x[0])+1/(1+state.x[0])+vc*state.x[0];
                                    state.g[0] = 1/math.sqr(1-state.x[0])-1/math.sqr(1+state.x[0])+vc;
                                }
                                else
                                {
                                    state.f = vm;
                                }
                            }
                        }
                        minbleic.minbleicresults(state, ref x, rep);
                        if( rep.terminationtype<=0 )
                        {
                            err = true;
                            return;
                        }
                        err = err | (double)(Math.Abs(1/math.sqr(1-x[0])-1/math.sqr(1+x[0])+vc))>(double)(epsg);
                    }
                }
            }
        }
        /*************************************************************************
        This function test feasibility properties.
        It launches a sequence of problems and examines their solutions.
        Most of the attention is directed towards feasibility properties,
        although we make some quick checks to ensure that actual solution is found.

        On failure sets FeasErr (or ConvErr, depending on failure type) to True,
        or leaves it unchanged otherwise.

        IntErr is set to True on internal errors (errors in the control flow).
        *************************************************************************/
        private static void testfeasibility(ref bool feaserr,
            ref bool converr,
            ref bool interr)
        {
            int pkind = 0;
            int preckind = 0;
            int passcount = 0;
            int pass = 0;
            int n = 0;
            int nmax = 0;
            int i = 0;
            int j = 0;
            int k = 0;
            int p = 0;
            double v = 0;
            double v2 = 0;
            double v3 = 0;
            double vv = 0;
            double[] bl = new double[0];
            double[] bu = new double[0];
            double[] x = new double[0];
            double[] g = new double[0];
            double[] x0 = new double[0];
            double[] xs = new double[0];
            double[,] c = new double[0,0];
            int[] ct = new int[0];
            minbleic.minbleicstate state = new minbleic.minbleicstate();
            double epsc = 0;
            double epsg = 0;
            minbleic.minbleicreport rep = new minbleic.minbleicreport();
            int dkind = 0;
            double diffstep = 0;
            int i_ = 0;

            nmax = 5;
            epsc = 1.0E-4;
            epsg = 1.0E-8;
            passcount = 10;
            for(pass=1; pass<=passcount; pass++)
            {
                
                //
                // Test problem 1:
                // * no boundary and inequality constraints
                // * randomly generated plane as equality constraint
                // * random point (not necessarily on the plane)
                // * f = |x|^P, P = {2, 4} is used as target function
                // * preconditioner is chosen at random (we just want to be
                //   sure that preconditioning won't prevent us from converging
                //   to the feasible point):
                //   * unit preconditioner
                //   * random diagonal-based preconditioner
                //   * random scale-based preconditioner
                // * either analytic gradient or numerical differentiation are used
                // * we check that after work is over we are on the plane and
                //   that we are in the stationary point of constrained F
                //
                diffstep = 1.0E-6;
                for(dkind=0; dkind<=1; dkind++)
                {
                    for(preckind=0; preckind<=2; preckind++)
                    {
                        for(pkind=1; pkind<=2; pkind++)
                        {
                            for(n=1; n<=nmax; n++)
                            {
                                
                                //
                                // Generate X, BL, BU, CT and left part of C.
                                //
                                // Right part of C is generated using somewhat complex algo:
                                // * we generate random vector and multiply it by C.
                                // * result is used as the right part.
                                // * calculations are done on the fly, vector itself is not stored
                                // We use such algo to be sure that our system is consistent.
                                //
                                p = 2*pkind;
                                x = new double[n];
                                g = new double[n];
                                c = new double[1, n+1];
                                ct = new int[1];
                                c[0,n] = 0;
                                for(i=0; i<=n-1; i++)
                                {
                                    x[i] = 2*math.randomreal()-1;
                                    c[0,i] = 2*math.randomreal()-1;
                                    v = 2*math.randomreal()-1;
                                    c[0,n] = c[0,n]+c[0,i]*v;
                                }
                                ct[0] = 0;
                                
                                //
                                // Create and optimize
                                //
                                if( dkind==0 )
                                {
                                    minbleic.minbleiccreate(n, x, state);
                                }
                                if( dkind==1 )
                                {
                                    minbleic.minbleiccreatef(n, x, diffstep, state);
                                }
                                minbleic.minbleicsetlc(state, c, ct, 1);
                                minbleic.minbleicsetinnercond(state, epsg, 0.0, 0.0);
                                minbleic.minbleicsetoutercond(state, epsc, epsc);
                                setrandompreconditioner(state, n, preckind);
                                while( minbleic.minbleiciteration(state) )
                                {
                                    if( state.needf | state.needfg )
                                    {
                                        state.f = 0;
                                    }
                                    for(i=0; i<=n-1; i++)
                                    {
                                        if( state.needf | state.needfg )
                                        {
                                            state.f = state.f+Math.Pow(state.x[i], p);
                                        }
                                        if( state.needfg )
                                        {
                                            state.g[i] = p*Math.Pow(state.x[i], p-1);
                                        }
                                    }
                                }
                                minbleic.minbleicresults(state, ref x, rep);
                                if( rep.terminationtype<=0 )
                                {
                                    converr = true;
                                    return;
                                }
                                
                                //
                                // Test feasibility of solution
                                //
                                v = 0.0;
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    v += c[0,i_]*x[i_];
                                }
                                feaserr = feaserr | (double)(Math.Abs(v-c[0,n]))>(double)(epsc);
                                
                                //
                                // if C is nonzero, test that result is
                                // a stationary point of constrained F.
                                //
                                // NOTE: this check is done only if C is nonzero
                                //
                                vv = 0.0;
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    vv += c[0,i_]*c[0,i_];
                                }
                                if( (double)(vv)!=(double)(0) )
                                {
                                    
                                    //
                                    // Calculate gradient at the result
                                    // Project gradient into C
                                    // Check projected norm
                                    //
                                    for(i=0; i<=n-1; i++)
                                    {
                                        g[i] = p*Math.Pow(x[i], p-1);
                                    }
                                    v2 = 0.0;
                                    for(i_=0; i_<=n-1;i_++)
                                    {
                                        v2 += c[0,i_]*c[0,i_];
                                    }
                                    v = 0.0;
                                    for(i_=0; i_<=n-1;i_++)
                                    {
                                        v += c[0,i_]*g[i_];
                                    }
                                    vv = v/v2;
                                    for(i_=0; i_<=n-1;i_++)
                                    {
                                        g[i_] = g[i_] - vv*c[0,i_];
                                    }
                                    v3 = 0.0;
                                    for(i_=0; i_<=n-1;i_++)
                                    {
                                        v3 += g[i_]*g[i_];
                                    }
                                    converr = converr | (double)(Math.Sqrt(v3))>(double)(0.001);
                                }
                            }
                        }
                    }
                }
                
                //
                // Test problem 2 (multiple equality constraints):
                // * 1<=N<=NMax, 1<=K<=N
                // * no boundary constraints
                // * N-dimensional space
                // * randomly generated point xs
                // * K randomly generated hyperplanes which all pass through xs
                //   define K equality constraints: (a[k],x)=b[k]
                // * preconditioner is chosen at random (we just want to be
                //   sure that preconditioning won't prevent us from converging
                //   to the feasible point):
                //   * unit preconditioner
                //   * random diagonal-based preconditioner
                //   * random scale-based preconditioner
                // * f(x) = |x-x0|^2, x0 = xs+a[0]
                // * either analytic gradient or numerical differentiation are used
                // * extremum of f(x) is exactly xs because:
                //   * xs is the closest point in the plane defined by (a[0],x)=b[0]
                //   * xs is feasible by definition
                //
                diffstep = 1.0E-6;
                for(dkind=0; dkind<=1; dkind++)
                {
                    for(preckind=0; preckind<=2; preckind++)
                    {
                        for(n=2; n<=nmax; n++)
                        {
                            for(k=1; k<=n; k++)
                            {
                                
                                //
                                // Generate X, X0, XS, BL, BU, CT and left part of C.
                                //
                                // Right part of C is generated using somewhat complex algo:
                                // * we generate random vector and multiply it by C.
                                // * result is used as the right part.
                                // * calculations are done on the fly, vector itself is not stored
                                // We use such algo to be sure that our system is consistent.
                                //
                                p = 2*pkind;
                                x = new double[n];
                                x0 = new double[n];
                                xs = new double[n];
                                g = new double[n];
                                c = new double[k, n+1];
                                ct = new int[k];
                                c[0,n] = 0;
                                for(i=0; i<=n-1; i++)
                                {
                                    x[i] = 2*math.randomreal()-1;
                                    xs[i] = 2*math.randomreal()-1;
                                }
                                for(i=0; i<=k-1; i++)
                                {
                                    for(j=0; j<=n-1; j++)
                                    {
                                        c[i,j] = 2*math.randomreal()-1;
                                    }
                                    v = 0.0;
                                    for(i_=0; i_<=n-1;i_++)
                                    {
                                        v += c[i,i_]*xs[i_];
                                    }
                                    c[i,n] = v;
                                    ct[i] = 0;
                                }
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    x0[i_] = xs[i_];
                                }
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    x0[i_] = x0[i_] + c[0,i_];
                                }
                                
                                //
                                // Create and optimize
                                //
                                if( dkind==0 )
                                {
                                    minbleic.minbleiccreate(n, x, state);
                                }
                                if( dkind==1 )
                                {
                                    minbleic.minbleiccreatef(n, x, diffstep, state);
                                }
                                minbleic.minbleicsetlc(state, c, ct, k);
                                minbleic.minbleicsetinnercond(state, epsg, 0.0, 0.0);
                                minbleic.minbleicsetoutercond(state, epsc, epsc);
                                setrandompreconditioner(state, n, preckind);
                                while( minbleic.minbleiciteration(state) )
                                {
                                    if( state.needf | state.needfg )
                                    {
                                        state.f = 0;
                                    }
                                    for(i=0; i<=n-1; i++)
                                    {
                                        if( state.needf | state.needfg )
                                        {
                                            state.f = state.f+math.sqr(state.x[i]-x0[i]);
                                        }
                                        if( state.needfg )
                                        {
                                            state.g[i] = 2*(state.x[i]-x0[i]);
                                        }
                                    }
                                }
                                minbleic.minbleicresults(state, ref x, rep);
                                if( rep.terminationtype<=0 )
                                {
                                    converr = true;
                                    return;
                                }
                                
                                //
                                // check feasiblity properties
                                //
                                for(i=0; i<=k-1; i++)
                                {
                                    v = 0.0;
                                    for(i_=0; i_<=n-1;i_++)
                                    {
                                        v += c[i,i_]*x[i_];
                                    }
                                    feaserr = feaserr | (double)(Math.Abs(v-c[i,n]))>(double)(epsc);
                                }
                                
                                //
                                // Compare with XS
                                //
                                v = 0;
                                for(i=0; i<=n-1; i++)
                                {
                                    v = v+math.sqr(x[i]-xs[i]);
                                }
                                v = Math.Sqrt(v);
                                converr = converr | (double)(Math.Abs(v))>(double)(0.001);
                            }
                        }
                    }
                }
                
                //
                // Another simple problem:
                // * bound constraints 0 <= x[i] <= 1
                // * no linear constraints
                // * preconditioner is chosen at random (we just want to be
                //   sure that preconditioning won't prevent us from converging
                //   to the feasible point):
                //   * unit preconditioner
                //   * random diagonal-based preconditioner
                //   * random scale-based preconditioner
                // * F(x) = |x-x0|^P, where P={2,4} and x0 is randomly selected from [-1,+2]^N
                // * with such simple boundaries and function it is easy to find
                //   analytic form of solution: S[i] = bound(x0[i], 0, 1)
                // * we also check that both final solution and subsequent iterates
                //   are strictly feasible
                //
                diffstep = 1.0E-6;
                for(dkind=0; dkind<=1; dkind++)
                {
                    for(preckind=0; preckind<=2; preckind++)
                    {
                        for(pkind=1; pkind<=2; pkind++)
                        {
                            for(n=1; n<=nmax; n++)
                            {
                                
                                //
                                // Generate X, BL, BU.
                                //
                                p = 2*pkind;
                                bl = new double[n];
                                bu = new double[n];
                                x = new double[n];
                                x0 = new double[n];
                                for(i=0; i<=n-1; i++)
                                {
                                    bl[i] = 0;
                                    bu[i] = 1;
                                    x[i] = math.randomreal();
                                    x0[i] = 3*math.randomreal()-1;
                                }
                                
                                //
                                // Create and optimize
                                //
                                if( dkind==0 )
                                {
                                    minbleic.minbleiccreate(n, x, state);
                                }
                                if( dkind==1 )
                                {
                                    minbleic.minbleiccreatef(n, x, diffstep, state);
                                }
                                minbleic.minbleicsetbc(state, bl, bu);
                                minbleic.minbleicsetinnercond(state, epsg, 0.0, 0.0);
                                minbleic.minbleicsetoutercond(state, epsc, epsc);
                                setrandompreconditioner(state, n, preckind);
                                while( minbleic.minbleiciteration(state) )
                                {
                                    if( state.needf | state.needfg )
                                    {
                                        state.f = 0;
                                    }
                                    for(i=0; i<=n-1; i++)
                                    {
                                        if( state.needf | state.needfg )
                                        {
                                            state.f = state.f+Math.Pow(state.x[i]-x0[i], p);
                                        }
                                        if( state.needfg )
                                        {
                                            state.g[i] = p*Math.Pow(state.x[i]-x0[i], p-1);
                                        }
                                        feaserr = feaserr | (double)(state.x[i])<(double)(0.0);
                                        feaserr = feaserr | (double)(state.x[i])>(double)(1.0);
                                    }
                                }
                                minbleic.minbleicresults(state, ref x, rep);
                                if( rep.terminationtype<=0 )
                                {
                                    converr = true;
                                    return;
                                }
                                
                                //
                                // * compare solution with analytic one
                                // * check feasibility
                                //
                                for(i=0; i<=n-1; i++)
                                {
                                    converr = converr | (double)(Math.Abs(x[i]-apserv.boundval(x0[i], 0.0, 1.0)))>(double)(0.01);
                                    feaserr = feaserr | (double)(x[i])<(double)(0.0);
                                    feaserr = feaserr | (double)(x[i])>(double)(1.0);
                                }
                            }
                        }
                    }
                }
                
                //
                // Same as previous one, but with bound constraints posed
                // as general linear ones:
                // * no bound constraints
                // * 2*N linear constraints 0 <= x[i] <= 1
                // * preconditioner is chosen at random (we just want to be
                //   sure that preconditioning won't prevent us from converging
                //   to the feasible point):
                //   * unit preconditioner
                //   * random diagonal-based preconditioner
                //   * random scale-based preconditioner
                // * F(x) = |x-x0|^P, where P={2,4} and x0 is randomly selected from [-1,+2]^N
                // * with such simple constraints and function it is easy to find
                //   analytic form of solution: S[i] = bound(x0[i], 0, 1).
                // * however, we can't guarantee that solution is strictly feasible
                //   with respect to nonlinearity constraint, so we check
                //   for approximate feasibility.
                //
                for(preckind=0; preckind<=2; preckind++)
                {
                    for(pkind=1; pkind<=2; pkind++)
                    {
                        for(n=1; n<=nmax; n++)
                        {
                            
                            //
                            // Generate X, BL, BU.
                            //
                            p = 2*pkind;
                            x = new double[n];
                            x0 = new double[n];
                            c = new double[2*n, n+1];
                            ct = new int[2*n];
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = math.randomreal();
                                x0[i] = 3*math.randomreal()-1;
                                for(j=0; j<=n; j++)
                                {
                                    c[2*i+0,j] = 0;
                                    c[2*i+1,j] = 0;
                                }
                                c[2*i+0,i] = 1;
                                c[2*i+0,n] = 0;
                                ct[2*i+0] = 1;
                                c[2*i+1,i] = 1;
                                c[2*i+1,n] = 1;
                                ct[2*i+1] = -1;
                            }
                            
                            //
                            // Create and optimize
                            //
                            minbleic.minbleiccreate(n, x, state);
                            minbleic.minbleicsetlc(state, c, ct, 2*n);
                            minbleic.minbleicsetinnercond(state, epsg, 0.0, 0.0);
                            minbleic.minbleicsetoutercond(state, epsc, epsc);
                            setrandompreconditioner(state, n, preckind);
                            while( minbleic.minbleiciteration(state) )
                            {
                                if( state.needfg )
                                {
                                    state.f = 0;
                                    for(i=0; i<=n-1; i++)
                                    {
                                        state.f = state.f+Math.Pow(state.x[i]-x0[i], p);
                                        state.g[i] = p*Math.Pow(state.x[i]-x0[i], p-1);
                                    }
                                    continue;
                                }
                                
                                //
                                // Unknown protocol specified
                                //
                                interr = true;
                                return;
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            if( rep.terminationtype<=0 )
                            {
                                converr = true;
                                return;
                            }
                            
                            //
                            // * compare solution with analytic one
                            // * check feasibility
                            //
                            for(i=0; i<=n-1; i++)
                            {
                                converr = converr | (double)(Math.Abs(x[i]-apserv.boundval(x0[i], 0.0, 1.0)))>(double)(0.05);
                                feaserr = feaserr | (double)(x[i])<(double)(0.0-epsc);
                                feaserr = feaserr | (double)(x[i])>(double)(1.0+epsc);
                            }
                        }
                    }
                }
                
                //
                // Infeasible problem:
                // * all bound constraints are 0 <= x[i] <= 1 except for one
                // * that one is 0 >= x[i] >= 1
                // * no linear constraints
                // * preconditioner is chosen at random (we just want to be
                //   sure that preconditioning won't prevent us from detecting
                //   infeasible point):
                //   * unit preconditioner
                //   * random diagonal-based preconditioner
                //   * random scale-based preconditioner
                // * F(x) = |x-x0|^P, where P={2,4} and x0 is randomly selected from [-1,+2]^N
                // * algorithm must return correct error code on such problem
                //
                for(preckind=0; preckind<=2; preckind++)
                {
                    for(pkind=1; pkind<=2; pkind++)
                    {
                        for(n=1; n<=nmax; n++)
                        {
                            
                            //
                            // Generate X, BL, BU.
                            //
                            p = 2*pkind;
                            bl = new double[n];
                            bu = new double[n];
                            x = new double[n];
                            x0 = new double[n];
                            for(i=0; i<=n-1; i++)
                            {
                                bl[i] = 0;
                                bu[i] = 1;
                                x[i] = math.randomreal();
                                x0[i] = 3*math.randomreal()-1;
                            }
                            i = math.randominteger(n);
                            bl[i] = 1;
                            bu[i] = 0;
                            
                            //
                            // Create and optimize
                            //
                            minbleic.minbleiccreate(n, x, state);
                            minbleic.minbleicsetbc(state, bl, bu);
                            minbleic.minbleicsetinnercond(state, epsg, 0.0, 0.0);
                            minbleic.minbleicsetoutercond(state, epsc, epsc);
                            setrandompreconditioner(state, n, preckind);
                            while( minbleic.minbleiciteration(state) )
                            {
                                if( state.needfg )
                                {
                                    state.f = 0;
                                    for(i=0; i<=n-1; i++)
                                    {
                                        state.f = state.f+Math.Pow(state.x[i]-x0[i], p);
                                        state.g[i] = p*Math.Pow(state.x[i]-x0[i], p-1);
                                    }
                                    continue;
                                }
                                
                                //
                                // Unknown protocol specified
                                //
                                interr = true;
                                return;
                            }
                            minbleic.minbleicresults(state, ref x, rep);
                            feaserr = feaserr | rep.terminationtype!=-3;
                        }
                    }
                }
                
                //
                // Infeasible problem (2):
                // * no bound and inequality constraints
                // * 1<=K<=N arbitrary equality constraints
                // * (K+1)th constraint which is equal to the first constraint a*x=c,
                //   but with c:=c+1. I.e. we have both a*x=c and a*x=c+1, which can't
                //   be true (other constraints may be inconsistent too, but we don't
                //   have to check it).
                // * preconditioner is chosen at random (we just want to be
                //   sure that preconditioning won't prevent us from detecting
                //   infeasible point):
                //   * unit preconditioner
                //   * random diagonal-based preconditioner
                //   * random scale-based preconditioner
                // * F(x) = |x|^P, where P={2,4}
                // * algorithm must return correct error code on such problem
                //
                for(preckind=0; preckind<=2; preckind++)
                {
                    for(pkind=1; pkind<=2; pkind++)
                    {
                        for(n=1; n<=nmax; n++)
                        {
                            for(k=1; k<=n; k++)
                            {
                                
                                //
                                // Generate X, BL, BU.
                                //
                                p = 2*pkind;
                                x = new double[n];
                                c = new double[k+1, n+1];
                                ct = new int[k+1];
                                for(i=0; i<=n-1; i++)
                                {
                                    x[i] = math.randomreal();
                                }
                                for(i=0; i<=k-1; i++)
                                {
                                    for(j=0; j<=n; j++)
                                    {
                                        c[i,j] = 2*math.randomreal()-1;
                                    }
                                    ct[i] = 0;
                                }
                                ct[k] = 0;
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    c[k,i_] = c[0,i_];
                                }
                                c[k,n] = c[0,n]+1;
                                
                                //
                                // Create and optimize
                                //
                                minbleic.minbleiccreate(n, x, state);
                                minbleic.minbleicsetlc(state, c, ct, k+1);
                                minbleic.minbleicsetinnercond(state, epsg, 0.0, 0.0);
                                minbleic.minbleicsetoutercond(state, epsc, epsc);
                                setrandompreconditioner(state, n, preckind);
                                while( minbleic.minbleiciteration(state) )
                                {
                                    if( state.needfg )
                                    {
                                        state.f = 0;
                                        for(i=0; i<=n-1; i++)
                                        {
                                            state.f = state.f+Math.Pow(state.x[i], p);
                                            state.g[i] = p*Math.Pow(state.x[i], p-1);
                                        }
                                        continue;
                                    }
                                    
                                    //
                                    // Unknown protocol specified
                                    //
                                    interr = true;
                                    return;
                                }
                                minbleic.minbleicresults(state, ref x, rep);
                                feaserr = feaserr | rep.terminationtype!=-3;
                            }
                        }
                    }
                }
            }
        }
Exemple #9
0
 public override void init()
 {
     a = new cqmodels.convexquadraticmodel();
     sparsea = new sparse.sparsematrix();
     b = new double[0];
     bndl = new double[0];
     bndu = new double[0];
     s = new double[0];
     havebndl = new bool[0];
     havebndu = new bool[0];
     xorigin = new double[0];
     startx = new double[0];
     cleic = new double[0,0];
     sas = new sactivesets.sactiveset();
     gc = new double[0];
     xn = new double[0];
     pg = new double[0];
     workbndl = new double[0];
     workbndu = new double[0];
     workcleic = new double[0,0];
     xs = new double[0];
     tmp0 = new double[0];
     tmp1 = new double[0];
     tmpb = new bool[0];
     rctmpg = new double[0];
     tmpi = new int[0];
     estimator = new normestimator.normestimatorstate();
     solver = new minbleic.minbleicstate();
     solverrep = new minbleic.minbleicreport();
 }