Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeShapeAnalyzer.cs @ 9419

Last change on this file since 9419 was 9419, checked in by bburlacu, 11 years ago

#1772: Refactoring of directed graph components, added code for correctly serializing vertices and edges. Added specific building blocks analyzers and new population diversity analyzer which correctly integrates with the parallel engine.

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.EvolutionaryTracking {
33  /// <summary>
34  /// An operator that gathers information on tree fragments that get passed from one generation to the next via genetic operators
35  /// (Tracking of genetic variance and heredity)
36  /// </summary>
37  [Item("SymbolicExpressionTreeFragmentsAnalyzer", "An operator that provides statistics about crossover fragments")]
38  [StorableClass]
39  public sealed class SymbolicExpressionTreeShapeAnalyzer : SingleSuccessorOperator, IAnalyzer {
40    #region Parameter names
41    private const string UpdateIntervalParameterName = "UpdateInterval";
42    private const string UpdateCounterParameterName = "UpdateCounter";
43    private const string ResultsParameterName = "Results";
44    private const string GenerationsParameterName = "Generations";
45    #endregion
46
47    #region Parameter properties
48    public ValueParameter<IntValue> UpdateIntervalParameter {
49      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
50    }
51    public ValueParameter<IntValue> UpdateCounterParameter {
52      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
53    }
54    public LookupParameter<ResultCollection> ResultsParameter {
55      get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
56    }
57    public LookupParameter<IntValue> GenerationsParameter {
58      get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
59    }
60    #endregion
61
62    #region Parameters
63    public bool EnabledByDefault {
64      get { return true; }
65    }
66    public IntValue UpdateInterval {
67      get { return UpdateIntervalParameter.Value; }
68    }
69    public IntValue UpdateCounter {
70      get { return UpdateCounterParameter.Value; }
71    }
72    public ResultCollection Results {
73      get { return ResultsParameter.ActualValue; }
74    }
75    public IntValue Generations {
76      get { return GenerationsParameter.ActualValue; }
77    }
78    #endregion
79
80    [StorableConstructor]
81    private SymbolicExpressionTreeShapeAnalyzer(bool deserializing) : base(deserializing) { }
82
83    private SymbolicExpressionTreeShapeAnalyzer(SymbolicExpressionTreeShapeAnalyzer original, Cloner cloner) : base(original, cloner) { }
84
85    private SymbolicExpressionTreeShapeAnalyzer()
86      : base() {
87      #region Add parameters
88      // analyzer update counter and update interval
89      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
90      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
91      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
92      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
93      #endregion
94
95      UpdateCounterParameter.Hidden = true;
96      UpdateIntervalParameter.Hidden = true;
97
98    }
99    #region After deserialization code
100    [StorableHook(HookType.AfterDeserialization)]
101    private void AfterDeserialization() {
102      // check if all the parameters are present and accounted for
103      if (!Parameters.ContainsKey(UpdateIntervalParameterName)) {
104        Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
105      }
106      if (!Parameters.ContainsKey(UpdateCounterParameterName)) {
107        Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
108        UpdateCounterParameter.Hidden = true;
109      }
110    }
111    #endregion
112    #region IStatefulItem members
113    public override void InitializeState() {
114      base.InitializeState();
115      UpdateCounter.Value = 0;
116    }
117
118    public override void ClearState() {
119      base.ClearState();
120      UpdateCounter.Value = 0;
121    }
122    #endregion
123
124    public override IOperation Apply() {
125
126      return base.Apply();
127    }
128
129    private static int VisitationLength(ISymbolicExpressionTree tree) {
130      return VisitationLength(tree.Root);
131    }
132
133    private static int PathLength(ISymbolicExpressionTree tree) {
134      return PathLength(tree.Root);
135    }
136
137    private static int VisitationLength(ISymbolicExpressionTreeNode node) {
138      return node.IterateNodesBreadth().Sum(n => n.GetLength());
139    }
140
141    private static int PathLength(ISymbolicExpressionTreeNode node) {
142      return node.IterateNodesBreadth().Sum(n => node.GetBranchLevel(n)) + 1;
143    }
144
145    public override IDeepCloneable Clone(Cloner cloner) {
146      return new SymbolicExpressionTreeShapeAnalyzer(this, cloner);
147    }
148  } //SymbolicExpressionTreeFragmentsAnalyzer
149}
Note: See TracBrowser for help on using the repository browser.