Exemplo n.º 1
0
 public mincgreport()
 {
     _innerobj = new mincg.mincgreport();
 }
Exemplo n.º 2
0
 public mincgreport(mincg.mincgreport obj)
 {
     _innerobj = obj;
 }
    public static int Main(string[] args)
    {
        int n = 0;
        mincg.mincgstate state = new mincg.mincgstate();
        mincg.mincgreport rep = new mincg.mincgreport();
        double[] s = new double[0];
        double x = 0;
        double y = 0;

        
        //
        // Function minimized:
        //     F = exp(x-1) + exp(1-x) + (y-x)^2
        // N = 2 - task dimension.
        //
        // Take a look at MinCGSetStpMax() call - it prevents us
        // from overflow (which may be result of too large step).
        // Try to comment it and see what will happen.
        //
        n = 2;
        s = new double[2];
        s[0] = 10;
        s[1] = AP.Math.RandomReal()-0.5;
        mincg.mincgcreate(n, ref s, ref state);
        mincg.mincgsetcond(ref state, 0.0, 0.0, 0.0001, 0);
        mincg.mincgsetxrep(ref state, true);
        mincg.mincgsetstpmax(ref state, 1.0);
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.Write("F = exp(x-1) + exp(1-x) + (y-x)^2");
        System.Console.WriteLine();
        System.Console.Write("OPTIMIZATION STARTED");
        System.Console.WriteLine();
        while( mincg.mincgiteration(ref state) )
        {
            if( state.needfg )
            {
                x = state.x[0];
                y = state.x[1];
                state.f = Math.Exp(x-1)+Math.Exp(1-x)+AP.Math.Sqr(y-x);
                state.g[0] = Math.Exp(x-1)-Math.Exp(1-x)+2*(x-y);
                state.g[1] = 2*(y-x);
            }
            if( state.xupdated )
            {
                System.Console.Write("    F(");
                System.Console.Write("{0,8:F5}",state.x[0]);
                System.Console.Write(",");
                System.Console.Write("{0,8:F5}",state.x[1]);
                System.Console.Write(")=");
                System.Console.Write("{0,0:F5}",state.f);
                System.Console.WriteLine();
            }
        }
        System.Console.Write("OPTIMIZATION STOPPED");
        System.Console.WriteLine();
        mincg.mincgresults(ref state, ref s, ref rep);
        
        //
        // output results
        //
        System.Console.Write("X = ");
        System.Console.Write("{0,4:F2}",s[0]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.Write("Y = ");
        System.Console.Write("{0,4:F2}",s[1]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.WriteLine();
        return 0;
    }
Exemplo n.º 4
0
 public minbleicstate()
 {
     diaghoriginal = new double[0];
     diagh = new double[0];
     x = new double[0];
     g = new double[0];
     rstate = new rcommstate();
     xcur = new double[0];
     xprev = new double[0];
     xstart = new double[0];
     xend = new double[0];
     lastg = new double[0];
     ceoriginal = new double[0,0];
     ceeffective = new double[0,0];
     cecurrent = new double[0,0];
     ct = new int[0];
     xe = new double[0];
     hasbndl = new bool[0];
     hasbndu = new bool[0];
     bndloriginal = new double[0];
     bnduoriginal = new double[0];
     bndleffective = new double[0];
     bndueffective = new double[0];
     activeconstraints = new bool[0];
     constrainedvalues = new double[0];
     transforms = new double[0];
     seffective = new double[0];
     soriginal = new double[0];
     w = new double[0];
     tmp0 = new double[0];
     tmp1 = new double[0];
     tmp2 = new double[0];
     r = new double[0];
     lmmatrix = new double[0,0];
     cgstate = new mincg.mincgstate();
     cgrep = new mincg.mincgreport();
 }
Exemplo n.º 5
0
        /*************************************************************************
        Other properties
        *************************************************************************/
        public static void testother(ref bool err)
        {
            int n = 0;
            double[] x = new double[0];
            double[] s = new double[0];
            double[] a = new double[0];
            double[] h = new double[0];
            double[] xlast = new double[0];
            double fprev = 0;
            double xprev = 0;
            double stpmax = 0;
            int i = 0;
            mincg.mincgstate state = new mincg.mincgstate();
            mincg.mincgreport rep = new mincg.mincgreport();
            int cgtype = 0;
            double tmpeps = 0;
            double epsg = 0;
            double v = 0;
            double r = 0;
            bool hasxlast = new bool();
            double lastscaledstep = 0;
            int pkind = 0;
            int ckind = 0;
            int mkind = 0;
            int dkind = 0;
            double diffstep = 0;
            double vc = 0;
            double vm = 0;
            bool wasf = new bool();
            bool wasfg = new bool();
            int i_ = 0;

            for(cgtype=-1; cgtype<=1; cgtype++)
            {
                
                //
                // Test reports (F should form monotone sequence)
                //
                n = 50;
                x = new double[n];
                xlast = new double[n];
                for(i=0; i<=n-1; i++)
                {
                    x[i] = 1;
                }
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 0, 0, 0, 100);
                mincg.mincgsetxrep(state, true);
                fprev = math.maxrealnumber;
                while( mincg.mincgiteration(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 )
                    {
                        err = err | (double)(state.f)>(double)(fprev);
                        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_];
                        }
                    }
                }
                mincg.mincgresults(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 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( dkind==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcond(state, 0, 0, 0, n/2);
                    wasf = false;
                    wasfg = false;
                    while( mincg.mincgiteration(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;
                    }
                    mincg.mincgresults(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;
                mincg.mincgcreatef(1, x, diffstep, state);
                mincg.mincgsetcond(state, 1.0E-6, 0, 0, 0);
                mincg.mincgsetscale(state, s);
                v = 0;
                while( mincg.mincgiteration(state) )
                {
                    state.f = math.sqr(state.x[0]);
                    v = Math.Max(v, Math.Abs(state.x[0]));
                }
                mincg.mincgresults(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 maximum step
                //
                n = 1;
                x = new double[n];
                x[0] = 100;
                stpmax = 0.05+0.05*math.randomreal();
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 1.0E-9, 0, 0, 0);
                mincg.mincgsetstpmax(state, stpmax);
                mincg.mincgsetxrep(state, true);
                xprev = x[0];
                while( mincg.mincgiteration(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];
                    }
                }
                
                //
                // 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]
                // * 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 stringent stopping conditions (we try EpsG and EpsX)
                // * and we test that in the extremum stopping conditions are
                //   satisfied subject to the current scaling coefficients.
                //
                tmpeps = 1.0E-10;
                for(n=1; n<=10; n++)
                {
                    for(pkind=0; pkind<=2; pkind++)
                    {
                        x = new double[n];
                        xlast = new double[n];
                        a = new double[n];
                        s = new double[n];
                        h = new double[n];
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = math.randomreal()+1;
                            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));
                        }
                        mincg.mincgcreate(n, x, state);
                        mincg.mincgsetscale(state, s);
                        mincg.mincgsetxrep(state, true);
                        if( pkind==1 )
                        {
                            mincg.mincgsetprecdiag(state, h);
                        }
                        if( pkind==2 )
                        {
                            mincg.mincgsetprecscale(state);
                        }
                        
                        //
                        // Test gradient-based stopping condition
                        //
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = math.randomreal()+1;
                        }
                        mincg.mincgsetcond(state, tmpeps, 0, 0, 0);
                        mincg.mincgrestartfrom(state, x);
                        while( mincg.mincgiteration(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);
                                }
                            }
                        }
                        mincg.mincgresults(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);
                        
                        //
                        // Test step-based stopping condition
                        //
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = math.randomreal()+1;
                        }
                        hasxlast = false;
                        mincg.mincgsetcond(state, 0, 0, tmpeps, 0);
                        mincg.mincgrestartfrom(state, x);
                        while( mincg.mincgiteration(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);
                                }
                            }
                            if( state.xupdated )
                            {
                                if( hasxlast )
                                {
                                    lastscaledstep = 0;
                                    for(i=0; i<=n-1; i++)
                                    {
                                        lastscaledstep = lastscaledstep+math.sqr(state.x[i]-xlast[i])/math.sqr(s[i]);
                                    }
                                    lastscaledstep = Math.Sqrt(lastscaledstep);
                                }
                                else
                                {
                                    lastscaledstep = 0;
                                }
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    xlast[i_] = state.x[i_];
                                }
                                hasxlast = true;
                            }
                        }
                        mincg.mincgresults(state, ref x, rep);
                        if( rep.terminationtype<=0 )
                        {
                            err = true;
                            return;
                        }
                        err = err | (double)(lastscaledstep)>(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;
                        mincg.mincgcreate(1, x, state);
                        mincg.mincgsetcond(state, epsg, 0, 0, 0);
                        mincg.mincgsetcgtype(state, cgtype);
                        while( mincg.mincgiteration(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;
                                }
                            }
                        }
                        mincg.mincgresults(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);
                    }
                }
            }
        }
