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 |
|
---|
24 | using System;
|
---|
25 | using System.Linq;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Analysis;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 |
|
---|
35 | namespace 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 |
|
---|
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 | // persistence
|
---|
58 | [StorableConstructor]
|
---|
59 | protected SymbolicExpressionTreeProblem(StorableConstructorFlag _) : base(_) { }
|
---|
60 | [StorableHook(HookType.AfterDeserialization)]
|
---|
61 | private void AfterDeserialization() {
|
---|
62 | RegisterEventHandlers();
|
---|
63 | }
|
---|
64 |
|
---|
65 | // cloning
|
---|
66 | protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
|
---|
67 | : base(original, cloner) {
|
---|
68 | TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
|
---|
69 | TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
|
---|
70 | GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
|
---|
71 | RegisterEventHandlers();
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected SymbolicExpressionTreeProblem() : this(new SymbolicExpressionTreeEncoding()) { }
|
---|
75 | protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeEncoding encoding)
|
---|
76 | : base(encoding) {
|
---|
77 | EncodingParameter.ReadOnly = true;
|
---|
78 | Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
|
---|
79 | Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
|
---|
80 | Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
|
---|
81 |
|
---|
82 | // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
|
---|
83 | Operators.Add(new QualitySimilarityCalculator());
|
---|
84 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
85 |
|
---|
86 | Parameterize();
|
---|
87 | RegisterEventHandlers();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results,
|
---|
91 | IRandom random) {
|
---|
92 | if (!results.ContainsKey("Best Solution Quality")) {
|
---|
93 | results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
|
---|
94 | }
|
---|
95 | if (!results.ContainsKey("Best Solution")) {
|
---|
96 | results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
|
---|
97 | }
|
---|
98 |
|
---|
99 | var bestQuality = Maximization ? qualities.Max() : qualities.Min();
|
---|
100 |
|
---|
101 | if (results["Best Solution Quality"].Value == null ||
|
---|
102 | IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
|
---|
103 | var bestIdx = Array.IndexOf(qualities, bestQuality);
|
---|
104 | var bestClone = (IItem)trees[bestIdx].Clone();
|
---|
105 |
|
---|
106 | results["Best Solution"].Value = bestClone;
|
---|
107 | results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void ParameterizeOperators() {
|
---|
112 | base.ParameterizeOperators();
|
---|
113 | Parameterize();
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void Parameterize() {
|
---|
117 | // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
|
---|
118 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
119 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
120 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | private void RegisterEventHandlers() {
|
---|
124 | IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
|
---|
125 | IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
|
---|
126 | ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected virtual void TreeLengthOnChanged() { }
|
---|
130 | protected virtual void TreeDepthOnChanged() { }
|
---|
131 | protected virtual void GrammarOnChanged() { }
|
---|
132 | }
|
---|
133 | }
|
---|