ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// <copyright file="BfgsBMinimizer.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2017 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using System.Collections.Generic;
using IStation.Numerics.LinearAlgebra;
using IStation.Numerics.LinearAlgebra.Double;
using IStation.Numerics.Optimization.LineSearch;
 
namespace IStation.Numerics.Optimization
{
    /// <summary>
    /// Broyden–Fletcher–Goldfarb–Shanno Bounded (BFGS-B) algorithm is an iterative method for solving box-constrained nonlinear optimization problems
    /// http://www.ece.northwestern.edu/~nocedal/PSfiles/limited.ps.gz
    /// </summary>
    public class BfgsBMinimizer : BfgsMinimizerBase
    {
        public BfgsBMinimizer(double gradientTolerance, double parameterTolerance, double functionProgressTolerance, int maximumIterations = 1000)
            : base(gradientTolerance,parameterTolerance,functionProgressTolerance,maximumIterations)
        {
        }
 
        /// <summary>
        /// Find the minimum of the objective function given lower and upper bounds
        /// </summary>
        /// <param name="objective">The objective function, must support a gradient</param>
        /// <param name="lowerBound">The lower bound</param>
        /// <param name="upperBound">The upper bound</param>
        /// <param name="initialGuess">The initial guess</param>
        /// <returns>The MinimizationResult which contains the minimum and the ExitCondition</returns>
        public MinimizationResult FindMinimum(IObjectiveFunction objective, Vector<double> lowerBound, Vector<double> upperBound, Vector<double> initialGuess)
        {
            _lowerBound = lowerBound;
            _upperBound = upperBound;
            if (!objective.IsGradientSupported)
                throw new IncompatibleObjectiveException("Gradient not supported in objective function, but required for BFGS minimization.");
 
            // Check that dimensions match
            if (lowerBound.Count != upperBound.Count || lowerBound.Count != initialGuess.Count)
                throw new ArgumentException("Dimensions of bounds and/or initial guess do not match.");
 
            // Check that initial guess is feasible
            for (int ii = 0; ii < initialGuess.Count; ++ii)
                if (initialGuess[ii] < lowerBound[ii] || initialGuess[ii] > upperBound[ii])
                    throw new ArgumentException("Initial guess is not in the feasible region");
 
            objective.EvaluateAt(initialGuess);
            ValidateGradientAndObjective(objective);
 
            // Check that we're not already done
            var currentExitCondition = ExitCriteriaSatisfied(objective, null, 0);
            if (currentExitCondition != ExitCondition.None)
                return new MinimizationResult(objective, 0, currentExitCondition);
 
            // Set up line search algorithm
            var lineSearcher = new StrongWolfeLineSearch(1e-4, 0.9, Math.Max(ParameterTolerance, 1e-5), maxIterations: 1000);
 
            // Declare state variables
            Vector<double> reducedSolution1, reducedGradient, reducedInitialPoint, reducedCauchyPoint, solution1;
            Matrix<double> reducedHessian;
            List<int> reducedMap;
 
            // First step
            var pseudoHessian = CreateMatrix.DiagonalIdentity<double>(initialGuess.Count);
 
            // Determine active set
            var gradientProjectionResult = QuadraticGradientProjectionSearch.Search(objective.Point, objective.Gradient, pseudoHessian, lowerBound, upperBound);
            var cauchyPoint = gradientProjectionResult.CauchyPoint;
            var fixedCount = gradientProjectionResult.FixedCount;
            var isFixed = gradientProjectionResult.IsFixed;
            var freeCount = lowerBound.Count - fixedCount;
 
            if (freeCount > 0)
            {
                reducedGradient = new DenseVector(freeCount);
                reducedHessian = new DenseMatrix(freeCount, freeCount);
                reducedMap = new List<int>(freeCount);
                reducedInitialPoint = new DenseVector(freeCount);
                reducedCauchyPoint = new DenseVector(freeCount);
 
                CreateReducedData(objective.Point, cauchyPoint, isFixed, lowerBound, upperBound, objective.Gradient, pseudoHessian, reducedInitialPoint, reducedCauchyPoint, reducedGradient, reducedHessian, reducedMap);
 
                // Determine search direction and maximum step size
                reducedSolution1 = reducedInitialPoint + reducedHessian.Cholesky().Solve(-reducedGradient);
 
                solution1 = ReducedToFull(reducedMap, reducedSolution1, cauchyPoint);
            }
            else
            {
                solution1 = cauchyPoint;
            }
 
            var directionFromCauchy = solution1 - cauchyPoint;
            var maxStepFromCauchyPoint = FindMaxStep(cauchyPoint, directionFromCauchy, lowerBound, upperBound);
 
            var solution2 = cauchyPoint + Math.Min(maxStepFromCauchyPoint, 1.0)*directionFromCauchy;
 
            var lineSearchDirection = solution2 - objective.Point;
            var maxLineSearchStep = FindMaxStep(objective.Point, lineSearchDirection, lowerBound, upperBound);
            var estStepSize = -objective.Gradient*lineSearchDirection/(lineSearchDirection*pseudoHessian*lineSearchDirection);
 
            var startingStepSize = Math.Min(Math.Max(estStepSize, 1.0), maxLineSearchStep);
 
            // Line search
            LineSearchResult lineSearchResult;
            try
            {
                lineSearchResult = lineSearcher.FindConformingStep(objective, lineSearchDirection, startingStepSize, upperBound: maxLineSearchStep);
            }
            catch (Exception e)
            {
                throw new InnerOptimizationException("Line search failed.", e);
            }
 
            var previousPoint = objective.Fork();
            var candidatePoint = lineSearchResult.FunctionInfoAtMinimum;
            ValidateGradientAndObjective(candidatePoint);
 
            // Check that we're not done
            currentExitCondition = ExitCriteriaSatisfied(candidatePoint, previousPoint, 0);
            if (currentExitCondition != ExitCondition.None)
                return new MinimizationResult(candidatePoint, 0, currentExitCondition);
 
            var gradient = candidatePoint.Gradient;
            var step = candidatePoint.Point - initialGuess;
 
            // Subsequent steps
            int totalLineSearchSteps = lineSearchResult.Iterations;
            int iterationsWithNontrivialLineSearch = lineSearchResult.Iterations > 0 ? 0 : 1;
 
            int iterations = DoBfgsUpdate(ref currentExitCondition, lineSearcher, ref pseudoHessian, ref lineSearchDirection, ref previousPoint, ref lineSearchResult, ref candidatePoint, ref step, ref totalLineSearchSteps, ref iterationsWithNontrivialLineSearch);
 
            if (iterations == MaximumIterations && currentExitCondition == ExitCondition.None)
                throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({MaximumIterations}) reached."));
 
            return new MinimizationWithLineSearchResult(candidatePoint, iterations, currentExitCondition, totalLineSearchSteps, iterationsWithNontrivialLineSearch);
        }
 