Exemplo n.º 6
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 j = 0;
            int k = 0;
            int vs = 0;
            double[,] v = new double[0,0];
            double[] vd = new double[0];
            double[] d = new double[0];
            double[] s = new double[0];
            int cntb1 = 0;
            int cntg1 = 0;
            int cntb2 = 0;
            int cntg2 = 0;
            double epsg = 0;
            double[] diagh = new double[0];
            mincg.mincgstate state = new mincg.mincgstate();
            mincg.mincgreport rep = new mincg.mincgreport();
            int cgtype = 0;

            k = 50;
            epsg = 1.0E-10;
            for(cgtype=-1; cgtype<=1; cgtype++)
            {
                
                //
                // Preconditioner test 1.
                //
                // If
                // * B1 is default preconditioner
                // * G1 is diagonal precomditioner based on approximate diagonal of Hessian matrix
                // then "bad" preconditioner is worse than "good" one.
                // "Worse" means more iterations to converge.
                //
                //
                // We test it using f(x) = sum( ((i*i+1)*x[i])^2, i=0..N-1).
                //
                // N        - problem size
                // K        - number of repeated passes (should be large enough to average out random factors)
                //
                for(n=10; n<=15; n++)
                {
                    x = new double[n];
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 0;
                    }
                    mincg.mincgcreate(n, x, state);
                    mincg.mincgsetcgtype(state, cgtype);
                    
                    //
                    // Test it with default preconditioner
                    //
                    mincg.mincgsetprecdefault(state);
                    cntb1 = 0;
                    for(pass=0; pass<=k-1; pass++)
                    {
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 2*math.randomreal()-1;
                        }
                        mincg.mincgrestartfrom(state, x);
                        while( mincg.mincgiteration(state) )
                        {
                            calciip2(state, n);
                        }
                        mincg.mincgresults(state, ref x, rep);
                        cntb1 = cntb1+rep.iterationscount;
                        err = err | rep.terminationtype<=0;
                    }
                    
                    //
                    // Test it with perturbed diagonal preconditioner
                    //
                    diagh = new double[n];
                    for(i=0; i<=n-1; i++)
                    {
                        diagh[i] = 2*math.sqr(i*i+1)*(0.8+0.4*math.randomreal());
                    }
                    mincg.mincgsetprecdiag(state, diagh);
                    cntg1 = 0;
                    for(pass=0; pass<=k-1; pass++)
                    {
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 2*math.randomreal()-1;
                        }
                        mincg.mincgrestartfrom(state, x);
                        while( mincg.mincgiteration(state) )
                        {
                            calciip2(state, n);
                        }
                        mincg.mincgresults(state, ref x, rep);
                        cntg1 = cntg1+rep.iterationscount;
                        err = err | rep.terminationtype<=0;
                    }
                    
                    //
                    // Compare
                    //
                    err = err | cntb1<cntg1;
                }
                
                //
                // Preconditioner test 2.
                //
                // If
                // * B1 is default preconditioner
                // * G1 is low rank exact preconditioner
                // then "bad" preconditioner is worse than "good" one.
                // "Worse" means more iterations to converge.
                //
                // Target function is f(x) = 0.5*(x-x0)'*A*(x-x0), A = D+V'*Vd*V
                //
                // N        - problem size
                // K        - number of repeated passes (should be large enough to average out random factors)
                //
                for(n=10; n<=15; n++)
                {
                    for(vs=0; vs<=5; vs++)
                    {
                        x = new double[n];
                        x0 = new double[n];
                        d = new double[n];
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 0;
                            x0[i] = 2*math.randomreal()-1;
                            d[i] = Math.Exp(2*math.randomreal());
                        }
                        if( vs>0 )
                        {
                            v = new double[vs, n];
                            vd = new double[vs];
                            for(i=0; i<=vs-1; i++)
                            {
                                for(j=0; j<=n-1; j++)
                                {
                                    v[i,j] = 2*math.randomreal()-1;
                                }
                                vd[i] = Math.Exp(2*math.randomreal());
                            }
                        }
                        mincg.mincgcreate(n, x, state);
                        mincg.mincgsetcgtype(state, cgtype);
                        
                        //
                        // Test it with default preconditioner
                        //
                        mincg.mincgsetprecdefault(state);
                        cntb1 = 0;
                        for(pass=0; pass<=k-1; pass++)
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = 2*math.randomreal()-1;
                            }
                            mincg.mincgrestartfrom(state, x);
                            while( mincg.mincgiteration(state) )
                            {
                                calclowrank(state, n, vs, d, v, vd, x0);
                            }
                            mincg.mincgresults(state, ref x, rep);
                            cntb1 = cntb1+rep.iterationscount;
                            err = err | rep.terminationtype<=0;
                        }
                        
                        //
                        // Test it with low rank preconditioner
                        //
                        mincg.mincgsetpreclowrankfast(state, d, vd, v, vs);
                        cntg1 = 0;
                        for(pass=0; pass<=k-1; pass++)
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                x[i] = 2*math.randomreal()-1;
                            }
                            mincg.mincgrestartfrom(state, x);
                            while( mincg.mincgiteration(state) )
                            {
                                calclowrank(state, n, vs, d, v, vd, x0);
                            }
                            mincg.mincgresults(state, ref x, rep);
                            cntg1 = cntg1+rep.iterationscount;
                            err = err | rep.terminationtype<=0;
                        }
                        
                        //
                        // Compare
                        //
                        err = err | cntb1<cntg1;
                    }
                }
                
                //
                // Preconditioner test 3.
                //
                // If
                // * 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 B2 is worse than G2.
                // "Worse" means more iterations to converge.
                //
                for(n=10; n<=15; n++)
                {
                    x = new double[n];
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 0;
                    }
                    mincg.mincgcreate(n, x, state);
                    s = new double[n];
                    for(i=0; i<=n-1; i++)
                    {
                        s[i] = 1/Math.Sqrt(2*Math.Pow(i*i+1, 2)*(0.8+0.4*math.randomreal()));
                    }
                    mincg.mincgsetprecdefault(state);
                    mincg.mincgsetscale(state, s);
                    cntb2 = 0;
                    for(pass=0; pass<=k-1; pass++)
                    {
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 2*math.randomreal()-1;
                        }
                        mincg.mincgrestartfrom(state, x);
                        while( mincg.mincgiteration(state) )
                        {
                            calciip2(state, n);
                        }
                        mincg.mincgresults(state, ref x, rep);
                        cntb2 = cntb2+rep.iterationscount;
                        err = err | rep.terminationtype<=0;
                    }
                    mincg.mincgsetprecscale(state);
                    mincg.mincgsetscale(state, s);
                    cntg2 = 0;
                    for(pass=0; pass<=k-1; pass++)
                    {
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 2*math.randomreal()-1;
                        }
                        mincg.mincgrestartfrom(state, x);
                        while( mincg.mincgiteration(state) )
                        {
                            calciip2(state, n);
                        }
                        mincg.mincgresults(state, ref x, rep);
                        cntg2 = cntg2+rep.iterationscount;
                        err = err | rep.terminationtype<=0;
                    }
                    err = err | cntb2<cntg2;
                }
            }
        }
