Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeMultiObjectiveProblem.cs

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

#2521: worked on refactoring

  • add results to problem base classes
  • fix external evaluation problem
  • Add result descriptions
File size: 5.7 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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.Parameters;
33
34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
35  [StorableType("f4819c68-b6fc-469f-bcb5-cb5b2a9d8aff")]
36  public abstract class SymbolicExpressionTreeMultiObjectiveProblem : MultiObjectiveProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
37    [Storable] private ReferenceParameter<IntValue> TreeLengthRefParameter { get; set; }
38    [Storable] private ReferenceParameter<IntValue> TreeDepthRefParameter { get; set; }
39    [Storable] private ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; set; }
40    [Storable] public IResult<ParetoFrontScatterPlot<ISymbolicExpressionTree>> BestParetoFrontResult { get; private set; }
41
42    public int TreeLength {
43      get => TreeLengthRefParameter.Value.Value;
44      set => TreeLengthRefParameter.Value.Value = value;
45    }
46
47    public int TreeDepth {
48      get => TreeDepthRefParameter.Value.Value;
49      set => TreeDepthRefParameter.Value.Value = value;
50    }
51
52    public ISymbolicExpressionGrammar Grammar {
53      get => GrammarRefParameter.Value;
54      set => GrammarRefParameter.Value = value;
55    }
56
57    protected ParetoFrontScatterPlot<ISymbolicExpressionTree> BestParetoFront {
58      get => BestParetoFrontResult.Value;
59      set => BestParetoFrontResult.Value = value;
60    }
61
62    [StorableConstructor]
63    protected SymbolicExpressionTreeMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66      RegisterEventHandlers();
67    }
68
69    protected SymbolicExpressionTreeMultiObjectiveProblem(SymbolicExpressionTreeMultiObjectiveProblem original, Cloner cloner)
70      : base(original, cloner) {
71      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
72      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
73      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
74      BestParetoFrontResult = cloner.Clone(original.BestParetoFrontResult);
75      RegisterEventHandlers();
76    }
77
78    protected SymbolicExpressionTreeMultiObjectiveProblem(SymbolicExpressionTreeEncoding encoding)
79      : base(encoding) {
80      EncodingParameter.ReadOnly = true;
81      EvaluatorParameter.ReadOnly = true;
82      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
83      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
84      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
85      Results.Add(BestParetoFrontResult = new Result<ParetoFrontScatterPlot<ISymbolicExpressionTree>>("Best Pareto Front", "The best Pareto front found so far."));
86
87      Parameterize();
88      RegisterEventHandlers();
89    }
90
91    public override void Analyze(ISymbolicExpressionTree[] trees, double[][] qualities, ResultCollection results,
92      IRandom random) {
93      base.Analyze(trees, qualities, results, random);
94
95      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(trees, qualities, Maximization);
96      var plot = new ParetoFrontScatterPlot<ISymbolicExpressionTree>(fronts, trees, qualities, Objectives, BestKnownFront);
97
98      BestParetoFront = plot;
99    }
100
101    protected override sealed void OnEvaluatorChanged() {
102      throw new InvalidOperationException("Evaluator may not change!");
103    }
104
105    protected override sealed void OnEncodingChanged() {
106      throw new InvalidOperationException("Encoding may not change!");
107    }
108
109    protected override void ParameterizeOperators() {
110      base.ParameterizeOperators();
111      Parameterize();
112    }
113
114    private void Parameterize() {
115      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
116        similarityCalculator.SolutionVariableName = Encoding.Name;
117        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
118      }
119    }
120
121    private void RegisterEventHandlers() {
122      IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
123      IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
124      ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
125    }
126
127    protected virtual void TreeLengthOnChanged() { }
128    protected virtual void TreeDepthOnChanged() { }
129    protected virtual void GrammarOnChanged() { }
130  }
131}
Note: See TracBrowser for help on using the repository browser.