1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.GradientDescent {
|
---|
33 | [StorableClass("3393605C-8AA5-4A4B-8F43-83966A8E60A9")]
|
---|
34 | [Item(Name = "LBFGS Initializer", Description = "Initializes the necessary data structures for the LM-BFGS algorithm.")]
|
---|
35 | public sealed class LbfgsInitializer : SingleSuccessorOperator {
|
---|
36 | private const string PointParameterName = "Point";
|
---|
37 | private const string StateParameterName = "State";
|
---|
38 | private const string IterationsParameterName = "Iterations";
|
---|
39 | private const string ApproximateGradientsParameterName = "ApproximateGradients";
|
---|
40 | private const string GradientCheckStepSizeParameterName = "GradientCheckStepSize";
|
---|
41 |
|
---|
42 | #region Parameter Properties
|
---|
43 | // in
|
---|
44 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
45 | get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
46 | }
|
---|
47 | public ILookupParameter<RealVector> PointParameter {
|
---|
48 | get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
|
---|
49 | }
|
---|
50 | // out
|
---|
51 | public ILookupParameter<LbfgsState> StateParameter {
|
---|
52 | get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<BoolValue> ApproximateGradientsParameter {
|
---|
55 | get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<DoubleValue> GradientStepSizeParameter {
|
---|
58 | get { return (ILookupParameter<DoubleValue>)Parameters[GradientCheckStepSizeParameterName]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Properties
|
---|
65 | private RealVector Point { get { return PointParameter.ActualValue; } }
|
---|
66 | private IntValue Iterations { get { return IterationsParameter.ActualValue; } }
|
---|
67 | private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
|
---|
68 | #endregion
|
---|
69 |
|
---|
70 | [StorableConstructor]
|
---|
71 | private LbfgsInitializer(bool deserializing) : base(deserializing) { }
|
---|
72 | private LbfgsInitializer(LbfgsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
73 | public LbfgsInitializer()
|
---|
74 | : base() {
|
---|
75 | // in
|
---|
76 | Parameters.Add(new LookupParameter<RealVector>(PointParameterName, "The initial point for the LM-BFGS algorithm."));
|
---|
77 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The maximal number of iterations for the LM-BFGS algorithm."));
|
---|
78 | Parameters.Add(new LookupParameter<BoolValue>(ApproximateGradientsParameterName,
|
---|
79 | "Flag that indicates if gradients should be approximated."));
|
---|
80 | Parameters.Add(new LookupParameter<DoubleValue>(GradientCheckStepSizeParameterName, "Step size for the gradient check (should be used for debugging the gradient calculation only)."));
|
---|
81 | // out
|
---|
82 | Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
86 | return new LbfgsInitializer(this, cloner);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override IOperation Apply() {
|
---|
90 | double[] initialPoint = Point.ToArray();
|
---|
91 | int n = initialPoint.Length;
|
---|
92 | alglib.minlbfgs.minlbfgsstate state = new alglib.minlbfgs.minlbfgsstate();
|
---|
93 | if (ApproximateGradients.Value) {
|
---|
94 | alglib.minlbfgs.minlbfgscreatef(n, Math.Min(n, 10), initialPoint, 1E-5, state);
|
---|
95 | } else {
|
---|
96 | alglib.minlbfgs.minlbfgscreate(n, Math.Min(n, 10), initialPoint, state);
|
---|
97 | }
|
---|
98 | alglib.minlbfgs.minlbfgssetcond(state, 0.0, 0, 0, Iterations.Value);
|
---|
99 | alglib.minlbfgs.minlbfgssetxrep(state, true);
|
---|
100 | if (GradientStepSizeParameter.ActualValue != null && GradientStepSizeParameter.ActualValue.Value > 0)
|
---|
101 | alglib.minlbfgs.minlbfgssetgradientcheck(state, GradientStepSizeParameter.ActualValue.Value);
|
---|
102 |
|
---|
103 | PointParameter.ActualValue = new RealVector(initialPoint);
|
---|
104 | StateParameter.ActualValue = new LbfgsState(state);
|
---|
105 | return base.Apply();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|