Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7788


Ignore:
Timestamp:
05/09/12 17:29:33 (13 years ago)
Author:
bburlacu
Message:

#1772: Fixed bug in fragment tracing.

Location:
branches/HeuristicLab.EvolutionaryTracking
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/TracingSymbolicExpressionTreeCrossover.cs

    r7522 r7788  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Parameters;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3435  /// A base class for operators that perform a crossover of symbolic expression trees.
    3536  /// </summary>
    36   [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
     37  [Item("SymbolicExpressionTreeCrossover",
     38    "A base class for operators that perform a crossover of symbolic expression trees.")]
    3739  [StorableClass]
    3840  public abstract class TracingSymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
     
    4143    private const string GlobalTraceMapParameterName = "GlobalTraceMap";
    4244    private const string GlobalCloneMapParameterName = "GlobalCloneMap";
     45    private const string GlobalFragmentMapParameterName = "GlobalFragmentMap";
     46
    4347    #region Parameter Properties
     48
    4449    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
    4550      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
    4651    }
     52
    4753    public ILookupParameter<ISymbolicExpressionTree> ChildParameter {
    4854      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; }
    4955    }
     56
    5057    public LookupParameter<CloneMapType> GlobalCloneMapParameter {
    5158      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
    5259    }
     60
    5361    public LookupParameter<TraceMapType> GlobalTraceMapParameter {
    5462      get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
    5563    }
     64
     65    public LookupParameter<CloneMapType> GlobalFragmentMapParameter {
     66      get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }
     67    }
     68
    5669    #endregion
     70
    5771    #region Properties
     72
    5873    public ItemArray<ISymbolicExpressionTree> Parents {
    5974      get { return ParentsParameter.ActualValue; }
    6075    }
     76
    6177    public ISymbolicExpressionTree Child {
    6278      get { return ChildParameter.ActualValue; }
    6379      set { ChildParameter.ActualValue = value; }
    6480    }
     81
    6582    public CloneMapType GlobalCloneMap {
    6683      get { return GlobalCloneMapParameter.ActualValue; }
    6784    }
     85
     86    public CloneMapType GlobalFragmentMap {
     87      get { return GlobalFragmentMapParameter.ActualValue; }
     88    }
     89
    6890    public TraceMapType GlobalTraceMap {
    6991      get { return GlobalTraceMapParameter.ActualValue; }
    7092    }
     93
    7194    #endregion
     95
    7296    [StorableConstructor]
    73     protected TracingSymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { }
    74     protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { }
     97    protected TracingSymbolicExpressionTreeCrossover(bool deserializing)
     98      : base(deserializing) {
     99    }
     100
     101    protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner)
     102      : base(original, cloner) {
     103    }
     104
    75105    protected TracingSymbolicExpressionTreeCrossover()
    76106      : base() {
    77       Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
    78       Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
    79       Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
    80       Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing tracing info."));
     107      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName,
     108                                                                           "The parent symbolic expression trees which should be crossed."));
     109      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName,
     110                                                                  "The child symbolic expression tree resulting from the crossover."));
     111      Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName,
     112                                                       "A global map keeping track of trees and their clones (made during selection)."));
     113      Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName,
     114                                                       "A global map keeping track of tree fragments received via crossover."));
     115      Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName,
     116                                                       "A global cache containing tracing info."));
    81117    }
    82118
     
    85121        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
    86122      // add global trace cache if not already present in global scope
    87       if (GlobalTraceMap == null) {
    88         var gScope = ExecutionContext.Scope;
    89         while (gScope.Parent != null) gScope = gScope.Parent;
     123      var gScope = ExecutionContext.Scope;
     124      while (gScope.Parent != null) gScope = gScope.Parent;
     125      if (GlobalTraceMap == null)
    90126        gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType()));
    91       }
     127      if (GlobalFragmentMap == null)
     128        gScope.Variables.Add(new Variable(GlobalFragmentMapParameterName, new CloneMapType()));
     129      // get original parents
    92130      var originalParents = new ItemList<IItem>(Parents.Select(x => GlobalCloneMap[x]));
    93       ISymbolicExpressionTree result = Crossover(Random, Parents[0], Parents[1]);
    94       Child = result;
    95       GlobalTraceMap.Add(Child, originalParents);
    96 
     131      // save the nodes of parent0 in a list so we can track what modifications are made by crossover
     132      var nodes0 = Parents[0].IterateNodes().ToList();
     133      //perform crossover
     134      Child = Crossover(Random, Parents[0], Parents[1]);
     135      // create another list of parent0's nodes, so we can compare it with the first list to see what changed
     136      var nodes1 = Child.IterateNodes().ToList();
     137      // compare the two nodes lists and check the difference (comparing node references so we avoid false functional identity).
     138      // if no difference is found, then it is assumed that the whole tree was swapped with itself (it can happen), so the index is 0
     139      int i, min = Math.Max(nodes0.Count, nodes1.Count);
     140      for (i = 0; i != min; ++i)
     141        if (nodes0[i] != nodes1[i]) break;
     142      if (i == min) i = 0;
     143      GlobalTraceMap[Child] = originalParents;
     144      GlobalFragmentMap[Child] = new IntValue(i);
    97145      return base.Apply();
    98146    }
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj.user

    r7785 r7788  
    77  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
    88    <StartAction>Program</StartAction>
    9     <StartProgram>C:\Users\Bogdan\Desktop\Trunk\sources\bin\HeuristicLab 3.3.exe</StartProgram>
     9    <StartProgram>C:\Users\bburlacu\Desktop\HL-Core\trunk\sources\bin\HeuristicLab 3.3.exe</StartProgram>
    1010  </PropertyGroup>
    1111  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Plugin.cs

    r7785 r7788  
    2626
    2727namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    28   [Plugin("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","Provides operators and related classes for the symbolic expression tree encoding.", "3.4.2.7779")]
     28  [Plugin("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","Provides operators and related classes for the symbolic expression tree encoding.", "3.4.2.7785")]
    2929  [PluginFile("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll", PluginFileType.Assembly)]
    3030  [PluginDependency("HeuristicLab.Analysis", "3.3")]
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeMatching.cs

    r7785 r7788  
    9292    // convenience methods for less typing :)
    9393    private static IEnumerator<ISymbolicExpressionTreeNode> Enumerator(this ISymbolicExpressionTree tree) {
    94       return tree.IterateNodesPostfix().GetEnumerator();
     94      return tree.IterateNodes().GetEnumerator();
    9595    }
    9696    private static IEnumerator<ISymbolicExpressionTreeNode> Enumerator(this ISymbolicExpressionTreeNode tree) {
    97       return tree.IterateNodesPostfix().GetEnumerator();
     97      return tree.IterateNodes().GetEnumerator();
    9898    }
    9999    // method for breath-width iteration of nodes
    100     private static IEnumerable<ISymbolicExpressionTreeNode> IterateNodes(this ISymbolicExpressionTree tree) {
     100    public static IEnumerable<ISymbolicExpressionTreeNode> IterateNodes(this ISymbolicExpressionTree tree) {
    101101      return IterateNodes(tree.Root);
    102102    }
     
    128128    /// <returns>A symbolic expression tree node representing the difference fragment between parent and child</returns>
    129129    public static ISymbolicExpressionTreeNode GetFragmentDiff(ISymbolicExpressionTree parent, ISymbolicExpressionTree child) {
    130       var e1 = parent.Enumerator();
    131       var e2 = child.Enumerator();
     130      var nodes1 = parent.IterateNodes().ToList();
     131      var nodes2 = child.IterateNodes().ToList();
     132      var e1 = nodes1.AsEnumerable().GetEnumerator();
     133      var e2 = nodes2.AsEnumerable().GetEnumerator();
    132134      while (e1.MoveNext() && e2.MoveNext()) {
    133135        var comparer = new SymbolicExpressionTreeNodeComparer((int)SimilarityLevel.Exact);
    134         if (!comparer.Equals(e1.Current, e2.Current)) { return e2.Current; } // return the fragment by which child differs from parent
     136        if (!comparer.Equals(e1.Current, e2.Current)) {
     137          return e2.Current;
     138        }
     139        //if (!e1.Current.IterateNodes().SequenceEqual(e2.Current.IterateNodes(), comparer)) {
     140        //  return e2.Current;
     141        //} // return the fragment by which child differs from parent
    135142      }
    136143      return null;
     
    145152      var comparer = new SymbolicExpressionTreeNodeComparer(mode);
    146153      if (patlen == seqlen) return seq.SequenceEqual(pat, comparer) ? 0 : -1;
    147       //int i = patlen;           
    148       //while (i <= seqlen) {
    149       //  var ch = seq[i - 1];
    150       //  if (comparer.Equals(ch, pat.Last()))
    151       //    if (seq.Skip(i - patlen).Take(patlen).SequenceEqual(pat, comparer))
    152       //      return i - patlen;
    153       //  ++i;
    154       //}
    155154      for (int i = patlen; i <= seqlen; ++i) {
    156155        if (comparer.Equals(seq[i - 1], pat.Last())) // first check if last element is a match
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeFragmentsAnalyzer.cs

    r7785 r7788  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Text;
    2624using HeuristicLab.Analysis;
    2725using HeuristicLab.Common;
     
    3331using HeuristicLab.Parameters;
    3432using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    35 using HeuristicLab.Problems.DataAnalysis;
    36 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    3733// type definitions for ease of use
    3834using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
     
    5248    private const string GlobalTraceMapParameterName = "GlobalTraceMap";
    5349    private const string GlobalCloneMapParameterName = "GlobalCloneMap";
     50    private const string GlobalFragmentMapParameterName = "GlobalFragmentMap";
    5451    private const string GenerationsParameterName = "Generations";
    5552    private const string FragmentStatisticsParameterName = "FragmentStatistics";
     
    7168      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
    7269    }
     70    public LookupParameter<CloneMapType> GlobalFragmentMapParameter {
     71      get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }
     72    }
    7373    public LookupParameter<IntValue> GenerationsParameter {
    7474      get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
     
    7676    public LookupParameter<DataTable> FragmentStatisticsParameter {
    7777      get { return (LookupParameter<DataTable>)Parameters[FragmentStatisticsParameterName]; }
    78 
    7978    }
    8079    #endregion
     
    9897    public TraceMapType GlobalTraceMap {
    9998      get { return GlobalTraceMapParameter.ActualValue; }
     99    }
     100    public CloneMapType GlobalFragmentMap {
     101      get { return GlobalFragmentMapParameter.ActualValue; }
    100102    }
    101103    public IntValue Generations {
     
    123125      Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing the whole genealogy."));
    124126      Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
     127      Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, "A global map keeping track of tree fragments received via crossover."));
    125128      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
    126129      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
     
    159162        ResultCollection results = ResultsParameter.ActualValue;
    160163        if (FragmentStatistics == null) {
    161           FragmentStatisticsParameter.ActualValue = new DataTable("Fragment Statistics", "Statistical measurements of fragments aggregated over the whole population");
     164          FragmentStatisticsParameter.ActualValue = new DataTable("Fragment Statistics",
     165                                                                  "Statistical measurements of fragments aggregated over the whole population");
    162166          FragmentStatistics.VisualProperties.YAxisTitle = "Fragment length/Similarities";
    163167          results.Add(new Result("Fragment Statistics", FragmentStatistics));
    164168        }
    165 
    166         int numberOfValues = FragmentStatistics.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
    167169
    168170        UpdateCounter.Value = 0; // reset counter
     
    171173
    172174        #region Fragment Statistics
    173         // write these to file for now, later we will use a DataTable and display the results inside HeuristicLab
    174         var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    175         //using (var file = System.IO.File.AppendText(path + @"\tracking.csv")) {
    176175        var fragments = new List<ISymbolicExpressionTreeNode>();
    177176        var parents = new List<ISymbolicExpressionTree>();
    178         double best = gScope.SubScopes.Select(x => x.Variables["Quality"].Value as DoubleValue).Max(x => x.Value);
    179177        var trees = (from s in gScope.SubScopes select s.Variables.First().Value as ISymbolicExpressionTree).ToList();
    180178        foreach (var tree in trees.Where(x => GlobalTraceMap.ContainsKey(x))) {
    181           var parent0 = (ISymbolicExpressionTree)GlobalTraceMap[tree][0];
    182           var fragment = SymbolicExpressionTreeMatching.GetFragmentDiff(parent0, tree);
    183           if (fragment != null) {
    184             parents.Add(parent0);
    185             fragments.Add(fragment);
    186             if (GlobalTraceMap.ContainsKey(parent0)) {
    187               var p = (ISymbolicExpressionTree)GlobalTraceMap[parent0][0];
    188               fragment = SymbolicExpressionTreeMatching.GetFragmentDiff(p, parent0);
    189               if (fragment != null) {
    190                 parents.Add(p);
    191                 fragments.Add(fragment);
     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                }
    192193              }
    193194            }
     
    200201        double a3 = parents.Average(x => x.Length);
    201202        double s1 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.Exact);
    202         double s2 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.High);
    203         double s3 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.Relaxed);
     203        //double s2 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.High);
     204        //double s3 = CalculateSimilarity(fragments, (int)SymbolicExpressionTreeMatching.SimilarityLevel.Relaxed);
    204205
    205206        #region Table data
     
    232233        }
    233234        FragmentStatistics.Rows["Similarity (exact)"].Values.Add(s1);
    234         // exact similarity
    235         if (!FragmentStatistics.Rows.ContainsKey("Similarity (high)")) {
    236           DataRow row = new DataRow("Similarity (high)", "");
    237           row.VisualProperties.StartIndexZero = true;
    238           FragmentStatistics.Rows.Add(row);
    239         }
    240         FragmentStatistics.Rows["Similarity (high)"].Values.Add(s2);
    241         // exact similarity
    242         if (!FragmentStatistics.Rows.ContainsKey("Similarity (relaxed)")) {
    243           DataRow row = new DataRow("Similarity (relaxed)", "");
    244           row.VisualProperties.StartIndexZero = true;
    245           FragmentStatistics.Rows.Add(row);
    246         }
    247         FragmentStatistics.Rows["Similarity (relaxed)"].Values.Add(s3);
    248 
    249         //file.WriteLine(best * 10.0 + " " + a1 + " " + a2 + " " + a3 + " " + s1 + " " + s2 + " " + s3);
     235        // high similarity
     236        //if (!FragmentStatistics.Rows.ContainsKey("Similarity (high)")) {
     237        //  DataRow row = new DataRow("Similarity (high)", "");
     238        //  row.VisualProperties.StartIndexZero = true;
     239        //  FragmentStatistics.Rows.Add(row);
    250240        //}
     241        //FragmentStatistics.Rows["Similarity (high)"].Values.Add(s2);
     242        //// relaxed similarity
     243        //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);
    251249        #endregion
     250
    252251        #endregion
     252
    253253        // clear the global maps to save memory
    254254        GlobalCloneMap.Clear();
    255255        GlobalTraceMap.Clear();
     256        GlobalFragmentMap.Clear();
    256257      }
    257258      return base.Apply();
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Selection/3.3/Plugin.cs

    r7785 r7788  
    2626  /// Plugin class for HeuristicLab.Selection plugin.
    2727  /// </summary>
    28   [Plugin("HeuristicLab.Selection", "3.3.6.7779")]
     28  [Plugin("HeuristicLab.Selection", "3.3.6.7785")]
    2929  [PluginFile("HeuristicLab.Selection-3.3.dll", PluginFileType.Assembly)]
    3030  [PluginDependency("HeuristicLab.Collections", "3.3")]
Note: See TracChangeset for help on using the changeset viewer.