Beispiel #1
0
 protected override bool QuadraticProgrammingConstrainedImpl(out Vector x
                                                             , Matrix H, Vector f
                                                             , Matrix A       = null, Vector b   = null
                                                             , Matrix Aeq     = null, Vector beq = null
                                                             , Vector lb      = null, Vector ub  = null
                                                             , Vector x0      = null
                                                             , string options = null
                                                             )
 {
     using (new Matlab.NamedLock("NUMSLV"))
     {
         Matlab.Clear("NUMSLV");
         Matlab.PutMatrix("NUMSLV.H", H.ToArray());
         Matlab.PutVector("NUMSLV.f", f.ToArray());
         Matlab.Execute("NUMSLV.A   = [];"); if (A != null)
         {
             Matlab.PutMatrix("NUMSLV.A", A.ToArray());
         }
         Matlab.Execute("NUMSLV.b   = [];"); if (b != null)
         {
             Matlab.PutVector("NUMSLV.b", b.ToArray());
         }
         Matlab.Execute("NUMSLV.Aeq = [];"); if (Aeq != null)
         {
             Matlab.PutMatrix("NUMSLV.Aeq", Aeq.ToArray());
         }
         Matlab.Execute("NUMSLV.beq = [];"); if (beq != null)
         {
             Matlab.PutVector("NUMSLV.beq", beq.ToArray());
         }
         Matlab.Execute("NUMSLV.lb  = [];"); if (lb != null)
         {
             Matlab.PutVector("NUMSLV.lb", lb.ToArray());
         }
         Matlab.Execute("NUMSLV.ub  = [];"); if (ub != null)
         {
             Matlab.PutVector("NUMSLV.ub", ub.ToArray());
         }
         Matlab.Execute("NUMSLV.x0  = [];"); if (x0 != null)
         {
             Matlab.PutVector("NUMSLV.x0", x0.ToArray());
         }
         if (options == null)
         {
             options = "[]";
         }
         Matlab.Execute("NUMSLV.x = quadprog(NUMSLV.H, NUMSLV.f, NUMSLV.A, NUMSLV.b, NUMSLV.Aeq, NUMSLV.beq, NUMSLV.lb, NUMSLV.ub, NUMSLV.x0, " + options + ");", true);
         x = Matlab.GetVector("NUMSLV.x");
         Matlab.Clear("NUMSLV");
     }
     return(true);
 }
Beispiel #2
0
 protected override bool LeastSquareConstrainedImpl(out Vector x
                                                    , Matrix C, Vector d
                                                    , Matrix A       = null, Vector b   = null
                                                    , Matrix Aeq     = null, Vector beq = null
                                                    , Vector lb      = null, Vector ub  = null
                                                    , Vector x0      = null
                                                    , string options = null
                                                    )
 {
     using (new Matlab.NamedLock("NUMSLV"))
     {
         Matlab.Clear("NUMSLV");
         Matlab.PutMatrix("NUMSLV.C", C.ToArray());
         Matlab.PutVector("NUMSLV.d", d.ToArray());
         Matlab.Execute("NUMSLV.A   = [];"); if (A != null)
         {
             Matlab.PutMatrix("NUMSLV.A", A.ToArray());
         }
         Matlab.Execute("NUMSLV.b   = [];"); if (b != null)
         {
             Matlab.PutVector("NUMSLV.b", b.ToArray());
         }
         Matlab.Execute("NUMSLV.Aeq = [];"); if (Aeq != null)
         {
             Matlab.PutMatrix("NUMSLV.Aeq", Aeq.ToArray());
         }
         Matlab.Execute("NUMSLV.beq = [];"); if (beq != null)
         {
             Matlab.PutVector("NUMSLV.beq", beq.ToArray());
         }
         Matlab.Execute("NUMSLV.lb  = [];"); if (lb != null)
         {
             Matlab.PutVector("NUMSLV.lb", lb.ToArray());
         }
         Matlab.Execute("NUMSLV.ub  = [];"); if (ub != null)
         {
             Matlab.PutVector("NUMSLV.ub", ub.ToArray());
         }
         Matlab.Execute("NUMSLV.x0  = [];"); if (x0 != null)
         {
             Matlab.PutVector("NUMSLV.x0", x0.ToArray());
         }
         if (options == null)
         {
             options = "[]";
         }
         Matlab.Execute("NUMSLV.x = lsqlin(NUMSLV.C, NUMSLV.d, NUMSLV.A, NUMSLV.b, NUMSLV.Aeq, NUMSLV.beq, NUMSLV.lb, NUMSLV.ub, NUMSLV.x0, " + options + ");", true);
         x = Matlab.GetVector("NUMSLV.x");
         Matlab.Clear("NUMSLV");
     }
     return(true);
 }
