1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 |
|
---|
40 | #region Parameter Properties
|
---|
41 | public ILookupParameter<BoolValue> ApproximateGradientsParameter {
|
---|
42 | get { return (ILookupParameter<BoolValue>)Parameters[ApproximateGradientsParameterName]; }
|
---|
43 | }
|
---|
44 | public ILookupParameter<RealVector> QualityGradientsParameter {
|
---|
45 | get { return (ILookupParameter<RealVector>)Parameters[QualityGradientsParameterName]; }
|
---|
46 | }
|
---|
47 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
48 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
49 | }
|
---|
50 | public ILookupParameter<LbfgsState> StateParameter {
|
---|
51 | get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
|
---|
52 | }
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | #region Properties
|
---|
56 | private BoolValue ApproximateGradients { get { return ApproximateGradientsParameter.ActualValue; } }
|
---|
57 | private RealVector QualityGradients { get { return QualityGradientsParameter.ActualValue; } }
|
---|
58 | private DoubleValue Quality { get { return QualityParameter.ActualValue; } }
|
---|
59 | private LbfgsState State { get { return StateParameter.ActualValue; } }
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | [StorableConstructor]
|
---|
63 | private LbfgsUpdateResults(bool deserializing) : base(deserializing) { }
|
---|
64 | private LbfgsUpdateResults(LbfgsUpdateResults original, Cloner cloner) : base(original, cloner) { }
|
---|
65 | public LbfgsUpdateResults()
|
---|
66 | : base() {
|
---|
67 | // in
|
---|
68 | Parameters.Add(new LookupParameter<RealVector>(QualityGradientsParameterName, "The gradients at the evaluated point of the function to optimize."));
|
---|
69 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The value at the evaluated point of the function to optimize."));
|
---|
70 | Parameters.Add(new LookupParameter<BoolValue>(ApproximateGradientsParameterName,
|
---|
71 | "Flag that indicates if gradients should be approximated."));
|
---|
72 | // in & out
|
---|
73 | Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new LbfgsUpdateResults(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override IOperation Apply() {
|
---|
81 | var state = State;
|
---|
82 | var f = Quality.Value;
|
---|
83 | state.State.f = f;
|
---|
84 | if (!ApproximateGradients.Value) {
|
---|
85 | var g = QualityGradients.ToArray();
|
---|
86 | state.State.g = g;
|
---|
87 | }
|
---|
88 | return base.Apply();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|