Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.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: 5.2 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.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
34  [StorableType("A1B9F4C8-5E29-493C-A483-2AC68453BC63")]
35  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveProblem<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 SymbolicExpressionTreeProblem(StorableConstructorFlag _) : base(_) { }
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      RegisterEventHandlers();
61    }
62
63    // cloning
64    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
65      : base(original, cloner) {
66      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
67      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
68      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
69      RegisterEventHandlers();
70    }
71
72    protected SymbolicExpressionTreeProblem() : this(new SymbolicExpressionTreeEncoding()) { }   
73    protected SymbolicExpressionTreeProblem(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      if (!results.ContainsKey("Best Solution Quality")) {
87        results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
88      }
89      if (!results.ContainsKey("Best Solution")) {
90        results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
91      }
92
93      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
94
95      if (results["Best Solution Quality"].Value == null ||
96          IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
97        var bestIdx = Array.IndexOf(qualities, bestQuality);
98        var bestClone = (IItem)trees[bestIdx].Clone();
99
100        results["Best Solution"].Value = bestClone;
101        results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
102      }
103    }
104
105    private void Parameterize() {
106      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
107        similarityCalculator.SolutionVariableName = Encoding.Name;
108        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
109      }
110    }
111    private void RegisterEventHandlers() {
112      IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
113      IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
114      ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
115    }
116
117    protected virtual void TreeLengthOnChanged() { }
118    protected virtual void TreeDepthOnChanged() { }
119    protected virtual void GrammarOnChanged() { }
120  }
121}
Note: See TracBrowser for help on using the repository browser.