/// ------------------------------------------------------ /// 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 linear curve f(x) = A*x + B. /// This is meant as an example, you will want to use /// linear regression instead for real applications. /// public class CurveFittingLin : 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 CurveFittingLin( 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 Get and set parameters. /// /// 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 "CurveFittingLinear"; } } /// /// 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.01; } } 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 y = Y[i]; gradientA += x * (2 * x * a + b - y); gradientB += a * x + 2 * b - 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 a * x + b; } #endregion } }