Swann bracketing

by Trent Guidry 3. October 2009 07:51

In this post I develop the code for the Swann bracketing class.

The Swann bracketing algorithm starts with an initial starting value and an initial step size. The function values at the starting point x0 and the starting point plus the step size, x1 are then computed. If the function value at x1 is greater than the function value at x0, then the points are reversed and the sign on the step is reversed. From that point forward, new x values are calculated based on xk+1 = xk + 2k times the initial step size and the function is evaluated at the new point. This process is continued until the functional value at the new point is greater than the functional value of the last point.

The source code for this class is given at the bottom of this post.

To give an example of how to use this class, a WPF application was created. In the Loaded event of the form, the code below was added.

            BracketingBase bracket = new SwannBracketing();
            RegionEliminationBase re = new CubicRegionElimination();
            Func<double, double> function = (x) =>
            {
                return (100 - x) * (100 - x);
            };
            SearchPointCollection col = bracket.Bracket(function, 0.0, 1.0, re.NumStartingPoints);
            RegionEliminationResults res = re.RegionEliminate(function, col, 1e-5);

Running this gives the expected value of 100.0 for the minimum.

The source code for the Swann bracketing class is given below. The code for the bracketing base class was posted previously.

    public class SwannBracketing : BracketingBase
    {
        public SwannBracketing()
            : base()
        {
        }

        public override SearchPointCollection Bracket(Func<double, double> function, double startPosition, double step, int targetReturnPoints)
        {
            SearchPointCollection returnCollection = new SearchPointCollection();
            returnCollection.MaxSize = targetReturnPoints;
            double delta = step;
            double currentPosition = startPosition;
            double functionValue = function(currentPosition);
            SearchPoint newSearchPoint = new SearchPoint(currentPosition, functionValue);
            returnCollection.Add(newSearchPoint);

            currentPosition += delta;
            functionValue = function(currentPosition);
            if (functionValue > newSearchPoint.Y)
            {
                delta *= -1.0;
                currentPosition = startPosition + delta;
                functionValue = function(currentPosition);
            }
            while (functionValue < newSearchPoint.Y && !Cancel)
            {
                newSearchPoint = new SearchPoint(currentPosition, functionValue);
                returnCollection.Add(newSearchPoint);
                delta *= 2.0;
                currentPosition += delta;
                functionValue = function(currentPosition);
            }
            newSearchPoint = new SearchPoint(currentPosition, functionValue);
            returnCollection.Add(newSearchPoint);

            return returnCollection;
        }
    }

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



TextBox

I live in Clear Lake, Houston, Texas.

RecentComments

Comment RSS