Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9376 was 9128, checked in by gkronber, 12 years ago

#1423 removed reference to test functions

File size: 10.1 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.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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(ISingleObjectiveHeuristicOptimizationProblem); }
44    }
45
46    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
47      get { return (ISingleObjectiveHeuristicOptimizationProblem)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    [Storable]
79    private LbfgsInitializer initializer;
80    [Storable]
81    private LbfgsMakeStep makeStep;
82    [Storable]
83    private LbfgsUpdateResults updateResults;
84    [Storable]
85    private LbfgsAnalyzer analyzer;
86    [Storable]
87    private LbfgsAnalyzer finalAnalyzer;
88    [Storable]
89    private Placeholder solutionCreator;
90    [Storable]
91    private Placeholder evaluator;
92
93    [StorableConstructor]
94    private LbfgsAlgorithm(bool deserializing) : base(deserializing) { }
95    private LbfgsAlgorithm(LbfgsAlgorithm original, Cloner cloner)
96      : base(original, cloner) {
97      initializer = cloner.Clone(original.initializer);
98      makeStep = cloner.Clone(original.makeStep);
99      updateResults = cloner.Clone(original.updateResults);
100      analyzer = cloner.Clone(original.analyzer);
101      finalAnalyzer = cloner.Clone(original.finalAnalyzer);
102      solutionCreator = cloner.Clone(original.solutionCreator);
103      evaluator = cloner.Clone(original.evaluator);
104      RegisterEvents();
105    }
106    public LbfgsAlgorithm()
107      : base() {
108      this.name = ItemName;
109      this.description = ItemDescription;
110
111      Parameters.Add(new ValueParameter<IntValue>(MaxIterationsParameterName, "The maximal number of iterations for.", new IntValue(20)));
112      Parameters.Add(new ValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
113      Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
114      Parameters.Add(new ValueParameter<BoolValue>(ApproximateGradientsParameterName, "Indicates that gradients should be approximated.", new BoolValue(true)));
115      Parameters[ApproximateGradientsParameterName].Hidden = true; // should not be changed
116
117      var randomCreator = new RandomCreator();
118      solutionCreator = new Placeholder();
119      initializer = new LbfgsInitializer();
120      makeStep = new LbfgsMakeStep();
121      var branch = new ConditionalBranch();
122      evaluator = new Placeholder();
123      updateResults = new LbfgsUpdateResults();
124      analyzer = new LbfgsAnalyzer();
125      finalAnalyzer = new LbfgsAnalyzer();
126
127      OperatorGraph.InitialOperator = randomCreator;
128
129      randomCreator.SeedParameter.ActualName = SeedParameterName;
130      randomCreator.SeedParameter.Value = null;
131      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
132      randomCreator.SetSeedRandomlyParameter.Value = null;
133      randomCreator.Successor = solutionCreator;
134
135      solutionCreator.Name = "Solution Creator (placeholder)";
136      solutionCreator.Successor = initializer;
137
138      initializer.IterationsParameter.ActualName = MaxIterationsParameterName;
139      initializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
140      initializer.Successor = makeStep;
141
142      makeStep.StateParameter.ActualName = initializer.StateParameter.Name;
143      makeStep.Successor = branch;
144
145      branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
146      branch.FalseBranch = evaluator;
147      branch.TrueBranch = finalAnalyzer;
148
149      evaluator.Name = "Evaluator (placeholder)";
150      evaluator.Successor = updateResults;
151
152      updateResults.StateParameter.ActualName = initializer.StateParameter.Name;
153      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
154      updateResults.Successor = analyzer;
155
156      analyzer.StateParameter.ActualName = initializer.StateParameter.Name;
157      analyzer.Successor = makeStep;
158
159      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
160      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
161      finalAnalyzer.QualitiesTableParameter.ActualName = analyzer.QualitiesTableParameter.ActualName;
162    }
163
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      RegisterEvents();
167    }
168
169    public override IDeepCloneable Clone(Cloner cloner) {
170      return new LbfgsAlgorithm(this, cloner);
171    }
172
173    #region events
174    private void RegisterEvents() {
175      if (Problem != null) {
176        RegisterSolutionCreatorEvents();
177        RegisterEvaluatorEvents();
178      }
179    }
180
181    protected override void OnProblemChanged() {
182      base.OnProblemChanged();
183      if (Problem != null) {
184        RegisterEvents();
185        solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
186        evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
187      }
188    }
189
190    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
191      base.Problem_SolutionCreatorChanged(sender, e);
192      RegisterSolutionCreatorEvents();
193      ParameterizeOperators();
194    }
195
196    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
197      base.Problem_EvaluatorChanged(sender, e);
198      RegisterEvaluatorEvents();
199      ParameterizeOperators();
200    }
201
202    private void RegisterSolutionCreatorEvents() {
203      var realVectorCreator = Problem.SolutionCreator as RealVectorCreator;
204      // ignore if we have a different kind of problem
205      if (realVectorCreator != null) {
206        realVectorCreator.RealVectorParameter.ActualNameChanged += (sender, args) => ParameterizeOperators();
207      }
208    }
209
210    private void RegisterEvaluatorEvents() {
211      Problem.Evaluator.QualityParameter.ActualNameChanged += (sender, args) => ParameterizeOperators();
212    }
213    #endregion
214
215    protected override void OnStarted() {
216      var realVectorCreator = Problem.SolutionCreator as RealVectorCreator;
217      // must catch the case that user loaded an unsupported problem
218      if (realVectorCreator == null)
219        throw new InvalidOperationException("LM-BFGS only works with problems using a real-value encoding.");
220      base.OnStarted();
221    }
222
223    public override void Prepare() {
224      if (Problem != null) base.Prepare();
225    }
226
227    private void ParameterizeOperators() {
228      var realVectorCreator = Problem.SolutionCreator as RealVectorCreator;
229      // ignore if we have a different kind of problem
230      if (realVectorCreator != null) {
231        var realVectorParameterName = realVectorCreator.RealVectorParameter.ActualName;
232        initializer.PointParameter.ActualName = realVectorParameterName;
233        makeStep.PointParameter.ActualName = realVectorParameterName;
234        analyzer.PointParameter.ActualName = realVectorParameterName;
235        finalAnalyzer.PointParameter.ActualName = realVectorParameterName;
236      }
237
238      var qualityParameterName = Problem.Evaluator.QualityParameter.ActualName;
239      updateResults.QualityParameter.ActualName = qualityParameterName;
240      analyzer.QualityParameter.ActualName = qualityParameterName;
241      finalAnalyzer.QualityParameter.ActualName = qualityParameterName;
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.