Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/21/13 16:02:47 (11 years ago)
Author:
bburlacu
Message:

#1772: Added base class for the fragment analyzers. Improved analyzers, added SymbolicExpressionTreeRelativeFragmentDepthAnalyzer. Added LineageExplorer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/SymbolicExpressionTreeGenealogyGraphBuilder.cs

    r9082 r9238  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using System.Threading;
     
    6362    private const string SymbolicExpressionTreeQualityParameterName = "Quality";
    6463    #endregion
    65     #region Parameter properties
     64    #region Parameters
    6665    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
    6766      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     
    109108    }
    110109    #endregion
    111     #region Properties
     110    #region Parameter properties
    112111    public bool EnabledByDefault {
    113112      get { return true; }
     
    189188
    190189    public override IOperation Apply() {
    191       if (GlobalTraceMap == null) return base.Apply(); // no trace map, no genealogy graph
    192 
    193190      if (!Parameters.ContainsKey("Results")) return base.Apply();
    194191
     
    211208      var pairs = trees.Zip(qualities, (t, q) => new { Tree = t, Quality = q.Value }).OrderByDescending(x => x.Quality).ToList();
    212209
    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();
    226244
    227245      foreach (var node in graphNodes) {
     
    229247      }
    230248
     249      graphNodes.Sort((a, b) => b.Rank.CompareTo(a.Rank));
     250
    231251      foreach (var node in graphNodes) {
    232252        var tree = node.SymbolicExpressionTree;
    233         if (!GlobalTraceMap.ContainsKey(tree)) continue;
     253        if (!GlobalTraceMap.ContainsKey(tree)) {
     254          continue;
     255        }
    234256        var parents = GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>();
    235257        foreach (var p in parents) {
    236258          var nodes = graph.GetGraphNodes(p);
    237           if (nodes == null) continue;
     259          if (nodes == null) {
     260            throw new Exception("Node cannot have no parents");
     261          }
    238262          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          }
    240266          var arc = new Arc { Source = sourceNode, Target = node, Data = GlobalFragmentMap[tree] };
    241267          sourceNode.AddForwardArc(arc);
     
    249275    /// Evaluate a symbolic expression tree. We perform evaluation by adding a temporary subscope with the tree in it, and calling evaluator.Apply()
    250276    /// </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>
    253279    private double Evaluate(IItem tree) {
    254280      var subScope = new Scope();
Note: See TracChangeset for help on using the changeset viewer.