Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsUpdateResults.cs @ 16375

Last change on this file since 16375 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.GradientDescent {
32  [StorableClass]
33  [Item(Name = "LBFGS UpdateResults", Description = "Sets the results (function value and gradients) for the next optimization step in the LM-BFGS algorithm.")]
34  public sealed class LbfgsUpdateResults : SingleSuccessorOperator {
35    private const string QualityGradientsParameterName = "QualityGradients";
36    private const string QualityParameterName = "Quality";
37    private const string StateParameterName = "State";
38    private const string ApproximateGradientsParameterName = "ApproximateGradients";
39    private const string MaximizationParameterName = "Maximization";
40
41    #region Parameter Properties
42    public ILookupParameter<BoolValue> ApproximateGradientsParameter {
43      get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
44    }
45    public ILookupParameter<RealVector> QualityGradientsParameter {
46      get { return (ILookupParameter<RealVector>)Parameters[QualityGradientsParameterName]; }
47    }
48    public ILookupParameter<DoubleValue> QualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
50    }
51    public ILookupParameter<LbfgsState> StateParameter {
52      get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
53    }
54    public ILookupParameter<BoolValue> MaximizationParameter {
55      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
56    }
57    #endregion
58
59    #region Properties
60    private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
61    private RealVector QualityGradients { get { return QualityGradientsParameter.ActualValue; } }
62    private DoubleValue Quality { get { return QualityParameter.ActualValue; } }
63    private LbfgsState State { get { return StateParameter.ActualValue; } }
64
65    private BoolValue Maximization {
66      get {
67        // BackwardsCompatibility3.3
68        #region Backwards compatible code, remove with 3.4
69        // the parameter is new, previously we assumed minimization problems
70        if (MaximizationParameter.ActualValue == null) return new BoolValue(false);
71        #endregion
72        return MaximizationParameter.ActualValue;
73      }
74    }
75
76    #endregion
77
78    [StorableConstructor]
79    private LbfgsUpdateResults(bool deserializing) : base(deserializing) { }
80    private LbfgsUpdateResults(LbfgsUpdateResults original, Cloner cloner) : base(original, cloner) { }
81    public LbfgsUpdateResults()
82      : base() {
83      // in
84      Parameters.Add(new LookupParameter<RealVector>(QualityGradientsParameterName, "The gradients at the evaluated point of the function to optimize."));
85      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The value at the evaluated point of the function to optimize."));
86      Parameters.Add(new LookupParameter<BoolValue>(ApproximateGradientsParameterName,
87                                                    "Flag that indicates if gradients should be approximated."));
88      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Flag that indicates if we solve a maximization problem."));
89      // in & out
90      Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
91    }
92
93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
95      // BackwardsCompatibility3.3
96
97      #region Backwards compatible code, remove with 3.4
98      if (!Parameters.ContainsKey(MaximizationParameterName)) {
99        // previous behaviour defaulted to minimization
100        Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Flag that indicates if we solve a maximization problem."));
101      }
102      #endregion
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new LbfgsUpdateResults(this, cloner);
107    }
108
109    public override IOperation Apply() {
110      var state = State;
111      var sign = Maximization.Value ? -1.0 : 1.0;
112      var f = sign * Quality.Value;
113      state.State.f = f;
114      if (!ApproximateGradients.Value) {
115        var g = QualityGradients.Select(gi => sign * gi).ToArray();
116        state.State.g = g;
117      }
118      return base.Apply();
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.