1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
34 | [StorableClass]
|
---|
35 | [Item(Name = "LBFGS MakeStep", Description = "Makes a step in the LM-BFGS optimization algorithm.")]
|
---|
36 | public sealed class LbfgsMakeStep : SingleSuccessorOperator {
|
---|
37 | private const string TerminationCriterionParameterName = "TerminationCriterion";
|
---|
38 | private const string PointParameterName = "Point";
|
---|
39 | private const string StateParameterName = "State";
|
---|
40 |
|
---|
41 | #region Parameter Properties
|
---|
42 | public ILookupParameter<LbfgsState> StateParameter {
|
---|
43 | get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<BoolValue> TerminationCriterionParameter {
|
---|
46 | get { return (ILookupParameter<BoolValue>)Parameters[TerminationCriterionParameterName]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<RealVector> PointParameter {
|
---|
49 | get { return (ILookupParameter<RealVector>)Parameters[PointParameterName]; }
|
---|
50 | }
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 |
|
---|
54 | #region Properties
|
---|
55 | private LbfgsState State { get { return StateParameter.ActualValue; } }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | private LbfgsMakeStep(bool deserializing) : base(deserializing) { }
|
---|
60 | private LbfgsMakeStep(LbfgsMakeStep original, Cloner cloner) : base(original, cloner) { }
|
---|
61 | public LbfgsMakeStep()
|
---|
62 | : base() {
|
---|
63 | // in & out
|
---|
64 | Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
|
---|
65 | // out
|
---|
66 | Parameters.Add(new LookupParameter<BoolValue>(TerminationCriterionParameterName, "The termination criterion indicating that the LM-BFGS optimization algorithm should stop."));
|
---|
67 | Parameters.Add(new LookupParameter<RealVector>(PointParameterName, "The next point that should be evaluated in the LM-BFGS algorithm."));
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new LbfgsMakeStep(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IOperation Apply() {
|
---|
75 | var state = State;
|
---|
76 | bool @continue = alglib.minlbfgs.minlbfgsiteration(state.State);
|
---|
77 | TerminationCriterionParameter.ActualValue = new BoolValue(!@continue);
|
---|
78 | if (@continue) {
|
---|
79 | PointParameter.ActualValue = new RealVector(state.State.x);
|
---|
80 | } else {
|
---|
81 | double[] x = new double[state.State.x.Length];
|
---|
82 | alglib.minlbfgs.minlbfgsreport rep = new alglib.minlbfgs.minlbfgsreport();
|
---|
83 | alglib.minlbfgs.minlbfgsresults(state.State, ref x, rep);
|
---|
84 | PointParameter.ActualValue = new RealVector(x);
|
---|
85 | }
|
---|
86 | return base.Apply();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|