Exemplo n.º 7
0
        public static bool testmincg(bool silent)
        {
            bool result = new bool();
            bool waserrors = new bool();
            bool referror = new bool();
            bool eqerror = new bool();
            bool linerror1 = new bool();
            bool linerror2 = new bool();
            bool restartserror = new bool();
            bool precerror = new bool();
            bool converror = new bool();
            bool othererrors = new bool();
            int n = 0;
            double[] x = new double[0];
            double[] xe = new double[0];
            double[] b = new double[0];
            double[] xlast = new double[0];
            int i = 0;
            int j = 0;
            double v = 0;
            double[,] a = new double[0,0];
            double[] diagh = new double[0];
            mincg.mincgstate state = new mincg.mincgstate();
            mincg.mincgreport rep = new mincg.mincgreport();
            int cgtype = 0;
            int difftype = 0;
            double diffstep = 0;
            int i_ = 0;

            waserrors = false;
            referror = false;
            linerror1 = false;
            linerror2 = false;
            eqerror = false;
            converror = false;
            restartserror = false;
            othererrors = false;
            precerror = false;
            testpreconditioning(ref precerror);
            testother(ref othererrors);
            for(difftype=0; difftype<=1; difftype++)
            {
                for(cgtype=-1; cgtype<=1; cgtype++)
                {
                    
                    //
                    // Reference problem
                    //
                    x = new double[2+1];
                    n = 3;
                    diffstep = 1.0E-6;
                    x[0] = 100*math.randomreal()-50;
                    x[1] = 100*math.randomreal()-50;
                    x[2] = 100*math.randomreal()-50;
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        if( state.needf | state.needfg )
                        {
                            state.f = math.sqr(state.x[0]-2)+math.sqr(state.x[1])+math.sqr(state.x[2]-state.x[0]);
                        }
                        if( state.needfg )
                        {
                            state.g[0] = 2*(state.x[0]-2)+2*(state.x[0]-state.x[2]);
                            state.g[1] = 2*state.x[1];
                            state.g[2] = 2*(state.x[2]-state.x[0]);
                        }
                    }
                    mincg.mincgresults(state, ref x, rep);
                    referror = (((referror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-2))>(double)(0.001)) | (double)(Math.Abs(x[1]))>(double)(0.001)) | (double)(Math.Abs(x[2]-2))>(double)(0.001);
                    
                    //
                    // F2 problem with restarts:
                    // * make several iterations and restart BEFORE termination
                    // * iterate and restart AFTER termination
                    //
                    // NOTE: step is bounded from above to avoid premature convergence
                    //
                    x = new double[3];
                    n = 3;
                    diffstep = 1.0E-6;
                    x[0] = 10+10*math.randomreal();
                    x[1] = 10+10*math.randomreal();
                    x[2] = 10+10*math.randomreal();
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcgtype(state, cgtype);
                    mincg.mincgsetstpmax(state, 0.1);
                    mincg.mincgsetcond(state, 0.0000001, 0.0, 0.0, 0);
                    for(i=0; i<=10; i++)
                    {
                        if( !mincg.mincgiteration(state) )
                        {
                            break;
                        }
                        testfunc2(state);
                    }
                    x[0] = 10+10*math.randomreal();
                    x[1] = 10+10*math.randomreal();
                    x[2] = 10+10*math.randomreal();
                    mincg.mincgrestartfrom(state, x);
                    while( mincg.mincgiteration(state) )
                    {
                        testfunc2(state);
                    }
                    mincg.mincgresults(state, ref x, rep);
                    restartserror = (((restartserror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.01)) | (double)(Math.Abs(x[1]))>(double)(0.01)) | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.01);
                    x[0] = 10+10*math.randomreal();
                    x[1] = 10+10*math.randomreal();
                    x[2] = 10+10*math.randomreal();
                    mincg.mincgrestartfrom(state, x);
                    while( mincg.mincgiteration(state) )
                    {
                        testfunc2(state);
                    }
                    mincg.mincgresults(state, ref x, rep);
                    restartserror = (((restartserror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.01)) | (double)(Math.Abs(x[1]))>(double)(0.01)) | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.01);
                    
                    //
                    // 1D problem #1
                    //
                    x = new double[0+1];
                    n = 1;
                    diffstep = 1.0E-6;
                    x[0] = 100*math.randomreal()-50;
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        if( state.needf | state.needfg )
                        {
                            state.f = -Math.Cos(state.x[0]);
                        }
                        if( state.needfg )
                        {
                            state.g[0] = Math.Sin(state.x[0]);
                        }
                    }
                    mincg.mincgresults(state, ref x, rep);
                    linerror1 = (linerror1 | rep.terminationtype<=0) | (double)(Math.Abs(x[0]/Math.PI-(int)Math.Round(x[0]/Math.PI)))>(double)(0.001);
                    
                    //
                    // 1D problem #2
                    //
                    x = new double[0+1];
                    n = 1;
                    diffstep = 1.0E-6;
                    x[0] = 100*math.randomreal()-50;
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        if( state.needf | state.needfg )
                        {
                            state.f = math.sqr(state.x[0])/(1+math.sqr(state.x[0]));
                        }
                        if( state.needfg )
                        {
                            state.g[0] = (2*state.x[0]*(1+math.sqr(state.x[0]))-math.sqr(state.x[0])*2*state.x[0])/math.sqr(1+math.sqr(state.x[0]));
                        }
                    }
                    mincg.mincgresults(state, ref x, rep);
                    linerror2 = (linerror2 | rep.terminationtype<=0) | (double)(Math.Abs(x[0]))>(double)(0.001);
                    
                    //
                    // Linear equations
                    //
                    diffstep = 1.0E-6;
                    for(n=1; n<=10; n++)
                    {
                        
                        //
                        // Prepare task
                        //
                        a = new double[n-1+1, n-1+1];
                        x = new double[n-1+1];
                        xe = new double[n-1+1];
                        b = new double[n-1+1];
                        for(i=0; i<=n-1; i++)
                        {
                            xe[i] = 2*math.randomreal()-1;
                        }
                        for(i=0; i<=n-1; i++)
                        {
                            for(j=0; j<=n-1; j++)
                            {
                                a[i,j] = 2*math.randomreal()-1;
                            }
                            a[i,i] = a[i,i]+3*Math.Sign(a[i,i]);
                        }
                        for(i=0; i<=n-1; i++)
                        {
                            v = 0.0;
                            for(i_=0; i_<=n-1;i_++)
                            {
                                v += a[i,i_]*xe[i_];
                            }
                            b[i] = v;
                        }
                        
                        //
                        // Solve task
                        //
                        for(i=0; i<=n-1; i++)
                        {
                            x[i] = 2*math.randomreal()-1;
                        }
                        if( difftype==0 )
                        {
                            mincg.mincgcreate(n, x, state);
                        }
                        if( difftype==1 )
                        {
                            mincg.mincgcreatef(n, x, diffstep, state);
                        }
                        mincg.mincgsetcgtype(state, cgtype);
                        while( mincg.mincgiteration(state) )
                        {
                            if( state.needf | state.needfg )
                            {
                                state.f = 0;
                            }
                            if( state.needfg )
                            {
                                for(i=0; i<=n-1; i++)
                                {
                                    state.g[i] = 0;
                                }
                            }
                            for(i=0; i<=n-1; i++)
                            {
                                v = 0.0;
                                for(i_=0; i_<=n-1;i_++)
                                {
                                    v += a[i,i_]*state.x[i_];
                                }
                                if( state.needf | state.needfg )
                                {
                                    state.f = state.f+math.sqr(v-b[i]);
                                }
                                if( state.needfg )
                                {
                                    for(j=0; j<=n-1; j++)
                                    {
                                        state.g[j] = state.g[j]+2*(v-b[i])*a[i,j];
                                    }
                                }
                            }
                        }
                        mincg.mincgresults(state, ref x, rep);
                        eqerror = eqerror | rep.terminationtype<=0;
                        for(i=0; i<=n-1; i++)
                        {
                            eqerror = eqerror | (double)(Math.Abs(x[i]-xe[i]))>(double)(0.001);
                        }
                    }
                    
                    //
                    // Testing convergence properties
                    //
                    diffstep = 1.0E-6;
                    n = 3;
                    x = new double[n];
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 6*math.randomreal()-3;
                    }
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcond(state, 0.001, 0.0, 0.0, 0);
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        testfunc3(state);
                    }
                    mincg.mincgresults(state, ref x, rep);
                    converror = converror | rep.terminationtype!=4;
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 6*math.randomreal()-3;
                    }
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcond(state, 0.0, 0.001, 0.0, 0);
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        testfunc3(state);
                    }
                    mincg.mincgresults(state, ref x, rep);
                    converror = converror | rep.terminationtype!=1;
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 6*math.randomreal()-3;
                    }
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcond(state, 0.0, 0.0, 0.001, 0);
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        testfunc3(state);
                    }
                    mincg.mincgresults(state, ref x, rep);
                    converror = converror | rep.terminationtype!=2;
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 2*math.randomreal()-1;
                    }
                    if( difftype==0 )
                    {
                        mincg.mincgcreate(n, x, state);
                    }
                    if( difftype==1 )
                    {
                        mincg.mincgcreatef(n, x, diffstep, state);
                    }
                    mincg.mincgsetcond(state, 0.0, 0.0, 0.0, 10);
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        testfunc3(state);
                    }
                    mincg.mincgresults(state, ref x, rep);
                    converror = converror | !((rep.terminationtype==5 & rep.iterationscount==10) | rep.terminationtype==7);
                }
            }
            
            //
            // end
            //
            waserrors = ((((((referror | eqerror) | linerror1) | linerror2) | converror) | othererrors) | restartserror) | precerror;
            if( !silent )
            {
                System.Console.Write("TESTING CG OPTIMIZATION");
                System.Console.WriteLine();
                System.Console.Write("REFERENCE PROBLEM:                        ");
                if( referror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("LIN-1 PROBLEM:                            ");
                if( linerror1 )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("LIN-2 PROBLEM:                            ");
                if( linerror2 )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("LINEAR EQUATIONS:                         ");
                if( eqerror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("RESTARTS:                                 ");
                if( restartserror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("PRECONDITIONING:                          ");
                if( precerror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("CONVERGENCE PROPERTIES:                   ");
                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;
        }
Exemplo n.º 8
0
    public static int Main(string[] args)
    {
        int n = 0;

        mincg.mincgstate  state = new mincg.mincgstate();
        mincg.mincgreport rep   = new mincg.mincgreport();
        double[]          s     = new double[0];
        double            x     = 0;
        double            y     = 0;


        //
        // Function minimized:
        //     F = exp(x-1) + exp(1-x) + (y-x)^2
        // N = 2 - task dimension.
        //
        // Take a look at MinCGSetStpMax() call - it prevents us
        // from overflow (which may be result of too large step).
        // Try to comment it and see what will happen.
        //
        n    = 2;
        s    = new double[2];
        s[0] = 10;
        s[1] = AP.Math.RandomReal() - 0.5;
        mincg.mincgcreate(n, ref s, ref state);
        mincg.mincgsetcond(ref state, 0.0, 0.0, 0.0001, 0);
        mincg.mincgsetxrep(ref state, true);
        mincg.mincgsetstpmax(ref state, 1.0);
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.Write("F = exp(x-1) + exp(1-x) + (y-x)^2");
        System.Console.WriteLine();
        System.Console.Write("OPTIMIZATION STARTED");
        System.Console.WriteLine();
        while (mincg.mincgiteration(ref state))
        {
            if (state.needfg)
            {
                x          = state.x[0];
                y          = state.x[1];
                state.f    = Math.Exp(x - 1) + Math.Exp(1 - x) + AP.Math.Sqr(y - x);
                state.g[0] = Math.Exp(x - 1) - Math.Exp(1 - x) + 2 * (x - y);
                state.g[1] = 2 * (y - x);
            }
            if (state.xupdated)
            {
                System.Console.Write("    F(");
                System.Console.Write("{0,8:F5}", state.x[0]);
                System.Console.Write(",");
                System.Console.Write("{0,8:F5}", state.x[1]);
                System.Console.Write(")=");
                System.Console.Write("{0,0:F5}", state.f);
                System.Console.WriteLine();
            }
        }
        System.Console.Write("OPTIMIZATION STOPPED");
        System.Console.WriteLine();
        mincg.mincgresults(ref state, ref s, ref rep);

        //
        // output results
        //
        System.Console.Write("X = ");
        System.Console.Write("{0,4:F2}", s[0]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.Write("Y = ");
        System.Console.Write("{0,4:F2}", s[1]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.WriteLine();
        return(0);
    }
    public static int Main(string[] args)
    {
        int n = 0;
        mincg.mincgstate state = new mincg.mincgstate();
        mincg.mincgreport rep = new mincg.mincgreport();
        double[] s = new double[0];
        double x = 0;
        double y = 0;

        
        //
        // Function minimized:
        //     F = (x-1)^4 + (y-x)^2
        // N = 2 - task dimension.
        //
        n = 2;
        s = new double[2];
        s[0] = 10;
        s[1] = 11;
        mincg.mincgcreate(n, ref s, ref state);
        mincg.mincgsetcond(ref state, 0.0, 0.0, 0.00001, 0);
        mincg.mincgsetxrep(ref state, true);
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.Write("F = (x-1)^4 + (y-x)^2");
        System.Console.WriteLine();
        System.Console.Write("OPTIMIZATION STARTED");
        System.Console.WriteLine();
        while( mincg.mincgiteration(ref state) )
        {
            if( state.needfg )
            {
                x = state.x[0];
                y = state.x[1];
                state.f = AP.Math.Sqr(AP.Math.Sqr(x-1))+AP.Math.Sqr(y-x);
                state.g[0] = 4*AP.Math.Sqr(x-1)*(x-1)+2*(x-y);
                state.g[1] = 2*(y-x);
            }
            if( state.xupdated )
            {
                System.Console.Write("    F(");
                System.Console.Write("{0,8:F5}",state.x[0]);
                System.Console.Write(",");
                System.Console.Write("{0,8:F5}",state.x[1]);
                System.Console.Write(")=");
                System.Console.Write("{0,0:F5}",state.f);
                System.Console.WriteLine();
            }
        }
        System.Console.Write("OPTIMIZATION STOPPED");
        System.Console.WriteLine();
        mincg.mincgresults(ref state, ref s, ref rep);
        
        //
        // output results
        //
        System.Console.Write("X = ");
        System.Console.Write("{0,4:F2}",s[0]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.Write("Y = ");
        System.Console.Write("{0,4:F2}",s[1]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.WriteLine();
        return 0;
    }
Exemplo n.º 10
0
    public static int Main(string[] args)
    {
        int n = 0;

        mincg.mincgstate  state = new mincg.mincgstate();
        mincg.mincgreport rep   = new mincg.mincgreport();
        double[]          s     = new double[0];
        double            x     = 0;
        double            y     = 0;


        //
        // Function minimized:
        //     F = (x-1)^4 + (y-x)^2
        // N = 2 - task dimension.
        //
        n    = 2;
        s    = new double[2];
        s[0] = 10;
        s[1] = 11;
        mincg.mincgcreate(n, ref s, ref state);
        mincg.mincgsetcond(ref state, 0.0, 0.0, 0.00001, 0);
        mincg.mincgsetxrep(ref state, true);
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.Write("F = (x-1)^4 + (y-x)^2");
        System.Console.WriteLine();
        System.Console.Write("OPTIMIZATION STARTED");
        System.Console.WriteLine();
        while (mincg.mincgiteration(ref state))
        {
            if (state.needfg)
            {
                x          = state.x[0];
                y          = state.x[1];
                state.f    = AP.Math.Sqr(AP.Math.Sqr(x - 1)) + AP.Math.Sqr(y - x);
                state.g[0] = 4 * AP.Math.Sqr(x - 1) * (x - 1) + 2 * (x - y);
                state.g[1] = 2 * (y - x);
            }
            if (state.xupdated)
            {
                System.Console.Write("    F(");
                System.Console.Write("{0,8:F5}", state.x[0]);
                System.Console.Write(",");
                System.Console.Write("{0,8:F5}", state.x[1]);
                System.Console.Write(")=");
                System.Console.Write("{0,0:F5}", state.f);
                System.Console.WriteLine();
            }
        }
        System.Console.Write("OPTIMIZATION STOPPED");
        System.Console.WriteLine();
        mincg.mincgresults(ref state, ref s, ref rep);

        //
        // output results
        //
        System.Console.Write("X = ");
        System.Console.Write("{0,4:F2}", s[0]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.Write("Y = ");
        System.Console.Write("{0,4:F2}", s[1]);
        System.Console.Write(" (should be 1.00)");
        System.Console.WriteLine();
        System.Console.WriteLine();
        System.Console.WriteLine();
        return(0);
    }
Exemplo n.º 11
0
        public static bool testmincg(bool silent)
        {
            bool result = new bool();
            bool waserrors = new bool();
            bool referror = new bool();
            bool eqerror = new bool();
            bool linerror1 = new bool();
            bool linerror2 = new bool();
            bool restartserror = new bool();
            bool converror = new bool();
            bool othererrors = new bool();
            int n = 0;
            double[] x = new double[0];
            double[] xe = new double[0];
            double[] b = new double[0];
            double[] xlast = new double[0];
            double fprev = 0;
            double xprev = 0;
            double stpmax = 0;
            int i = 0;
            int j = 0;
            double v = 0;
            double[,] a = new double[0,0];
            mincg.mincgstate state = new mincg.mincgstate();
            mincg.mincgreport rep = new mincg.mincgreport();
            int cgtype = 0;
            int i_ = 0;

            waserrors = false;
            referror = false;
            linerror1 = false;
            linerror2 = false;
            eqerror = false;
            converror = false;
            restartserror = false;
            othererrors = false;
            for(cgtype=0; cgtype<=1; cgtype++)
            {
                
                //
                // Reference problem
                //
                x = new double[2+1];
                n = 3;
                x[0] = 100*math.randomreal()-50;
                x[1] = 100*math.randomreal()-50;
                x[2] = 100*math.randomreal()-50;
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    state.f = math.sqr(state.x[0]-2)+math.sqr(state.x[1])+math.sqr(state.x[2]-state.x[0]);
                    state.g[0] = 2*(state.x[0]-2)+2*(state.x[0]-state.x[2]);
                    state.g[1] = 2*state.x[1];
                    state.g[2] = 2*(state.x[2]-state.x[0]);
                }
                mincg.mincgresults(state, ref x, rep);
                referror = (((referror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-2))>(double)(0.001)) | (double)(Math.Abs(x[1]))>(double)(0.001)) | (double)(Math.Abs(x[2]-2))>(double)(0.001);
                
                //
                // F2 problem with restarts:
                // * make several iterations and restart BEFORE termination
                // * iterate and restart AFTER termination
                //
                // NOTE: step is bounded from above to avoid premature convergence
                //
                x = new double[3];
                n = 3;
                x[0] = 10+10*math.randomreal();
                x[1] = 10+10*math.randomreal();
                x[2] = 10+10*math.randomreal();
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcgtype(state, cgtype);
                mincg.mincgsetstpmax(state, 0.1);
                mincg.mincgsetcond(state, 0.0000001, 0.0, 0.0, 0);
                for(i=0; i<=10; i++)
                {
                    if( !mincg.mincgiteration(state) )
                    {
                        break;
                    }
                    testfunc2(state);
                }
                x[0] = 10+10*math.randomreal();
                x[1] = 10+10*math.randomreal();
                x[2] = 10+10*math.randomreal();
                mincg.mincgrestartfrom(state, x);
                while( mincg.mincgiteration(state) )
                {
                    testfunc2(state);
                }
                mincg.mincgresults(state, ref x, rep);
                restartserror = (((restartserror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.01)) | (double)(Math.Abs(x[1]))>(double)(0.01)) | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.01);
                x[0] = 10+10*math.randomreal();
                x[1] = 10+10*math.randomreal();
                x[2] = 10+10*math.randomreal();
                mincg.mincgrestartfrom(state, x);
                while( mincg.mincgiteration(state) )
                {
                    testfunc2(state);
                }
                mincg.mincgresults(state, ref x, rep);
                restartserror = (((restartserror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.01)) | (double)(Math.Abs(x[1]))>(double)(0.01)) | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.01);
                
                //
                // 1D problem #1
                //
                x = new double[0+1];
                n = 1;
                x[0] = 100*math.randomreal()-50;
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    state.f = -Math.Cos(state.x[0]);
                    state.g[0] = Math.Sin(state.x[0]);
                }
                mincg.mincgresults(state, ref x, rep);
                linerror1 = (linerror1 | rep.terminationtype<=0) | (double)(Math.Abs(x[0]/Math.PI-(int)Math.Round(x[0]/Math.PI)))>(double)(0.001);
                
                //
                // 1D problem #2
                //
                x = new double[0+1];
                n = 1;
                x[0] = 100*math.randomreal()-50;
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    state.f = math.sqr(state.x[0])/(1+math.sqr(state.x[0]));
                    state.g[0] = (2*state.x[0]*(1+math.sqr(state.x[0]))-math.sqr(state.x[0])*2*state.x[0])/math.sqr(1+math.sqr(state.x[0]));
                }
                mincg.mincgresults(state, ref x, rep);
                linerror2 = (linerror2 | rep.terminationtype<=0) | (double)(Math.Abs(x[0]))>(double)(0.001);
                
                //
                // Linear equations
                //
                for(n=1; n<=10; n++)
                {
                    
                    //
                    // Prepare task
                    //
                    a = new double[n-1+1, n-1+1];
                    x = new double[n-1+1];
                    xe = new double[n-1+1];
                    b = new double[n-1+1];
                    for(i=0; i<=n-1; i++)
                    {
                        xe[i] = 2*math.randomreal()-1;
                    }
                    for(i=0; i<=n-1; i++)
                    {
                        for(j=0; j<=n-1; j++)
                        {
                            a[i,j] = 2*math.randomreal()-1;
                        }
                        a[i,i] = a[i,i]+3*Math.Sign(a[i,i]);
                    }
                    for(i=0; i<=n-1; i++)
                    {
                        v = 0.0;
                        for(i_=0; i_<=n-1;i_++)
                        {
                            v += a[i,i_]*xe[i_];
                        }
                        b[i] = v;
                    }
                    
                    //
                    // Solve task
                    //
                    for(i=0; i<=n-1; i++)
                    {
                        x[i] = 2*math.randomreal()-1;
                    }
                    mincg.mincgcreate(n, x, state);
                    mincg.mincgsetcgtype(state, cgtype);
                    while( mincg.mincgiteration(state) )
                    {
                        state.f = 0;
                        for(i=0; i<=n-1; i++)
                        {
                            state.g[i] = 0;
                        }
                        for(i=0; i<=n-1; i++)
                        {
                            v = 0.0;
                            for(i_=0; i_<=n-1;i_++)
                            {
                                v += a[i,i_]*state.x[i_];
                            }
                            state.f = state.f+math.sqr(v-b[i]);
                            for(j=0; j<=n-1; j++)
                            {
                                state.g[j] = state.g[j]+2*(v-b[i])*a[i,j];
                            }
                        }
                    }
                    mincg.mincgresults(state, ref x, rep);
                    eqerror = eqerror | rep.terminationtype<=0;
                    for(i=0; i<=n-1; i++)
                    {
                        eqerror = eqerror | (double)(Math.Abs(x[i]-xe[i]))>(double)(0.001);
                    }
                }
                
                //
                // Testing convergence properties
                //
                x = new double[2+1];
                n = 3;
                for(i=0; i<=2; i++)
                {
                    x[i] = 6*math.randomreal()-3;
                }
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 0.001, 0.0, 0.0, 0);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    testfunc3(state);
                }
                mincg.mincgresults(state, ref x, rep);
                converror = converror | rep.terminationtype!=4;
                for(i=0; i<=2; i++)
                {
                    x[i] = 6*math.randomreal()-3;
                }
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 0.0, 0.001, 0.0, 0);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    testfunc3(state);
                }
                mincg.mincgresults(state, ref x, rep);
                converror = converror | rep.terminationtype!=1;
                for(i=0; i<=2; i++)
                {
                    x[i] = 6*math.randomreal()-3;
                }
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 0.0, 0.0, 0.001, 0);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    testfunc3(state);
                }
                mincg.mincgresults(state, ref x, rep);
                converror = converror | rep.terminationtype!=2;
                for(i=0; i<=2; i++)
                {
                    x[i] = 2*math.randomreal()-1;
                }
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 0.0, 0.0, 0.0, 10);
                mincg.mincgsetcgtype(state, cgtype);
                while( mincg.mincgiteration(state) )
                {
                    testfunc3(state);
                }
                mincg.mincgresults(state, ref x, rep);
                converror = converror | !((rep.terminationtype==5 & rep.iterationscount==10) | rep.terminationtype==7);
                
                //
                // Other properties:
                // 1. test reports (F should form monotone sequence)
                // 2. test maximum step
                //
                n = 50;
                x = new double[n];
                xlast = new double[n];
                for(i=0; i<=n-1; i++)
                {
                    x[i] = 1;
                }
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 0, 0, 0, 100);
                mincg.mincgsetxrep(state, true);
                fprev = math.maxrealnumber;
                while( mincg.mincgiteration(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 )
                    {
                        othererrors = othererrors | (double)(state.f)>(double)(fprev);
                        if( (double)(fprev)==(double)(math.maxrealnumber) )
                        {
                            for(i=0; i<=n-1; i++)
                            {
                                othererrors = othererrors | (double)(state.x[i])!=(double)(x[i]);
                            }
                        }
                        fprev = state.f;
                        for(i_=0; i_<=n-1;i_++)
                        {
                            xlast[i_] = state.x[i_];
                        }
                    }
                }
                mincg.mincgresults(state, ref x, rep);
                for(i=0; i<=n-1; i++)
                {
                    othererrors = othererrors | (double)(x[i])!=(double)(xlast[i]);
                }
                n = 1;
                x = new double[n];
                x[0] = 100;
                stpmax = 0.05+0.05*math.randomreal();
                mincg.mincgcreate(n, x, state);
                mincg.mincgsetcond(state, 1.0E-9, 0, 0, 0);
                mincg.mincgsetstpmax(state, stpmax);
                mincg.mincgsetxrep(state, true);
                xprev = x[0];
                while( mincg.mincgiteration(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]);
                        othererrors = othererrors | (double)(Math.Abs(state.x[0]-xprev))>(double)((1+Math.Sqrt(math.machineepsilon))*stpmax);
                    }
                    if( state.xupdated )
                    {
                        othererrors = othererrors | (double)(Math.Abs(state.x[0]-xprev))>(double)((1+Math.Sqrt(math.machineepsilon))*stpmax);
                        xprev = state.x[0];
                    }
                }
            }
            
            //
            // end
            //
            waserrors = (((((referror | eqerror) | linerror1) | linerror2) | converror) | othererrors) | restartserror;
            if( !silent )
            {
                System.Console.Write("TESTING CG OPTIMIZATION");
                System.Console.WriteLine();
                System.Console.Write("REFERENCE PROBLEM:                        ");
                if( referror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("LIN-1 PROBLEM:                            ");
                if( linerror1 )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("LIN-2 PROBLEM:                            ");
                if( linerror2 )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("LINEAR EQUATIONS:                         ");
                if( eqerror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("RESTARTS:                                 ");
                if( restartserror )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("CONVERGENCE PROPERTIES:                   ");
                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;
        }