Changeset 9238 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/SymbolicExpressionTreeGenealogyGraphBuilder.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/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.