        protected override Vector<double> CalculateSearchDirection(ref Matrix<double> pseudoHessian,
            out double maxLineSearchStep,
            out double startingStepSize,
            IObjectiveFunction previousPoint,
            IObjectiveFunction candidatePoint,
            Vector<double> step)
        {
            Vector<double> lineSearchDirection;
            var y = candidatePoint.Gradient - previousPoint.Gradient;
 
            double sy = step * y;
            if (sy > 0.0) // only do update if it will create a positive definite matrix
            {
                double sts = step * step;
 
                var Hs = pseudoHessian * step;
                var sHs = step * pseudoHessian * step;
                pseudoHessian = pseudoHessian + y.OuterProduct(y) * (1.0 / sy) - Hs.OuterProduct(Hs) * (1.0 / sHs);
            }
            else
            {
                //pseudo_hessian = LinearAlgebra.Double.DiagonalMatrix.Identity(initial_guess.Count);
            }
 
            // Determine active set
            var gradientProjectionResult = QuadraticGradientProjectionSearch.Search(candidatePoint.Point, candidatePoint.Gradient, pseudoHessian, _lowerBound, _upperBound);
            var cauchyPoint = gradientProjectionResult.CauchyPoint;
            var fixedCount = gradientProjectionResult.FixedCount;
            var isFixed = gradientProjectionResult.IsFixed;
            var freeCount = _lowerBound.Count - fixedCount;
            Vector<double> solution1;
            if (freeCount > 0)
            {
                var reducedGradient = new DenseVector(freeCount);
                var reducedHessian = new DenseMatrix(freeCount, freeCount);
                var reducedMap = new List<int>(freeCount);
                var reducedInitialPoint = new DenseVector(freeCount);
                var reducedCauchyPoint = new DenseVector(freeCount);
 
                CreateReducedData(candidatePoint.Point, cauchyPoint, isFixed, _lowerBound, _upperBound, candidatePoint.Gradient, pseudoHessian, reducedInitialPoint, reducedCauchyPoint, reducedGradient, reducedHessian, reducedMap);
 
                // Determine search direction and maximum step size
                Vector<double> reducedSolution1 = reducedInitialPoint + reducedHessian.Cholesky().Solve(-reducedGradient);
 
                solution1 = ReducedToFull(reducedMap, reducedSolution1, cauchyPoint);
            }
            else
            {
                solution1 = cauchyPoint;
            }
 
            var directionFromCauchy = solution1 - cauchyPoint;
            var maxStepFromCauchyPoint = FindMaxStep(cauchyPoint, directionFromCauchy, _lowerBound, _upperBound);
 
            var solution2 = cauchyPoint + Math.Min(maxStepFromCauchyPoint, 1.0) * directionFromCauchy;
 
            lineSearchDirection = solution2 - candidatePoint.Point;
            maxLineSearchStep = FindMaxStep(candidatePoint.Point, lineSearchDirection, _lowerBound, _upperBound);
 
            if (maxLineSearchStep == 0.0)
            {
                lineSearchDirection = cauchyPoint - candidatePoint.Point;
                maxLineSearchStep = FindMaxStep(candidatePoint.Point, lineSearchDirection, _lowerBound, _upperBound);
            }
 
            double estStepSize = -candidatePoint.Gradient * lineSearchDirection / (lineSearchDirection * pseudoHessian * lineSearchDirection);
 
            startingStepSize = Math.Min(Math.Max(estStepSize, 1.0), maxLineSearchStep);
            return lineSearchDirection;
        }
 
