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