source: trunk/sources/HeuristicLab.Algorithms.GradientDescent/3.3/Lbfgs.cs @ 8401

Last change on this file since 8401 was 8401, checked in by gkronber, 9 years ago

#1423 moved LM-BFGS implementation from data-analysis into the gradient descent algorithm plugin.

File size: 7.3 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.TestFunctions;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Algorithms.GradientDescent {
35  /// <summary>
36  /// Limited-Memory BFGS optimization algorithm.
37  /// </summary>
38  [Item("LM-BFGS", "The limited-memory BFGS (Broyden–Fletcher–Goldfarb–Shanno) optimization algorithm.")]
39  [Creatable("Algorithms")]
40  [StorableClass]
41  public sealed class LbfgsAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
42    public override Type ProblemType {
43      get { return typeof(SingleObjectiveTestFunctionProblem); }
44    }
45
46    public new SingleObjectiveTestFunctionProblem Problem {
47      get { return (SingleObjectiveTestFunctionProblem)base.Problem; }
48      set { base.Problem = value; }
49    }
50
51    public string Filename { get; set; }
52
53    private const string MaxIterationsParameterName = "MaxIterations";
54    private const string ApproximateGradientsParameterName = "ApproximateGradients";
55    private const string SeedParameterName = "Seed";
56    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
57
58    #region parameter properties
59    public IValueParameter<IntValue> MaxIterationsParameter {
60      get { return (IValueParameter<IntValue>)Parameters[MaxIterationsParameterName]; }
61    }
62    public IValueParameter<IntValue> SeedParameter {
63      get { return (IValueParameter<IntValue>)Parameters[SeedParameterName]; }
64    }
65    public IValueParameter<BoolValue> SetSeedRandomlyParameter {
66      get { return (IValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
67    }
68    #endregion
69    #region properties
70    public int MaxIterations {
71      set { MaxIterationsParameter.Value.Value = value; }
72      get { return MaxIterationsParameter.Value.Value; }
73    }
74    public int Seed { get { return SeedParameter.Value.Value; } set { SeedParameter.Value.Value = value; } }
75    public bool SetSeedRandomly { get { return SetSeedRandomlyParameter.Value.Value; } set { SetSeedRandomlyParameter.Value.Value = value; } }
76    #endregion
77
78    [StorableConstructor]
79    private LbfgsAlgorithm(bool deserializing) : base(deserializing) { }
80    private LbfgsAlgorithm(LbfgsAlgorithm original, Cloner cloner)
81      : base(original, cloner) {
82    }
83    public LbfgsAlgorithm()
84      : base() {
85      this.name = ItemName;
86      this.description = ItemDescription;
87
88      Problem = new SingleObjectiveTestFunctionProblem();
89
90      Parameters.Add(new ValueParameter<IntValue>(MaxIterationsParameterName, "The maximal number of iterations for.", new IntValue(20)));
91      Parameters.Add(new ValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
92      Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
93      Parameters.Add(new ValueParameter<BoolValue>(ApproximateGradientsParameterName, "Indicates that gradients should be approximated.", new BoolValue(true)));
94      Parameters[ApproximateGradientsParameterName].Hidden = true; // should not be changed
95
96      var randomCreator = new RandomCreator();
97      var solutionCreator = new Placeholder();
98      var bfgsInitializer = new LbfgsInitializer();
99      var makeStep = new LbfgsMakeStep();
100      var branch = new ConditionalBranch();
101      var evaluator = new Placeholder();
102      var updateResults = new LbfgsUpdateResults();
103      var analyzer = new LbfgsAnalyzer();
104      var finalAnalyzer = new LbfgsAnalyzer();
105
106      OperatorGraph.InitialOperator = randomCreator;
107
108      randomCreator.SeedParameter.ActualName = SeedParameterName;
109      randomCreator.SeedParameter.Value = null;
110      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
111      randomCreator.SetSeedRandomlyParameter.Value = null;
112      randomCreator.Successor = solutionCreator;
113
114      solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
115      solutionCreator.Successor = bfgsInitializer;
116
117      bfgsInitializer.IterationsParameter.ActualName = MaxIterationsParameterName;
118      bfgsInitializer.PointParameter.ActualName = Problem.SolutionCreator.RealVectorParameter.ActualName;
119      bfgsInitializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
120      bfgsInitializer.Successor = makeStep;
121
122      makeStep.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
123      makeStep.PointParameter.ActualName = bfgsInitializer.PointParameter.ActualName;
124      makeStep.Successor = branch;
125
126      branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
127      branch.FalseBranch = evaluator;
128      branch.TrueBranch = finalAnalyzer;
129
130      evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
131      evaluator.Successor = updateResults;
132
133      updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
134      updateResults.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
135      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
136      updateResults.Successor = analyzer;
137
138      analyzer.QualityParameter.ActualName = updateResults.QualityParameter.ActualName;
139      analyzer.PointParameter.ActualName = makeStep.PointParameter.ActualName;
140      analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
141      analyzer.Successor = makeStep;
142
143      finalAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
144      finalAnalyzer.PointParameter.ActualName = makeStep.PointParameter.ActualName;
145      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
146      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
147      finalAnalyzer.QualitiesTableParameter.ActualName = analyzer.QualitiesTableParameter.ActualName;
148    }
149
150    [StorableHook(HookType.AfterDeserialization)]
151    private void AfterDeserialization() { }
152
153    public override IDeepCloneable Clone(Cloner cloner) {
154      return new LbfgsAlgorithm(this, cloner);
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.