Ignore:
Timestamp:
01/08/13 16:55:20 (9 years ago)
Author:
gkronber
Message:

#1423 changed LBFGS to take any single-objective optimization algorithm and throw an exception if the solution creator does not have the correct type in OnStarted().
Also added wiring for parameter names.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.GradientDescent/3.3/Lbfgs.cs

    r8401 r9127  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Encodings.RealVectorEncoding;
    2728using HeuristicLab.Operators;
    2829using HeuristicLab.Optimization;
     
    4142  public sealed class LbfgsAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
    4243    public override Type ProblemType {
    43       get { return typeof(SingleObjectiveTestFunctionProblem); }
    44     }
    45 
    46     public new SingleObjectiveTestFunctionProblem Problem {
    47       get { return (SingleObjectiveTestFunctionProblem)base.Problem; }
     44      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
     45    }
     46
     47    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
     48      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
    4849      set { base.Problem = value; }
    4950    }
     
    7677    #endregion
    7778
     79    [Storable]
     80    private LbfgsInitializer initializer;
     81    [Storable]
     82    private LbfgsMakeStep makeStep;
     83    [Storable]
     84    private LbfgsUpdateResults updateResults;
     85    [Storable]
     86    private LbfgsAnalyzer analyzer;
     87    [Storable]
     88    private LbfgsAnalyzer finalAnalyzer;
     89    [Storable]
     90    private Placeholder solutionCreator;
     91    [Storable]
     92    private Placeholder evaluator;
     93
    7894    [StorableConstructor]
    7995    private LbfgsAlgorithm(bool deserializing) : base(deserializing) { }
    8096    private LbfgsAlgorithm(LbfgsAlgorithm original, Cloner cloner)
    8197      : base(original, cloner) {
     98      initializer = cloner.Clone(original.initializer);
     99      makeStep = cloner.Clone(original.makeStep);
     100      updateResults = cloner.Clone(original.updateResults);
     101      analyzer = cloner.Clone(original.analyzer);
     102      finalAnalyzer = cloner.Clone(original.finalAnalyzer);
     103      solutionCreator = cloner.Clone(original.solutionCreator);
     104      evaluator = cloner.Clone(original.evaluator);
     105      RegisterEvents();
    82106    }
    83107    public LbfgsAlgorithm()
     
    85109      this.name = ItemName;
    86110      this.description = ItemDescription;
    87 
    88       Problem = new SingleObjectiveTestFunctionProblem();
    89111
    90112      Parameters.Add(new ValueParameter<IntValue>(MaxIterationsParameterName, "The maximal number of iterations for.", new IntValue(20)));
     
    95117
    96118      var randomCreator = new RandomCreator();
    97       var solutionCreator = new Placeholder();
    98       var bfgsInitializer = new LbfgsInitializer();
    99       var makeStep = new LbfgsMakeStep();
     119      solutionCreator = new Placeholder();
     120      initializer = new LbfgsInitializer();
     121      makeStep = new LbfgsMakeStep();
    100122      var branch = new ConditionalBranch();
    101       var evaluator = new Placeholder();
    102       var updateResults = new LbfgsUpdateResults();
    103       var analyzer = new LbfgsAnalyzer();
    104       var finalAnalyzer = new LbfgsAnalyzer();
     123      evaluator = new Placeholder();
     124      updateResults = new LbfgsUpdateResults();
     125      analyzer = new LbfgsAnalyzer();
     126      finalAnalyzer = new LbfgsAnalyzer();
    105127
    106128      OperatorGraph.InitialOperator = randomCreator;
     
    112134      randomCreator.Successor = solutionCreator;
    113135
    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;
     136      solutionCreator.Name = "Solution Creator (placeholder)";
     137      solutionCreator.Successor = initializer;
     138
     139      initializer.IterationsParameter.ActualName = MaxIterationsParameterName;
     140      initializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
     141      initializer.Successor = makeStep;
     142
     143      makeStep.StateParameter.ActualName = initializer.StateParameter.Name;
    124144      makeStep.Successor = branch;
    125145
     
    128148      branch.TrueBranch = finalAnalyzer;
    129149
    130       evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     150      evaluator.Name = "Evaluator (placeholder)";
    131151      evaluator.Successor = updateResults;
    132152
    133       updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
    134       updateResults.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
     153      updateResults.StateParameter.ActualName = initializer.StateParameter.Name;
    135154      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
    136155      updateResults.Successor = analyzer;
    137156
    138       analyzer.QualityParameter.ActualName = updateResults.QualityParameter.ActualName;
    139       analyzer.PointParameter.ActualName = makeStep.PointParameter.ActualName;
    140       analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
     157      analyzer.StateParameter.ActualName = initializer.StateParameter.Name;
    141158      analyzer.Successor = makeStep;
    142159
    143       finalAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
    144       finalAnalyzer.PointParameter.ActualName = makeStep.PointParameter.ActualName;
    145160      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
    146161      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
     
    149164
    150165    [StorableHook(HookType.AfterDeserialization)]
    151     private void AfterDeserialization() { }
     166    private void AfterDeserialization() {
     167      RegisterEvents();
     168    }
    152169
    153170    public override IDeepCloneable Clone(Cloner cloner) {
    154171      return new LbfgsAlgorithm(this, cloner);
    155172    }
     173
     174    #region events
     175    private void RegisterEvents() {
     176      if (Problem != null) {
     177        RegisterSolutionCreatorEvents();
     178        RegisterEvaluatorEvents();
     179      }
     180    }
     181
     182    protected override void OnProblemChanged() {
     183      base.OnProblemChanged();
     184      if (Problem != null) {
     185        RegisterEvents();
     186        solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
     187        evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     188      }
     189    }
     190
     191    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
     192      base.Problem_SolutionCreatorChanged(sender, e);
     193      RegisterSolutionCreatorEvents();
     194      ParameterizeOperators();
     195    }
     196
     197    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
     198      base.Problem_EvaluatorChanged(sender, e);
     199      RegisterEvaluatorEvents();
     200      ParameterizeOperators();
     201    }
     202
     203    private void RegisterSolutionCreatorEvents() {
     204      var realVectorCreator = Problem.SolutionCreator as RealVectorCreator;
     205      // ignore if we have a different kind of problem
     206      if (realVectorCreator != null) {
     207        realVectorCreator.RealVectorParameter.ActualNameChanged += (sender, args) => ParameterizeOperators();
     208      }
     209    }
     210
     211    private void RegisterEvaluatorEvents() {
     212      Problem.Evaluator.QualityParameter.ActualNameChanged += (sender, args) => ParameterizeOperators();
     213    }
     214    #endregion
     215
     216    protected override void OnStarted() {
     217      var realVectorCreator = Problem.SolutionCreator as RealVectorCreator;
     218      // must catch the case that user loaded an unsupported problem
     219      if (realVectorCreator == null)
     220        throw new InvalidOperationException("LM-BFGS only works with problems using a real-value encoding.");
     221      base.OnStarted();
     222    }
     223
     224    public override void Prepare() {
     225      if (Problem != null) base.Prepare();
     226    }
     227
     228    private void ParameterizeOperators() {
     229      var realVectorCreator = Problem.SolutionCreator as RealVectorCreator;
     230      // ignore if we have a different kind of problem
     231      if (realVectorCreator != null) {
     232        var realVectorParameterName = realVectorCreator.RealVectorParameter.ActualName;
     233        initializer.PointParameter.ActualName = realVectorParameterName;
     234        makeStep.PointParameter.ActualName = realVectorParameterName;
     235        analyzer.PointParameter.ActualName = realVectorParameterName;
     236        finalAnalyzer.PointParameter.ActualName = realVectorParameterName;
     237      }
     238
     239      var qualityParameterName = Problem.Evaluator.QualityParameter.ActualName;
     240      updateResults.QualityParameter.ActualName = qualityParameterName;
     241      analyzer.QualityParameter.ActualName = qualityParameterName;
     242      finalAnalyzer.QualityParameter.ActualName = qualityParameterName;
     243    }
    156244  }
    157245}
Note: See TracChangeset for help on using the changeset viewer.