[8371] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[8396] | 25 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[8371] | 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[8371] | 29 |
|
---|
[8401] | 30 | namespace HeuristicLab.Algorithms.GradientDescent {
|
---|
[16565] | 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]
|
---|
[16565] | 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;
|
---|
[17931] | 73 | bool @continue = alglib.minlbfgsiteration(state.State);
|
---|
[8375] | 74 | TerminationCriterionParameter.ActualValue = new BoolValue(!@continue);
|
---|
| 75 | if (@continue) {
|
---|
[8396] | 76 | PointParameter.ActualValue = new RealVector(state.State.x);
|
---|
[8371] | 77 | } else {
|
---|
[17931] | 78 | alglib.minlbfgsresults(state.State, out var x, out var rep);
|
---|
[9406] | 79 | if (rep.terminationtype < 0) {
|
---|
| 80 | if (rep.terminationtype == -1)
|
---|
| 81 | throw new OperatorExecutionException(this, "Incorrect parameters were specified.");
|
---|
| 82 | else if (rep.terminationtype == -2)
|
---|
| 83 | throw new OperatorExecutionException(this, "Rounding errors prevent further improvement.");
|
---|
| 84 | else if (rep.terminationtype == -7)
|
---|
| 85 | throw new OperatorExecutionException(this, "Gradient verification failed.");
|
---|
| 86 | }
|
---|
[8396] | 87 | PointParameter.ActualValue = new RealVector(x);
|
---|
[8371] | 88 | }
|
---|
| 89 | return base.Apply();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|