[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[8396] | 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[8371] | 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
[8401] | 32 | namespace 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";
|
---|
[8371] | 40 |
|
---|
| 41 | #region Parameter Properties
|
---|
| 42 | // in
|
---|
| 43 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
| 44 | get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
| 45 | }
|
---|
[8396] | 46 | public ILookupParameter<RealVector> PointParameter {
|
---|
| 47 | get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
|
---|
| 48 | }
|
---|
[8371] | 49 | // out
|
---|
[8396] | 50 | public ILookupParameter<LbfgsState> StateParameter {
|
---|
| 51 | get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
|
---|
[8371] | 52 | }
|
---|
[8396] | 53 | public ILookupParameter<BoolValue> ApproximateGradientsParameter {
|
---|
| 54 | get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
|
---|
[8371] | 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region Properties
|
---|
[8396] | 61 | private RealVector Point { get { return PointParameter.ActualValue; } }
|
---|
[8375] | 62 | private IntValue Iterations { get { return IterationsParameter.ActualValue; } }
|
---|
[8396] | 63 | private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
|
---|
[8371] | 64 | #endregion
|
---|
| 65 |
|
---|
| 66 | [StorableConstructor]
|
---|
[8396] | 67 | private LbfgsInitializer(bool deserializing) : base(deserializing) { }
|
---|
| 68 | private LbfgsInitializer(LbfgsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 69 | public LbfgsInitializer()
|
---|
[8371] | 70 | : base() {
|
---|
| 71 | // in
|
---|
[8396] | 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."));
|
---|
[8371] | 76 | // out
|
---|
[8396] | 77 | Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
|
---|
[8371] | 78 | }
|
---|
| 79 |
|
---|
| 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8396] | 81 | return new LbfgsInitializer(this, cloner);
|
---|
[8371] | 82 | }
|
---|
| 83 |
|
---|
| 84 | public override IOperation Apply() {
|
---|
[8396] | 85 | double[] initialPoint = Point.ToArray();
|
---|
| 86 | int n = initialPoint.Length;
|
---|
[8371] | 87 | alglib.minlbfgs.minlbfgsstate state = new alglib.minlbfgs.minlbfgsstate();
|
---|
[8396] | 88 | if (ApproximateGradients.Value) {
|
---|
[8473] | 89 | alglib.minlbfgs.minlbfgscreatef(n, Math.Min(n, 10), initialPoint, 1E-5, state);
|
---|
[8396] | 90 | } else {
|
---|
[8473] | 91 | alglib.minlbfgs.minlbfgscreate(n, Math.Min(n, 10), initialPoint, state);
|
---|
[8396] | 92 | }
|
---|
[8473] | 93 | alglib.minlbfgs.minlbfgssetcond(state, 0.0, 0, 0, Iterations.Value);
|
---|
[8375] | 94 | alglib.minlbfgs.minlbfgssetxrep(state, true);
|
---|
[8371] | 95 |
|
---|
[8396] | 96 | PointParameter.ActualValue = new RealVector(initialPoint);
|
---|
| 97 | StateParameter.ActualValue = new LbfgsState(state);
|
---|
[8371] | 98 | return base.Apply();
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|