/// <summary> /// Finds minimum of functions by transforming it to form solvable by Hooke-Jeeves algorithm. /// </summary> /// <param name="implicitRestrictions">List of implicit restrictions.</param> /// <param name="function">Function used in algorithm.</param> /// <param name="t">Parameter of transformation.</param> /// <param name="e">Precision.</param> /// <param name="startingPoint">Starting point of algorithm.</param> /// <param name="epsilon">Precision used in Hooke-Jeeves method.</param> /// <param name="deltaX">Step used for calculating new point.</param> /// <param name="iterations">Number of iterations of algorithm.</param> /// <returns>Minimum of function.</returns> public static Matrica FindMinimum(List <RestrictionExpression> implicitRestrictions, Function function, double t, double e, Matrica startingPoint, Matrica epsilon, Matrica deltaX, int iterations) { Matrica x = new Matrica(); x.Equals(startingPoint); int counter = 0; while (true) { Matrica dx = new Matrica(); dx.Equals(deltaX); TransformedFunction transformedFunction = new TransformedFunction(function, implicitRestrictions, t); Matrica xs = new Matrica(); xs.Equals(x); x = HookeJeevesMethod.FindMinimum(xs, transformedFunction, epsilon, dx, algorithmName); t *= 10; counter++; if (OptimizationUtils.CheckIfPointsAreSimilar(x, xs, e) || counter > iterations) { break; } } function.DeleteParameters(); return(x); }
/// <summary> /// Finds minimum of function using Box optimization method. /// </summary> /// <param name="function">Function used to optimize.</param> /// <param name="implicitRestrictions">List of implicit restrictions used in Box method.</param> /// <param name="explicitRestriction">List of explicit restrictions used in Box method.</param> /// <param name="startingPoint">Starting point of algorithm.</param> /// <param name="alfa">Constant alpha used in algorithm.</param> /// <param name="iterations">Number of iterations of algorithm.</param> /// <returns></returns> public static Matrica FindMinimum(Function function, List <RestrictionExpression> implicitRestrictions, ExplicitRestriction explicitRestriction, Matrica startingPoint, double alfa, int iterations) { if (OptimizationUtils.CheckIfExplicitConstraintsAreMet(explicitRestriction, startingPoint) == false || OptimizationUtils.CheckIfImplicitConstraintsAreMet(implicitRestrictions, startingPoint) == false) { throw new ArgumentException("Starting point does not meet explicit and/or implicit conditions!"); } int iter = 0; function.SetParametersSize(startingPoint.NoOfRows); Matrica centroid = new Matrica(); centroid.Equals(startingPoint); int n = startingPoint.NoOfRows; Random random = new Random(); double R; List <Matrica> simplex = new List <Matrica>(); UnimodalInterval interval = explicitRestriction.GetInterval(); for (int t = 0; t < 2 * n; t++) { double[] point = new double[n]; for (int i = 0; i < n; i++) { R = random.NextDouble() * (maximumForRandom - minimumForRandom) + minimumForRandom; point[i] = interval.Minimum + R * (interval.Maximum - interval.Minimum); } simplex.Insert(t, Matrica.ArrayToVector(point)); bool[] simplexPointMeetsImplicitRestrictions = new bool[implicitRestrictions.Count]; for (int i = 0; i < implicitRestrictions.Count; i++) { simplexPointMeetsImplicitRestrictions[i] = false; } while (true) { for (int i = 0; i < implicitRestrictions.Count; i++) { simplexPointMeetsImplicitRestrictions[i] = implicitRestrictions[i].CheckIfMet(simplex[t].VectorToArray()); if (simplexPointMeetsImplicitRestrictions[i] == false) { simplex[t].AddValue(centroid); simplex[t].MultiplyByScalar(1.0 / 2); break; } } if (!simplexPointMeetsImplicitRestrictions.Contains(false)) { break; } } centroid = GetCentroidPoint(simplex); } List <double> functionValues = GetSimplexPointsFunctionValues(simplex, function); Matrica xr = new Matrica(); do { int h = GetIndexOfWorstPoint(functionValues); int h2 = GetIndexOfSecondWorstPoint(functionValues, h); centroid = GetCentroidPoint(simplex, h); xr = Reflection(centroid, simplex[h], alfa); for (int i = 0; i < xr.NoOfRows; i++) { if (xr.LoadedMatrix[i][0] < interval.Minimum) { xr.LoadedMatrix[i][0] = interval.Minimum; } else if (xr.LoadedMatrix[i][0] > interval.Maximum) { xr.LoadedMatrix[i][0] = interval.Maximum; } } bool[] reflectionMeetsImplicitRestrictions = new bool[implicitRestrictions.Count]; for (int i = 0; i < implicitRestrictions.Count; i++) { reflectionMeetsImplicitRestrictions[i] = false; } while (true) { for (int i = 0; i < implicitRestrictions.Count; i++) { reflectionMeetsImplicitRestrictions[i] = implicitRestrictions[i].CheckIfMet(xr.VectorToArray()); if (reflectionMeetsImplicitRestrictions[i] == false) { xr.AddValue(centroid); xr.MultiplyByScalar(1.0 / 2); break; } } if (!reflectionMeetsImplicitRestrictions.Contains(false)) { break; } } double xrFunctionValue = function.CalculateValue(xr.VectorToArray()); function.IncreaseCounterValue(algorithmName); if (xrFunctionValue > functionValues[h2]) { xr.AddValue(centroid); xr.MultiplyByScalar(1.0 / 2); } simplex[h] = xr; functionValues[h] = xrFunctionValue; iter++; } while (iter < iterations); function.DeleteParameters(); return(simplex.ElementAt(functionValues.IndexOf(functionValues.Min()))); }