示例#1
0
 public QuasiNewtonMethod(double eps, Vector startVector)
 {
     StartVector = startVector;
     epsilon = eps;
     currentValue = startVector;
     int n = startVector.GetVectorSize;
     hessianMatrix = new Matrix(n, n).IdentityMatrix();
 }
示例#2
0
 public NelderMead(Vector initialVertex, int multiplier, double eps)
 {
     dimension = initialVertex.GetVectorSize;
     simplex = new Vector[dimension + 1];
     initialSimplex = new Vector[dimension + 1];
     this.initialVertex = initialVertex;
     this.multiplier = multiplier;
     UnitVectors = Vector.GetUnitVectors(dimension);
     epsilon = eps;
     simplex[0] = initialVertex;
     for (int i = 0; i < dimension; i++)
     {
         simplex[i + 1] = simplex[i] + multiplier * UnitVectors[i];
     }
     initialSimplex = simplex.Clone() as Vector[];
 }
示例#3
0
 public MethodResult Execute(FunctionDelegate F, GradientDelegate Gradient, OptimizationMethod OptimizationMethod, Vector StartVector, int MaxIterations)
 {
     stopwatch.Start();
     currentVector = StartVector;
     for (int i = 0; i < MaxIterations; i++)
     {
         oldVector = currentVector;
         deltaGradient = Gradient(currentVector);
         var lambda = OptimizationMethod((x) => { return F(currentVector - x * deltaGradient); }, -10, 10, 1E-9, MaxIterations);
         currentVector = currentVector - lambda * deltaGradient;
         if (deltaGradient.LengthSquared < epsilon)
         {
             stopwatch.Stop();
             return new MethodResult() { Result = new Vector[] { currentVector }, IterationEnd = false, Iterations = i, MethodName = "Метод градиентного спуска", stopwatch = stopwatch, StartPoint = new Vector[] { StartVector }, FunctionValue = F(currentVector) };
         }
     }
     stopwatch.Stop();
     return new MethodResult() { Result = new Vector[] { currentVector }, IterationEnd = true, MethodName = "Метод градиентного спуска", Iterations = MaxIterations, stopwatch = stopwatch, StartPoint = new Vector[] { StartVector }, FunctionValue = F(currentVector) };
 }
 public MethodResult Execute(FunctionDelegate F, GradientDelegate Gradient, OptimizationMethod OptimizationMethod, int MaxIterations)
 {
     stopwatch.Start();
     var antiGradient = -Gradient(x);
     var gradientSquare = antiGradient.LengthSquared;
     for (int i = 0; i < MaxIterations; i++)
     {
         var lambda = OptimizationMethod((alpha) => { return F(x + alpha * antiGradient); }, -10, 10, 1E-9, MaxIterations);
         x = x + lambda * antiGradient;
         var newAntiGradient = -Gradient(x);
         var newGradientSquare = newAntiGradient.LengthSquared;
         var beta = newGradientSquare / gradientSquare;
         if (i % (500) == 0 && i != 0) beta = 0;
         antiGradient = newAntiGradient + beta * antiGradient;
         gradientSquare = newGradientSquare;
         if (gradientSquare < epsilon)
         {
             stopwatch.Stop();
             return new MethodResult() { Result = new Vector[] { x }, IterationEnd = false, Iterations = i, MethodName = "Метод сопряженных градиентов", stopwatch = stopwatch , StartPoint = new Vector[] { StartVector}, FunctionValue = F(x)};
         }
     }
     stopwatch.Stop();
     return new MethodResult() { Result = new Vector[] { x }, IterationEnd = true, MethodName = "Метод сопряженных градиентов", Iterations = MaxIterations, stopwatch = stopwatch, StartPoint = new Vector[] { StartVector }, FunctionValue = F(x) };
 }
