File size: 6,606 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/// ------------------------------------------------------
/// 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
{
    /// <summary>
    /// 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.
    /// </summary>
    public class CurveFittingExp : CurveFitting
    {
        #region Constructors.
        /// <summary>
        /// Create the object.
        /// </summary>
        /// <param name="x">X-axis values.</param>
        /// <param name="y">Y-axis values, curve to be fitted.</param>
        /// <param name="minA">Minimum value for A parameter.</param>
        /// <param name="maxA">Maximum value for A parameter.</param>
        /// <param name="minB">Minimum value for B parameter.</param>
        /// <param name="maxB">Maximum value for B parameter.</param>
        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.
        /// <summary>
        /// Get the A parameter.
        /// </summary>
        public double GetA(double[] parameters)
        {
            return parameters[0];
        }

        /// <summary>
        /// Set the A parameter.
        /// </summary>
        public void SetA(ref double[] parameters, double a)
        {
            parameters[0] = a;
        }

        /// <summary>
        /// Get the B parameter.
        /// </summary>
        public double GetB(double[] parameters)
        {
            return parameters[1];
        }

        /// <summary>
        /// Set the B parameter.
        /// </summary>
        public void SetB(ref double[] parameters, double b)
        {
            parameters[1] = b;
        }
        #endregion

        #region Base-class overrides, Problem.
        /// <summary>
        /// Name of the optimization problem.
        /// </summary>
        public override string Name
        {
            get { return "CurveFittingExp"; }
        }

        /// <summary>
        /// Dimensionality of the optimization problem.
        /// </summary>
        public override int Dimensionality
        {
            get { return 2; }
        }

        /// <summary>
        /// Threshold for an acceptable fitness value.
        /// </summary>
        public override double AcceptableFitness
        {
            get
            {
                return 0.001;
            }
        }

        double[] _lowerBound;

        /// <summary>
        /// Lower boundary for the search-space.
        /// </summary>
        public override double[] LowerBound
        {
            get { return _lowerBound; }
        }

        double[] _upperBound;

        /// <summary>
        /// Upper boundary for the search-space.
        /// </summary>
        public override double[] UpperBound
        {
            get { return _upperBound; }
        }

        double[] _lowerInit;

        /// <summary>
        /// Lower initialization boundary.
        /// </summary>
        public override double[] LowerInit
        {
            get { return _lowerInit; }
        }

        double[] _upperInit;

        /// <summary>
        /// Upper initialization boundary.
        /// </summary>
        public override double[] UpperInit
        {
            get { return _upperInit; }
        }

        string[] _parameterName = { "A", "B" };

        /// <summary>
        /// Array with names of parameters.
        /// </summary>
        public override string[] ParameterName
        {
            get { return _parameterName; }
        }

        /// <summary>
        /// Has the gradient has been implemented?
        /// </summary>
        public override bool HasGradient
        {
            get { return true; }
        }

        /// <summary>
        /// Compute the gradient of the fitness-function.
        /// </summary>
        /// <param name="parameters">Candidate solution.</param>
        /// <param name="v">Array for holding the gradient.</param>
        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.
        /// <summary>
        /// Compute the value y given x using the curve-fitting function.
        /// </summary>
        /// <param name="parameters">Parameters for curve-fitting function.</param>
        /// <param name="x">X-axis value.</param>
        /// <returns>Computed Y-axis value.</returns>
        public override double ComputeY(double[] parameters, double x)
        {
            double a = GetA(parameters);
            double b = GetB(parameters);

            return b * System.Math.Pow(a, x);
        }
        #endregion
    }
}