Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsMakeStep.cs @ 17328

Last change on this file since 17328 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.0 KB
RevLine 
[8371]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
[8396]25using HeuristicLab.Encodings.RealVectorEncoding;
[8371]26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
[17097]28using HEAL.Attic;
[8371]29
[8401]30namespace HeuristicLab.Algorithms.GradientDescent {
[17097]31  [StorableType("DC5D8418-FD1E-4D4C-AEF6-79F77AD8A5D9")]
[8396]32  [Item(Name = "LBFGS MakeStep", Description = "Makes a step in the LM-BFGS optimization algorithm.")]
33  public sealed class LbfgsMakeStep : SingleSuccessorOperator {
[8371]34    private const string TerminationCriterionParameterName = "TerminationCriterion";
[8375]35    private const string PointParameterName = "Point";
[8396]36    private const string StateParameterName = "State";
[8371]37
38    #region Parameter Properties
[8396]39    public ILookupParameter<LbfgsState> StateParameter {
40      get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
[8371]41    }
42    public ILookupParameter<BoolValue> TerminationCriterionParameter {
43      get { return (ILookupParameter<BoolValue>)Parameters[TerminationCriterionParameterName]; }
44    }
[8396]45    public ILookupParameter<RealVector> PointParameter {
46      get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
[8371]47    }
48    #endregion
49
50
51    #region Properties
[8396]52    private LbfgsState State { get { return StateParameter.ActualValue; } }
[8371]53    #endregion
54
55    [StorableConstructor]
[17097]56    private LbfgsMakeStep(StorableConstructorFlag _) : base(_) { }
[8396]57    private LbfgsMakeStep(LbfgsMakeStep original, Cloner cloner) : base(original, cloner) { }
58    public LbfgsMakeStep()
[8371]59      : base() {
60      // in & out
[8396]61      Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
[8371]62      // out
[8396]63      Parameters.Add(new LookupParameter<BoolValue>(TerminationCriterionParameterName, "The termination criterion indicating that the LM-BFGS optimization algorithm should stop."));
64      Parameters.Add(new LookupParameter<RealVector>(PointParameterName, "The next point that should be evaluated in the LM-BFGS algorithm."));
[8371]65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
[8396]68      return new LbfgsMakeStep(this, cloner);
[8371]69    }
70
71    public override IOperation Apply() {
[8396]72      var state = State;
[8375]73      bool @continue = alglib.minlbfgs.minlbfgsiteration(state.State);
74      TerminationCriterionParameter.ActualValue = new BoolValue(!@continue);
75      if (@continue) {
[8396]76        PointParameter.ActualValue = new RealVector(state.State.x);
[8371]77      } else {
78        double[] x = new double[state.State.x.Length];
79        alglib.minlbfgs.minlbfgsreport rep = new alglib.minlbfgs.minlbfgsreport();
80        alglib.minlbfgs.minlbfgsresults(state.State, ref x, rep);
[9406]81        if (rep.terminationtype < 0) {
82          if (rep.terminationtype == -1)
83            throw new OperatorExecutionException(this, "Incorrect parameters were specified.");
84          else if (rep.terminationtype == -2)
85            throw new OperatorExecutionException(this, "Rounding errors prevent further improvement.");
86          else if (rep.terminationtype == -7)
87            throw new OperatorExecutionException(this, "Gradient verification failed.");
88        }
[8396]89        PointParameter.ActualValue = new RealVector(x);
[8371]90      }
91      return base.Apply();
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.