public static PointN MethodHookJivs(F function, PointN b1, VectorN h, double eps, double z = 0.1) { PointN x; do { do { PointN xk = b1; PointN b2 = HookJivsHelper.ExploringSearch(function, xk, h); Console.WriteLine("> Exploring search ( xk{0} ) = b2{1}", xk.ToString(), b2.ToString()); do { Console.WriteLine("> Doing step xk = b1 + (b2 - b1) * 2, will give xk{0}", xk.ToString()); xk = b1 + (b2 - b1) * 2; x = HookJivsHelper.ExploringSearch(function, xk, h); Console.WriteLine("> Exploring search ( xk{0} ) = x{1}", xk.ToString(), x.ToString()); b1 = b2; b2 = x; } while (function.Value(x) < function.Value(b1)); } while (function.Value(x) > function.Value(b1)); if (h.Length <= eps) { break; } h = h * z; }while(true); return(b1); }
public static void TestHookJivs() { Console.WriteLine("Method Hook Jivs"); PointN basicPoint = new PointN(100.0, 100.0); VectorN h = new VectorN(1.0, 1.0); const double eps = 0.001; F minimizedFunction = new F((point) => 4 * Math.Pow(point.Coordinates[0] - 5.123, 2) + Math.Pow(point.Coordinates[1] - 6.789, 2)); PointN calculatedMinimum = MethodHookJivs.HookJivs.MethodHookJivs(minimizedFunction, basicPoint, h, eps); PointN expectedMinimum = new PointN(new List <double> { 1.0, 1.0 }); Console.WriteLine("================================================================="); Console.WriteLine("Minimum Point: {0} ", calculatedMinimum.ToString()); Console.WriteLine("F({0}) = {1}", calculatedMinimum.ToString(), minimizedFunction.Value(calculatedMinimum)); Console.WriteLine("Press enter to close..."); Console.ReadLine(); }
public static void TestRandomSearch() { Console.WriteLine("Method Random Search"); PointN basicPoint = new PointN(8.0, 9.0); const double alpha = 1.618, betta = 0.618, t0 = 1, R = 0.8; const int N = 10, M = 3; F minimizedFunction = new F((point) => 4 * Math.Pow(point.Coordinates[0] - 5, 2) + Math.Pow(point.Coordinates[1] - 6, 2)); PointN calculatedMinimum = MethodRandomSearch.RandomSearch.MethodRandomSearch(minimizedFunction, basicPoint, alpha, betta, t0, R, N, M); PointN expectedMinimum = new PointN(new List <double> { 1.0, 1.0 }); Console.WriteLine("================================================================="); Console.WriteLine("Minimum Point: {0} ", calculatedMinimum.ToString()); Console.WriteLine("F({0}) = {1}", calculatedMinimum.ToString(), minimizedFunction.Value(calculatedMinimum)); Console.WriteLine("Press enter to close..."); Console.ReadLine(); }
/// <summary> /// Метод случайного поиска для функции n переменных /// </summary> /// <param name="basicPoint">Начальная точка x0</param> /// <param name="a">Коэффициент расширения (1;+inf): 1.618 </param> /// <param name="b">Коэффициент сжатия (0;1): 0.618 </param> /// <param name="M">Максимальное число неудачно выполненых испытаний на данной итерации :(3n)</param> /// <param name="t0">Начальная величина шага</param> /// <param name="R">Минимальная величина шага</param> /// <param name="N">Максимальное число итераций</param> /// <returns>Точка минимума</returns> public static PointN MethodRandomSearch(F function, PointN basicPoint, double a, double b, double t0, double R, int N, int M) { double tk = t0; // Length of k-th step PointN xk = basicPoint; // Опорная точка PointN yj; // Точки, лежащие на гиперсфере радиуса tk с центром в точке xk PointN zj; // Точка, получающаяся после прыжка int dimensionsCount = basicPoint.Coordinates.Count; int k = 0, j = 1; do { // Step 2 Make random vector VectorN randomVector = new VectorN(dimensionsCount); MakeRandomVector(randomVector); // TODO What will be after this operator? Console.WriteLine("> Generated random vector {0}", randomVector.ToString()); // Step 3 Find yj yj = xk + (randomVector / randomVector.Length) * tk; // Step 4 // Good step bool isCurrentStepGood = function.Value(yj) < function.Value(xk); bool isNextStepAlsoGood = false; if (isCurrentStepGood) { Console.WriteLine("> Good step F(yj {0}) < F(xk {1})", yj.ToString(), xk.ToString()); zj = xk + (yj - xk) * a; isNextStepAlsoGood = function.Value(zj) < function.Value(xk); if (isNextStepAlsoGood) { Console.WriteLine("> Good direction F(zj {0}) < F(xk {1})", zj.ToString(), xk.ToString()); xk = zj; tk *= a; k++; bool tooManyIterations = k == N; if (tooManyIterations) { Console.WriteLine("Limit of iterations N = {0} exceeded", N); return(xk); } else { j = 1; } } } // Bad step if (!isCurrentStepGood || !isNextStepAlsoGood) { bool maxNumberOfFailsReached = j == M; if (!maxNumberOfFailsReached) { j++; } else if (maxNumberOfFailsReached) { Console.WriteLine("Number of bad steps from this point is exceeded"); if (tk <= R) { Console.WriteLine("Step size (tk) is less than Low limit (R)"); return(xk); } else if (maxNumberOfFailsReached && tk > R) { Console.WriteLine("Increase step size (t0) and repeat search"); tk *= b; j = 1; } } } } while (true); }