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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | // type definitions for convenience
|
---|
32 | using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
|
---|
33 | using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
36 | /// <summary>
|
---|
37 | /// An operator that gathers information on tree fragments that get passed from one generation to the next via genetic operators
|
---|
38 | /// (Tracking of genetic variance and heredity)
|
---|
39 | /// </summary>
|
---|
40 | [Item("SymbolicExpressionTreeRelativeFragmentDepthAnalyzer", "An operator that provides statistics about crossover fragments")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class SymbolicExpressionTreeRelativeFragmentDepthAnalyzer : SymbolicExpressionTreeFragmentsAnalyzer {
|
---|
43 | private const string RelativeFragmentDepthParameterName = "Relative fragment depth";
|
---|
44 | public ILookupParameter<DataTable> RelativeFragmentDepthParameter { get { return (ILookupParameter<DataTable>)Parameters[RelativeFragmentDepthParameterName]; } }
|
---|
45 |
|
---|
46 | public DataTable RelativeFragmentDepths {
|
---|
47 | get { return RelativeFragmentDepthParameter.ActualValue; }
|
---|
48 | set { RelativeFragmentDepthParameter.ActualValue = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | private SymbolicExpressionTreeRelativeFragmentDepthAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
53 | private SymbolicExpressionTreeRelativeFragmentDepthAnalyzer(SymbolicExpressionTreeRelativeFragmentDepthAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
55 | return new SymbolicExpressionTreeRelativeFragmentDepthAnalyzer(this, cloner);
|
---|
56 | }
|
---|
57 | public SymbolicExpressionTreeRelativeFragmentDepthAnalyzer()
|
---|
58 | : base() {
|
---|
59 | #region Add parameters
|
---|
60 | Parameters.Add(new LookupParameter<DataTable>(RelativeFragmentDepthParameterName));
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | UpdateCounterParameter.Hidden = true;
|
---|
64 | UpdateIntervalParameter.Hidden = true;
|
---|
65 |
|
---|
66 | }
|
---|
67 | #region After deserialization code
|
---|
68 |
|
---|
69 | [StorableHook(HookType.AfterDeserialization)]
|
---|
70 | private void AfterDeserialization() {
|
---|
71 | }
|
---|
72 |
|
---|
73 | #endregion
|
---|
74 | #region IStatefulItem members
|
---|
75 | public override void InitializeState() {
|
---|
76 | base.InitializeState();
|
---|
77 | UpdateCounter.Value = 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override void ClearState() {
|
---|
81 | base.ClearState();
|
---|
82 | UpdateCounter.Value = 0;
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | public override IOperation Apply() {
|
---|
87 | UpdateCounter.Value++;
|
---|
88 | if (UpdateCounter.Value == UpdateInterval.Value) {
|
---|
89 | UpdateCounter.Value = 0; // reset counter
|
---|
90 | if (Generations.Value > 1) {
|
---|
91 | SecondaryTraceMap = SecondaryTraceMap ?? new TraceMapType();
|
---|
92 | SecondaryFragmentMap = SecondaryFragmentMap ?? new CloneMapType();
|
---|
93 | SecondaryCloneMap = SecondaryCloneMap ?? new CloneMapType();
|
---|
94 |
|
---|
95 | InitializeParameters();
|
---|
96 | InitializeRows();
|
---|
97 |
|
---|
98 | var fragmentDepths = (from m in SecondaryFragmentMap
|
---|
99 | let tree = (ISymbolicExpressionTree)m.Key
|
---|
100 | let fragment = (IFragment)m.Value
|
---|
101 | where fragment.Root != null
|
---|
102 | select tree.Root.GetBranchLevel(fragment.Root)
|
---|
103 | ).ToList();
|
---|
104 |
|
---|
105 | // fragments of selected offspring
|
---|
106 | var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()));
|
---|
107 | var goodFragmentDepths = (from m in SecondaryFragmentMap
|
---|
108 | let tree = (ISymbolicExpressionTree)m.Key
|
---|
109 | let fragment = (IFragment)m.Value
|
---|
110 | where fragment.Root != null
|
---|
111 | where selected.Contains(tree)
|
---|
112 | select tree.Root.GetBranchLevel(fragment.Root)
|
---|
113 | ).ToList();
|
---|
114 |
|
---|
115 | RelativeFragmentDepths.Rows["Average relative fragment depth (all)"].Values.Add(fragmentDepths.Count > 0 ? fragmentDepths.Average() : 0.0);
|
---|
116 | RelativeFragmentDepths.Rows["Average relative fragment depth (good)"].Values.Add(goodFragmentDepths.Count > 0 ? goodFragmentDepths.Average() : 0.0);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | return base.Apply();
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected override void InitializeParameters() {
|
---|
123 | var results = ResultsParameter.ActualValue;
|
---|
124 | if (!results.ContainsKey("Relative fragment depths")) {
|
---|
125 | RelativeFragmentDepths = RelativeFragmentDepths ?? new DataTable("Relative fragment depths") { VisualProperties = { YAxisTitle = "Relative depth" } };
|
---|
126 | results.Add(new Result("Relative fragment depths", RelativeFragmentDepths));
|
---|
127 | }
|
---|
128 | base.InitializeParameters();
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void InitializeRows() {
|
---|
132 | string[] rowNames = { "Average relative fragment depth (good)", "Average relative fragment depth (all)" };
|
---|
133 | foreach (var rowName in rowNames.Where(rowName => !RelativeFragmentDepths.Rows.ContainsKey(rowName))) {
|
---|
134 | RelativeFragmentDepths.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true } });
|
---|
135 | }
|
---|
136 | }
|
---|
137 | } //SymbolicExpressionTreeFragmentLengthAnalyzer
|
---|
138 | }
|
---|