コード例 #1
0
        /// <summary>
        /// Solve the quadratic programming optimization problem using the Goldfarb-Idnani dual method
        /// </summary>
        /// <param name="Dmat"></param>
        /// <param name="dvec"></param>
        /// <param name="Amat"></param>
        /// <param name="bvec"></param>
        /// <param name="meq"></param>
        /// <param name="factorized"></param>
        /// <returns></returns>
        public static OptimizationResult Solve(MathLib.Matrix <double> Dmat, double[] dvec, MathLib.Matrix <double> Amat, double[] bvec, int meq = 0, bool factorized = false)
        {
            //PerformanceLogger.Start("QuadProg", "Solve", "Initialize");
            Initialize();
            //PerformanceLogger.Stop("QuadProg", "Solve", "Initialize");

            DenseVector vec;

            if (dvec == null)
            {
                vec = new DenseVector(Dmat.ColumnCount, 0);
            }
            else
            {
                vec = new DenseVector(dvec);
            }

            //PerformanceLogger.Start("QuadProg", "Solve", "R.CallFunction");

            Engine.SetSymbol("result", Engine.CallFunction("solve.QP", Engine.RMatrix(Dmat.ToArray()),
                                                           Engine.RVector(vec.ToArray()), Engine.RMatrix(Amat.ToArray()),
                                                           Engine.RVector(bvec), Engine.RNumeric(meq), Engine.RBool(factorized)));

            var sol = Engine.RunCommand("result$solution").AsNumeric().ToArray <double>();
            var val = Engine.RunCommand("result$value").AsNumeric().ToArray <double>().First();

            //PerformanceLogger.Stop("QuadProg", "Solve", "R.CallFunction");

            return(new OptimizationResult(sol, val * 2));
        }
コード例 #2
0
        /// <summary>
        /// Quadratic programming optimization solver using the dual method algorithm of Goldfarb and Idnani
        /// </summary>
        /// <param name="Dmat"></param>
        /// <param name="dvec"></param>
        /// <param name="Amat"></param>
        /// <param name="bvec"></param>
        /// <param name="meq"></param>
        public static OptimizationResult Solve(MathLib.Matrix <double> Dmat, double[] dvec, MathLib.Matrix <double> Amat, double[] bvec, int meq = 0, bool factorized = false)
        {
            // return optim = optim, weights= weights, targetReturn = bvec[1],  targetRisk = sqrt(weights*Dmat*weights)[[1,1]]
            var target = new GoldfarbIdnaniQuadraticSolver(Dmat.RowCount, Amat.ToArray(), bvec, meq);
            var min    = target.Minimize(Dmat.ToArray(), dvec);
            var sol    = target.Solution;

            return(new OptimizationResult(sol, min * 2));
        }