[8396] | 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 |
|
---|
| 23 | using System;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[9127] | 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[8396] | 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Random;
|
---|
| 33 |
|
---|
[8401] | 34 | namespace HeuristicLab.Algorithms.GradientDescent {
|
---|
[8396] | 35 | /// <summary>
|
---|
| 36 | /// Limited-Memory BFGS optimization algorithm.
|
---|
| 37 | /// </summary>
|
---|
| 38 | [Item("LM-BFGS", "The limited-memory BFGS (BroydenFletcherGoldfarbShanno) optimization algorithm.")]
|
---|
| 39 | [Creatable("Algorithms")]
|
---|
| 40 | [StorableClass]
|
---|
| 41 | public sealed class LbfgsAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
| 42 | public override Type ProblemType {
|
---|
[9127] | 43 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
[8396] | 44 | }
|
---|
| 45 |
|
---|
[9127] | 46 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 47 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[8396] | 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";
|
---|
[8397] | 55 | private const string SeedParameterName = "Seed";
|
---|
| 56 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
[8396] | 57 |
|
---|
| 58 | #region parameter properties
|
---|
| 59 | public IValueParameter<IntValue> MaxIterationsParameter {
|
---|
| 60 | get { return (IValueParameter<IntValue>)Parameters[MaxIterationsParameterName]; }
|
---|
| 61 | }
|
---|
[8397] | 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 | }
|
---|
[8396] | 68 | #endregion
|
---|
| 69 | #region properties
|
---|
| 70 | public int MaxIterations {
|
---|
| 71 | set { MaxIterationsParameter.Value.Value = value; }
|
---|
| 72 | get { return MaxIterationsParameter.Value.Value; }
|
---|
| 73 | }
|
---|
[8397] | 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; } }
|
---|
[8396] | 76 | #endregion
|
---|
[8397] | 77 |
|
---|
[9127] | 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 |
|
---|
[8396] | 93 | [StorableConstructor]
|
---|
| 94 | private LbfgsAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 95 | private LbfgsAlgorithm(LbfgsAlgorithm original, Cloner cloner)
|
---|
| 96 | : base(original, cloner) {
|
---|
[9127] | 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();
|
---|
[8396] | 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)));
|
---|
[8397] | 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)));
|
---|
[8396] | 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();
|
---|
[9127] | 118 | solutionCreator = new Placeholder();
|
---|
| 119 | initializer = new LbfgsInitializer();
|
---|
| 120 | makeStep = new LbfgsMakeStep();
|
---|
[8396] | 121 | var branch = new ConditionalBranch();
|
---|
[9127] | 122 | evaluator = new Placeholder();
|
---|
| 123 | updateResults = new LbfgsUpdateResults();
|
---|
| 124 | analyzer = new LbfgsAnalyzer();
|
---|
| 125 | finalAnalyzer = new LbfgsAnalyzer();
|
---|
[8396] | 126 |
|
---|
| 127 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 128 |
|
---|
[8397] | 129 | randomCreator.SeedParameter.ActualName = SeedParameterName;
|
---|
| 130 | randomCreator.SeedParameter.Value = null;
|
---|
| 131 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
|
---|
| 132 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
[8396] | 133 | randomCreator.Successor = solutionCreator;
|
---|
| 134 |
|
---|
[9127] | 135 | solutionCreator.Name = "Solution Creator (placeholder)";
|
---|
| 136 | solutionCreator.Successor = initializer;
|
---|
[8396] | 137 |
|
---|
[9127] | 138 | initializer.IterationsParameter.ActualName = MaxIterationsParameterName;
|
---|
| 139 | initializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
|
---|
| 140 | initializer.Successor = makeStep;
|
---|
[8396] | 141 |
|
---|
[9127] | 142 | makeStep.StateParameter.ActualName = initializer.StateParameter.Name;
|
---|
[8396] | 143 | makeStep.Successor = branch;
|
---|
| 144 |
|
---|
| 145 | branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
|
---|
| 146 | branch.FalseBranch = evaluator;
|
---|
| 147 | branch.TrueBranch = finalAnalyzer;
|
---|
| 148 |
|
---|
[9127] | 149 | evaluator.Name = "Evaluator (placeholder)";
|
---|
[8396] | 150 | evaluator.Successor = updateResults;
|
---|
| 151 |
|
---|
[9127] | 152 | updateResults.StateParameter.ActualName = initializer.StateParameter.Name;
|
---|
[8396] | 153 | updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
|
---|
| 154 | updateResults.Successor = analyzer;
|
---|
| 155 |
|
---|
[9127] | 156 | analyzer.StateParameter.ActualName = initializer.StateParameter.Name;
|
---|
[8396] | 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)]
|
---|
[9127] | 165 | private void AfterDeserialization() {
|
---|
| 166 | RegisterEvents();
|
---|
| 167 | }
|
---|
[8396] | 168 |
|
---|
| 169 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 170 | return new LbfgsAlgorithm(this, cloner);
|
---|
| 171 | }
|
---|
[9127] | 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 | }
|
---|
[8396] | 243 | }
|
---|
| 244 | }
|
---|