/// ------------------------------------------------------ /// SwarmOps - Numeric and heuristic optimization for C# /// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen. /// Please see the file license.txt for license details. /// SwarmOps on the internet: http://www.Hvass-Labs.org/ /// ------------------------------------------------------ using System.Diagnostics; namespace SwarmOps.Problems { /// /// Curve-fitting to the exponential curve f(x) = B*Pow(A, x). /// You may wish to use a form of regression instead, /// depending on your requirements for statistical minimization /// of error. /// public class CurveFittingExp : CurveFitting { #region Constructors. /// /// Create the object. /// /// X-axis values. /// Y-axis values, curve to be fitted. /// Minimum value for A parameter. /// Maximum value for A parameter. /// Minimum value for B parameter. /// Maximum value for B parameter. public CurveFittingExp( double[] x, double[] y, double minA, double maxA, double minB, double maxB) : base(x, y) { _lowerBound = new double[] { minA, minB }; _lowerInit = _lowerBound; _upperBound = new double[] { maxA, maxB }; _upperInit = _upperBound; } #endregion #region Public methods. /// /// Get the A parameter. /// public double GetA(double[] parameters) { return parameters[0]; } /// /// Set the A parameter. /// public void SetA(ref double[] parameters, double a) { parameters[0] = a; } /// /// Get the B parameter. /// public double GetB(double[] parameters) { return parameters[1]; } /// /// Set the B parameter. /// public void SetB(ref double[] parameters, double b) { parameters[1] = b; } #endregion #region Base-class overrides, Problem. /// /// Name of the optimization problem. /// public override string Name { get { return "CurveFittingExp"; } } /// /// Dimensionality of the optimization problem. /// public override int Dimensionality { get { return 2; } } /// /// Threshold for an acceptable fitness value. /// public override double AcceptableFitness { get { return 0.001; } } double[] _lowerBound; /// /// Lower boundary for the search-space. /// public override double[] LowerBound { get { return _lowerBound; } } double[] _upperBound; /// /// Upper boundary for the search-space. /// public override double[] UpperBound { get { return _upperBound; } } double[] _lowerInit; /// /// Lower initialization boundary. /// public override double[] LowerInit { get { return _lowerInit; } } double[] _upperInit; /// /// Upper initialization boundary. /// public override double[] UpperInit { get { return _upperInit; } } string[] _parameterName = { "A", "B" }; /// /// Array with names of parameters. /// public override string[] ParameterName { get { return _parameterName; } } /// /// Has the gradient has been implemented? /// public override bool HasGradient { get { return true; } } /// /// Compute the gradient of the fitness-function. /// /// Candidate solution. /// Array for holding the gradient. public override int Gradient(double[] parameters, ref double[] v) { Debug.Assert(parameters != null && parameters.Length == Dimensionality); Debug.Assert(v != null && v.Length == Dimensionality); Debug.Assert(X.Length == Y.Length); // Get parameters. double a = GetA(parameters); double b = GetB(parameters); double gradientA = 0; double gradientB = 0; for (int i = 0; i < X.Length; i++) { double x = X[i]; double x2 = x * x; double lnx = System.Math.Log(x); double lnx2 = System.Math.Log(x2); double xPowa = System.Math.Pow(x, a); double x2Powa = System.Math.Pow(x2, a); double y = Y[i]; gradientA += b * b * lnx2 * x2Powa - 2 * b * y * lnx * xPowa; gradientB += 2 * (b * x2Powa - xPowa * y); } SetA(ref v, gradientA); SetB(ref v, gradientB); return 0; } #endregion #region Base-class overrides, CurveFitting. /// /// Compute the value y given x using the curve-fitting function. /// /// Parameters for curve-fitting function. /// X-axis value. /// Computed Y-axis value. public override double ComputeY(double[] parameters, double x) { double a = GetA(parameters); double b = GetB(parameters); return b * System.Math.Pow(a, x); } #endregion } }