Пример #1
0
        public void ComparePerformance <E>(List <E> listArray, E [] csArr, DynamicIntArray <E> dynArr) where E : IComparable
        {
            // Will Compare performance by searching element..
            // All the elements will have same data and size.. Fair to Compare.
            int       index;
            Random    rand      = new Random();
            E         find      = csArr[rand.Next(900000, 1000000)]; // picks one element on random but from almost end so that it takes time
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            index = Array.IndexOf(csArr, find);

            stopwatch.Stop();
            long ts = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("C# Array\nIndex " + index + "\nTime : " + ts + "Milliseconds\n");

            ts = 0;
            stopwatch.Reset();
            stopwatch.Restart();
            index = listArray.IndexOf(find);
            stopwatch.Stop();
            ts = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("List\nIndex " + index + "\nTime : " + ts + " Milliseconds\n");


            ts = 0;
            stopwatch.Reset();
            stopwatch.Restart();
            index = dynArr.IndexOf(find);
            stopwatch.Stop();
            ts = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("Dynamic Array\nIndex " + index + "\nTime : " + ts + " Milliseconds");
        }
        public void testIntArray()
        {
            DynamicIntArray dia = new DynamicIntArray(10);

            for (int i = 0; i < 10000; ++i)
            {
                dia.add(2 * i);
            }
            Assert.Equal(10000, dia.size());
            for (int i = 0; i < 10000; ++i)
            {
                Assert.Equal(2 * i, dia.get(i));
            }
            dia.clear();
            Assert.Equal(0, dia.size());
            dia.add(3);
            dia.add(12);
            dia.add(65);
            Assert.Equal("{3,12,65}", dia.ToString());
            for (int i = 0; i < 5; ++i)
            {
                dia.increment(i, 3);
            }
            Assert.Equal("{6,15,68,3,3}", dia.ToString());
        }
        public void RemoveTest_InvalidItem()
        {
            DynamicIntArray array = CreateArray(10);

            Assert.ThrowsException <IndexOutOfRangeException>(() => array.Remove(10), "");
            Assert.ThrowsException <IndexOutOfRangeException>(() => array.Remove(-1), "");
        }
        public void RemoveTest_LastItem()
        {
            DynamicIntArray array = CreateArray(10);

            array.Remove(9);
            string result = " 0 1 2 3 4 5 6 7 8";

            Assert.AreEqual(result, array.ToString());
        }
        private DynamicIntArray CreateArray(int numOfElements)
        {
            DynamicIntArray array = new DynamicIntArray();

            for (int i = 0; i < numOfElements; ++i)
            {
                array.Add(i);
            }
            return(array);
        }
        public void AddTest()
        {
            DynamicIntArray array = CreateArray(11);

            array.Add(32);
            array.Add(42);
            string result = " 0 1 2 3 4 5 6 7 8 9 10 32 42";

            Assert.AreEqual(result, array.ToString());
        }
        public void RemoveTest()
        {
            DynamicIntArray array = CreateArray(11);

            array.Remove(5);
            array.Remove(0);
            string result = " 1 2 3 4 6 7 8 9 10";

            Assert.AreEqual(result, array.ToString());
        }
Пример #8
0
    public int obtain_by_position_list(int window_size, DynamicIntArray positions)
    {
        int ret = modshogunPINVOKE.StringShortRealFeatures_obtain_by_position_list__SWIG_1(swigCPtr, window_size, DynamicIntArray.getCPtr(positions));

        if (modshogunPINVOKE.SWIGPendingException.Pending)
        {
            throw modshogunPINVOKE.SWIGPendingException.Retrieve();
        }
        return(ret);
    }
        public void InsertTest()
        {
            DynamicIntArray array = CreateArray(11);

            array.Insert(8, 223);
            array.Insert(100, 654);

            string result = " 0 1 2 3 4 5 6 7 223 8 9 10 654";

            Assert.AreEqual(result, array.ToString());
        }
	public static void Main() {
		modshogun.init_shogun_with_defaults();
		String[] strings = new String[] {"AAAAAAAAAACCCCCCCCCCGGGGGGGGGGTTTTTTTTTT"};
		StringCharFeatures f = new StringCharFeatures(strings, EAlphabet.DNA);
		f.obtain_by_sliding_window(5,1);

		DynamicIntArray positions = new DynamicIntArray();
		positions.append_element(0);
		positions.append_element(6);
		positions.append_element(16);
		positions.append_element(25);

		//f.obtain_by_position_list(8,positions);

	}
    internal static ArrayList run(string[] strs)
    {
        modshogun.init_shogun_with_defaults();
        StringCharFeatures f = new StringCharFeatures(strs, DNA);
        f.obtain_by_sliding_window(5,1);

        f.set_features(strs);
        DynamicIntArray positions = new DynamicIntArray();
        positions.append_element(0);
        positions.append_element(6);
        positions.append_element(16);
        positions.append_element(25);

        //f.obtain_by_position_list(8,positions);

        ArrayList result = new ArrayList();
        result.Add(f);

        modshogun.exit_shogun();
        return result;
    }
