Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HivePerformance/sources/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsInitializer.cs @ 9522

Last change on this file since 9522 was 9444, checked in by pfleck, 12 years ago

#2030
Switched Text encoding to Mtom encoding for better performance for binary data.
Merged trunk into branch.

File size: 5.0 KB
RevLine 
[8371]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;
[8396]27using HeuristicLab.Encodings.RealVectorEncoding;
[8371]28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[8401]32namespace HeuristicLab.Algorithms.GradientDescent {
[8371]33  [StorableClass]
[8396]34  [Item(Name = "LBFGS Initializer", Description = "Initializes the necessary data structures for the LM-BFGS algorithm.")]
35  public sealed class LbfgsInitializer : SingleSuccessorOperator {
[8375]36    private const string PointParameterName = "Point";
[8396]37    private const string StateParameterName = "State";
[8371]38    private const string IterationsParameterName = "Iterations";
[8396]39    private const string ApproximateGradientsParameterName = "ApproximateGradients";
[9444]40    private const string GradientCheckStepSizeParameterName = "GradientCheckStepSize";
[8371]41
42    #region Parameter Properties
43    // in
44    public ILookupParameter<IntValue> IterationsParameter {
45      get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
46    }
[8396]47    public ILookupParameter<RealVector> PointParameter {
48      get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
49    }
[8371]50    // out
[8396]51    public ILookupParameter<LbfgsState> StateParameter {
52      get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
[8371]53    }
[8396]54    public ILookupParameter<BoolValue> ApproximateGradientsParameter {
55      get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
[8371]56    }
[9444]57    public ILookupParameter<DoubleValue> GradientStepSizeParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters[GradientCheckStepSizeParameterName]; }
59    }
[8371]60
61
62    #endregion
63
64    #region Properties
[8396]65    private RealVector Point { get { return PointParameter.ActualValue; } }
[8375]66    private IntValue Iterations { get { return IterationsParameter.ActualValue; } }
[8396]67    private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
[8371]68    #endregion
69
70    [StorableConstructor]
[8396]71    private LbfgsInitializer(bool deserializing) : base(deserializing) { }
72    private LbfgsInitializer(LbfgsInitializer original, Cloner cloner) : base(original, cloner) { }
73    public LbfgsInitializer()
[8371]74      : base() {
75      // in
[8396]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."));
[9444]80      Parameters.Add(new LookupParameter<DoubleValue>(GradientCheckStepSizeParameterName, "Step size for the gradient check (should be used for debugging the gradient calculation only)."));
[8371]81      // out
[8396]82      Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
[8371]83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
[8396]86      return new LbfgsInitializer(this, cloner);
[8371]87    }
88
89    public override IOperation Apply() {
[8396]90      double[] initialPoint = Point.ToArray();
91      int n = initialPoint.Length;
[8371]92      alglib.minlbfgs.minlbfgsstate state = new alglib.minlbfgs.minlbfgsstate();
[8396]93      if (ApproximateGradients.Value) {
[8473]94        alglib.minlbfgs.minlbfgscreatef(n, Math.Min(n, 10), initialPoint, 1E-5, state);
[8396]95      } else {
[8473]96        alglib.minlbfgs.minlbfgscreate(n, Math.Min(n, 10), initialPoint, state);
[8396]97      }
[8473]98      alglib.minlbfgs.minlbfgssetcond(state, 0.0, 0, 0, Iterations.Value);
[8375]99      alglib.minlbfgs.minlbfgssetxrep(state, true);
[9444]100      if (GradientStepSizeParameter.ActualValue != null && GradientStepSizeParameter.ActualValue.Value > 0)
101        alglib.minlbfgs.minlbfgssetgradientcheck(state, GradientStepSizeParameter.ActualValue.Value);
[8371]102
[8396]103      PointParameter.ActualValue = new RealVector(initialPoint);
104      StateParameter.ActualValue = new LbfgsState(state);
[8371]105      return base.Apply();
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.