- Timestamp:
- 05/10/12 17:17:14 (12 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeFragmentsAnalyzer.cs
r7788 r7792 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 162 163 ResultCollection results = ResultsParameter.ActualValue; 163 164 if (FragmentStatistics == null) { 164 FragmentStatisticsParameter.ActualValue = new DataTable("Fragment Statistics", 165 "Statistical measurements of fragments aggregated over the whole population"); 165 FragmentStatisticsParameter.ActualValue = new DataTable("Fragment Statistics", "Statistical measurements of fragments aggregated over the whole population"); 166 166 FragmentStatistics.VisualProperties.YAxisTitle = "Fragment length/Similarities"; 167 167 results.Add(new Result("Fragment Statistics", FragmentStatistics)); … … 177 177 var trees = (from s in gScope.SubScopes select s.Variables.First().Value as ISymbolicExpressionTree).ToList(); 178 178 foreach (var tree in trees.Where(x => GlobalTraceMap.ContainsKey(x))) { 179 if (GlobalTraceMap[tree].Count == 2) { 180 var fragment = tree.IterateNodes().ElementAt(((IntValue)GlobalFragmentMap[tree]).Value); 181 if (fragment != null) { 182 parents.AddRange(GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>()); 183 fragments.Add(fragment); 184 } else { // "intermediate crossovers" (immediately followed by mutation) 185 var parent0 = (ISymbolicExpressionTree)GlobalTraceMap[tree][0]; 186 if (GlobalTraceMap.ContainsKey(parent0)) { 187 var p = (ISymbolicExpressionTree)GlobalTraceMap[parent0][0]; 188 fragment = parent0.IterateNodes().ElementAt(((IntValue)GlobalFragmentMap[parent0]).Value); 189 if (fragment != null) { 190 parents.AddRange(GlobalTraceMap[parent0].Cast<ISymbolicExpressionTree>()); 191 fragments.Add(fragment); 192 } 193 } 194 } 179 if (GlobalTraceMap[tree].Count == 2) { // crossover 180 var nodes = tree.IterateNodesBreadth() as List<ISymbolicExpressionTreeNode>; 181 var index = ((IntValue)GlobalFragmentMap[tree]).Value; 182 var fragment = nodes[index]; 183 if (fragment == null) throw new ArgumentException("Fragment not found"); 184 parents.AddRange(GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>()); 185 fragments.Add(fragment); 186 } else { // crossover followed by mutation 187 // maybe mutation fragments should be also taken into account? 188 var parent0 = (ISymbolicExpressionTree)GlobalTraceMap[tree][0]; 189 if (!GlobalFragmentMap.ContainsKey(parent0)) throw new ArgumentException("Fragment not found"); 190 var nodes = parent0.IterateNodesBreadth() as List<ISymbolicExpressionTreeNode>; 191 var index = ((IntValue)GlobalFragmentMap[parent0]).Value; 192 var fragment = nodes[index]; 193 fragments.Add(fragment); 194 if (!GlobalTraceMap.ContainsKey(parent0)) throw new ArgumentException("Parent information not found"); 195 parents.AddRange(GlobalTraceMap[parent0].Cast<ISymbolicExpressionTree>()); 195 196 } 196 197 } … … 201 202 double a3 = parents.Average(x => x.Length); 202 203 double s1 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.Exact); 203 //double s2 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.High);204 //double s3 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.Relaxed);205 204 206 205 #region Table data 207 206 // fragments 208 207 if (!FragmentStatistics.Rows.ContainsKey("Average fragment length")) { 209 DataRow row = new DataRow("Average fragment length", ""); 210 row.VisualProperties.StartIndexZero = true; 208 var row = new DataRow("Average fragment length", "") { VisualProperties = { StartIndexZero = true } }; 211 209 FragmentStatistics.Rows.Add(row); 212 210 } … … 214 212 // child trees 215 213 if (!FragmentStatistics.Rows.ContainsKey("Average child length")) { 216 DataRow row = new DataRow("Average child length", ""); 217 row.VisualProperties.StartIndexZero = true; 214 var row = new DataRow("Average child length", "") { VisualProperties = { StartIndexZero = true } }; 218 215 FragmentStatistics.Rows.Add(row); 219 216 } … … 221 218 // parents 222 219 if (!FragmentStatistics.Rows.ContainsKey("Average parent length")) { 223 DataRow row = new DataRow("Average parent length", ""); 224 row.VisualProperties.StartIndexZero = true; 220 var row = new DataRow("Average parent length", "") { VisualProperties = { StartIndexZero = true } }; 225 221 FragmentStatistics.Rows.Add(row); 226 222 } … … 228 224 // exact similarity 229 225 if (!FragmentStatistics.Rows.ContainsKey("Similarity (exact)")) { 230 DataRow row = new DataRow("Similarity (exact)", ""); 231 row.VisualProperties.StartIndexZero = true; 226 var row = new DataRow("Similarity (exact)", "") { VisualProperties = { StartIndexZero = true } }; 232 227 FragmentStatistics.Rows.Add(row); 233 228 } 234 229 FragmentStatistics.Rows["Similarity (exact)"].Values.Add(s1); 235 // high similarity236 //if (!FragmentStatistics.Rows.ContainsKey("Similarity (high)")) {237 // DataRow row = new DataRow("Similarity (high)", "");238 // row.VisualProperties.StartIndexZero = true;239 // FragmentStatistics.Rows.Add(row);240 //}241 //FragmentStatistics.Rows["Similarity (high)"].Values.Add(s2);242 //// relaxed similarity243 //if (!FragmentStatistics.Rows.ContainsKey("Similarity (relaxed)")) {244 // DataRow row = new DataRow("Similarity (relaxed)", "");245 // row.VisualProperties.StartIndexZero = true;246 // FragmentStatistics.Rows.Add(row);247 //}248 //FragmentStatistics.Rows["Similarity (relaxed)"].Values.Add(s3);249 230 #endregion 250 231 … … 252 233 253 234 // clear the global maps to save memory 254 GlobalCloneMap.Clear(); 255 GlobalTraceMap.Clear(); 235 if (this.Successor == null || !(this.Successor is SymbolicExpressionTreeGenealogyAnalyzer)) { 236 GlobalCloneMap.Clear(); 237 GlobalTraceMap.Clear(); 238 } 256 239 GlobalFragmentMap.Clear(); 257 240 } … … 315 298 #endregion 316 299 300 /// <summary> 301 /// Provide a measure of how similar the fragments that get passed by crossover from one generation to the next are. 302 /// </summary> 303 /// <param name="fragments">The symbolic expression tree fragments</param> 304 /// <param name="mode">The similarity mode (0 - exact, 1 - high, 2 - relaxed)</param> 305 /// <returns>The average number of similar fragments</returns> 317 306 private double CalculateSimilarity(List<ISymbolicExpressionTreeNode> fragments, int mode) { 318 bool[]visited = new bool[fragments.Count];319 int count= 0;307 var visited = new bool[fragments.Count]; 308 int groups = 0; 320 309 for (int i = 0; i != fragments.Count - 1; ++i) { 321 310 if (visited[i]) continue; … … 324 313 if (fragments[i].IsSimilarTo(fragments[j], mode)) visited[j] = true; 325 314 } 326 ++ count;327 } 328 return (double)fragments.Count / count;315 ++groups; 316 } 317 return (double)fragments.Count / groups; 329 318 } 330 319 #endregion -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeGenealogyAnalyzer.cs
r7785 r7792 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Drawing; 25 24 using System.Globalization; … … 219 218 Results.Add(new Result(PopulationGraphResultParameterName, PopulationGraphResultParameterDescription, graph)); 220 219 } else { 221 //graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphResultParameterName].Value;222 220 graph = (SymbolicExpressionTreeGenealogyGraph)(Results[PopulationGraphResultParameterName].Value); 223 221 } … … 233 231 string label; 234 232 if (generation == 0) { 235 // at generation 0 no trace map is present (since no reproduction has taken place yet), so we only add the initial trees as nodes in the genealogy graph 233 // at generation 0 no trace map is present (since no reproduction has taken place yet), 234 // so we only add the initial individuals as nodes in the genealogy graph 236 235 for (int i = 0; i != qualities.Count; ++i) { 237 236 var tree = qualities.ElementAt(i).Key; … … 246 245 var child = qualities.ElementAt(i).Key; 247 246 label = (generation * qualities.Count + i + 1).ToString(CultureInfo.InvariantCulture); 248 if (!graph.HasNode(child)) graph.AddNode(child, qualities[child], label, generation, i < Elites.Value); 247 if (!graph.HasNode(child)) 248 graph.AddNode(child, qualities[child], label, generation, i < Elites.Value); 249 249 if (!GlobalTraceMap.ContainsKey(child)) continue; 250 250 var parents = GlobalTraceMap[child]; … … 264 264 265 265 // clear trace and clone maps in preparation for the next generation 266 GlobalTraceMap.Clear(); 267 GlobalCloneMap.Clear(); 266 if (this.Successor == null || !(this.Successor is SymbolicExpressionTreeFragmentsAnalyzer)) { 267 GlobalTraceMap.Clear(); 268 GlobalCloneMap.Clear(); 269 } 268 270 269 271 #region end of the run (code for writing results to dot files) … … 315 317 } 316 318 317 319 318 320 319 321 private double Evaluate(ISymbolicExpressionTree tree) { -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenealogyGraph.cs
r7785 r7792 95 95 /// <param name="rank">The node rank</param> 96 96 /// <param name="elite">Specifies if this node is an elite</param> 97 public void AddNode(T t, double quality = 0.0, string label = "", double rank = 0, bool elite = false , int cutIndex = -1) {97 public void AddNode(T t, double quality = 0.0, string label = "", double rank = 0, bool elite = false) { 98 98 if (HasNode(t)) return; 99 _dictionary[t] = new GenealogyGraphNode { Data = t, Quality = quality, Label = label, IsElite = elite, Rank = rank , CutpointIndex = cutIndex};99 _dictionary[t] = new GenealogyGraphNode { Data = t, Quality = quality, Label = label, IsElite = elite, Rank = rank }; 100 100 } 101 101 … … 103 103 /// more of a low level method to minimize memory usage when creating and returning subgraphs 104 104 /// </summary> 105 /// <param name="n ">The node to be added</param>106 public void AddNode(GenealogyGraphNode n ) {107 var t = (T)n .Data;105 /// <param name="node">The node to be added</param> 106 public void AddNode(GenealogyGraphNode node) { 107 var t = (T)node.Data; 108 108 if (HasNode(t)) return; 109 _dictionary[t] = n ;109 _dictionary[t] = node; 110 110 } 111 111 … … 116 116 public void RemoveNode(T t) { 117 117 if (!_dictionary.ContainsKey(t)) return; 118 GenealogyGraphNode n= _dictionary[t];119 if (n .InEdges != null) {120 foreach (var e in n .InEdges.Where(e => e.Target.OutEdges != null)) {121 e.Target.OutEdges.RemoveAll(arc => arc.Target == n );118 var node = _dictionary[t]; 119 if (node.InEdges != null) { 120 foreach (var e in node.InEdges.Where(e => e.Target.OutEdges != null)) { 121 e.Target.OutEdges.RemoveAll(arc => arc.Target == node); 122 122 if (!e.Target.OutEdges.Any()) 123 123 e.Target.OutEdges = null; // set to null to be a little more memory efficient 124 124 } 125 n .InEdges = null;126 } 127 if (n .OutEdges != null) {128 foreach (var e in n .OutEdges.Where(e => e.Target.InEdges != null)) {129 e.Target.InEdges.RemoveAll(arc => arc.Target == n );125 node.InEdges = null; 126 } 127 if (node.OutEdges != null) { 128 foreach (var e in node.OutEdges.Where(e => e.Target.InEdges != null)) { 129 e.Target.InEdges.RemoveAll(arc => arc.Target == node); 130 130 if (!e.Target.InEdges.Any()) 131 131 e.Target.InEdges = null; // set to null to be a little more memory efficient 132 132 } 133 n .OutEdges = null;133 node.OutEdges = null; 134 134 } 135 135 _dictionary.Remove(t); … … 145 145 /// <param name="a"></param> 146 146 /// <param name="b"></param> 147 /// <param name="label"></param> 148 public void AddArc(T a, T b, int opType = 0) { 147 public void AddArc(T a, T b) { 149 148 GenealogyGraphNode src, dest; 150 149 if (!HasNode(a)) { … … 161 160 } 162 161 // add forward and reverse arcs 163 src.AddForwardArc(dest , opType);164 dest.AddReverseArc(src , opType);165 } 166 167 public void AddArcs(T[] a, T b , int opType = 0) {162 src.AddForwardArc(dest); 163 dest.AddReverseArc(src); 164 } 165 166 public void AddArcs(T[] a, T b) { 168 167 GenealogyGraphNode src, dest; 169 168 if (!HasNode(b)) { … … 180 179 src = _dictionary[o]; 181 180 } 182 src.AddForwardArc(dest , opType);183 dest.AddReverseArc(src , opType);181 src.AddForwardArc(dest); 182 dest.AddReverseArc(src); 184 183 } 185 184 } … … 193 192 public double Quality { get; set; } 194 193 public double Rank { get; set; } 195 public int CutpointIndex { get; set; }196 194 public List<GenealogyGraphArc> InEdges { get; set; } 197 195 public List<GenealogyGraphArc> OutEdges { get; set; } … … 210 208 InEdges = node.InEdges; 211 209 OutEdges = node.OutEdges; 212 CutpointIndex = node.CutpointIndex; 213 } 214 215 /// <summary> 216 /// Returns all the ancestors of the current node 217 /// </summary> 218 /// <returns></returns> 210 } 211 212 /// <summary> 213 /// Performs an upward-breath-traversal of the genealogy graph using the nodes InEdges 214 /// </summary> 215 /// <returns>All the ancestors of the current node</returns> 219 216 public IEnumerable<GenealogyGraphNode> Ancestors() { 220 217 var nodes = new HashSet<GenealogyGraphNode> { this }; … … 236 233 237 234 /// <summary> 238 /// Returns all the descendants of the current node (identical to above method except for using the OutEdges)239 /// </summary> 240 /// <returns> </returns>235 /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutEdges 236 /// </summary> 237 /// <returns>All the descendants of the current node</returns> 241 238 public IEnumerable<GenealogyGraphNode> Descendants() { 242 239 var nodes = new HashSet<GenealogyGraphNode> { this }; … … 257 254 } 258 255 259 public void AddForwardArc(GenealogyGraphNode target , int opType) {260 var e = new GenealogyGraphArc { Target = target , OperatorType = opType};256 public void AddForwardArc(GenealogyGraphNode target) { 257 var e = new GenealogyGraphArc { Target = target }; 261 258 if (OutEdges == null) OutEdges = new List<GenealogyGraphArc> { e }; 262 259 else OutEdges.Add(e); 263 260 } 264 261 265 public void AddReverseArc(GenealogyGraphNode target , int opType) {266 var e = new GenealogyGraphArc { Target = target , OperatorType = opType};262 public void AddReverseArc(GenealogyGraphNode target) { 263 var e = new GenealogyGraphArc { Target = target }; 267 264 if (InEdges == null) InEdges = new List<GenealogyGraphArc> { e }; 268 265 else InEdges.Add(e); … … 289 286 public class GenealogyGraphArc { 290 287 public GenealogyGraphNode Target { get; set; } 291 // OperationType is an identifier for the genetic operation (mutation, crossover) that a graph arc will represent292 // So basically it describes which operator was applied to an individual/graph node (the one emitting the arc),293 // while Target represents the end result of that operator (node ==GeneticOperation==> Target)294 public int OperatorType { get; set; }295 288 // these two fields are not used, but they might be useful later 296 289 public string Label { get; set; } // might be useful later -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Interfaces/IGenealogyGraph.cs
r7779 r7792 30 30 bool Any(Func<KeyValuePair<T, GenealogyGraphNode>, bool> predicate); // graph contains any nodes matching the given predicate? 31 31 void Clear(); // clear graph 32 void AddNode(T t, double quality = 0.0, string label = "", double rank = 0, bool elite = false , int cutIndex = -1);32 void AddNode(T t, double quality = 0.0, string label = "", double rank = 0, bool elite = false); 33 33 void RemoveNode(T t); // remove node if contained in the graph 34 34 GenealogyGraphNode GetNode(T t); // return node corresponding to object t, or null 35 35 // arc operation 36 void AddArc(T source, T target , int opType = 0);37 void AddArcs(T[] a, T b , int opType = 0);36 void AddArc(T source, T target); 37 void AddArcs(T[] a, T b); 38 38 } 39 39 }
Note: See TracChangeset
for help on using the changeset viewer.