Esempio n. 1
0
        private Matrix IterationSearch(Matrix x, double sigma)
        {
            int           n  = x.N;
            Matrix        y  = (Matrix)x.Clone();
            Matrix        u  = (Matrix)x.Clone();
            PenaltyVector fy = new PenaltyVector(functions.ExecuteFunctions(y));
            PenaltyVector fu = new PenaltyVector(functions.ExecuteFunctions(u));

            for (int i = 0; i < n; i++)
            {
                u[0][i] = y[0][i] + sigma;
                fu      = new PenaltyVector(functions.ExecuteFunctions(u));
                if (fu < fy)
                {
                    y[0][i] = u[0][i];
                    fy      = (PenaltyVector)fu.Clone();
                    continue;
                }
                u[0][i] = y[0][i] - sigma;
                fu      = new PenaltyVector(functions.ExecuteFunctions(u));
                if (fu < fy)
                {
                    y[0][i] = u[0][i];
                    fy      = (PenaltyVector)fu.Clone();
                }
                else
                {
                    u[0][i] = y[0][i];
                    fu      = (PenaltyVector)fy.Clone();
                }
            }

            return(y);
        }
Esempio n. 2
0
        public void Start()
        {
            PenaltyVector fx = new PenaltyVector(functions.ExecuteFunctions(x));

            double sigma = 2;
            int    i     = 0;

            while (sigma >= eps)
            {
                i++;
                sigma = sigma / 2;
                Matrix        y  = IterationSearch(x, sigma);
                PenaltyVector fy = new PenaltyVector(functions.ExecuteFunctions(y));

                while (fx < fy)
                {
                    Matrix        z  = (Matrix)((2 * y - x).Clone());
                    PenaltyVector fz = new PenaltyVector(functions.ExecuteFunctions(z));
                    x  = y;
                    fx = fy;
                    y  = IterationSearch(z, sigma);
                    fy = new PenaltyVector(functions.ExecuteFunctions(y));
                }

                Steps.Add(i, y);
            }
        }
Esempio n. 3
0
        public void VectorBetterCheck()
        {
            Vector a = new PenaltyVector(4);
            Vector b = new PenaltyVector(4);

            a[2] = 2;

            bool res = a > b;

            Assert.AreEqual(true, res);

            b[0] = 1;
            res  = a > b;
            Assert.AreEqual(false, res);
        }
Esempio n. 4
0
        public void Start()
        {
            Matrix        c  = (a + b) / 2;
            PenaltyVector Fc = functions.ExecuteFunctions(c);
            PenaltyVector Fa = functions.ExecuteFunctions(a);
            PenaltyVector Fb = functions.ExecuteFunctions(b);
            int           i  = 1;

            do
            {
                Steps.Add(i, c);
                i++;
                Matrix        U  = (a + c) / 2;
                PenaltyVector Fu = functions.ExecuteFunctions(U);
                if (Fu < Fc)
                {
                    b  = (Matrix)c.Clone();
                    c  = (Matrix)U.Clone();
                    Fc = functions.ExecuteFunctions(c);
                }
                else
                {
                    Matrix        V  = (c + b) / 2;
                    PenaltyVector Fv = functions.ExecuteFunctions(V);
                    if (Fv < Fc)
                    {
                        a  = (Matrix)c.Clone();
                        Fa = functions.ExecuteFunctions(a);
                        c  = (Matrix)V.Clone();
                        Fc = functions.ExecuteFunctions(c);
                    }
                    else
                    {
                        a  = (Matrix)U.Clone();
                        Fa = functions.ExecuteFunctions(a);
                        b  = (Matrix)V.Clone();
                        Fb = functions.ExecuteFunctions(b);
                    }
                }
            } while ((c - a).normEvcl() > eps);
        }
Esempio n. 5
0
        private bool SquereInterpolation(Matrix a, Matrix b, Matrix U, Matrix V)
        {
            V = (Matrix)U.Clone();
            PenaltyVector Fa = functions.ExecuteFunctions(a);
            PenaltyVector Fb = functions.ExecuteFunctions(b);
            PenaltyVector Fu = functions.ExecuteFunctions(U);
            Matrix        q  = (b - U) * (Fa - Fu);
            Matrix        p  = (U - a) * (Fb - Fu);
            Matrix        s  = p + q;

            if (s.normEvcl() <= 0)
            {
                return(false);
            }

            V = (p * (U + a) + q * (b + U)) / (s * 2);
            if (V.normEvcl() <= a.normEvcl() || V.normEvcl() > b.normEvcl())
            {
                return(false);
            }

            return(true);
        }
