Exemple #1
0
            private void PrimalDualLineSearch(floatLinalg.floatVector x, floatLinalg.floatVector fx, floatLinalg.floatVector lambda, floatLinalg.floatVector nu,
                floatLinalg.floatVector dx, floatLinalg.floatVector dlambda, floatLinalg.floatVector dnu,
                floatLinalg.floatVector xPlus, floatLinalg.floatVector lambdaPlus, floatLinalg.floatVector nuPlus, ComputeRestrictions F, ComputePDResidualNorm r, ref float t,
                out float surrogDualityGap)
            {
                float BETA = 0.6f;
                float ALPHA = 0.02f;

                //min(1, -lambda./dlambda)
                float s = 1;
                lambda.ReadFromDevice();
                dlambda.ReadFromDevice();

                for (int i = 0; i < lambda.Values.Length; i++)
                {
                    if (dlambda.Values[i] < 0) s = Math.Min(s, -lambda.Values[i] / dlambda.Values[i]);
                }

                s *= 0.99f;

                //fx can't have positive entries
                floatLinalg.BLAS.LinearCombination(1, x, s, dx, ref xPlus);
                floatLinalg.BLAS.LinearCombination(1, lambda, s, dlambda, ref lambdaPlus);
                if (nu != null) floatLinalg.BLAS.LinearCombination(1, nu, s, dnu, ref nuPlus);

                bool fxHasPositiveEntry = true;

                while (fxHasPositiveEntry && s > 0)
                {
                    F(xPlus, ref fx);
                    fxHasPositiveEntry = fx.HasPositiveEntries();
                    if (fx.HasPositiveEntries())
                    {
                        s *= BETA;
                        floatLinalg.BLAS.LinearCombination(1, x, s, dx, ref xPlus);
                        floatLinalg.BLAS.LinearCombination(1, lambda, s, dlambda, ref lambdaPlus);
                        if (nu != null) floatLinalg.BLAS.LinearCombination(1, nu, s, dnu, ref nuPlus);
                    }
                }

                //Save residuals at this point if necessary
                SolutionLog.LogResiduals = true;
                //Function decrease requirement
                float NormResPrev = r(x, lambda, nu, t);
                SolutionLog.LogResiduals = false;

                float NormPlus = r(xPlus, lambdaPlus, nuPlus, t);

                while (NormPlus > (1.0f - ALPHA * s) * NormResPrev)
                {
                    s *= BETA;
                    floatLinalg.BLAS.LinearCombination(1, x, s, dx, ref xPlus);
                    floatLinalg.BLAS.LinearCombination(1, lambda, s, dlambda, ref lambdaPlus);
                    if (nu != null) floatLinalg.BLAS.LinearCombination(1, nu, s, dnu, ref nuPlus);
                    NormPlus = r(xPlus, lambdaPlus, nuPlus, t);
                }

                if (!float.IsNaN(NormPlus))
                {
                    floatLinalg.BLAS.CopyVector(xPlus, x);
                    floatLinalg.BLAS.CopyVector(lambdaPlus, lambda);
                    if (nu != null) floatLinalg.BLAS.CopyVector(nuPlus, nu);

                    F(xPlus, ref fx);
                    surrogDualityGap = -floatLinalg.BLAS.Dot(fx, lambda, ref tempM);
                }
                else
                {
                    //Can't improve anymore
                    surrogDualityGap = 0.0f;
                }

                t = lambda.Values.Length * 10.0f / surrogDualityGap;

                if (SolutionLog.KeepLog)
                {
                    SolutionLog.StepSizes.Add(s);
                }

                //ReadAll();
            }
Exemple #2
0
            /// <summary>Checks if it's possible to satisfy Mx less than d and Ax = b. Returns a feasible point in x0 if so</summary>
            /// <param name="x0">Initial guess</param>
            /// <param name="M">Inequality constraint matrix</param>
            /// <param name="d">Inequality rhs</param>
            /// <param name="A">Equality constr matrix</param>
            /// <param name="b">Equality rhs</param>
            /// <param name="Mxd">Temporary vector to hold M*x - d</param>
            public static bool CheckFeasibility(floatLinalg.floatVector x0, floatLinalg.floatMatrix M, floatLinalg.floatVector d, floatLinalg.floatMatrix A,
                floatLinalg.floatVector b, floatLinalg.floatVector Mxd)
            {
                SolutionLog.Clear();

                //Computes Mx - d
                floatLinalg.BLAS.MatrVecProdSumVec(M, x0, 1, d, -1, ref Mxd);

                //Problem is already feasible
                if (!Mxd.HasPositiveEntries())
                {
                    return true;
                }

                //If problem is not feasible, computes largest term
                Mxd.ReadFromDevice();
                float max = 1.0f;
                for (int i = 0; i < Mxd.Values.Length; i++) if (max < Mxd.Values[i]) max = Mxd.Values[i];
                //Augments max to create a feasible point for the feasibility problem
                max *= 1.1f;

                //Constructs the instance of the feasibility problem.
                //Augments M and A by one. M will contain the -1s in the extra row; A has to contain an extra zero in the last additional column
                if (CLCalc.CLAcceleration==CLCalc.CLAccelerationType.UsingCL) M.CLValues.ReadFromDeviceTo(M.Values);

                float[,] Mfeas = new float[M.Rows, M.Cols + 1];
                for (int i = 0; i < M.Rows; i++)
                {
                    for (int j = 0; j < M.Cols; j++)
                    {
                        Mfeas[i, j] = M[i, j];
                    }
                    Mfeas[i, M.Cols] = -1;
                }

                float[,] Afeas = null;
                if (A != null)
                {
                    Afeas = new float[A.Rows, A.Cols + 1];
                    for (int i = 0; i < A.Rows; i++)
                    {
                        for (int j = 0; j < A.Cols; j++)
                        {
                            Afeas[i, j] = A[i, j];
                        }
                        Afeas[i, M.Cols] = 0;
                    }
                }

                //Feasibility problem feasible point
                float[] xFeas = new float[x0.Length + 1];
                x0.ReadFromDevice();
                for (int i = 0; i < x0.Length; i++) xFeas[i] = x0.Values[i];
                xFeas[x0.Length] = max;

                float[] lambda = new float[M.Rows];
                for (int i = 0; i < lambda.Length; i++) lambda[i] = 0.1f;
                float[] nu = null;
                if (A != null) nu = new float[A.Rows];

                //Objective is to minimize last variable
                float[] q = new float[x0.Length + 1];
                //for (int i = 0; i < x0.Length; i++) q[i] = 0.001f;
                q[x0.Length] = 10 * max * (float)lambda.Length * (float)lambda.Length;

                QuadraticProgramming qp = new QuadraticProgramming();

                d.ReadFromDevice();
                float[] bVals = null;
                if (A != null)
                {
                    b.ReadFromDevice();
                    bVals = b.Values;
                }

                float[] xF = qp.SolvePrimalDual(xFeas, lambda, nu, null, q, Mfeas, d.Values, Afeas, bVals, feasStopFunc);

                if (xF[xF.Length - 1] < 0) //feasible
                {
                    for (int i = 0; i < x0.Length; i++) x0.Values[i] = xF[i];
                    if (floatLinalg.UseOpenCLIfAvailable && CLCalc.CLAccelerationType.UsingCL == CLCalc.CLAcceleration)
                    {
                        x0.CLValues.WriteToDevice(x0.Values);
                    }
                    return true;
                }
                else //infeasible
                {
                    return false;
                }
            }