Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs @ 17747

Last change on this file since 17747 was 17747, checked in by abeham, 4 years ago

#2521: worked on refactoring

  • add results to problem base classes
  • fix external evaluation problem
  • Add result descriptions
File size: 6.1 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 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
22#endregion
23
24using System;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Parameters;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  [StorableType("A1B9F4C8-5E29-493C-A483-2AC68453BC63")]
37  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
38    [Storable] protected ReferenceParameter<IntValue> TreeLengthRefParameter { get; private set; }
39    [Storable] protected ReferenceParameter<IntValue> TreeDepthRefParameter { get; private set; }
40    [Storable] protected ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; private set; }
41    [Storable] public IResult<ISingleObjectiveSolutionContext<ISymbolicExpressionTree>> BestSolutionResult { get; private set; }
42
43    public int TreeLength {
44      get => TreeLengthRefParameter.Value.Value;
45      set => TreeLengthRefParameter.Value.Value = value;
46    }
47
48    public int TreeDepth {
49      get => TreeDepthRefParameter.Value.Value;
50      set => TreeDepthRefParameter.Value.Value = value;
51    }
52
53    public ISymbolicExpressionGrammar Grammar {
54      get => GrammarRefParameter.Value;
55      set => GrammarRefParameter.Value = value;
56    }
57
58    protected ISingleObjectiveSolutionContext<ISymbolicExpressionTree> BestSolution {
59      get => BestSolutionResult.Value;
60      set => BestSolutionResult.Value = value;
61    }
62
63    // persistence
64    [StorableConstructor]
65    protected SymbolicExpressionTreeProblem(StorableConstructorFlag _) : base(_) { }
66    [StorableHook(HookType.AfterDeserialization)]
67    private void AfterDeserialization() {
68      RegisterEventHandlers();
69    }
70
71    // cloning
72    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
73      : base(original, cloner) {
74      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
75      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
76      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
77      BestSolutionResult = cloner.Clone(original.BestSolutionResult);
78      RegisterEventHandlers();
79    }
80
81    protected SymbolicExpressionTreeProblem() : this(new SymbolicExpressionTreeEncoding()) { }
82    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeEncoding encoding)
83      : base(encoding) {
84      EncodingParameter.ReadOnly = true;
85      EvaluatorParameter.ReadOnly = true;
86      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
87      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
88      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
89      Results.Add(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<ISymbolicExpressionTree>>("Best Solution", "The best solution found so far."));
90
91      // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
92      Operators.Add(new QualitySimilarityCalculator());
93      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
94
95      Parameterize();
96      RegisterEventHandlers();
97    }
98
99    public override void Analyze(ISingleObjectiveSolutionContext<ISymbolicExpressionTree>[] solutionContexts, IRandom random) {
100      base.Analyze(solutionContexts, random);
101      var best = GetBest(solutionContexts);
102      if (BestSolution == null || IsBetter(best, BestSolution))
103        BestSolution = best.Clone() as SingleObjectiveSolutionContext<ISymbolicExpressionTree>;
104    }
105
106    protected sealed override void OnEvaluatorChanged() {
107      throw new InvalidOperationException("Evaluator may not change!");
108    }
109
110    protected sealed override void OnEncodingChanged() {
111      throw new InvalidOperationException("Encoding may not change!");
112    }
113
114    protected override void ParameterizeOperators() {
115      base.ParameterizeOperators();
116      Parameterize();
117    }
118
119    private void Parameterize() {
120      // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
121      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
122        similarityCalculator.SolutionVariableName = Encoding.Name;
123        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
124      }
125    }
126    private void RegisterEventHandlers() {
127      IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
128      IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
129      ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
130    }
131
132    protected virtual void TreeLengthOnChanged() { }
133    protected virtual void TreeDepthOnChanged() { }
134    protected virtual void GrammarOnChanged() { }
135  }
136}
Note: See TracBrowser for help on using the repository browser.