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("SymbolicExpressionTreeFragmentLengthAnalyzer", "An operator that provides statistics about fragment lengths")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class SymbolicExpressionTreeFragmentLengthAnalyzer : SymbolicExpressionTreeFragmentsAnalyzer {
|
---|
43 | private const string FragmentLengthsParameterName = "Fragment Lengths";
|
---|
44 | // impact values calculator
|
---|
45 | public ILookupParameter<DataTable> FragmentLengthsParameter { get { return (ILookupParameter<DataTable>)Parameters[FragmentLengthsParameterName]; } }
|
---|
46 | public DataTable FragmentLengths {
|
---|
47 | get { return FragmentLengthsParameter.ActualValue; }
|
---|
48 | set { FragmentLengthsParameter.ActualValue = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | private SymbolicExpressionTreeFragmentLengthAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
53 | private SymbolicExpressionTreeFragmentLengthAnalyzer(SymbolicExpressionTreeFragmentLengthAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
55 | return new SymbolicExpressionTreeFragmentLengthAnalyzer(this, cloner);
|
---|
56 | }
|
---|
57 | public SymbolicExpressionTreeFragmentLengthAnalyzer()
|
---|
58 | : base() {
|
---|
59 | #region Add parameters
|
---|
60 | Parameters.Add(new LookupParameter<DataTable>(FragmentLengthsParameterName));
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | UpdateCounterParameter.Hidden = true;
|
---|
64 | UpdateIntervalParameter.Hidden = true;
|
---|
65 |
|
---|
66 |
|
---|
67 | }
|
---|
68 | #region After deserialization code
|
---|
69 |
|
---|
70 | [StorableHook(HookType.AfterDeserialization)]
|
---|
71 | private void AfterDeserialization() {
|
---|
72 | }
|
---|
73 |
|
---|
74 | #endregion
|
---|
75 | #region IStatefulItem members
|
---|
76 | public override void InitializeState() {
|
---|
77 | base.InitializeState();
|
---|
78 | UpdateCounter.Value = 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override void ClearState() {
|
---|
82 | base.ClearState();
|
---|
83 | UpdateCounter.Value = 0;
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | public override IOperation Apply() {
|
---|
88 | UpdateCounter.Value++;
|
---|
89 | if (UpdateCounter.Value == UpdateInterval.Value) {
|
---|
90 | UpdateCounter.Value = 0; // reset counter
|
---|
91 | if (Generations.Value > 1) {
|
---|
92 | SecondaryTraceMap = SecondaryTraceMap ?? new TraceMapType();
|
---|
93 | SecondaryFragmentMap = SecondaryFragmentMap ?? new CloneMapType();
|
---|
94 | SecondaryCloneMap = SecondaryCloneMap ?? new CloneMapType();
|
---|
95 |
|
---|
96 | InitializeParameters();
|
---|
97 | InitializeRows();
|
---|
98 |
|
---|
99 | var parents = SecondaryTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()).ToList(); // parents of individuals at generation N-1
|
---|
100 | var offspring = SecondaryTraceMap.Keys.Cast<ISymbolicExpressionTree>().ToList(); // individuals at generation N-1
|
---|
101 | var fragments = SecondaryFragmentMap.Values.Cast<IFragment>().ToList();
|
---|
102 |
|
---|
103 |
|
---|
104 | // a hash of the offspring trees that got selected for reproduction at generation N
|
---|
105 | var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()));
|
---|
106 |
|
---|
107 | var selectedOffspringFragments = SecondaryFragmentMap.Where(m => selected.Contains((ISymbolicExpressionTree)m.Key)).Select(m => m.Key).ToList();
|
---|
108 |
|
---|
109 | var selectedOffspring = offspring.Where(selected.Contains).ToList();
|
---|
110 | var selectedOffspringParents = selectedOffspring.SelectMany(x => SecondaryTraceMap[x].Cast<ISymbolicExpressionTree>()).ToList();
|
---|
111 |
|
---|
112 | var rows = FragmentLengths.Rows;
|
---|
113 |
|
---|
114 | double avgParentsLength = parents.Count > 0 ? parents.Average(p => p.Length) : 0;
|
---|
115 | double avgGoodParentsLength = selectedOffspringParents.Count > 0 ? selectedOffspringParents.Average(p => p.Length) : 0;
|
---|
116 | rows["Parent lengths (all)"].Values.Add(avgParentsLength);
|
---|
117 | rows["Parent lengths (good)"].Values.Add(avgGoodParentsLength);
|
---|
118 | double avgOffspringLength = offspring.Count > 0 ? offspring.Average(o => o.Length) : 0;
|
---|
119 | double avgGoodOffspringLength = selectedOffspring.Count > 0 ? selectedOffspring.Average(o => o.Length) : 0;
|
---|
120 | rows["Offspring lengths (good)"].Values.Add(avgOffspringLength);
|
---|
121 | rows["Offspring lengths (all)"].Values.Add(avgGoodOffspringLength);
|
---|
122 | double avgFragmentLength = fragments.Count > 0 ? fragments.Average(f => f.Length) : 0;
|
---|
123 | double avgGoodFragmentLength = selectedOffspringFragments.Count > 0 ? fragments.Average(f => f.Length) : 0;
|
---|
124 | rows["Fragment lengths (good)"].Values.Add(avgGoodFragmentLength);
|
---|
125 | rows["Fragment lengths (all)"].Values.Add(avgFragmentLength);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | return base.Apply();
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected override void InitializeParameters() {
|
---|
133 | var results = ResultsParameter.ActualValue;
|
---|
134 | if (!results.ContainsKey("Fragment Lengths")) {
|
---|
135 | FragmentLengths = FragmentLengths ?? new DataTable("Fragment Lengths") { VisualProperties = { YAxisTitle = "Fragment length" } };
|
---|
136 | results.Add(new Result("Fragment Lengths", FragmentLengths));
|
---|
137 | }
|
---|
138 | base.InitializeParameters();
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void InitializeRows() {
|
---|
142 | string[] rowNames =
|
---|
143 | {
|
---|
144 | "Parent lengths (good)", "Parent lengths (all)",
|
---|
145 | "Offspring lengths (good)", "Offspring lengths (all)",
|
---|
146 | "Fragment lengths (good)", "Fragment lengths (all)"
|
---|
147 | };
|
---|
148 | foreach (var rowName in rowNames.Where(rowName => !FragmentLengths.Rows.ContainsKey(rowName))) {
|
---|
149 | FragmentLengths.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true } });
|
---|
150 | }
|
---|
151 | }
|
---|
152 | } //SymbolicExpressionTreeFragmentLengthAnalyzer
|
---|
153 | }
|
---|