Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsUpdateResults.cs @ 14763

Last change on this file since 14763 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.4 KB
RevLine 
[8371]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8371]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;
[8396]26using HeuristicLab.Encodings.RealVectorEncoding;
[8371]27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
[8401]31namespace HeuristicLab.Algorithms.GradientDescent {
[8371]32  [StorableClass]
[8396]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 {
[8375]35    private const string QualityGradientsParameterName = "QualityGradients";
36    private const string QualityParameterName = "Quality";
[8396]37    private const string StateParameterName = "State";
38    private const string ApproximateGradientsParameterName = "ApproximateGradients";
[12794]39    private const string MaximizationParameterName = "Maximization";
[8371]40
41    #region Parameter Properties
[8396]42    public ILookupParameter<BoolValue> ApproximateGradientsParameter {
43      get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
[8371]44    }
[8396]45    public ILookupParameter<RealVector> QualityGradientsParameter {
46      get { return (ILookupParameter<RealVector>)Parameters[QualityGradientsParameterName]; }
47    }
[8375]48    public ILookupParameter<DoubleValue> QualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
[8371]50    }
[8396]51    public ILookupParameter<LbfgsState> StateParameter {
52      get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
[8371]53    }
[12794]54    public ILookupParameter<BoolValue> MaximizationParameter {
55      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
56    }
[8371]57    #endregion
58
59    #region Properties
[8396]60    private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
61    private RealVector QualityGradients { get { return QualityGradientsParameter.ActualValue; } }
[8375]62    private DoubleValue Quality { get { return QualityParameter.ActualValue; } }
[8396]63    private LbfgsState State { get { return StateParameter.ActualValue; } }
[12797]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
[8371]76    #endregion
77
78    [StorableConstructor]
[8396]79    private LbfgsUpdateResults(bool deserializing) : base(deserializing) { }
80    private LbfgsUpdateResults(LbfgsUpdateResults original, Cloner cloner) : base(original, cloner) { }
81    public LbfgsUpdateResults()
[8371]82      : base() {
83      // in
[8396]84      Parameters.Add(new LookupParameter<RealVector>(QualityGradientsParameterName, "The gradients at the evaluated point of the function to optimize."));
[8375]85      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The value at the evaluated point of the function to optimize."));
[8396]86      Parameters.Add(new LookupParameter<BoolValue>(ApproximateGradientsParameterName,
87                                                    "Flag that indicates if gradients should be approximated."));
[12794]88      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Flag that indicates if we solve a maximization problem."));
[8371]89      // in & out
[8396]90      Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
[8371]91    }
92
[12794]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
[8371]105    public override IDeepCloneable Clone(Cloner cloner) {
[8396]106      return new LbfgsUpdateResults(this, cloner);
[8371]107    }
108
109    public override IOperation Apply() {
[8396]110      var state = State;
[12794]111      var sign = Maximization.Value ? -1.0 : 1.0;
112      var f = sign * Quality.Value;
[8371]113      state.State.f = f;
[8396]114      if (!ApproximateGradients.Value) {
[12794]115        var g = QualityGradients.Select(gi => sign * gi).ToArray();
[8396]116        state.State.g = g;
117      }
[8371]118      return base.Apply();
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.