void SetupFunctions() { uint minVars = 2; uint maxVars = 10; OptimizingFunction firstFunc = (List <double> vars) => { double pow = vars.Select(x => Math.Abs(x)).Sum(); return(-10 * Math.Exp(-Math.Sqrt(pow / vars.Count))); }; OptimizingFunction secondFunc = (List <double> vars) => { double pow = vars.Select(x => Math.Abs(x)).Sum(); double pow2 = vars.Select(x => Math.Cos(2 * Math.PI * x)).Sum(); return(-10 * Math.Exp(-Math.Sqrt(pow / vars.Count)) - Math.Exp(pow2 / vars.Count)); }; LipzitsFunction first = (double epsilon) => 25 / epsilon; LipzitsFunction second = (double epsilon) => 25 / epsilon - 2 * Math.PI * Math.E; List <FunctionItem> items = new List <FunctionItem>(2) { new FunctionItem(firstFunc, first, minVars, maxVars, "-10 * Math.Exp(-Math.Sqrt(pow / vars.Count))"), new FunctionItem(secondFunc, second, minVars, maxVars, "... - Math.Exp(pow2 / vars.Count)"), }; FunctionListComboBox.DisplayMember = "Name"; FunctionListComboBox.DataSource = items; FunctionListComboBox.SelectedIndex = 0; }
public static (double FunctionMinimum, int counter, int optimal, List <double> optimalPoint) Solve(OptimizingFunction function, LipzitsFunction lipzits, double precision, double epsilon, List <double> lowerBound, List <double> upperBound, RuleM ruleSubList, RuleM ruleMainList, GraphicSettings settings = null) { double lipVal = lipzits(epsilon); LinkedList <ModRectangle> rectangles = new LinkedList <ModRectangle>(); ModRectangle first = new ModRectangle(lowerBound, upperBound, settings); rectangles.AddFirst(first); int counter = 1; int optimal = counter; double currentMin = function(lowerBound); double h_base = 2d * (precision / lipVal - epsilon / lipVal); double h = h_base; List <double> optimalPoint = new List <double>(lowerBound); while (rectangles.Count != 0) { var currentRect = rectangles.Dequeue(); var iterPoint = currentRect.GetIterationPoint(h); var functionValue = function(iterPoint); if (functionValue > currentMin) { h = h_base + (functionValue - currentMin) / lipVal; } else { h = h_base; currentMin = functionValue; optimalPoint = new List <double>(iterPoint); optimal = counter; } var splitted = currentRect.Split(h, ruleSubList, settings); rectangles.ConcatList(splitted, ruleMainList); counter++; } GC.Collect(); return(currentMin, counter, optimal, optimalPoint); }
public static (double FunctionMinimum, int counter, int optimal, List <double> optimalPoint) Solve(OptimizingFunction function, LipzitsFunction lipzits, double precision, double epsilon, List <double> lowerBound, List <double> upperBound, RuleD rule, GraphicSettings settings = null) { double lipConst = lipzits(epsilon); LinkedList <DihtomiaRectangle> rectangles = new LinkedList <DihtomiaRectangle>(); DihtomiaRectangle first = new DihtomiaRectangle(lowerBound, upperBound, settings); first.EvalQ(function, lipConst); rectangles.AddFirst(first); int counter = 1; int optimal = counter; List <double> optimalPoint; double currentMin = Math.Min(function(lowerBound), function(first.center)); if (function(lowerBound) < function(first.center)) { optimalPoint = new List <double>(lowerBound); } else { optimalPoint = new List <double>(first.center); } if (first.Q >= currentMin - epsilon) { return(currentMin, counter, optimal, optimalPoint); } while (rectangles.Count != 0) { var current = GetRectangle(rectangles, rule); var newRects = current.SplitByMaxSide(settings); newRects.first.EvalQ(function, lipConst); newRects.second.EvalQ(function, lipConst); double funcInFirst = function(newRects.first.center); double funcInSecond = function(newRects.second.center); if (currentMin < Math.Min(funcInFirst, funcInSecond)) { if (newRects.first.Q < (currentMin - epsilon) && newRects.first.GetMaxSide().max >= (2d * precision / lipConst)) { rectangles.AddFirst(newRects.first); } if (newRects.second.Q < (currentMin - epsilon) && newRects.second.GetMaxSide().max >= (2d * precision / lipConst)) { rectangles.AddFirst(newRects.second); } } else { if (funcInFirst < funcInSecond) { currentMin = funcInFirst; rectangles.AddFirst(newRects.first); optimalPoint = new List <double>(newRects.first.center); } else { currentMin = funcInSecond; rectangles.AddFirst(newRects.second); optimalPoint = new List <double>(newRects.second.center); } rectangles.RemoveWorse(currentMin, epsilon); optimal = counter; } counter++; } GC.Collect(); return(currentMin, counter, optimal, optimalPoint); }