Beispiel #3
0
 protected override bool CorrImpl(Vector vec1, Vector vec2, out double corr)
 {
     NamedLock.FuncO <double, bool> func = delegate(out double lcorr)
     {
         Matlab.Clear("HTLib2_Matlab_CorrImpl");
         Matlab.PutVector("HTLib2_Matlab_CorrImpl.vec1", vec1.ToArray());
         Matlab.PutVector("HTLib2_Matlab_CorrImpl.vec2", vec2.ToArray());
         Matlab.Execute("HTLib2_Matlab_CorrImpl.corr = corr(HTLib2_Matlab_CorrImpl.vec1, HTLib2_Matlab_CorrImpl.vec2);");
         lcorr = Matlab.GetValue("HTLib2_Matlab_CorrImpl.corr");
         Matlab.Clear("HTLib2_Matlab_CorrImpl");
         return(true);
     };
     //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.CorrImpl(Vector, Vector, out double)", func, out corr);
     return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_CorrImpl"), func, out corr));
 }
Beispiel #4
0
        public static object LeastSquare
            (double[,] As, double[] bs
            , bool opt_get_stat = false
            )
        {
            if (HDebug.Selftest())
            {
                /// >> A = [ 1,3,2, 1 ; 4,5,6, 1 ; 7,9,9, 1 ; 11,11,12, 1 ; 13,16,15, 1 ]
                /// >> b = [1, 4, 6, 9, 12]'
                /// >> x = inv(A' * A) * (A' * b)
                ///     0.2171
                ///     0.2125
                ///     0.4205
                ///    -0.7339
                /// >> esti = A * x
                ///     0.9619
                ///     3.7203
                ///     6.4832
                ///     9.0381
                ///    11.7965
                /// >> corr(esti,b)
                ///     0.9976
                /// >> mean( (b-esti).^2 )
                ///     0.0712
                double[,] _A = new double[5, 3] {
                    { 1, 3, 2 }, { 4, 5, 6 }, { 7, 9, 9 }, { 11, 11, 12 }, { 13, 16, 15 }
                };
                double[] _b = new double[5] {
                    1, 4, 6, 9, 12
                };
                dynamic _out = LeastSquare(_A, _b, true);

                double   _matlab_corr = 0.9976;
                double   _matlab_mse  = 0.0712;
                double[] _matlab_x    = new double[] { 0.2171, 0.2125, 0.4205, -0.7339 };
                double[] _matlab_esti = new double[] { 0.9619, 3.7203, 6.4832, 9.0381, 11.7965 };

                double err1 = Math.Abs(_matlab_corr - _out.opt_estimation_corr);
                double err2 = Math.Abs(_matlab_mse - _out.opt_mean_square_err);
                double err3 = (_matlab_x - (Vector)_out.x).ToArray().MaxAbs();
                double err4 = (_matlab_esti - (Vector)_out.opt_estimation).ToArray().MaxAbs();

                HDebug.Assert(err1 < 0.0001);
                HDebug.Assert(err2 < 0.0001);
                HDebug.Assert(err3 < 0.0001);
                HDebug.Assert(err4 < 0.0001);
            }
            /// => A x = b
            ///
            /// => At A x = At b
            ///
            /// => AA * x = Ab
            /// => x = inv(AA) * Ab
            HDebug.Assert(As.GetLength(0) == bs.Length);
            int n = As.GetLength(0);
            int k = As.GetLength(1);

            Matrix A = Matrix.Zeros(n, k + 1);

            for (int c = 0; c < n; c++)
            {
                for (int r = 0; r < k; r++)
                {
                    A[c, r] = As[c, r];
                }
                A[c, k] = 1;
            }

            Matrix AA = LinAlg.MtM(A, A);
            Vector Ab = LinAlg.MtV(A, bs);

            Vector x;

            switch (k + 1)
            {
            case 2: { Matrix invAA = LinAlg.Inv2x2(AA.ToArray()); x = LinAlg.MV(invAA, Ab); } break;

            case 3: { Matrix invAA = LinAlg.Inv3x3(AA.ToArray()); x = LinAlg.MV(invAA, Ab); } break;

            case 4: { Matrix invAA = LinAlg.Inv4x4(AA.ToArray()); x = LinAlg.MV(invAA, Ab); } break;

            default:
                Matlab.PutMatrix("LinAlg_LeastSquare.AA", AA);
                Matlab.PutVector("LinAlg_LeastSquare.Ab", Ab);
                Matlab.Execute("LinAlg_LeastSquare.AA = inv(LinAlg_LeastSquare.AA);");
                Matlab.Execute("LinAlg_LeastSquare.x = LinAlg_LeastSquare.AA * LinAlg_LeastSquare.Ab;");
                x = Matlab.GetVector("LinAlg_LeastSquare.x");
                Matlab.Execute("clear LinAlg_LeastSquare;");
                break;
            }

            double?opt_mean_square_err = null;
            double?opt_estimation_corr = null;
            Vector opt_estimation      = null;

            if (opt_get_stat)
            {
                opt_estimation = new double[n];
                double avg_err2 = 0;
                for (int i = 0; i < n; i++)
                {
                    double esti = 0;
                    for (int j = 0; j < k; j++)
                    {
                        esti += As[i, j] * x[j];
                    }
                    esti += x[k];

                    opt_estimation[i] = esti;
                    avg_err2         += (esti - bs[i]) * (esti - bs[i]);
                }
                avg_err2 /= n;

                opt_mean_square_err = avg_err2;
                opt_estimation_corr = HMath.HCorr(opt_estimation, bs);
            }

            return(new
            {
                x = x,
                /// optional outputs
                opt_mean_square_err = opt_mean_square_err,
                opt_estimation_corr = opt_estimation_corr,
                opt_estimation = opt_estimation,
            });
        }
