1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.GradientDescent {
|
---|
31 | [StorableType("FDB51DFB-9668-448C-B75E-19BA1F61DB0D")]
|
---|
32 | [Item(Name = "LBFGS MakeStep", Description = "Makes a step in the LM-BFGS optimization algorithm.")]
|
---|
33 | public sealed class LbfgsMakeStep : SingleSuccessorOperator {
|
---|
34 | private const string TerminationCriterionParameterName = "TerminationCriterion";
|
---|
35 | private const string PointParameterName = "Point";
|
---|
36 | private const string StateParameterName = "State";
|
---|
37 |
|
---|
38 | #region Parameter Properties
|
---|
39 | public ILookupParameter<LbfgsState> StateParameter {
|
---|
40 | get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
|
---|
41 | }
|
---|
42 | public ILookupParameter<BoolValue> TerminationCriterionParameter {
|
---|
43 | get { return (ILookupParameter<BoolValue>)Parameters[TerminationCriterionParameterName]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<RealVector> PointParameter {
|
---|
46 | get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | private LbfgsState State { get { return StateParameter.ActualValue; } }
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | [StorableConstructor]
|
---|
56 | private LbfgsMakeStep(bool deserializing) : base(deserializing) { }
|
---|
57 | private LbfgsMakeStep(LbfgsMakeStep original, Cloner cloner) : base(original, cloner) { }
|
---|
58 | public LbfgsMakeStep()
|
---|
59 | : base() {
|
---|
60 | // in & out
|
---|
61 | Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
|
---|
62 | // out
|
---|
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."));
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
68 | return new LbfgsMakeStep(this, cloner);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IOperation Apply() {
|
---|
72 | var state = State;
|
---|
73 | bool @continue = alglib.minlbfgs.minlbfgsiteration(state.State);
|
---|
74 | TerminationCriterionParameter.ActualValue = new BoolValue(!@continue);
|
---|
75 | if (@continue) {
|
---|
76 | PointParameter.ActualValue = new RealVector(state.State.x);
|
---|
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);
|
---|
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 | }
|
---|
89 | PointParameter.ActualValue = new RealVector(x);
|
---|
90 | }
|
---|
91 | return base.Apply();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|