public QuasiNewtonMethod(double eps, Vector startVector) { StartVector = startVector; epsilon = eps; currentValue = startVector; int n = startVector.GetVectorSize; hessianMatrix = new Matrix(n, n).IdentityMatrix(); }
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[]; }
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) }; }
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) }; }
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; }
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; }
public static Vector f4(Vector v) { var x = v.X; var y = v.Y; return new Vector(2 * x, 2 * (y-1)); }
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; }
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)); }
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)); }
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); }
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); }
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); }
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; }
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; }