Changeset 9238 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreePopulationDiversityAnalyzer.cs
- Timestamp:
- 02/21/13 16:02:47 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreePopulationDiversityAnalyzer.cs
r9082 r9238 30 30 using HeuristicLab.Parameters; 31 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Problems.DataAnalysis.Symbolic; 32 33 33 34 // type definitions for ease of use 34 using HeuristicLab.Problems.DataAnalysis.Symbolic;35 using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>; 35 36 36 37 namespace HeuristicLab.EvolutionaryTracking { … … 46 47 private const string UpdateCounterParameterName = "UpdateCounter"; 47 48 private const string ResultsParameterName = "Results"; 48 #endregion 49 50 #region Parameter properties 49 private const string GenerationsParameterName = "Generations"; 50 private const string StoreHistoryParameterName = "StoreHistory"; 51 // comparer parameters 52 private const string MatchVariablesParameterName = "MatchVariableNames"; 53 private const string MatchVariableWeightsParameterName = "MatchVariableWeights"; 54 private const string MatchConstantValuesParameterName = "MatchConstantValues"; 55 private const string SortSubtreesParameterName = "SortSubtrees"; 56 // population graph 57 private const string PopulationGraphParameterName = "PopulationGraph"; 58 // clone map 59 private const string GlobalCloneMapParameterName = "GlobalCloneMap"; 60 #endregion 61 62 #region Parameters 51 63 public ValueParameter<IntValue> UpdateIntervalParameter { 52 64 get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; } … … 58 70 get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; } 59 71 } 72 public LookupParameter<IntValue> GenerationsParameter { 73 get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; } 74 } 75 public ValueParameter<BoolValue> StoreHistoryParameter { 76 get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; } 77 } 60 78 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 61 79 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 62 80 } 63 #endregion 64 65 #region Parameters 81 public ValueParameter<BoolValue> MatchVariableNamesParameter { 82 get { return (ValueParameter<BoolValue>)Parameters[MatchVariablesParameterName]; } 83 } 84 public ValueParameter<BoolValue> MatchVariableWeightsParameter { 85 get { return (ValueParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; } 86 } 87 public ValueParameter<BoolValue> MatchConstantValuesParameter { 88 get { return (ValueParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; } 89 } 90 public ValueParameter<BoolValue> SortSubtreesParameter { 91 get { return (ValueParameter<BoolValue>)Parameters[SortSubtreesParameterName]; } 92 } 93 public LookupParameter<CloneMapType> GlobalCloneMapParameter { 94 get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; } 95 } 96 #endregion 97 98 #region Parameter properties 66 99 public IntValue UpdateInterval { 67 100 get { return UpdateIntervalParameter.Value; } … … 72 105 public ResultCollection Results { 73 106 get { return ResultsParameter.ActualValue; } 107 } 108 public CloneMapType GlobalCloneMap { 109 get { return GlobalCloneMapParameter.ActualValue; } 110 } 111 public IntValue Generations { 112 get { return GenerationsParameter.ActualValue; } 113 } 114 public BoolValue StoreHistory { 115 get { return StoreHistoryParameter.Value; } 74 116 } 75 117 #endregion … … 87 129 Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0))); 88 130 Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored.")); 89 131 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far.")); 132 Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names.")); 133 Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights.")); 134 Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values.")); 135 Parameters.Add(new ValueParameter<BoolValue>(SortSubtreesParameterName, "Specifies whether the subtrees of a tree should be sorted before comparison.")); 136 Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection).")); 137 Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false))); 90 138 UpdateCounterParameter.Hidden = true; 91 139 UpdateIntervalParameter.Hidden = true; … … 106 154 #endregion 107 155 156 public bool EnabledByDefault { get { return true; } } 157 108 158 public override IOperation Apply() { 109 159 UpdateCounter.Value++; … … 113 163 114 164 var trees = SymbolicExpressionTreeParameter.ActualValue.ToList(); 115 var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter(); 116 foreach (var t in trees) canonicalSorter.SortSubtrees(t); 117 118 var comparer = new SymbolicExpressionTreeNodeSimilarityComparer { MatchConstantValues = false, MatchVariableNames = false, MatchVariableWeights = false }; 119 120 var distinct = 1.0 - trees.Select(x => x.Root).Distinct(comparer).Count() / (double)trees.Count; 121 122 var results = ResultsParameter.ActualValue; 123 124 if (!results.ContainsKey("PopulationDiversity")) 125 results.Add(new Result("PopulationDiversity", new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Count" } })); 126 var populationDiversityTable = results["PopulationDiversity"].Value as DataTable; 127 128 if (populationDiversityTable == null) return base.Apply(); // should never happen 129 165 if (SortSubtreesParameter.Value.Value) { 166 var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter(); 167 foreach (var t in trees) canonicalSorter.SortSubtrees(t); 168 } 169 170 bool matchConstants = MatchConstantValuesParameter.Value.Value; 171 bool matchVarNames = MatchVariableNamesParameter.Value.Value; 172 bool matchVarWeights = MatchVariableWeightsParameter.Value.Value; 173 174 var comparer = new SymbolicExpressionTreeEqualityComparer { 175 SimilarityComparer = new SymbolicExpressionTreeNodeSimilarityComparer { 176 MatchConstantValues = matchConstants, MatchVariableNames = matchVarNames, MatchVariableWeights = matchVarWeights 177 } 178 }; 179 ResultCollection results = ResultsParameter.ActualValue; 180 // population diversity 181 DataTable populationDiversityTable; 182 if (!results.ContainsKey("PopulationDiversity")) { 183 populationDiversityTable = new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Diversity" } }; 184 results.Add(new Result("PopulationDiversity", populationDiversityTable)); 185 } 186 populationDiversityTable = (DataTable)results["PopulationDiversity"].Value; 130 187 if (!populationDiversityTable.Rows.ContainsKey("Diversity")) 131 188 populationDiversityTable.Rows.Add(new DataRow("Diversity") { VisualProperties = { StartIndexZero = true } }); 132 189 double distinct = trees.Distinct(comparer).Count() / (double)trees.Count; 133 190 populationDiversityTable.Rows["Diversity"].Values.Add(distinct); 191 // selection diversity 192 if (GlobalCloneMap == null) return base.Apply(); 193 194 DataTable relativeSelectionCountsTable; 195 if (!results.ContainsKey("SelectedIndividuals")) { 196 relativeSelectionCountsTable = new DataTable("SelectedIndividuals") { VisualProperties = { YAxisTitle = "% Selected Individuals" } }; 197 results.Add(new Result("SelectedIndividuals", relativeSelectionCountsTable)); 198 } 199 relativeSelectionCountsTable = (DataTable)Results["SelectedIndividuals"].Value; 200 if (!relativeSelectionCountsTable.Rows.ContainsKey("SelectedIndividuals")) { 201 relativeSelectionCountsTable.Rows.Add(new DataRow("SelectedIndividuals") { VisualProperties = { StartIndexZero = true } }); 202 } 203 double relativeSelectionCount = GlobalCloneMap.Values.Distinct().Count() / (double)trees.Count; 204 relativeSelectionCountsTable.Rows["SelectedIndividuals"].Values.Add(relativeSelectionCount); 205 206 // do a histogram of selection frequency for each individual in the population 207 DataTable selectionFrequenciesTable; 208 if (!results.ContainsKey("SelectionFrequencies")) { 209 selectionFrequenciesTable = new DataTable("Selection Frequencies") { VisualProperties = { YAxisTitle = "Selection Count" } }; 210 results.Add(new Result("SelectionFrequencies", selectionFrequenciesTable)); 211 } 212 selectionFrequenciesTable = (DataTable)results["SelectionFrequencies"].Value; 213 DataRow histoRow; 214 if (!selectionFrequenciesTable.Rows.ContainsKey("SelectionFrequencies")) { 215 histoRow = new DataRow("SelectionFrequencies") { VisualProperties = { StartIndexZero = true } }; 216 selectionFrequenciesTable.Rows.Add(histoRow); 217 } 218 histoRow = selectionFrequenciesTable.Rows["SelectionFrequencies"]; 219 var frequencies = new double[trees.Count]; 220 221 var graph = (SymbolicExpressionTreeGenealogyGraph)results[PopulationGraphParameterName].Value; 222 // get graph nodes corresponding to individuals of the previous generation 223 var prevGen = graph.Nodes.Where(node => node.Rank == Generations.Value - 1).OrderByDescending(n => n.Quality).ToList(); 224 for (int i = 0; i != prevGen.Count; ++i) { 225 int selFreq = GlobalCloneMap.Values.Count(t => t == prevGen[i].SymbolicExpressionTree); 226 frequencies[i] = selFreq; 227 } 228 histoRow.Values.Replace(frequencies); 229 230 bool storeHistory = StoreHistory.Value; 231 if (storeHistory) { 232 DataTableHistory selectionFrequenciesHistory; 233 if (!results.ContainsKey("SelectionFrequenciesHistory")) { 234 selectionFrequenciesHistory = new DataTableHistory(); 235 results.Add(new Result("SelectionFrequenciesHistory", selectionFrequenciesHistory)); 236 } 237 selectionFrequenciesHistory = (DataTableHistory)results["SelectionFrequenciesHistory"].Value; 238 selectionFrequenciesHistory.Add((DataTable)selectionFrequenciesTable.Clone()); 239 } 134 240 } 135 241 return base.Apply(); 136 242 } 137 138 public bool EnabledByDefault { get { return true; } }139 243 } 140 244 }
Note: See TracChangeset
for help on using the changeset viewer.