Beispiel #5
0
        public static void PlotScatter
            (IEnumerable <double> x
            , IEnumerable <double> y
            , IEnumerable <double> size = null
            , double?sizeall            = null
            , IEnumerable <ValueTuple <double, double, double> > RGB = null
            , ValueTuple <double, double, double>?RGBall             = null
            , string xlabel     = null
            , string ylabel     = null
            , string title      = null
            , bool filled       = false
            , bool init_figure  = true
            , bool init_holdon  = true
            , bool last_holdoff = true
            )
        {
            Matlab.PutVector("htlib2_matlab_PlotScatter.x", x.ToArray());
            Matlab.PutVector("htlib2_matlab_PlotScatter.y", y.ToArray());
            if (size != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.sz", size.ToArray());
            }
            if (RGB != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.R", RGB.HListItem1().ToArray());
            }
            if (RGB != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.G", RGB.HListItem2().ToArray());
            }
            if (RGB != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.B", RGB.HListItem3().ToArray());
            }
            if (RGB != null)
            {
                Matlab.Execute("htlib2_matlab_PlotScatter.RGB = [htlib2_matlab_PlotScatter.R; htlib2_matlab_PlotScatter.G; htlib2_matlab_PlotScatter.B];");
            }
            if (init_figure)
            {
                Matlab.Execute("figure;");
            }
            if (init_holdon)
            {
                Matlab.Execute("hold on;");
            }
            string script;

            {
                script = "scatter" +
                         "( htlib2_matlab_PlotScatter.x" +
                         ", htlib2_matlab_PlotScatter.y";
                // size
                if (sizeall != null)
                {
                    script += ", " + sizeall.Value;
                }
                else if (size != null)
                {
                    script += ", htlib2_matlab_PlotScatter.sz";
                }
                else
                {
                    script += ", []";
                }
                // color
                if (RGBall != null)
                {
                    script += string.Format(", [{0},{1},{2}]", RGBall.Value.Item1, RGBall.Value.Item2, RGBall.Value.Item3);
                }
                else if (RGB != null)
                {
                    script += ", htlib2_matlab_PlotScatter.RGB";
                }
                // filled
                if (filled)
                {
                    script += ", 'filled'";
                }
                // close
                script += ");";
            }
            Matlab.Execute(script);
            if (xlabel != null)
            {
                Matlab.Execute("xlabel('" + xlabel + "');");
            }
            if (ylabel != null)
            {
                Matlab.Execute("ylabel('" + ylabel + "');");
            }
            if (title != null)
            {
                Matlab.Execute("title('" + title + "');");
            }
            if (last_holdoff)
            {
                Matlab.Execute("hold off;");
            }
            Matlab.Execute("clear htlib2_matlab_PlotScatter;");
        }