Пример #12
0
        static void Main(string[] args)
        {
            string textFile = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Values.txt");
            var    watch    = new Stopwatch();

            string[] lines = File.ReadAllLines(textFile);

            #region  Code for Generation if 1M random Value

            /*
             * try
             *  {
             *      using (StreamWriter writer = new StreamWriter(textFile))
             *          {
             *              for (int i = 1; i <= 100000; i++)
             *              {
             *                  Random r = new Random();
             *                  writer.WriteLine(r.Next(10,1000000));
             *              }
             *          }
             *  }
             * catch (Exception exp)
             *  {
             *      Console.Write(exp.Message);
             *  }*/
            #endregion

            DynamicIntArray dynamicIntArray = new DynamicIntArray(500000);
            int[]           myArray         = new int[100000];
            List <int>      myList          = new List <int>();
            ArrayList       myArrayList     = new ArrayList();


            #region DynamicIntArray Analysis
            Console.WriteLine("Performanace Anaylsis of Custom DynamicIntArray");
            watch.Start();
            int j = 1;
            foreach (string line in lines)
            {
                Console.WriteLine(j);
                dynamicIntArray.Add(Convert.ToInt32(line));
                j++;
            }
            watch.Stop();
            Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms");


            watch.Restart();
            int sum = 0;
            for (int i = 0; i < dynamicIntArray.curentSize; i++)
            {
                sum = sum + dynamicIntArray.Get(i);
            }
            watch.Stop();
            Console.WriteLine($"Sum of Elements in DynamicIntArray class:- {sum}");
            Console.WriteLine($"Execution Time for Traversal: {watch.ElapsedMilliseconds} ms");


            watch.Restart();
            Console.WriteLine($"Index of 722100:- {dynamicIntArray.indexOf(722100)}");
            Console.WriteLine($"Index of 786311:- {dynamicIntArray.indexOf(786311)}");
            Console.WriteLine($"Index of 820266:- {dynamicIntArray.indexOf(820266)}");
            Console.WriteLine($"Index of 557246:- {dynamicIntArray.indexOf(557246)}");
            Console.WriteLine($"Index of 495133:- {dynamicIntArray.indexOf(495133)}");
            watch.Stop();
            Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms");
            #endregion


            #region C# Array Analysis
            Console.WriteLine("\n\n\nPerformanace Anaylsis of C# Array Datastructure");
            watch.Start();
            j = 0;
            foreach (string line in lines)
            {
                if (myArray.Length <= j)
                {
                    Console.WriteLine("Cant Insert more In Array");
                    break;
                }
                else
                {
                    myArray[j] = (Convert.ToInt32(line));
                    j++;
                }
            }
            watch.Stop();
            Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            sum = 0;
            for (int i = 0; i < myArray.Length; i++)
            {
                sum = sum + myArray[i];
            }
            watch.Stop();
            Console.WriteLine($"Sum of Elements in Array:- {sum}");
            Console.WriteLine($"Execution Time for Traversal: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            Console.WriteLine($"Index of 722100:- {Array.FindIndex(myArray, item => item == 722100)}");
            Console.WriteLine($"Index of 786311:- {Array.FindIndex(myArray, item => item == 786311)}");
            Console.WriteLine($"Index of 820266:- {Array.FindIndex(myArray, item => item == 820266)}");
            Console.WriteLine($"Index of 557246:- {Array.FindIndex(myArray, item => item == 557246)}");
            Console.WriteLine($"Index of 495133:- {Array.FindIndex(myArray, item => item == 495133)}");
            watch.Stop();
            Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms");

            #endregion


            #region Generic List<Int> Analysis

            //Analysis for List<int> generics
            Console.WriteLine("\n\n\nPerformanace Anaylsis of System.Generics List<> ");

            watch.Start();
            foreach (string line in lines)
            {
                myList.Add(Convert.ToInt32(line));
            }
            watch.Stop();
            Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            sum = 0;
            for (int i = 0; i < myList.Count; i++)

            {
                sum = sum + myList[i];
            }
            watch.Stop();
            Console.WriteLine($"Sum of elements in List<>:- {sum}");
            Console.WriteLine($"Execution Time for Treversal: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            Console.WriteLine($"Index of 722100:- {myList.IndexOf(722100)}");
            Console.WriteLine($"Index of 786311:- {myList.IndexOf(786311)}");
            Console.WriteLine($"Index of 820266:- {myList.IndexOf(820266)}");
            Console.WriteLine($"Index of 557246:- {myList.IndexOf(557246)}");
            Console.WriteLine($"Index of 495133:- {myList.IndexOf(495133)}");
            watch.Stop();
            Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms");

            #endregion


            #region System.Collections ArrayList Analysis
            Console.WriteLine("\n\n\nPerformanace Anaylsis of System.Collection ArrayList");

            watch.Start();
            foreach (string line in lines)
            {
                myArrayList.Add(Convert.ToInt32(line));
            }
            watch.Stop();
            Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            sum = 0;
            for (int i = 0; i < myArrayList.Count; i++)
            {
                sum = sum + (int)myArrayList[i];
            }
            watch.Stop();
            Console.WriteLine($"Sum of Elements in ArrayList:- {sum}");
            Console.WriteLine($"Execution Time for Treversal: {watch.ElapsedMilliseconds} ms");


            watch.Restart();
            Console.WriteLine($"Index of 722100:- {myArrayList.IndexOf(722100)}");
            Console.WriteLine($"Index of 786311:- {myArrayList.IndexOf(786311)}");
            Console.WriteLine($"Index of 820266:- {myArrayList.IndexOf(820266)}");
            Console.WriteLine($"Index of 557246:- {myArrayList.IndexOf(557246)}");
            Console.WriteLine($"Index of 495133:- {myArrayList.IndexOf(495133)}");
            watch.Stop();
            Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms");
            #endregion
        }
Пример #13
0
        /**
         * Main algorithm.  Descriptions see "Practical Optimization"
         *
         * @param initX initial point of x, assuMing no value's on the bound!
         * @param constraints the bound constraints of each variable
         *                    constraints[0] is the lower bounds and 
         *                    constraints[1] is the upper bounds
         * @return the solution of x, null if number of iterations not enough
         * @throws Exception if an error occurs
         */
        public double[] findArgMin(double[] initX, double[,] constraints)
        {
            int l = initX.Length;

            // Initially all variables are free, all bounds are constraints of
            // non-working-set constraints
            bool[] isFixed = new bool[l];
            double[,] nwsBounds = new double[2, l];
            // Record indice of fixed variables, simply for efficiency
            DynamicIntArray wsBdsIndx = new DynamicIntArray(constraints.Length);
            // Vectors used to record the variable indices to be freed 	
            DynamicIntArray toFree = null, oldToFree = null;

            // Initial value of obj. function, gradient and inverse of the Hessian
            m_f = objectiveFunction(initX);

            double sum = 0;
            double[] grad = evaluateGradient(initX), oldGrad, oldX,
                     deltaGrad = new double[l], deltaX = new double[l],
                     direct = new double[l], x = new double[l];
            Matrix L = new Matrix(l, l);  // Lower triangle of Cholesky factor 
            double[] D = new double[l];   // Diagonal of Cholesky factor
            for (int i = 0; i < l; i++)
            {
                L.setRow(i, new double[l]);
                L.setElement(i, i, 1.0);
                D[i] = 1.0;
                direct[i] = -grad[i];
                sum += grad[i] * grad[i];
                x[i] = initX[i];
                nwsBounds[0, i] = constraints[0, i];
                nwsBounds[1, i] = constraints[1, i];
                isFixed[i] = false;
            }
            double stpMax = m_STPMX * Math.Max(Math.Sqrt(sum), l);

        iterates:
            for (int step = 0; step < m_MaxITS; step++)
            {

                // Try at most one feasible newton step, i.e. 0<lamda<=alpha
                oldX = x;
                oldGrad = grad;

                // Also update grad
                m_IsZeroStep = false;
                x = lnsrch(x, grad, direct, stpMax,
                     isFixed, nwsBounds, wsBdsIndx);


                if (m_IsZeroStep)
                { // Zero step, simply delete rows/cols of D and L
                    for (int f = 0; f < wsBdsIndx.msize(); f++)
                    {
                        int idx = wsBdsIndx.elementAt(f);
                        L.setRow(idx, new double[l]);
                        L.setColumn(idx, new double[l]);
                        D[idx] = 0.0;
                    }
                    grad = evaluateGradient(x);
                    step--;
                }
                else
                {
                    // Check converge on x
                    bool finish = false;
                    double test = 0.0;
                    for (int h = 0; h < l; h++)
                    {
                        deltaX[h] = x[h] - oldX[h];
                        double tmp = Math.Abs(deltaX[h]) /
                        Math.Max(Math.Abs(x[h]), 1.0);
                        if (tmp > test) test = tmp;
                    }
                    if (test < m_Zero)
                    {

                        finish = true;
                    }

                    // Check zero gradient	    
                    grad = evaluateGradient(x);
                    test = 0.0;
                    double denom = 0.0, dxSq = 0.0, dgSq = 0.0, newlyBounded = 0.0;
                    for (int g = 0; g < l; g++)
                    {
                        if (!isFixed[g])
                        {
                            deltaGrad[g] = grad[g] - oldGrad[g];
                            // Calculate the denoMinators			    
                            denom += deltaX[g] * deltaGrad[g];
                            dxSq += deltaX[g] * deltaX[g];
                            dgSq += deltaGrad[g] * deltaGrad[g];
                        }
                        else // Only newly bounded variables will be non-zero
                            newlyBounded += deltaX[g] * (grad[g] - oldGrad[g]);

                        // Note: CANNOT use projected gradient for testing 
                        // convergence because of newly bounded variables
                        double tmp = Math.Abs(grad[g]) *
                        Math.Max(Math.Abs(direct[g]), 1.0) /
                        Math.Max(Math.Abs(m_f), 1.0);
                        if (tmp > test) test = tmp;
                    }

                    if (test < m_Zero)
                    {
                        finish = true;
                    }

                    // dg'*dx could be < 0 using inexact lnsrch
                    // dg'*dx = 0
                    if (Math.Abs(denom + newlyBounded) < m_Zero)
                        finish = true;

                    int size = wsBdsIndx.msize();
                    bool isUpdate = true;  // Whether to update BFGS formula	    
                    // Converge: check whether release any current constraints
                    if (finish)
                    {

                        if (toFree != null)
                            oldToFree = (DynamicIntArray)toFree.copy();
                        toFree = new DynamicIntArray(wsBdsIndx.msize());

                        for (int m = size - 1; m >= 0; m--)
                        {
                            int index = wsBdsIndx.elementAt(m);
                            double[] hessian = evaluateHessian(x, index);
                            double deltaL = 0.0;
                            if (hessian != null)
                            {
                                for (int mm = 0; mm < hessian.Length; mm++)
                                    if (!isFixed[mm]) // Free variable
                                        deltaL += hessian[mm] * direct[mm];
                            }

                            // First and second order Lagrangian multiplier estimate
                            // If user didn't provide Hessian, use first-order only
                            double L1 = 0, L2 = 0;
                            if (x[index] >= constraints[1, index]) // Upper bound
                            {
                                L1 = -grad[index];
                            }
                            else
                            {
                                if (x[index] <= constraints[0, index])// Lower bound
                                {
                                    L1 = grad[index];
                                }
                            }

                            // L2 = L1 + deltaL
                            L2 = L1 + deltaL;

                            //Check validity of Lagrangian multiplier estimate
                            bool isConverge =
                                (2.0 * Math.Abs(deltaL)) < Math.Min(Math.Abs(L1),
                                                  Math.Abs(L2));
                            if ((L1 * L2 > 0.0) && isConverge)
                            { //Same sign and converge: valid
                                if (L2 < 0.0)
                                {// Negative Lagrangian: feasible
                                    toFree.addElement(index);
                                    wsBdsIndx.removeElementAt(m);
                                    finish = false; // Not optimal, cannot finish
                                }
                            }

                            // Although hardly happen, better check it
                            // If the first-order Lagrangian multiplier estimate is wrong,
                            // avoid zigzagging
                            if ((hessian == null) && (toFree != null) && toFree.Equal(oldToFree))
                                finish = true;
                        }

                        if (finish)
                        {// Min. found

                            m_f = objectiveFunction(x);

                            return x;
                        }

                        // Free some variables
                        for (int mmm = 0; mmm < toFree.msize(); mmm++)
                        {
                            int freeIndx = toFree.elementAt(mmm);
                            isFixed[freeIndx] = false; // Free this variable
                            if (x[freeIndx] <= constraints[0, freeIndx])
                            {// Lower bound
                                nwsBounds[0, freeIndx] = constraints[0, freeIndx];

                            }
                            else
                            { // Upper bound
                                nwsBounds[1, freeIndx] = constraints[1, freeIndx];

                            }
                            L.setElement(freeIndx, freeIndx, 1.0);
                            D[freeIndx] = 1.0;
                            isUpdate = false;
                        }
                    }

                    if (denom < Math.Max(m_Zero * Math.Sqrt(dxSq) * Math.Sqrt(dgSq), m_Zero))
                    {

                        isUpdate = false; // Do not update		    
                    }
                    // If Hessian will be positive definite, update it
                    if (isUpdate)
                    {

                        // modify once: dg*dg'/(dg'*dx)	
                        double coeff = 1.0 / denom; // 1/(dg'*dx)	
                        updateCholeskyFactor(L, D, deltaGrad, coeff, isFixed);

                        // modify twice: g*g'/(g'*p)	
                        coeff = 1.0 / m_Slope; // 1/(g'*p)
                        updateCholeskyFactor(L, D, oldGrad, coeff, isFixed);
                    }
                }

                // Find new direction 
                Matrix LD = new Matrix(l, l); // L*D
                double[] b = new double[l];

                for (int k = 0; k < l; k++)
                {
                    if (!isFixed[k]) b[k] = -grad[k];
                    else b[k] = 0.0;

                    for (int j = k; j < l; j++)
                    { // Lower triangle	
                        if (!isFixed[j] && !isFixed[k])
                            LD.setElement(j, k, L.getElement(j, k) * D[k]);
                    }
                }

                // Solve (LD)*y = -g, where y=L'*direct
                double[] LDIR = solveTriangle(LD, b, true, isFixed);
                LD = null;

                // Solve L'*direct = y
                direct = solveTriangle(L, LDIR, false, isFixed);

                //System.gc();
            }

            m_X = x;
            return null;
        }
Пример #14
0
        /**
         * Find a new point x in the direction p from a point xold at which the
         * value of the function has decreased sufficiently, the positive 
         * definiteness of B matrix (approximation of the inverse of the Hessian)
         * is preserved and no bound constraints are violated.  Details see "Numerical 
         * Methods for Unconstrained Optimization and Nonlinear Equations".
         * "Numeric Recipes in C" was also consulted.
         *
         * @param xold old x value 
         * @param gradient gradient at that point
         * @param direct direction vector
         * @param stpMax Maximum step .Length
         * @param isFixed indicating whether a variable has been fixed
         * @param nwsBounds non-working set bounds.  Means these variables are free and
         *                  subject to the bound constraints in this step
         * @param wsBdsIndx index of variables that has working-set bounds.  Means
         *                  these variables are already fixed and no longer subject to
         *                  the constraints
         * @return new value along direction p from xold, null if no step was taken
         * @throws Exception if an error occurs
         */
        public double[] lnsrch(double[] xold, double[] gradient,
                   double[] direct, double stpMax,
                   bool[] isFixed, double[,] nwsBounds,
                   DynamicIntArray wsBdsIndx)
        {
            int i, k, len = xold.Length,
            fixedOne = -1; // idx of variable to be fixed
            double alam, alaMin; // lambda to be found, and its lower bound

            // For convergence and bound test
            double temp, test, alpha = Double.PositiveInfinity, fold = m_f, sum;

            // For cubic interpolation
            double a, alam2 = 0, b, disc = 0, Maxalam = 1.0, rhs1, rhs2, tmplam;

            double[] x = new double[len]; // New variable values

            // Scale the step 
            for (sum = 0.0, i = 0; i < len; i++)
            {
                if (!isFixed[i]) // For fixed variables, direction = 0
                    sum += direct[i] * direct[i];
            }
            sum = Math.Sqrt(sum);

            if (sum > stpMax)
            {
                for (i = 0; i < len; i++)
                    if (!isFixed[i])
                        direct[i] *= stpMax / sum;
            }
            else
                Maxalam = stpMax / sum;

            // Compute initial rate of decrease, g'*d 
            m_Slope = 0.0;
            for (i = 0; i < len; i++)
            {
                x[i] = xold[i];
                if (!isFixed[i])
                    m_Slope += gradient[i] * direct[i];
            }



            // Slope too small
            if (Math.Abs(m_Slope) <= m_Zero)
            {
                return x;
            }

            // Err: slope > 0

            // Compute LAMBDAMin and upper bound of lambda--alpha
            test = 0.0;
            for (i = 0; i < len; i++)
            {
                if (!isFixed[i])
                {// No need for fixed variables
                    temp = Math.Abs(direct[i]) / Math.Max(Math.Abs(x[i]), 1.0);
                    if (temp > test) test = temp;
                }
            }

            if (test > m_Zero) // Not converge
                alaMin = m_TOLX / test;
            else
            {
                return x;
            }

            // Check whether any non-working-set bounds are "binding"
            for (i = 0; i < len; i++)
            {
                if (!isFixed[i])
                {// No need for fixed variables
                    double alpi;
                    if ((direct[i] < -m_Epsilon) && !Double.IsNaN(nwsBounds[0, i]))
                    {//Not feasible
                        alpi = (nwsBounds[0, i] - xold[i]) / direct[i];
                        if (alpi <= m_Zero)
                        { // Zero
                            x[i] = nwsBounds[0, i];
                            isFixed[i] = true; // Fix this variable
                            alpha = 0.0;
                            nwsBounds[0, i] = Double.NaN; //Add cons. to working set
                            wsBdsIndx.addElement(i);
                        }
                        else if (alpha > alpi)
                        { // Fix one variable in one iteration
                            alpha = alpi;
                            fixedOne = i;
                        }
                    }
                    else if ((direct[i] > m_Epsilon) && !Double.IsNaN(nwsBounds[1, i]))
                    {//Not feasible
                        alpi = (nwsBounds[1, i] - xold[i]) / direct[i];
                        if (alpi <= m_Zero)
                        { // Zero
                            x[i] = nwsBounds[1, i];
                            isFixed[i] = true; // Fix this variable
                            alpha = 0.0;
                            nwsBounds[1, i] = Double.NaN; //Add cons. to working set
                            wsBdsIndx.addElement(i);
                        }
                        else if (alpha > alpi)
                        {
                            alpha = alpi;
                            fixedOne = i;
                        }
                    }
                }
            }



            if (alpha <= m_Zero)
            { // Zero	   
                m_IsZeroStep = true;
                return x;
            }

            alam = alpha; // Always try full feasible newton step 
            if (alam > 1.0)
                alam = 1.0;

            // Iteration of one newton step, if necessary, backtracking is done
            double initF = fold, // Initial function value
                hi = alam, lo = alam, newSlope = 0, fhi = m_f, flo = m_f;// Variables used for beta condition
            double[] newGrad;  // Gradient on the new variable values
            bool control = true;
        kloop:
            for (k = 0; control == true; k++)
            {

                for (i = 0; i < len; i++)
                {
                    if (!isFixed[i])
                    {
                        x[i] = xold[i] + alam * direct[i];  // Compute xnew
                        if (!Double.IsNaN(nwsBounds[0, i]) && (x[i] < nwsBounds[0, i]))
                        {
                            x[i] = nwsBounds[0, i]; //Rounding error	
                        }
                        else if (!Double.IsNaN(nwsBounds[1, i]) && (x[i] > nwsBounds[1, i]))
                        {
                            x[i] = nwsBounds[1, i]; //Rounding error	
                        }
                    }
                }

                m_f = objectiveFunction(x);    // Compute fnew

                while (Double.IsPositiveInfinity(m_f))
                { // Avoid infinity

                    alam *= 0.5; // Shrink by half
                    if (alam <= m_Epsilon)
                    {

                        return x;
                    }

                    for (i = 0; i < len; i++)
                        if (!isFixed[i])
                            x[i] = xold[i] + alam * direct[i];

                    m_f = objectiveFunction(x);

                    initF = Double.PositiveInfinity;
                }

                if (m_f <= fold + m_ALF * alam * m_Slope)
                {// Alpha condition: sufficient function decrease
                    newGrad = evaluateGradient(x);
                    for (newSlope = 0.0, i = 0; i < len; i++)
                        if (!isFixed[i])
                            newSlope += newGrad[i] * direct[i];

                    if (newSlope >= m_BETA * m_Slope)
                    { // Beta condition: ensure pos. defnty.	

                        if ((fixedOne != -1) && (alam >= alpha))
                        { // Has bounds and over
                            if (direct[fixedOne] > 0)
                            {
                                x[fixedOne] = nwsBounds[1, fixedOne]; // Avoid rounding error
                                nwsBounds[1, fixedOne] = Double.NaN; //Add cons. to working set
                            }
                            else
                            {
                                x[fixedOne] = nwsBounds[0, fixedOne]; // Avoid rounding error
                                nwsBounds[0, fixedOne] = Double.NaN; //Add cons. to working set
                            }

                            isFixed[fixedOne] = true; // Fix the variable
                            wsBdsIndx.addElement(fixedOne);
                        }
                        return x;
                    }
                    else if (k == 0)
                    { // First time: increase alam 
                        // Search for the smallest value not complying with alpha condition
                        double upper = Math.Min(alpha, Maxalam);
                        while (!((alam >= upper) || (m_f > fold + m_ALF * alam * m_Slope)))
                        {
                            lo = alam;
                            flo = m_f;
                            alam *= 2.0;
                            if (alam >= upper)  // Avoid rounding errors
                                alam = upper;

                            for (i = 0; i < len; i++)
                                if (!isFixed[i])
                                    x[i] = xold[i] + alam * direct[i];
                            m_f = objectiveFunction(x);

                            newGrad = evaluateGradient(x);
                            for (newSlope = 0.0, i = 0; i < len; i++)
                                if (!isFixed[i])
                                    newSlope += newGrad[i] * direct[i];

                            if (newSlope >= m_BETA * m_Slope)
                            {
                                if ((fixedOne != -1) && (alam >= alpha))
                                { // Has bounds and over
                                    if (direct[fixedOne] > 0)
                                    {
                                        x[fixedOne] = nwsBounds[1, fixedOne]; // Avoid rounding error
                                        nwsBounds[1, fixedOne] = Double.NaN; //Add cons. to working set
                                    }
                                    else
                                    {
                                        x[fixedOne] = nwsBounds[0, fixedOne]; // Avoid rounding error
                                        nwsBounds[0, fixedOne] = Double.NaN; //Add cons. to working set
                                    }

                                    isFixed[fixedOne] = true; // Fix the variable
                                    wsBdsIndx.addElement(fixedOne);
                                }
                                return x;
                            }
                        }
                        hi = alam;
                        fhi = m_f;
                        control = false;
                        goto kloop;
                    }
                    else
                    {
                        hi = alam2; lo = alam; flo = m_f;
                        control = false;
                        goto kloop;
                    }
                }
                else if (alam < alaMin)
                { // No feasible lambda found       
                    if (initF < fold)
                    {
                        alam = Math.Min(1.0, alpha);
                        for (i = 0; i < len; i++)
                            if (!isFixed[i])
                                x[i] = xold[i] + alam * direct[i]; //Still take Alpha

                        if ((fixedOne != -1) && (alam >= alpha))
                        { // Has bounds and over
                            if (direct[fixedOne] > 0)
                            {
                                x[fixedOne] = nwsBounds[1, fixedOne]; // Avoid rounding error
                                nwsBounds[1, fixedOne] = Double.NaN; //Add cons. to working set
                            }
                            else
                            {
                                x[fixedOne] = nwsBounds[0, fixedOne]; // Avoid rounding error
                                nwsBounds[0, fixedOne] = Double.NaN; //Add cons. to working set
                            }

                            isFixed[fixedOne] = true; // Fix the variable
                            wsBdsIndx.addElement(fixedOne);
                        }
                    }
                    else
                    {   // Convergence on delta(x)
                        for (i = 0; i < len; i++)
                            x[i] = xold[i];
                        m_f = fold;
                    }

                    return x;
                }
                else
                { // Backtracking by polynomial interpolation
                    if (k == 0)
                    { // First time backtrack: quadratic interpolation
                        if (!Double.IsPositiveInfinity(initF))
                            initF = m_f;
                        // lambda = -g'(0)/(2*g''(0))
                        tmplam = -0.5 * alam * m_Slope / ((m_f - fold) / alam - m_Slope);
                    }
                    else
                    {    // Subsequent backtrack: cubic interpolation 
                        rhs1 = m_f - fold - alam * m_Slope;
                        rhs2 = fhi - fold - alam2 * m_Slope;
                        a = (rhs1 / (alam * alam) - rhs2 / (alam2 * alam2)) / (alam - alam2);
                        b = (-alam2 * rhs1 / (alam * alam) + alam * rhs2 / (alam2 * alam2)) / (alam - alam2);
                        if (a == 0.0) tmplam = -m_Slope / (2.0 * b);
                        else
                        {
                            disc = b * b - 3.0 * a * m_Slope;
                            if (disc < 0.0) disc = 0.0;
                            double numerator = -b + Math.Sqrt(disc);
                            if (numerator >= Double.MaxValue)
                            {
                                numerator = Double.MaxValue;
                            }
                            tmplam = numerator / (3.0 * a);
                        }
                        if (tmplam > 0.5 * alam)
                            tmplam = 0.5 * alam;             // lambda <= 0.5*lambda_old
                    }
                }
                alam2 = alam;
                fhi = m_f;
                alam = Math.Max(tmplam, 0.1 * alam);          // lambda >= 0.1*lambda_old

            } // Endfor(k=0;;k++)

            // Quadratic interpolation between lamda values between lo and hi.
            // If cannot find a value satisfying beta condition, use lo.
            double ldiff = hi - lo, lincr;

            while ((newSlope < m_BETA * m_Slope) && (ldiff >= alaMin))
            {
                lincr = -0.5 * newSlope * ldiff * ldiff / (fhi - flo - newSlope * ldiff);

                if (lincr < 0.2 * ldiff) lincr = 0.2 * ldiff;
                alam = lo + lincr;
                if (alam >= hi)
                { // We cannot go beyond the bounds, so the best we can try is hi
                    alam = hi;
                    lincr = ldiff;
                }
                for (i = 0; i < len; i++)
                    if (!isFixed[i])
                        x[i] = xold[i] + alam * direct[i];
                m_f = objectiveFunction(x);

                if (m_f > fold + m_ALF * alam * m_Slope)
                {
                    // Alpha condition fails, shrink lambda_upper
                    ldiff = lincr;
                    fhi = m_f;
                }
                else
                { // Alpha condition holds	    
                    newGrad = evaluateGradient(x);
                    for (newSlope = 0.0, i = 0; i < len; i++)
                        if (!isFixed[i])
                            newSlope += newGrad[i] * direct[i];

                    if (newSlope < m_BETA * m_Slope)
                    {
                        // Beta condition fails, shrink lambda_lower
                        lo = alam;
                        ldiff -= lincr;
                        flo = m_f;
                    }
                }
            }

            if (newSlope < m_BETA * m_Slope)
            { // Cannot satisfy beta condition, take lo
                alam = lo;
                for (i = 0; i < len; i++)
                    if (!isFixed[i])
                        x[i] = xold[i] + alam * direct[i];
                m_f = flo;
            }

            if ((fixedOne != -1) && (alam >= alpha))
            { // Has bounds and over
                if (direct[fixedOne] > 0)
                {
                    x[fixedOne] = nwsBounds[1, fixedOne]; // Avoid rounding error
                    nwsBounds[1, fixedOne] = Double.NaN; //Add cons. to working set
                }
                else
                {
                    x[fixedOne] = nwsBounds[0, fixedOne]; // Avoid rounding error
                    nwsBounds[0, fixedOne] = Double.NaN; //Add cons. to working set
                }

                isFixed[fixedOne] = true; // Fix the variable
                wsBdsIndx.addElement(fixedOne);
            }

            return x;
        }
Пример #15
0
		/// <summary> Find a new point x in the direction p from a point xold at which the
		/// value of the function has decreased sufficiently, the positive 
		/// definiteness of B matrix (approximation of the inverse of the Hessian)
		/// is preserved and no bound constraints are violated.  Details see "Numerical 
		/// Methods for Unconstrained Optimization and Nonlinear Equations".
		/// "Numeric Recipes in C" was also consulted.
		/// 
		/// </summary>
		/// <param name="xold">old x value 
		/// </param>
		/// <param name="gradient">gradient at that point
		/// </param>
		/// <param name="direct">direction vector
		/// </param>
		/// <param name="stpmax">maximum step length
		/// </param>
		/// <param name="isFixed">indicating whether a variable has been fixed
		/// </param>
		/// <param name="nwsBounds">non-working set bounds.  Means these variables are free and
		/// subject to the bound constraints in this step
		/// </param>
		/// <param name="wsBdsIndx">index of variables that has working-set bounds.  Means
		/// these variables are already fixed and no longer subject to
		/// the constraints
		/// </param>
		/// <returns> new value along direction p from xold, null if no step was taken
		/// </returns>
		/// <exception cref="Exception">if an error occurs
		/// </exception>
		public virtual double[] lnsrch(double[] xold, double[] gradient, double[] direct, double stpmax, bool[] isFixed, double[][] nwsBounds, DynamicIntArray wsBdsIndx)
		{
			
			int i, j, k, len = xold.Length, fixedOne = - 1; // idx of variable to be fixed
			double alam, alamin; // lambda to be found, and its lower bound
			
			// For convergence and bound test
			double temp, test, alpha = System.Double.PositiveInfinity, fold = m_f, sum;
			
			// For cubic interpolation
			double a, alam2 = 0, b, disc = 0, maxalam = 1.0, rhs1, rhs2, tmplam;
			
			double[] x = new double[len]; // New variable values
			
			// Scale the step 
			for (sum = 0.0, i = 0; i < len; i++)
			{
				if (!isFixed[i])
				// For fixed variables, direction = 0
					sum += direct[i] * direct[i];
			}
			sum = System.Math.Sqrt(sum);
			
			if (m_Debug)
				System.Console.Error.WriteLine("fold:  " + Utils.doubleToString(fold, 10, 7) + "\n" + "sum:  " + Utils.doubleToString(sum, 10, 7) + "\n" + "stpmax:  " + Utils.doubleToString(stpmax, 10, 7));
			if (sum > stpmax)
			{
				for (i = 0; i < len; i++)
					if (!isFixed[i])
						direct[i] *= stpmax / sum;
			}
			else
				maxalam = stpmax / sum;
			
			// Compute initial rate of decrease, g'*d 
			m_Slope = 0.0;
			for (i = 0; i < len; i++)
			{
				x[i] = xold[i];
				if (!isFixed[i])
					m_Slope += gradient[i] * direct[i];
			}
			
			if (m_Debug)
				System.Console.Error.Write("slope:  " + Utils.doubleToString(m_Slope, 10, 7) + "\n");
			
			// Slope too small
			if (System.Math.Abs(m_Slope) <= m_Zero)
			{
				if (m_Debug)
					System.Console.Error.WriteLine("Gradient and direction orthogonal -- " + "Min. found with current fixed variables" + " (or all variables fixed). Try to release" + " some variables now.");
				return x;
			}
			
			// Err: slope > 0
			if (m_Slope > m_Zero)
			{
				if (m_Debug)
					for (int h = 0; h < x.Length; h++)
						System.Console.Error.WriteLine(h + ": isFixed=" + isFixed[h] + ", x=" + x[h] + ", grad=" + gradient[h] + ", direct=" + direct[h]);
				throw new System.Exception("g'*p positive! -- Try to debug from here: line 327.");
			}
			
			// Compute LAMBDAmin and upper bound of lambda--alpha
			test = 0.0;
			for (i = 0; i < len; i++)
			{
				if (!isFixed[i])
				{
					// No need for fixed variables
					temp = System.Math.Abs(direct[i]) / System.Math.Max(System.Math.Abs(x[i]), 1.0);
					if (temp > test)
						test = temp;
				}
			}
			
			if (test > m_Zero)
			// Not converge
				alamin = m_TOLX / test;
			else
			{
				if (m_Debug)
					System.Console.Error.WriteLine("Zero directions for all free variables -- " + "Min. found with current fixed variables" + " (or all variables fixed). Try to release" + " some variables now.");
				return x;
			}
			
			// Check whether any non-working-set bounds are "binding"
			for (i = 0; i < len; i++)
			{
				if (!isFixed[i])
				{
					// No need for fixed variables
					double alpi;
					if ((direct[i] < - m_Epsilon) && !System.Double.IsNaN(nwsBounds[0][i]))
					{
						//Not feasible
						alpi = (nwsBounds[0][i] - xold[i]) / direct[i];
						if (alpi <= m_Zero)
						{
							// Zero
							if (m_Debug)
								System.Console.Error.WriteLine("Fix variable " + i + " to lower bound " + nwsBounds[0][i] + " from value " + xold[i]);
							x[i] = nwsBounds[0][i];
							isFixed[i] = true; // Fix this variable
							alpha = 0.0;
							nwsBounds[0][i] = System.Double.NaN; //Add cons. to working set
							wsBdsIndx.addElement(i);
						}
						else if (alpha > alpi)
						{
							// Fix one variable in one iteration
							alpha = alpi;
							fixedOne = i;
						}
					}
					else if ((direct[i] > m_Epsilon) && !System.Double.IsNaN(nwsBounds[1][i]))
					{
						//Not feasible
						alpi = (nwsBounds[1][i] - xold[i]) / direct[i];
						if (alpi <= m_Zero)
						{
							// Zero
							if (m_Debug)
								System.Console.Error.WriteLine("Fix variable " + i + " to upper bound " + nwsBounds[1][i] + " from value " + xold[i]);
							x[i] = nwsBounds[1][i];
							isFixed[i] = true; // Fix this variable
							alpha = 0.0;
							nwsBounds[1][i] = System.Double.NaN; //Add cons. to working set
							wsBdsIndx.addElement(i);
						}
						else if (alpha > alpi)
						{
							alpha = alpi;
							fixedOne = i;
						}
					}
				}
			}
			
			if (m_Debug)
			{
				System.Console.Error.WriteLine("alamin: " + Utils.doubleToString(alamin, 10, 7));
				System.Console.Error.WriteLine("alpha: " + Utils.doubleToString(alpha, 10, 7));
			}
			
			if (alpha <= m_Zero)
			{
				// Zero	   
				m_IsZeroStep = true;
				if (m_Debug)
					System.Console.Error.WriteLine("Alpha too small, try again");
				return x;
			}
			
			alam = alpha; // Always try full feasible newton step 
			if (alam > 1.0)
				alam = 1.0;
			
			// Iteration of one newton step, if necessary, backtracking is done
			double initF = fold, hi = alam, lo = alam, newSlope = 0, fhi = m_f, flo = m_f; // Variables used for beta condition
			double[] newGrad; // Gradient on the new variable values
			
			for (k = 0; ; k++)
			{
				if (m_Debug)
					System.Console.Error.WriteLine("\nLine search iteration: " + k);
				
				for (i = 0; i < len; i++)
				{
					if (!isFixed[i])
					{
						x[i] = xold[i] + alam * direct[i]; // Compute xnew
						if (!System.Double.IsNaN(nwsBounds[0][i]) && (x[i] < nwsBounds[0][i]))
						{
							x[i] = nwsBounds[0][i]; //Rounding error	
						}
						else if (!System.Double.IsNaN(nwsBounds[1][i]) && (x[i] > nwsBounds[1][i]))
						{
							x[i] = nwsBounds[1][i]; //Rounding error	
						}
					}
				}
				
				m_f = objectiveFunction(x); // Compute fnew
				if (System.Double.IsNaN(m_f))
					throw new System.Exception("Objective function value is NaN!");
				
				while (System.Double.IsInfinity(m_f))
				{
					// Avoid infinity
					if (m_Debug)
						System.Console.Error.WriteLine("Too large m_f.  Shrink step by half.");
					alam *= 0.5; // Shrink by half
					if (alam <= m_Epsilon)
					{
						if (m_Debug)
							System.Console.Error.WriteLine("Wrong starting points, change them!");
						return x;
					}
					
					for (i = 0; i < len; i++)
						if (!isFixed[i])
							x[i] = xold[i] + alam * direct[i];
					
					m_f = objectiveFunction(x);
					if (System.Double.IsNaN(m_f))
						throw new System.Exception("Objective function value is NaN!");
					
					initF = System.Double.PositiveInfinity;
				}
				
				if (m_Debug)
				{
					System.Console.Error.WriteLine("obj. function: " + Utils.doubleToString(m_f, 10, 7));
					System.Console.Error.WriteLine("threshold: " + Utils.doubleToString(fold + m_ALF * alam * m_Slope, 10, 7));
				}
				
				if (m_f <= fold + m_ALF * alam * m_Slope)
				{
					// Alpha condition: sufficient function decrease
					if (m_Debug)
						System.Console.Error.WriteLine("Sufficient function decrease (alpha condition): ");
					newGrad = evaluateGradient(x);
					for (newSlope = 0.0, i = 0; i < len; i++)
						if (!isFixed[i])
							newSlope += newGrad[i] * direct[i];
					
					if (newSlope >= m_BETA * m_Slope)
					{
						// Beta condition: ensure pos. defnty.	
						if (m_Debug)
							System.Console.Error.WriteLine("Increasing derivatives (beta condition): ");
						
						if ((fixedOne != - 1) && (alam >= alpha))
						{
							// Has bounds and over
							if (direct[fixedOne] > 0)
							{
								x[fixedOne] = nwsBounds[1][fixedOne]; // Avoid rounding error
								nwsBounds[1][fixedOne] = System.Double.NaN; //Add cons. to working set
							}
							else
							{
								x[fixedOne] = nwsBounds[0][fixedOne]; // Avoid rounding error
								nwsBounds[0][fixedOne] = System.Double.NaN; //Add cons. to working set
							}
							
							if (m_Debug)
								System.Console.Error.WriteLine("Fix variable " + fixedOne + " to bound " + x[fixedOne] + " from value " + xold[fixedOne]);
							isFixed[fixedOne] = true; // Fix the variable
							wsBdsIndx.addElement(fixedOne);
						}
						return x;
					}
					else if (k == 0)
					{
						// First time: increase alam 
						// Search for the smallest value not complying with alpha condition
						double upper = System.Math.Min(alpha, maxalam);
						if (m_Debug)
							System.Console.Error.WriteLine("Alpha condition holds, increase alpha... ");
						while (!((alam >= upper) || (m_f > fold + m_ALF * alam * m_Slope)))
						{
							lo = alam;
							flo = m_f;
							alam *= 2.0;
							if (alam >= upper)
							// Avoid rounding errors
								alam = upper;
							
							for (i = 0; i < len; i++)
								if (!isFixed[i])
									x[i] = xold[i] + alam * direct[i];
							m_f = objectiveFunction(x);
							if (System.Double.IsNaN(m_f))
								throw new System.Exception("Objective function value is NaN!");
							
							newGrad = evaluateGradient(x);
							for (newSlope = 0.0, i = 0; i < len; i++)
								if (!isFixed[i])
									newSlope += newGrad[i] * direct[i];
							
							if (newSlope >= m_BETA * m_Slope)
							{
								if (m_Debug)
									System.Console.Error.WriteLine("Increasing derivatives (beta condition): \n" + "newSlope = " + Utils.doubleToString(newSlope, 10, 7));
								
								if ((fixedOne != - 1) && (alam >= alpha))
								{
									// Has bounds and over
									if (direct[fixedOne] > 0)
									{
										x[fixedOne] = nwsBounds[1][fixedOne]; // Avoid rounding error
										nwsBounds[1][fixedOne] = System.Double.NaN; //Add cons. to working set
									}
									else
									{
										x[fixedOne] = nwsBounds[0][fixedOne]; // Avoid rounding error
										nwsBounds[0][fixedOne] = System.Double.NaN; //Add cons. to working set
									}
									
									if (m_Debug)
										System.Console.Error.WriteLine("Fix variable " + fixedOne + " to bound " + x[fixedOne] + " from value " + xold[fixedOne]);
									isFixed[fixedOne] = true; // Fix the variable
									wsBdsIndx.addElement(fixedOne);
								}
								return x;
							}
						}
						hi = alam;
						fhi = m_f;
						//UPGRADE_NOTE: Labeled break statement was changed to a goto statement. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1012'"
						goto kloop_brk;
					}
					else
					{
						if (m_Debug)
							System.Console.Error.WriteLine("Alpha condition holds.");
						hi = alam2; lo = alam; flo = m_f;
						//UPGRADE_NOTE: Labeled break statement was changed to a goto statement. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1012'"
						goto kloop_brk;
					}
				}
				else if (alam < alamin)
				{
					// No feasible lambda found       
					if (initF < fold)
					{
						alam = System.Math.Min(1.0, alpha);
						for (i = 0; i < len; i++)
							if (!isFixed[i])
								x[i] = xold[i] + alam * direct[i]; //Still take Alpha
						
						if (m_Debug)
							System.Console.Error.WriteLine("No feasible lambda: still take" + " alpha=" + alam);
						
						if ((fixedOne != - 1) && (alam >= alpha))
						{
							// Has bounds and over
							if (direct[fixedOne] > 0)
							{
								x[fixedOne] = nwsBounds[1][fixedOne]; // Avoid rounding error
								nwsBounds[1][fixedOne] = System.Double.NaN; //Add cons. to working set
							}
							else
							{
								x[fixedOne] = nwsBounds[0][fixedOne]; // Avoid rounding error
								nwsBounds[0][fixedOne] = System.Double.NaN; //Add cons. to working set
							}
							
							if (m_Debug)
								System.Console.Error.WriteLine("Fix variable " + fixedOne + " to bound " + x[fixedOne] + " from value " + xold[fixedOne]);
							isFixed[fixedOne] = true; // Fix the variable
							wsBdsIndx.addElement(fixedOne);
						}
					}
					else
					{
						// Convergence on delta(x)
						for (i = 0; i < len; i++)
							x[i] = xold[i];
						m_f = fold;
						if (m_Debug)
							System.Console.Error.WriteLine("Cannot find feasible lambda");
					}
					
					return x;
				}
				else
				{
					// Backtracking by polynomial interpolation
					if (k == 0)
					{
						// First time backtrack: quadratic interpolation
						if (!System.Double.IsInfinity(initF))
							initF = m_f;
						// lambda = -g'(0)/(2*g''(0))
						tmplam = (- 0.5) * alam * m_Slope / ((m_f - fold) / alam - m_Slope);
					}
					else
					{
						// Subsequent backtrack: cubic interpolation 
						rhs1 = m_f - fold - alam * m_Slope;
						rhs2 = fhi - fold - alam2 * m_Slope;
						a = (rhs1 / (alam * alam) - rhs2 / (alam2 * alam2)) / (alam - alam2);
						b = ((- alam2) * rhs1 / (alam * alam) + alam * rhs2 / (alam2 * alam2)) / (alam - alam2);
						if (a == 0.0)
							tmplam = (- m_Slope) / (2.0 * b);
						else
						{
							disc = b * b - 3.0 * a * m_Slope;
							if (disc < 0.0)
								disc = 0.0;
							double numerator = - b + System.Math.Sqrt(disc);
							//UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
							if (numerator >= System.Double.MaxValue)
							{
								//UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
								numerator = System.Double.MaxValue;
								if (m_Debug)
									System.Console.Error.Write("-b+sqrt(disc) too large! Set it to MAX_VALUE.");
							}
							tmplam = numerator / (3.0 * a);
						}
						if (m_Debug)
							System.Console.Error.Write("Cubic interpolation: \n" + "a:   " + Utils.doubleToString(a, 10, 7) + "\n" + "b:   " + Utils.doubleToString(b, 10, 7) + "\n" + "disc:   " + Utils.doubleToString(disc, 10, 7) + "\n" + "tmplam:   " + tmplam + "\n" + "alam:   " + Utils.doubleToString(alam, 10, 7) + "\n");
						if (tmplam > 0.5 * alam)
							tmplam = 0.5 * alam; // lambda <= 0.5*lambda_old
					}
				}
				alam2 = alam;
				fhi = m_f;
				alam = System.Math.Max(tmplam, 0.1 * alam); // lambda >= 0.1*lambda_old
				
				if (alam > alpha)
				{
					throw new System.Exception("Sth. wrong in lnsrch:" + "Lambda infeasible!(lambda=" + alam + ", alpha=" + alpha + ", upper=" + tmplam + "|" + ((- alpha) * m_Slope / (2.0 * ((m_f - fold) / alpha - m_Slope))) + ", m_f=" + m_f + ", fold=" + fold + ", slope=" + m_Slope);
				}
			}
			//UPGRADE_NOTE: Label 'kloop_brk' was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1011'"

kloop_brk: ;
			 // Endfor(k=0;;k++)
			
			// Quadratic interpolation between lamda values between lo and hi.
			// If cannot find a value satisfying beta condition, use lo.
			double ldiff = hi - lo, lincr;
			if (m_Debug)
				System.Console.Error.WriteLine("Last stage of searching for beta condition (alam between " + Utils.doubleToString(lo, 10, 7) + " and " + Utils.doubleToString(hi, 10, 7) + ")...\n" + "Quadratic Interpolation(QI):\n" + "Last newSlope = " + Utils.doubleToString(newSlope, 10, 7));
			
			while ((newSlope < m_BETA * m_Slope) && (ldiff >= alamin))
			{
				lincr = (- 0.5) * newSlope * ldiff * ldiff / (fhi - flo - newSlope * ldiff);
				
				if (m_Debug)
					System.Console.Error.WriteLine("fhi = " + fhi + "\n" + "flo = " + flo + "\n" + "ldiff = " + ldiff + "\n" + "lincr (using QI) = " + lincr + "\n");
				
				if (lincr < 0.2 * ldiff)
					lincr = 0.2 * ldiff;
				alam = lo + lincr;
				if (alam >= hi)
				{
					// We cannot go beyond the bounds, so the best we can try is hi
					alam = hi;
					lincr = ldiff;
				}
				for (i = 0; i < len; i++)
					if (!isFixed[i])
						x[i] = xold[i] + alam * direct[i];
				m_f = objectiveFunction(x);
				if (System.Double.IsNaN(m_f))
					throw new System.Exception("Objective function value is NaN!");
				
				if (m_f > fold + m_ALF * alam * m_Slope)
				{
					// Alpha condition fails, shrink lambda_upper
					ldiff = lincr;
					fhi = m_f;
				}
				else
				{
					// Alpha condition holds	    
					newGrad = evaluateGradient(x);
					for (newSlope = 0.0, i = 0; i < len; i++)
						if (!isFixed[i])
							newSlope += newGrad[i] * direct[i];
					
					if (newSlope < m_BETA * m_Slope)
					{
						// Beta condition fails, shrink lambda_lower
						lo = alam;
						ldiff -= lincr;
						flo = m_f;
					}
				}
			}
			
			if (newSlope < m_BETA * m_Slope)
			{
				// Cannot satisfy beta condition, take lo
				if (m_Debug)
					System.Console.Error.WriteLine("Beta condition cannot be satisfied, take alpha condition");
				alam = lo;
				for (i = 0; i < len; i++)
					if (!isFixed[i])
						x[i] = xold[i] + alam * direct[i];
				m_f = flo;
			}
			else if (m_Debug)
				System.Console.Error.WriteLine("Both alpha and beta conditions are satisfied. alam=" + Utils.doubleToString(alam, 10, 7));
			
			if ((fixedOne != - 1) && (alam >= alpha))
			{
				// Has bounds and over
				if (direct[fixedOne] > 0)
				{
					x[fixedOne] = nwsBounds[1][fixedOne]; // Avoid rounding error
					nwsBounds[1][fixedOne] = System.Double.NaN; //Add cons. to working set
				}
				else
				{
					x[fixedOne] = nwsBounds[0][fixedOne]; // Avoid rounding error
					nwsBounds[0][fixedOne] = System.Double.NaN; //Add cons. to working set
				}
				
				if (m_Debug)
					System.Console.Error.WriteLine("Fix variable " + fixedOne + " to bound " + x[fixedOne] + " from value " + xold[fixedOne]);
				isFixed[fixedOne] = true; // Fix the variable
				wsBdsIndx.addElement(fixedOne);
			}
			
			return x;
		}
Пример #16
0
 public int obtain_by_position_list(int window_size, DynamicIntArray positions) {
   int ret = modshogunPINVOKE.StringShortRealFeatures_obtain_by_position_list__SWIG_1(swigCPtr, window_size, DynamicIntArray.getCPtr(positions));
   if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve();
   return ret;
 }
Пример #17
0
 internal static HandleRef getCPtr(DynamicIntArray obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
Пример #18
0
 /**
  * Create a set with the given initial capacity.
  */
 public RedBlackTree(int initialCapacity)
 {
     data = new DynamicIntArray(initialCapacity * ELEMENT_SIZE);
 }
Пример #19
0
			/// <summary> Check whether the two integer vectors equal to each other
			/// Two integer vectors are equal if all the elements are the 
			/// same, regardless of the order of the elements
			/// 
			/// </summary>
			/// <param name="a">one integer vector
			/// </param>
			/// <param name="b">another integer vector
			/// </param>
			/// <returns> whether they are equal
			/// </returns>
			public bool equal(DynamicIntArray b)
			{
				if ((b == null) || (size() != b.size()))
					return false;
				
				int mysize = size();
				
				// Only values matter, order does not matter
				int[] sorta = Utils.sort(m_Objects), sortb = Utils.sort(b.m_Objects);
                for (int j = 0; j < mysize; j++)
					if (m_Objects[sorta[j]] != b.m_Objects[sortb[j]])
						return false;
				
				return true;
			}
Пример #20
0
            /**
             * Check whether the two integer vectors equal to each other
             * Two integer vectors are equal if all the elements are the 
             * same, regardless of the order of the elements
             *
             * @param b another integer vector
             * @return whether they are equal
             */
            public bool Equal(DynamicIntArray b)
            {

                if ((b == null) || (msize() != b.msize()))
                    return false;

                int size = msize();

                // Only values matter, order does not matter
                int[] sorta = m_Objects,
                      sortb = b.m_Objects;
                for (int j = 0; j < size; j++)
                    if (m_Objects[sorta[j]] != b.m_Objects[sortb[j]])
                        return false;

                return true;
            }
Пример #21
0
        static void Main(string[] args)
        {
            int             oneMill = 1000000;
            DynamicIntArray Darr    = new DynamicIntArray(oneMill);

            int []     Carray = new int [oneMill];
            ArrayList  Aarray = new ArrayList(oneMill);
            List <int> Larray = new List <int>(oneMill);
            Random     rand   = new Random();

            for (int i = 0; i < oneMill; i++)
            {
                int r = rand.Next(10000);

                Carray[i] = r;
                Aarray.Add(r);
                Larray.Add(r);
                Darr.Add(r);
            }

            // Traversal
            int       sum       = 0;
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            foreach (int i in Carray)
            {
                sum = sum + i;
            }
            stopWatch.Stop();

            long ts = stopWatch.ElapsedMilliseconds;

            Console.WriteLine("C# Array        :  Sum " + sum + " Time " + ts + " MilliSeconds");
            //   Console.WriteLine(sum + " " + seconds  *1000000 + "   " );
            //   Console.WriteLine(date2.Subtract(date1).TotalMilliseconds.ToString());

            stopWatch.Reset();

            ts  = 0;
            sum = 0;
            stopWatch.Start();
            foreach (int i in Aarray)
            {
                sum = sum + i;
            }
            stopWatch.Stop();
            ts = stopWatch.ElapsedMilliseconds;
            Console.WriteLine("ArrayList       :  Sum " + sum + " Time " + ts + " MilliSeconds");


            stopWatch.Reset();

            sum = 0;
            ts  = 0;
            stopWatch.Start();
            foreach (int i in Larray)
            {
                sum = sum + i;
            }
            stopWatch.Stop();
            ts = stopWatch.ElapsedMilliseconds;
            Console.WriteLine("LIST            :  Sum " + sum + " Time " + ts + " MilliSeconds");



            stopWatch.Reset();

            sum = 0;
            ts  = 0;
            stopWatch.Start();
            for (int i = 0; i < oneMill; i++)
            {
                sum = sum + Darr.Get(i);
            }
            stopWatch.Stop();
            ts = stopWatch.ElapsedMilliseconds;
            Console.WriteLine("DynamicIntArray :  Sum " + sum + " Time " + ts + " MilliSeconds");

            int[] fiveRand = new int[5];
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(rand.Next(995082, oneMill));
                fiveRand[i] = Darr.Get(rand.Next(999175, oneMill)); // indexes random number between 0 and 1 million adds value at that position to array
            }

            for (int i = 0; i < 5; i++)
            {
                // For C# Array
                ts = 0;
                stopWatch.Reset();
                stopWatch.Start();
                Array.IndexOf(Carray, fiveRand[i]);
                stopWatch.Stop();
                ts = stopWatch.ElapsedMilliseconds;
                Console.WriteLine("Time taken to Search " + fiveRand[i] + " in C# Array  : " + ts);


                // For ArrayList
                ts = 0;
                stopWatch.Reset();
                stopWatch.Start();
                Aarray.IndexOf(fiveRand[i]);
                stopWatch.Stop();
                ts = stopWatch.ElapsedMilliseconds;
                Console.WriteLine("Time taken to Search " + fiveRand[i] + " in Linked List  : " + ts);



                // For List
                ts = 0;
                stopWatch.Reset();
                stopWatch.Start();
                Larray.IndexOf(fiveRand[i]);
                stopWatch.Stop();
                ts = stopWatch.ElapsedMilliseconds;
                Console.WriteLine("Time taken to Search " + fiveRand[i] + " in Linked List  : " + ts);



                // For List
                ts = 0;
                stopWatch.Reset();
                stopWatch.Start();
                Darr.IndexOf(fiveRand[i]);
                stopWatch.Stop();
                ts = stopWatch.ElapsedMilliseconds;
                Console.WriteLine("Time taken to Search " + fiveRand[i] + " in Linked List  : " + ts);


                Console.WriteLine("\n\n\n");
            }
        }
Пример #22
0
        static void Main(string[] args)
        {
            int     oneMill = 1000000;
            Program p       = new Program();

            DynamicIntArray <Decimal> Dyn = new DynamicIntArray <Decimal>();
            List <Decimal>            lis = new List <decimal>();

            Decimal[] d = new Decimal[oneMill];


            DynamicIntArray <String> DynS = new DynamicIntArray <String>();
            List <String>            lisS = new List <String>();

            String[] dS = new String[oneMill];



            DynamicIntArray <Boolean> DynB = new DynamicIntArray <Boolean>();
            List <Boolean>            lisB = new List <Boolean>();

            Boolean[] dB = new Boolean[oneMill];


            for (int i = 0; i < oneMill; i++)
            {
                //Decimal
                Dyn.Add(i);
                lis.Add(i);
                d[i] = i;


                //String
                DynS.Add("String" + i);
                lisS.Add("String" + i);
                dS[i] = "String" + i;


                // Boolean
                bool addBool = true;
                if (i % 2 == 0)
                {
                    addBool = false;
                }

                DynB.Add(addBool);
                DynB.Add(addBool);
                dB[i] = addBool;
            }

            Console.WriteLine("*****************\n");
            Console.WriteLine(" Decimal \n");
            p.ComparePerformance(lis, d, Dyn);
            Console.WriteLine("\n*****************\n");



            Console.WriteLine("*****************\n");
            Console.WriteLine(" String ");
            p.ComparePerformance(lisS, dS, DynS);
            Console.WriteLine("\n*****************\n");


            Console.WriteLine("*****************\n");
            Console.WriteLine(" Boolean ");
            p.ComparePerformance(lisB, dB, DynB);
            Console.WriteLine("\n*****************\n");
        }
Пример #23
0
            /**
             * Produces a copy of this vector.
             *
             * @return the new vector
             */
            public Object copy()
            {


                DynamicIntArray copy = new DynamicIntArray(m_Objects.Length);

                copy.m_Size = m_Size;
                copy.m_CapacityIncrement = m_CapacityIncrement;
                copy.m_CapacityMultiplier = m_CapacityMultiplier;
                Array.Copy(m_Objects, 0, copy.m_Objects, 0, m_Size);
                return copy;
            }
Пример #24
0
 internal static HandleRef getCPtr(DynamicIntArray obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }