- Timestamp:
- 02/21/13 16:02:47 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4
- Files:
-
- 3 added
- 1 deleted
- 6 edited
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeFragmentLengthsAnalyzer.cs
r9082 r9238 63 63 UpdateCounterParameter.Hidden = true; 64 64 UpdateIntervalParameter.Hidden = true; 65 66 67 65 } 68 66 #region After deserialization code 69 70 67 [StorableHook(HookType.AfterDeserialization)] 71 68 private void AfterDeserialization() { … … 97 94 InitializeRows(); 98 95 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(); 96 var fragmentLengths = (from m in SecondaryFragmentMap 97 let fragment = (IFragment)m.Value 98 where fragment.Root != null 99 select fragment.Length 100 ).ToList(); 102 101 102 // fragments of selected offspring 103 var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>())); 104 var goodFragmentlengths = (from m in SecondaryFragmentMap 105 let tree = (ISymbolicExpressionTree)m.Key 106 let fragment = (IFragment)m.Value 107 where fragment.Root != null 108 where selected.Contains(tree) 109 select fragment.Length 110 ).ToList(); 103 111 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); 112 FragmentLengths.Rows["Average fragment length (all)"].Values.Add(fragmentLengths.Count > 0 ? fragmentLengths.Average() : 0.0); 113 FragmentLengths.Rows["Average fragment length (good)"].Values.Add(goodFragmentlengths.Count > 0 ? goodFragmentlengths.Average() : 0.0); 126 114 } 127 115 } 128 129 116 return base.Apply(); 130 117 } … … 132 119 protected override void InitializeParameters() { 133 120 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));121 if (!results.ContainsKey("Fragment lengths")) { 122 FragmentLengths = FragmentLengths ?? new DataTable("Fragment lengths") { VisualProperties = { YAxisTitle = "Number of nodes" } }; 123 results.Add(new Result("Fragment lengths", FragmentLengths)); 137 124 } 138 125 base.InitializeParameters(); … … 142 129 string[] rowNames = 143 130 { 144 "Parent lengths (good)", "Parent lengths (all)", 145 "Offspring lengths (good)", "Offspring lengths (all)", 146 "Fragment lengths (good)", "Fragment lengths (all)" 131 "Average fragment length (good)", "Average fragment length (all)" 147 132 }; 148 133 foreach (var rowName in rowNames.Where(rowName => !FragmentLengths.Rows.ContainsKey(rowName))) { -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeFragmentsAnalyzer.cs
r9082 r9238 272 272 foreach (var m in GlobalFragmentMap) 273 273 SecondaryFragmentMap.Add(m.Key, m.Value); 274 274 275 return base.Apply(); 275 276 } … … 294 295 /// </summary> 295 296 /// <param name="fragments">The symbolic expression tree fragments</param> 296 /// <param name=" mode">The similarity mode (0 - exact, 1 - high, 2 - relaxed)</param>297 /// <param name="comparer">The node similarity comparer</param> 297 298 /// <returns>The average number of similar fragments</returns> 298 299 private static double CalculateSimilarity(IList<IFragment> fragments, SymbolicExpressionTreeNodeSimilarityComparer comparer) { -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs
r9082 r9238 42 42 public sealed class SymbolicExpressionTreeRelativeFragmentDepthAnalyzer : SymbolicExpressionTreeFragmentsAnalyzer { 43 43 private const string RelativeFragmentDepthParameterName = "Relative fragment depth"; 44 // impact values calculator45 44 public ILookupParameter<DataTable> RelativeFragmentDepthParameter { get { return (ILookupParameter<DataTable>)Parameters[RelativeFragmentDepthParameterName]; } } 46 45 … … 131 130 132 131 private void InitializeRows() { 133 string[] rowNames = 134 { 135 "Average relative fragment depth (good)", "Average relative fragment depth (all)" 136 }; 132 string[] rowNames = { "Average relative fragment depth (good)", "Average relative fragment depth (all)" }; 137 133 foreach (var rowName in rowNames.Where(rowName => !RelativeFragmentDepths.Rows.ContainsKey(rowName))) { 138 134 RelativeFragmentDepths.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true } }); -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/GeneticOperatorImprovementAnalyzer.cs
r9236 r9238 10 10 using HeuristicLab.Parameters; 11 11 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 12 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 12 13 13 14 namespace HeuristicLab.EvolutionaryTracking { 14 15 /// An analyzer that looks at individual lineages in the genealogy graph 15 16 /// </summary> 16 [Item("GeneticOperator sAverageImprovementAnalyzer", "An operator that records the average success of genetic operators such as crossover and mutation.")]17 [Item("GeneticOperatorImprovementAnalyzer", "An operator that records the average success of genetic operators such as crossover and mutation.")] 17 18 [StorableClass] 18 class GeneticOperator sAverageImprovementAnalyzer : SingleSuccessorOperator, IAnalyzer {19 class GeneticOperatorImprovementAnalyzer : SingleSuccessorOperator, IAnalyzer { 19 20 #region Parameter names 20 21 private const string UpdateIntervalParameterName = "UpdateInterval"; … … 24 25 private const string PopulationGraphParameterName = "PopulationGraph"; 25 26 private const string GeneticOperatorsAverageImprovementResultName = "GeneticOperatorsAverageImprovement"; 27 private const string UpdateConstantsInIntermediateNodesParameterName = "UpdateConstantsInIntermediateNodes"; 26 28 #endregion 27 29 … … 32 34 public ValueParameter<IntValue> UpdateCounterParameter { 33 35 get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; } 36 } 37 public ValueParameter<BoolValue> UpdateConstantsInIntermediateNodesParameter { 38 get { return (ValueParameter<BoolValue>)Parameters[UpdateConstantsInIntermediateNodesParameterName]; } 34 39 } 35 40 public LookupParameter<ResultCollection> ResultsParameter { … … 48 53 get { return UpdateCounterParameter.Value; } 49 54 } 55 public BoolValue UpdateConstantsInIntermediateNodes { 56 get { return UpdateConstantsInIntermediateNodesParameter.Value; } 57 } 50 58 public IntValue Generations { 51 59 get { return GenerationsParameter.ActualValue; } … … 59 67 60 68 [StorableConstructor] 61 private GeneticOperator sAverageImprovementAnalyzer(bool deserializing) : base(deserializing) { }62 private GeneticOperator sAverageImprovementAnalyzer(GeneticOperatorsAverageImprovementAnalyzer original, Cloner cloner) : base(original, cloner) { }69 private GeneticOperatorImprovementAnalyzer(bool deserializing) : base(deserializing) { } 70 private GeneticOperatorImprovementAnalyzer(GeneticOperatorImprovementAnalyzer original, Cloner cloner) : base(original, cloner) { } 63 71 public override IDeepCloneable Clone(Cloner cloner) { 64 return new GeneticOperator sAverageImprovementAnalyzer(this, cloner);72 return new GeneticOperatorImprovementAnalyzer(this, cloner); 65 73 } 66 public GeneticOperator sAverageImprovementAnalyzer() {74 public GeneticOperatorImprovementAnalyzer() { 67 75 Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1))); 68 76 Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0))); 69 77 Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored.")); 70 78 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far")); 79 Parameters.Add(new ValueParameter<BoolValue>(UpdateConstantsInIntermediateNodesParameterName, "Controls whether or not the constant values of the intermediate nodes should be updated.", new BoolValue(false))); 71 80 72 81 UpdateCounterParameter.Hidden = true; … … 77 86 [StorableHook(HookType.AfterDeserialization)] 78 87 private void AfterDeserialization() { 88 if (!Parameters.ContainsKey(UpdateConstantsInIntermediateNodesParameterName)) 89 Parameters.Add(new ValueParameter<BoolValue>(UpdateConstantsInIntermediateNodesParameterName, "Controls whether or not the constant values of the intermediate nodes should be updated.")); 79 90 } 80 91 #endregion … … 106 117 var mutationImprovements = new List<double>(); 107 118 108 foreach (var node in nodes) { 109 if (node.InEdges == null) continue; // it means the corresponding individual did not reproduce 110 var parentsQuality = node.InEdges.Select(e => e.Source).Max(v => ((SymbolicExpressionGenealogyGraphNode)v).Quality); 111 var childQuality = node.Quality; 112 var improvement = childQuality / parentsQuality; 119 // in case we need to use it 120 var evaluator = new SymbolicRegressionConstantOptimizationEvaluator(); 121 evaluator.EvaluationPartitionParameter.ActualName = "FitnessCalculationPartition"; 122 123 foreach (var node in nodes.Where(n => n.InEdges != null).OrderByDescending(n => n.InEdges.Count)) { 124 double parentsQuality = node.InEdges.Select(e => e.Source).Max(v => ((SymbolicExpressionGenealogyGraphNode)v).Quality); 125 double childQuality = node.Quality; 126 double improvement = childQuality - parentsQuality; 113 127 if (double.IsNaN(improvement) || double.IsInfinity(improvement) || parentsQuality.IsAlmost(0.0)) improvement = 0; 114 128 switch (node.InEdges.Count) { … … 117 131 break; 118 132 case 1: // mutation 133 // determine if we should update the quality 119 134 mutationImprovements.Add(improvement); 120 135 break; … … 139 154 mutMaxImprovement = mutationImprovements.Max(); 140 155 } 156 141 157 142 158 if (!table.Rows.ContainsKey("Average crossover improvement")) { -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeFrequentPatternsAnalyzer.cs
r9082 r9238 96 96 } 97 97 #endregion 98 99 98 #region Properties 100 99 public bool EnabledByDefault { … … 361 360 // adjust weights according to the probability a child will find itself on a certain argument position 362 361 for (int j = 0; j != possibleChildConnections.Count; ++j) { 363 var childSymbolNode = possibleChildConnections[j].Target as SymbolNode;362 var childSymbolNode = (SymbolNode)possibleChildConnections[j].Target; 364 363 double sumPos = childSymbolNode.Positions.Sum(x => x.Value); 365 364 int v; childSymbolNode.Positions.TryGetValue(i, out v); -
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 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/DirectedGraph/Vertex.cs
r9082 r9238 100 100 return list; 101 101 } 102 103 102 // this method can be used to add arcs towards targets that are not visible in the graph 104 103 // (they do not appear in the nodes Dictionary). It is the programmers responsibility to … … 114 113 if (OutEdges == null) { OutEdges = new List<Arc> { arc }; } else { OutEdges.Add(arc); } 115 114 } 116 117 115 public void AddReverseArc(Arc arc) { 118 116 if (arc.Target == null) { arc.Target = this; } … … 120 118 if (InEdges == null) { InEdges = new List<Arc> { arc }; } else { InEdges.Add(arc); } 121 119 } 122 123 124 120 public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) { 125 121 var e = new Arc { Source = source, Target = this, Data = data, Weight = weight }; -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/HeuristicLab.EvolutionaryTracking-3.4.csproj
r9082 r9238 87 87 </ItemGroup> 88 88 <ItemGroup> 89 <Compile Include="Analyzers\SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs" /> 90 <Compile Include="Analyzers\SymbolicExpressionTreeFragmentLengthsAnalyzer.cs" /> 89 <Compile Include="Analyzers\SymbolicExpressionTreeShapeAnalyzer.cs" /> 90 <Compile Include="Analyzers\SymbolicExpressionTreeEvolvabilityAnalyzer.cs" /> 91 <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs" /> 92 <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeFragmentLengthsAnalyzer.cs" /> 91 93 <Compile Include="Analyzers\GeneticOperatorsAverageImprovementAnalyzer.cs" /> 92 <Compile Include="Analyzers\SymbolicExpressionTreeEvolvabilityAnalyzer.cs" />93 94 <Compile Include="Analyzers\SymbolicExpressionTreeFrequentPatternsAnalyzer.cs" /> 94 <Compile Include="Analyzers\ SymbolicExpressionTreeFragmentsAnalyzer.cs" />95 <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeFragmentsAnalyzer.cs" /> 95 96 <Compile Include="FPGraph.cs" /> 97 <Compile Include="LineageExplorer.cs" /> 98 <Compile Include="Operators\LineageAggregator.cs" /> 96 99 <Compile Include="Operators\SymbolicExpressionTreeFPBuilder.cs" /> 97 100 <Compile Include="Operators\SymbolicExpressionTreeGenealogyGraphBuilder.cs" /> -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/CleanupOperator.cs
r9082 r9238 20 20 #endregion 21 21 22 using System.Collections.Generic; 23 using System.Linq; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; 24 26 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 28 using HeuristicLab.Operators; 26 29 using HeuristicLab.Optimization; … … 47 50 private const string MaximumGenerationsParameterName = "MaximumGenerations"; 48 51 private const string ResultsParameterName = "Results"; 49 private const string DeleteGenealogyGraphParameterName = "DeleteGenealogyGraph"; 52 private const string CleanGenealogyGraphParameterName = "CleanGenealogyGraph"; 53 private const string UpdateGenealogyGraphParameterName = "UpdateGenealogyGraph"; 54 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 55 private const string SymbolicExpressionTreeQualityParameterName = "Quality"; 50 56 #endregion 51 57 … … 72 78 get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; } 73 79 } 74 public ValueParameter<BoolValue> DeleteGenealogyGraphParameter { 75 get { return (ValueParameter<BoolValue>)Parameters[DeleteGenealogyGraphParameterName]; } 80 public ValueParameter<BoolValue> CleanGenealogyGraphParameter { 81 get { return (ValueParameter<BoolValue>)Parameters[CleanGenealogyGraphParameterName]; } 82 } 83 public ValueParameter<BoolValue> UpdateGenealogyGraphParameter { 84 get { return (ValueParameter<BoolValue>)Parameters[UpdateGenealogyGraphParameterName]; } 85 } 86 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 87 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 88 } 89 public IScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeQualityParameter { 90 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeQualityParameterName]; } 76 91 } 77 92 #endregion 78 93 79 94 #region Properties 80 public CloneMapType GlobalCloneMap { 81 get { return GlobalCloneMapParameter.ActualValue; } 82 } 83 public TraceMapType GlobalTraceMap { 84 get { return GlobalTraceMapParameter.ActualValue; } 85 } 86 public CloneMapType GlobalFragmentMap { 87 get { return GlobalFragmentMapParameter.ActualValue; } 88 } 89 public GeneticExchangeMapType GlobalGeneticExchangeMap { 90 get { return GlobalGeneticExchangeMapParameter.ActualValue; } 91 } 92 public IntValue Generations { 93 get { return GenerationsParameter.ActualValue; } 94 } 95 public IntValue MaximumGenerations { 96 get { return MaximumGenerationsParameter.ActualValue; } 97 } 98 public ResultCollection Results { 99 get { return ResultsParameter.ActualValue; } 100 } 95 private CloneMapType GlobalCloneMap { get { return GlobalCloneMapParameter.ActualValue; } } 96 private TraceMapType GlobalTraceMap { get { return GlobalTraceMapParameter.ActualValue; } } 97 private CloneMapType GlobalFragmentMap { get { return GlobalFragmentMapParameter.ActualValue; } } 98 private GeneticExchangeMapType GlobalGeneticExchangeMap { get { return GlobalGeneticExchangeMapParameter.ActualValue; } } 99 private IntValue Generations { get { return GenerationsParameter.ActualValue; } } 100 private IntValue MaximumGenerations { get { return MaximumGenerationsParameter.ActualValue; } } 101 private ResultCollection Results { get { return ResultsParameter.ActualValue; } } 102 public BoolValue CleanGenealogyGraph { get { return CleanGenealogyGraphParameter.Value; } } 103 public BoolValue UpdateGenealogyGraph { get { return UpdateGenealogyGraphParameter.Value; } } 104 private IEnumerable<DoubleValue> Quality { get { return SymbolicExpressionTreeQualityParameter.ActualValue; } } 105 private IEnumerable<ISymbolicExpressionTree> SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } } 101 106 #endregion 102 107 … … 105 110 if (!Parameters.ContainsKey(GlobalGeneticExchangeMapParameterName)) 106 111 Parameters.Add(new LookupParameter<GeneticExchangeMapType>(GlobalGeneticExchangeMapParameterName)); 112 if (!Parameters.ContainsKey(CleanGenealogyGraphParameterName)) 113 Parameters.Add(new ValueParameter<BoolValue>(CleanGenealogyGraphParameterName)); 114 if (!Parameters.ContainsKey(UpdateGenealogyGraphParameterName)) 115 Parameters.Add(new ValueParameter<BoolValue>(UpdateGenealogyGraphParameterName)); 116 if (!Parameters.ContainsKey(SymbolicExpressionTreeParameterName)) 117 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze.")); 118 if (!Parameters.ContainsKey(SymbolicExpressionTreeQualityParameterName)) 119 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees")); 107 120 } 108 121 … … 121 134 Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName)); 122 135 Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored.")); 136 Parameters.Add(new ValueParameter<BoolValue>(CleanGenealogyGraphParameterName)); 137 Parameters.Add(new ValueParameter<BoolValue>(UpdateGenealogyGraphParameterName)); 138 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze.")); 139 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees")); 123 140 } 124 141 … … 129 146 if (GlobalGeneticExchangeMap != null) GlobalGeneticExchangeMap.Clear(); 130 147 148 if (!Results.ContainsKey(PopulationGraphParameterName)) return base.Apply(); 149 var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value; 150 if (graph == null) return base.Apply(); 151 152 if (UpdateGenealogyGraph.Value) { 153 // update vertex qualities if the trees in the scope were optimized by the ConstantOptimizationAnalyzer 154 ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray(); 155 double[] qualities = Quality.Select(x => x.Value).ToArray(); 156 157 for (int i = 0; i != trees.Length; ++i) { 158 foreach (var graphNode in graph.GetGraphNodes(trees[i])) { 159 graphNode.Quality = qualities[i]; 160 } 161 } 162 } 163 131 164 // if we are at the end of the run, then we clear the genealogy graph (to save memory) 132 if ( Generations.Value == MaximumGenerations.Value)133 if ( Results.ContainsKey(PopulationGraphParameterName)) {134 var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value;135 Results.Remove(PopulationGraphParameterName);136 graph.Dispose();137 }138 165 if (CleanGenealogyGraph.Value) { 166 if (Generations.Value == MaximumGenerations.Value) 167 if (Results.ContainsKey(PopulationGraphParameterName)) { 168 Results.Remove(PopulationGraphParameterName); 169 graph.Dispose(); 170 } 171 } 139 172 return base.Apply(); 140 173 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/SymbolicExpressionTreeGenealogyGraphBuilder.cs
r9082 r9238 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using System.Threading; … … 63 62 private const string SymbolicExpressionTreeQualityParameterName = "Quality"; 64 63 #endregion 65 #region Parameter properties64 #region Parameters 66 65 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 67 66 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } … … 109 108 } 110 109 #endregion 111 #region P roperties110 #region Parameter properties 112 111 public bool EnabledByDefault { 113 112 get { return true; } … … 189 188 190 189 public override IOperation Apply() { 191 if (GlobalTraceMap == null) return base.Apply(); // no trace map, no genealogy graph192 193 190 if (!Parameters.ContainsKey("Results")) return base.Apply(); 194 191 … … 211 208 var pairs = trees.Zip(qualities, (t, q) => new { Tree = t, Quality = q.Value }).OrderByDescending(x => x.Quality).ToList(); 212 209 213 var graphNodes = new List<SymbolicExpressionGenealogyGraphNode>( 214 (from i in Enumerable.Range(0, trees.Length) 215 let tree = pairs[i].Tree 216 let quality = pairs[i].Quality 217 select new SymbolicExpressionGenealogyGraphNode { SymbolicExpressionTree = tree, Quality = quality, Rank = generation, IsElite = i < Elites.Value } 218 ).Concat 219 (from t in trees 220 where GlobalTraceMap.ContainsKey(t) 221 let parents = GlobalTraceMap[t] 222 where parents.Count == 1 // 1 parent means mutation 223 let p = (ISymbolicExpressionTree)parents[0] 224 select new SymbolicExpressionGenealogyGraphNode { SymbolicExpressionTree = p, Quality = Evaluate(p), Rank = generation - 0.5 } 225 )).ToList(); 210 if (generation == 0) { 211 // add the trees in the graph and return 212 foreach (var pair in pairs) { 213 var tree = pair.Tree; 214 var quality = pair.Quality; 215 graph.AddNode(new SymbolicExpressionGenealogyGraphNode { 216 SymbolicExpressionTree = tree, Quality = quality, Rank = generation 217 }); 218 } 219 return base.Apply(); 220 } 221 222 var graphNodes = (from i in Enumerable.Range(0, trees.Length) 223 let tree = pairs[i].Tree 224 let quality = pairs[i].Quality 225 select new SymbolicExpressionGenealogyGraphNode { 226 SymbolicExpressionTree = tree, 227 Quality = quality, 228 Rank = generation, 229 IsElite = i < Elites.Value 230 }).Concat 231 // the following query addds the intermediate nodes that were created when 232 // crossover was followed by mutation (they are not present in the scope) 233 (from t in trees 234 where GlobalTraceMap.ContainsKey(t) 235 let parents = GlobalTraceMap[t] 236 where parents.Count == 1 // 1 parent means mutation 237 let p = (ISymbolicExpressionTree)parents[0] 238 select new SymbolicExpressionGenealogyGraphNode { 239 SymbolicExpressionTree = p, 240 Quality = Evaluate(p), 241 Rank = generation - 0.5 // an intermediate parent that would normale be 'invisible' to the evolutionary process 242 } 243 ).ToList(); 226 244 227 245 foreach (var node in graphNodes) { … … 229 247 } 230 248 249 graphNodes.Sort((a, b) => b.Rank.CompareTo(a.Rank)); 250 231 251 foreach (var node in graphNodes) { 232 252 var tree = node.SymbolicExpressionTree; 233 if (!GlobalTraceMap.ContainsKey(tree)) continue; 253 if (!GlobalTraceMap.ContainsKey(tree)) { 254 continue; 255 } 234 256 var parents = GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>(); 235 257 foreach (var p in parents) { 236 258 var nodes = graph.GetGraphNodes(p); 237 if (nodes == null) continue; 259 if (nodes == null) { 260 throw new Exception("Node cannot have no parents"); 261 } 238 262 var sourceNode = nodes.LastOrDefault(n => n.Rank < node.Rank); 239 if (sourceNode == null) continue; 263 if (sourceNode == null) { 264 throw new Exception("Source node cannot be null"); 265 } 240 266 var arc = new Arc { Source = sourceNode, Target = node, Data = GlobalFragmentMap[tree] }; 241 267 sourceNode.AddForwardArc(arc); … … 249 275 /// Evaluate a symbolic expression tree. We perform evaluation by adding a temporary subscope with the tree in it, and calling evaluator.Apply() 250 276 /// </summary> 251 /// <param name="tree"> </param>252 /// <returns> </returns>277 /// <param name="tree">The symbolic expression tree to evaluate</param> 278 /// <returns>A double value representing the fitness</returns> 253 279 private double Evaluate(IItem tree) { 254 280 var subScope = new Scope();
Note: See TracChangeset
for help on using the changeset viewer.