Esempio n. 6
0
        public Matrix Gradient()
        {
            Matrix res = new Matrix(x.N, x.M);

            for (int i = 0; i < x.N; i++)
            {
                Matrix temp1 = new Matrix(x);
                Matrix temp2 = new Matrix(x);


                for (int j = 0; j < x.M; j++)
                {
                    temp1[i][j] += 0.000001;
                    temp2[i][j] -= 0.000001;
                    PenaltyVector vec1 = new PenaltyVector(functions.ExecuteFunctions(temp1));
                    PenaltyVector vec2 = new PenaltyVector(functions.ExecuteFunctions(temp2));
                    res[i][j]    = (vec1 - vec2) / 0.000002;
                    temp1[i][j] -= 0.000001;
                    temp2[i][j] += 0.000001;
                }
            }

            return(res);
        }
Esempio n. 7
0
        public void Start()
        {
            Matrix        V = new Matrix(a.N, a.M);
            Matrix        d;
            PenaltyVector Fv;
            PenaltyVector Fa      = functions.ExecuteFunctions(a);
            PenaltyVector Fb      = functions.ExecuteFunctions(b);
            PenaltyVector Fu      = functions.ExecuteFunctions(U);
            int           counter = 1;

            bool interpolationFound = SquereInterpolation(a, b, U, V);

            if (interpolationFound)
            {
                d = U - V;
                do
                {
                    interpolationFound = SquereInterpolation(a, b, U, V);
                    if (interpolationFound)
                    {
                        Fv = functions.ExecuteFunctions(V);
                        Fu = functions.ExecuteFunctions(U);
                        d  = U - V;
                        bool Sign = false;
                        for (int i = 0; i < d.N; i++)
                        {
                            for (int j = 0; j < d.M; j++)
                            {
                                if (d[i][j] > 0)
                                {
                                    Sign = true;
                                    break;
                                }
                            }
                            if (Sign)
                            {
                                break;
                            }
                        }
                        if (Fv < Fu)
                        {
                            if (Sign)
                            {
                                a  = (Matrix)U.Clone();
                                Fa = functions.ExecuteFunctions(a);
                            }
                            else
                            {
                                b  = (Matrix)U.Clone();
                                Fb = functions.ExecuteFunctions(b);
                            }

                            U  = (Matrix)V.Clone();
                            Fu = functions.ExecuteFunctions(U);
                        }
                        else
                        {
                            if (Sign)
                            {
                                b  = (Matrix)V.Clone();
                                Fb = functions.ExecuteFunctions(b);
                            }
                            else
                            {
                                a  = (Matrix)V.Clone();
                                Fa = functions.ExecuteFunctions(a);
                            }
                        }
                    }
                    else
                    {
                        break;
                    }

                    Steps.Add(counter, U);
                    counter++;
                } while (d.normEvcl() > eps);
            }
        }
Esempio n. 8
0
        public void Start()
        {
            double r = 0;
            Matrix h0;
            int    i = 0;

            while (h.normEvcl() > Eps)
            {
                Steps.Add(i, x);
                h0 = h;
                Matrix y = x + h0;

                Vector f = new PenaltyVector(functions.ExecuteFunctions(x));
                Vector g = new PenaltyVector(functions.ExecuteFunctions(y));


                if (g < f)
                {
                    x = y;
                    f = g;
                }

                if (g < f && r >= 0.5)
                {
                    r = 2;
                }

                if (g < f && r < 0.5)
                {
                    r = 0.5;
                }

                if (g < f && r == 2)
                {
                    r = 0.25;
                }
                else
                {
                    r = -0.5;
                }

                h = h * r;
                x = x + h;
                i++;
            }
            LastStep = Steps[i];
            bool IsColumn = x.M == 1;

            if (IsColumn)
            {
                for (int j = 0; j < x.N; j++)
                {
                    Distance += (x[i][0] - Steps[0][i][0]) * (x[i][0] - Steps[0][i][0]);
                }
                Distance = Math.Sqrt(Distance);
            }
            else
            {
                for (int j = 0; j < x.M; j++)
                {
                    Distance += (x[0][i] - Steps[0][0][i]) * (x[0][i] - Steps[0][0][i]);
                }
                Distance = Math.Sqrt(Distance);
            }
        }