示例#5
0
 public MethodResult Execute(FunctionDelegate F, GradientDelegate Gradient, OptimizationMethod OptimizationMethod, int MaxIterations)
 {
     stopwatch.Start();
     for (int i = 0; i < MaxIterations; i++)
     {
         var oldGradientValue = Gradient(currentValue);
         Vector newDirection = -(hessianMatrix * oldGradientValue);
         var lambda = OptimizationMethod((alpha) => { return F(currentValue + alpha * newDirection); }, -10, 10, 1E-9, MaxIterations);
         var vector = lambda * newDirection;
         currentValue = vector + currentValue;
         //var functionValue = F(currentValue);
         var gradientValue = Gradient(currentValue);
         if (gradientValue.LengthSquared < epsilon || vector.LengthSquared < epsilon)
         {
             stopwatch.Stop();
             return new MethodResult() { Result = new Vector[] { currentValue }, IterationEnd = false, MethodName = "Квазиньютоновский метод", Iterations = i, stopwatch = stopwatch, StartPoint = new Vector[] { StartVector }, FunctionValue = F(currentValue) };
         }
         var matrixU = gradientValue - oldGradientValue;
         var matrixA = (vector * vector.GetTranspose()) / (vector.ToMatrix().GetTranspose() * matrixU)[0, 0];
         var matrixB = -(hessianMatrix * matrixU * matrixU.GetTranspose() * hessianMatrix) / (matrixU.GetTranspose() * hessianMatrix * matrixU)[0, 0];
         hessianMatrix = hessianMatrix + matrixA + matrixB;
     }
     return new MethodResult() { Result = new Vector[] { currentValue }, IterationEnd = true, MethodName = "Квазиньютоновский метод", Iterations = MaxIterations, stopwatch = stopwatch, StartPoint = new Vector[] { StartVector }, FunctionValue = F(currentValue) };
 }
示例#6
0
 public static double f4(Vector v)
 {
     var x = v.X;
     var y = v.Y;
     return x * x + (y - 1) * (y - 1);
 }
 public ConjugateGradientMethod(double eps, Vector startVector)
 {
     epsilon = eps;
     x = startVector;
     StartVector = startVector;
 }
示例#8
0
 public static Vector operator *(Vector v, double d)
 {
     Vector result = new Vector(v.ndim);
     for (int i = 0; i < v.ndim; i++)
     {
         result[i] = v[i] * d;
     }
     return result;
 }
示例#9
0
 public static Vector f4(Vector v)
 {
     var x = v.X;
     var y = v.Y;
     return new Vector(2 * x, 2 * (y-1));
 }
示例#10
0
 public static Vector operator -(Vector v1, Vector v2)
 {
     Vector result = new Vector(v1.ndim);
     for (int i = 0; i < v1.ndim; i++)
     {
         result[i] = v1[i] - v2[i];
     }
     return result;
 }
示例#11
0
 public static Vector f2(Vector v)
 {
     return new Vector(2 * (2 * v.X * (v.X * v.X + v.Y - 11) + v.X + v.Y * v.Y - 7), 2 * (v.X * v.X + 2 * v.Y * (v.X + v.Y * v.Y - 7) + v.Y - 11));
 }
示例#12
0
 public static Vector f3(Vector v)
 {
     return new Vector(10 * (40 * v.X * v.X * v.X - 40 * v.X * v.Y + v.X - 1), 200 * (v.Y - v.X * v.X), 10 * (v.Z - 1));
 }
示例#13
0
 public Matrix ReplaceCol(Vector vec, int n)
 {
     if (n < 0 || n > nCols)
     {
         throw new Exception("Неправильный индекс столбца");
     }
     if (vec.GetVectorSize != nRows)
     {
         throw new Exception("Размерности не совпадают");
     }
     for (int i = 0; i < nRows; i++)
     {
         matrix[i, n] = vec[i];
     }
     return new Matrix(matrix);
 }
示例#14
0
 public static double f3(Vector v)
 {
     return 100 * (v.Y - v.X * v.X) * (v.Y - v.X * v.X) + 5 * (1 - v.X) * (1 - v.X) + 5 * (1 - v.Z) * (1 - v.Z);
 }
示例#15
0
 public static double f2(Vector v)
 {
     return (v.X * v.X + v.Y - 11) * (v.X * v.X + v.Y - 11) + (v.X + v.Y * v.Y - 7) * (v.X + v.Y * v.Y - 7);
 }
示例#16
0
 public static Vector[] GetUnitVectors(int n)
 {
     var unitVectors = new Vector[n];
     for (int i = 0; i < n;i++ )
     {
         unitVectors[i] = new Vector(n);
         unitVectors[i][i] = 1;
     }
     return unitVectors;
 }
示例#17
0
 public static Vector operator /(double d, Vector v)
 {
     Vector result = new Vector(v.ndim);
     for (int i = 0; i < v.ndim; i++)
     {
         result[i] = v[i] / d;
     }
     return result;
 }