Exemple #1
0
 public void MinInterval3()
 {
     int maxCount=30;
     LineSearch ls = new LineSearch();
     TestFunction1 f = new TestFunction1();
     double[] res = new double[3];
     int counter = ls.FindMinInterval(f,4,2,maxCount, ref res);
     double aVal = f.GetVal(res[0]);
     double bVal = f.GetVal(res[1]);
     double cVal = f.GetVal(res[2]);
     Assert.IsTrue(counter<maxCount);
     Assert.IsTrue((aVal>bVal)&&(cVal>bVal));
 }
Exemple #2
0
 public void MinBrent3()
 {
     double xmin=0;
     double eps = 1e-5;
     double realXmin = -0.75;
     int maxCount = 50;
     LineSearch ls = new LineSearch();
     TestFunction2 f = new TestFunction2();
     double[] res = new double[3];
     int counter = ls.FindMinInterval(f,-10,1,30,ref res);
     counter = ls.FindMinimumViaBrent(f,res[0],res[1],res[2],maxCount,eps, ref xmin);
     Assert.IsTrue(counter<maxCount);
     Assert.IsTrue (System.Math.Abs(xmin-realXmin)<eps);
 }
Exemple #3
0
 public void MinGold3()
 {
     double xmin=0;
     double eps = 1e-5;
     double realXmin = -0.75;
     int counterMax = 50;
     LineSearch ls = new LineSearch();
     TestFunction2 f = new TestFunction2();
     double [] res = new double[3];
     int counter1 = ls.FindMinInterval(f,-10,1,30,ref res);
     int counter2 = ls.FindMinimumViaGoldenSection(f,res[0],res[1],res[2],counterMax,eps, ref xmin);
     Assert.IsTrue(counter2<counterMax);
     Assert.IsTrue (System.Math.Abs(xmin-realXmin)<eps);
 }
Exemple #4
0
        public void MinInterval3()
        {
            int           maxCount = 30;
            LineSearch    ls       = new LineSearch();
            TestFunction1 f        = new TestFunction1();

            double[] res     = new double[3];
            int      counter = ls.FindMinInterval(f, 4, 2, maxCount, ref res);
            double   aVal    = f.GetVal(res[0]);
            double   bVal    = f.GetVal(res[1]);
            double   cVal    = f.GetVal(res[2]);

            Assert.IsTrue(counter < maxCount);
            Assert.IsTrue((aVal > bVal) && (cVal > bVal));
        }
Exemple #5
0
        public void MinGold4()
        {
            double        xmin       = 0;
            double        eps        = 1e-5;
            double        realXmin   = -0.75;
            int           counterMax = 50;
            LineSearch    ls         = new LineSearch();
            TestFunction2 f          = new TestFunction2();

            double[] res      = new double[3];
            int      counter1 = ls.FindMinInterval(f, 1000, 1, 30, ref res);
            int      counter2 = ls.FindMinimumViaGoldenSection(f, res[0], res[1], res[2], counterMax, eps, ref xmin);

            Assert.IsTrue(counter2 < counterMax);
            Assert.IsTrue(System.Math.Abs(xmin - realXmin) < eps);
        }
Exemple #6
0
        public void MinBrent4()
        {
            double        xmin     = 0;
            double        eps      = 1e-5;
            double        realXmin = -0.75;
            int           maxCount = 50;
            LineSearch    ls       = new LineSearch();
            TestFunction2 f        = new TestFunction2();

            double[] res     = new double[3];
            int      counter = ls.FindMinInterval(f, 1000, 1, 30, ref res);

            counter = ls.FindMinimumViaBrent(f, res[0], res[1], res[2], 50, eps, ref xmin);
            Assert.IsTrue(counter < maxCount);
            Assert.IsTrue(System.Math.Abs(xmin - realXmin) < eps);
        }
Exemple #7
0
        public void MinBrent1()
        {
            double        xmin     = 0;
            int           maxCount = 50;
            double        realXmin = -1.364641;
            double        eps      = 1e-5;
            LineSearch    ls       = new LineSearch();
            TestFunction1 f        = new TestFunction1();

            double[] res     = new double[3];
            int      counter = ls.FindMinInterval(f, -10, 1, maxCount, ref res);

            counter = ls.FindMinimumViaBrent(f, res[0], res[1], res[2], maxCount, eps, ref xmin);
            Assert.IsTrue(counter < maxCount);
            Assert.IsTrue(System.Math.Abs(xmin - realXmin) < eps);
        }
Exemple #8
0
        private double[] CalculateNextPoint(double[] pX, double[] pGrad, GeneralMatrix hessian)
        {
            int i=0;
            double xmin=0;
            double step = _step;
            GeneralMatrix alfaX = new GeneralMatrix(_nDim,1);
            GeneralMatrix prevX = new GeneralMatrix(pX,_nDim);
            GeneralMatrix prevGrad = new GeneralMatrix(pGrad,_nDim);
            double[] intermediate = new double[_nDim];;

            alfaX = hessian.Multiply(prevGrad);

            //doing a line search to minimize alpha
            OneDWrapper wrapper = new OneDWrapper(_f,prevX,alfaX);
            LineSearch search = new LineSearch();
            double[] interval = new double[Constants.BRACKET_POINTS];
            int it1 = search.FindMinInterval(wrapper,_alpha,step,50,ref interval);
            int it2 = search.FindMinimumViaBrent(wrapper,interval[0],interval[1],interval[2],50,_epsilon, ref xmin);

            for (i=0;i<_nDim; i++)
                intermediate[i] = prevX.GetElement(i,0) - xmin*alfaX.GetElement(i,0);

            _alpha = xmin;

            return intermediate;
        }