Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsInitializer.cs @ 11813

Last change on this file since 11813 was 9211, checked in by gkronber, 12 years ago

#1902 reformulated calculation of periodic covariance function

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.GradientDescent {
33  [StorableClass]
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
41    #region Parameter Properties
42    // in
43    public ILookupParameter<IntValue> IterationsParameter {
44      get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
45    }
46    public ILookupParameter<RealVector> PointParameter {
47      get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
48    }
49    // out
50    public ILookupParameter<LbfgsState> StateParameter {
51      get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
52    }
53    public ILookupParameter<BoolValue> ApproximateGradientsParameter {
54      get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
55    }
56
57
58    #endregion
59
60    #region Properties
61    private RealVector Point { get { return PointParameter.ActualValue; } }
62    private IntValue Iterations { get { return IterationsParameter.ActualValue; } }
63    private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
64    #endregion
65
66    [StorableConstructor]
67    private LbfgsInitializer(bool deserializing) : base(deserializing) { }
68    private LbfgsInitializer(LbfgsInitializer original, Cloner cloner) : base(original, cloner) { }
69    public LbfgsInitializer()
70      : base() {
71      // in
72      Parameters.Add(new LookupParameter<RealVector>(PointParameterName, "The initial point for the LM-BFGS algorithm."));
73      Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The maximal number of iterations for the LM-BFGS algorithm."));
74      Parameters.Add(new LookupParameter<BoolValue>(ApproximateGradientsParameterName,
75                                                    "Flag that indicates if gradients should be approximated."));
76      // out
77      Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new LbfgsInitializer(this, cloner);
82    }
83
84    public override IOperation Apply() {
85      double[] initialPoint = Point.ToArray();
86      int n = initialPoint.Length;
87      alglib.minlbfgs.minlbfgsstate state = new alglib.minlbfgs.minlbfgsstate();
88      if (ApproximateGradients.Value) {
89        alglib.minlbfgs.minlbfgscreatef(n, Math.Min(n, 10), initialPoint, 1E-5, state);
90      } else {
91        alglib.minlbfgs.minlbfgscreate(n, Math.Min(n, 10), initialPoint, state);
92      }
93      alglib.minlbfgs.minlbfgssetcond(state, 0.0, 0, 0, Iterations.Value);
94      alglib.minlbfgs.minlbfgssetxrep(state, true);
95      // alglib.minlbfgs.minlbfgssetgradientcheck(state, 0.000001);
96
97      PointParameter.ActualValue = new RealVector(initialPoint);
98      StateParameter.ActualValue = new LbfgsState(state);
99      return base.Apply();
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.