Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: work in progress (removed solution creator parameter from encoding), OrienteeringProblem and test functions are broken

File size: 4.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.Linq;
25using HEAL.Attic;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
34  [StorableType("f4819c68-b6fc-469f-bcb5-cb5b2a9d8aff")]
35  public abstract class SymbolicExpressionTreeMultiObjectiveProblem : MultiObjectiveProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
36    [Storable] private ReferenceParameter<IntValue> TreeLengthRefParameter { get; set; }
37    [Storable] private ReferenceParameter<IntValue> TreeDepthRefParameter { get; set; }
38    [Storable] private ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; set; }
39
40    public int TreeLength {
41      get => TreeLengthRefParameter.Value.Value;
42      set => TreeLengthRefParameter.Value.Value = value;
43    }
44
45    public int TreeDepth {
46      get => TreeDepthRefParameter.Value.Value;
47      set => TreeDepthRefParameter.Value.Value = value;
48    }
49
50    public ISymbolicExpressionGrammar Grammar {
51      get => GrammarRefParameter.Value;
52      set => GrammarRefParameter.Value = value;
53    }
54
55    // persistence
56    [StorableConstructor]
57    protected SymbolicExpressionTreeMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      RegisterEventHandlers();
61    }
62
63
64    // cloning
65    protected SymbolicExpressionTreeMultiObjectiveProblem(SymbolicExpressionTreeMultiObjectiveProblem original, Cloner cloner)
66      : base(original, cloner) {
67      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
68      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
69      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
70      RegisterEventHandlers();
71    }
72
73    protected SymbolicExpressionTreeMultiObjectiveProblem(SymbolicExpressionTreeEncoding encoding)
74      : base(encoding) {
75      EncodingParameter.ReadOnly = true;
76      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
77      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
78      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
79
80      Parameterize();
81      RegisterEventHandlers();
82    }
83
84    public override void Analyze(ISymbolicExpressionTree[] trees, double[][] qualities, ResultCollection results,
85      IRandom random) {
86      base.Analyze(trees, qualities, results, random);
87
88      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(trees, qualities, Maximization);
89      var plot = new ParetoFrontScatterPlot<ISymbolicExpressionTree>(fronts, trees, qualities, Objectives, BestKnownFront);
90      results.AddOrUpdateResult("Pareto Front Scatter Plot", plot);
91    }
92
93    private void Parameterize() {
94      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
95        similarityCalculator.SolutionVariableName = Encoding.Name;
96        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
97      }
98    }
99
100    private void RegisterEventHandlers() {
101      IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
102      IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
103      ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
104    }
105
106    protected virtual void TreeLengthOnChanged() { }
107    protected virtual void TreeDepthOnChanged() { }
108    protected virtual void GrammarOnChanged() { }
109  }
110}
Note: See TracBrowser for help on using the repository browser.