Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/BestSymbolicExpressionTreeAnalyzer.cs @ 17412

Last change on this file since 17412 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  /// <summary>
33  /// An operator that tracks the best symbolic expression trees
34  /// </summary>
35  [Item("BestSymbolicExpressionTreeAnalyzer", "An operator that tracks the best symbolic expression trees")]
36  [StorableType("6C1884A9-F954-4E64-97DA-8831E2FF1561")]
37  [NonDiscoverableType]
38  public sealed class BestSymbolicExpressionTreeAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
39    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
40    private const string ResultsParameterName = "Results";
41    private const string QualityParameterName = "Quality";
42    private const string BestTreeQualityParameterName = "BestTreeQuality";
43    private const string MaximizationParameterName = "Maximization";
44
45    #region Parameter properties
46    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
47      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
48    }
49    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
51    }
52    public ILookupParameter<BoolValue> MaximizationParameter {
53      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
54    }
55    public ILookupParameter<DoubleValue> BestTreeQualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters[BestTreeQualityParameterName]; }
57    }
58    public ILookupParameter<ResultCollection> ResultsParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
60    }
61    #endregion
62
63    #region Properties
64    public bool EnabledByDefault {
65      get { return true; }
66    }
67    #endregion
68
69    [StorableConstructor]
70    private BestSymbolicExpressionTreeAnalyzer(StorableConstructorFlag _) : base(_) { }
71    private BestSymbolicExpressionTreeAnalyzer(BestSymbolicExpressionTreeAnalyzer original, Cloner cloner)
72      : base(original, cloner) {
73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new BestSymbolicExpressionTreeAnalyzer(this, cloner);
76    }
77    public BestSymbolicExpressionTreeAnalyzer()
78      : base() {
79      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees from which the best should be determined."));
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The quality value of a tree."));
81      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The order relation for qualities of trees."));
82      Parameters.Add(new LookupParameter<DoubleValue>(BestTreeQualityParameterName, "The quality of the best tree so far."));
83      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the best symbolic expression tree should be stored."));
84
85      SymbolicExpressionTreeParameter.Hidden = true;
86      QualityParameter.Hidden = true;
87      MaximizationParameter.Hidden = true;
88      ResultsParameter.Hidden = true;
89    }
90
91    [StorableHook(HookType.AfterDeserialization)]
92    private void AfterDeserialization() {
93    }
94
95    public override IOperation Apply() {
96      var qualities = QualityParameter.ActualValue;
97      var trees = SymbolicExpressionTreeParameter.ActualValue;
98      bool maximization = MaximizationParameter.ActualValue.Value;
99      int bestIdx = 0;
100      double bestQuality = qualities[bestIdx].Value;
101      if (maximization) {
102        for (int i = 1; i < qualities.Length; i++)
103          if (qualities[i].Value > bestQuality) {
104            bestIdx = i;
105            bestQuality = qualities[i].Value;
106          }
107      } else {
108        for (int i = 1; i < qualities.Length; i++)
109          if (qualities[i].Value < bestQuality) {
110            bestIdx = i;
111            bestQuality = qualities[i].Value;
112          }
113      }
114      var bestTree = trees[bestIdx];
115
116      var res = ResultsParameter.ActualValue;
117      if (res.ContainsKey("Best tree")) {
118        var prevBestQuality = (DoubleValue)BestTreeQualityParameter.ActualValue;
119        if ((maximization && prevBestQuality.Value < bestQuality) ||
120            (!maximization && prevBestQuality.Value > bestQuality)) {
121          res["Best tree"].Value = bestTree;
122          prevBestQuality.Value = bestQuality;
123        }
124      } else {
125        BestTreeQualityParameter.ActualValue = new DoubleValue(bestQuality);
126        res.Add(new Result("Best tree", bestTree));
127      }
128
129      return base.Apply();
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.