        static Vector<double> ReducedToFull(List<int> reducedMap, Vector<double> reducedVector, Vector<double> fullVector)
        {
            var output = fullVector.Clone();
            for (int ii = 0; ii < reducedMap.Count; ++ii)
                output[reducedMap[ii]] = reducedVector[ii];
            return output;
        }
 
        Vector<double> _lowerBound;
        Vector<double> _upperBound;
 
        static double FindMaxStep(Vector<double> startingPoint, Vector<double> searchDirection, Vector<double> lowerBound, Vector<double> upperBound)
        {
            double maxStep = Double.PositiveInfinity;
            for (int ii = 0; ii < startingPoint.Count; ++ii)
            {
                double paramMaxStep;
                if (searchDirection[ii] > 0)
                    paramMaxStep = (upperBound[ii] - startingPoint[ii])/searchDirection[ii];
                else if (searchDirection[ii] < 0)
                    paramMaxStep = (startingPoint[ii] - lowerBound[ii])/-searchDirection[ii];
                else
                    paramMaxStep = Double.PositiveInfinity;
 
                if (paramMaxStep < maxStep)
                    maxStep = paramMaxStep;
            }
            return maxStep;
        }
 
        static void CreateReducedData(Vector<double> initialPoint, Vector<double> cauchyPoint, List<bool> isFixed, Vector<double> lowerBound, Vector<double> upperBound, Vector<double> gradient, Matrix<double> pseudoHessian, Vector<double> reducedInitialPoint, Vector<double> reducedCauchyPoint, Vector<double> reducedGradient, Matrix<double> reducedHessian, List<int> reducedMap)
        {
            int ll = 0;
            for (int ii = 0; ii < lowerBound.Count; ++ii)
            {
                if (!isFixed[ii])
                {
                    // hessian
                    int mm = 0;
                    for (int jj = 0; jj < lowerBound.Count; ++jj)
                    {
                        if (!isFixed[jj])
                        {
                            reducedHessian[ll, mm++] = pseudoHessian[ii, jj];
                        }
                    }
 
                    // gradient
                    reducedInitialPoint[ll] = initialPoint[ii];
                    reducedCauchyPoint[ll] = cauchyPoint[ii];
                    reducedGradient[ll] = gradient[ii];
                    ll += 1;
                    reducedMap.Add(ii);
 
                }
            }
        }
 
        protected override double GetProjectedGradient(IObjectiveFunctionEvaluation candidatePoint, int ii)
        {
            double projectedGradient;
            bool atLowerBound = candidatePoint.Point[ii] - _lowerBound[ii] < VerySmall;
            bool atUpperBound = _upperBound[ii] - candidatePoint.Point[ii] < VerySmall;
 
            if (atLowerBound && atUpperBound)
                projectedGradient = 0.0;
            else if (atLowerBound)
                projectedGradient = Math.Min(candidatePoint.Gradient[ii], 0.0);
            else if (atUpperBound)
                projectedGradient = Math.Max(candidatePoint.Gradient[ii], 0.0);
            else
                projectedGradient = base.GetProjectedGradient(candidatePoint, ii);
            return projectedGradient;
        }
    }
}