File size: 5,227 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
#region Copyright © 2009, De Santiago-Castillo JA. All rights reserved.

//Copyright © 2009 Jose Antonio De Santiago-Castillo 
//E-mail:JAntonioDeSantiago@gmail.com
//Web: www.DotNumerics.com
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using DotNumerics.Optimization.LBFGSB;

namespace DotNumerics.Optimization
{
    /// <summary>
    /// Class used to minimizes a function of several variables by using Limited memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS) method. This class can be used for unconstrained and bounded constrained minimization.
    /// </summary>
    public class L_BFGS_B : xMinimizationBase
    {

        #region Fields

        LBFGSBDriver _Driver = new LBFGSBDriver();

        /// <summary>
        /// Accuracy factor
        /// </summary>
        private double _AccuracyFactor = 1E7;


        #endregion


        #region Constructor
        /// <summary>
        /// Initializes a new instance of the L_BFG_S class.
        /// </summary>
        public L_BFGS_B()
        {
        }

        #endregion


        #region Properties

        /// <summary>
        /// Accuracy factor. The iteration will stop when (f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} is least than AccuracyFactor*epsmch
        /// where epsmch is the machine precision. Typical values for AccuracyFactor: 1E12 for low accuracy; 1E7 for moderate accuracy; 
        /// 1E1 for extremely high accuracy.
        /// </summary>
        public double AccuracyFactor
        {
            get { return _AccuracyFactor; }
            set { _AccuracyFactor = value; }
        }

        #endregion


        #region Public methods

        /// <summary>
        /// Computes the minimum point of a function of several variables.
        /// </summary>
        /// <param name="function">The function to minimize.</param>
        /// <param name="gradient">A delegate that computes the gradient.</param>
        /// <param name="initialGuess">Array of size N containing the initial guess. N is the number of variables.</param>
        /// <returns>Array containing the solution.</returns>
        public double[] ComputeMin(OptMultivariateFunction function, OptMultivariateGradient gradient,  double[] initialGuess)
        {

            if (initialGuess == null) return new double[0];
            if (initialGuess.Length == 0) return new double[0];

            OptVariable[] variables = this.GetVariables(initialGuess);

            int maxFunc = this._MaxFunEvaluations;

            double[] minimum= this._Driver.ComputeMin(function, gradient, variables, this._Tolerance, this._AccuracyFactor, ref maxFunc);
            this._FunEvaluations = maxFunc;
            return minimum;
        }


        /// <summary>
        /// Computes the minimum point of a function of several variables.
        /// </summary>
        /// <param name="function">The function to minimize.</param>
        /// <param name="gradient">A delegate that computes the gradient.</param>
        /// <param name="variables">Array of size N containing the varaibles.</param>
        /// <returns>Array containing the solution.</returns>
        public double[] ComputeMin(OptMultivariateFunction function, OptMultivariateGradient gradient, OptVariable[] variables)
        {

            if (variables == null) return new double[0];
            if (variables.Length == 0) return new double[0];

            int maxFunc = this._MaxFunEvaluations;

            double[] minimum = this._Driver.ComputeMin(function, gradient, variables, this._Tolerance, this._AccuracyFactor, ref maxFunc);
            this._FunEvaluations = maxFunc;
            return minimum;
        }


        /// <summary>
        /// Computes the minimum point of a function of several variables.
        /// </summary>
        /// <param name="function">The function to minimize.</param>
        /// <param name="gradient">A delegate that computes the gradient.</param>
        /// <param name="variables">Array of size N containing the varaibles.</param>
        /// <returns>Array containing the solution.</returns>
        public double[] ComputeMin(OptMultivariateFunction function, OptMultivariateGradient gradient, OptBoundVariable[] variables)
        {

            if (variables == null) return new double[0];
            if (variables.Length == 0) return new double[0];

            int maxFunc = this._MaxFunEvaluations;

            double[] minimum = this._Driver.ComputeMin(function, gradient, variables, this._Tolerance, this._AccuracyFactor, ref maxFunc);
            this._FunEvaluations = maxFunc;
            return minimum;
        }

        #endregion


        #region private Methods

        private OptVariable[] GetVariables(double[] variablesArray)
        {
            OptVariable[] vars = new OptVariable[variablesArray.Length];

            for (int i = 0; i < variablesArray.Length; i++)
            {
                vars[i] = new OptVariable(variablesArray[i]);
            }

            return vars;
        }

        #endregion




    }
}