Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8216


Ignore:
Timestamp:
07/04/12 18:00:00 (12 years ago)
Author:
jkarder
Message:

#1886:

  • added SchemaAnalyzer
  • added BrokenInteritanceSchemaAnalyzer
Location:
branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/BuildingBlockAnalyzer.cs

    r8191 r8216  
    2929
    3030namespace HeuristicLab.Analysis.AlgorithmBehavior {
     31  using System;
    3132  using System.Collections.Generic;
    3233  using HeuristicLab.Data;
     
    3738  /// An operator that analyzes building blocks.
    3839  /// </summary>
    39   [Item("BuildingBlockAnalyzer", "An operator that tracks the genealogy of every individual.")]
     40  [Item("BuildingBlockAnalyzer", "An operator that analyzes building blocks.")]
    4041  [StorableClass]
    4142  public sealed class BuildingBlockAnalyzer : SingleSuccessorOperator, IAnalyzer {
     
    4445    private const string PopulationGraphResultParameterName = "PopulationGraph";
    4546    private const string BuildingBlockSchemaMatrixParameterName = "BuildingBlockSchemaMatrix";
     47    private const string BrokenInteritanceSchemaOccurrenceName = "BrokenInteritanceSchemaOccurrenceMatrix";
     48    private const string UnbrokenInteritanceSchemaOccurrenceName = "UnbrokenInteritanceSchemaOccurrence";
    4649    private const string BuildingBlockPatternMatrixParameterName = "BuildingBlockPatternMatrix";
    4750
     
    100103            var categories = new int[3];
    101104            categories[0] = ExploreGlobalSchemaOccurrence(graph, schema);
    102             categories[1] = ExploreBrokenInteritanceSchemaOccurrence(graph, schema);
    103             categories[2] = ExploreUnbrokenInteritanceSchemaOccurrence(graph, schema);
     105            categories[1] = -1;
     106            categories[2] = -1;
    104107            schemataMatrix.Add(schema, categories);
    105108          }
     
    109112      // order schemas according to their global occurrence count
    110113      var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value);
    111 
    112       var stringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 4);
     114      IList<string> rowNames = new List<string>();
     115      var schemataStringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 3);
    113116      for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
    114117        var element = sortedSchemataMatrix.ElementAt(i);
    115         stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();
     118        rowNames.Add(new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString());
    116119        for (int j = 0; j < element.Value.Count(); j++) {
    117           stringMatrix[i, j + 1] = element.Value[j].ToString();
    118         }
    119       }
    120       stringMatrix.ColumnNames = new[] { "Schema", "GlobalSchemaOccurrence", "BrokenInteritanceSchemaOccurrence", "UnbrokenInteritanceSchemaOccurrence" };
    121 
    122       Results.Add(new Result(BuildingBlockSchemaMatrixParameterName, stringMatrix));
     120          schemataStringMatrix[i, j] = element.Value[j].ToString();
     121        }
     122      }
     123      schemataStringMatrix.RowNames = rowNames;
     124      schemataStringMatrix.ColumnNames = new[] { "GlobalSchemaOccurrence", "<unused>", "<unused>" };
     125      Results.Add(new Result(BuildingBlockSchemaMatrixParameterName, schemataStringMatrix));
     126
     127      var brokenInteritanceSchemaOccurrenceMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, generationZero.Count());
     128      IList<string> brokenRowNames = new List<string>();
     129      IList<string> brokenColumnNames = new List<string>();
     130      for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
     131        var entry = sortedSchemataMatrix.ElementAt(i);
     132        brokenRowNames.Add(new Permutation(PermutationTypes.RelativeUndirected, entry.Key).ToString());
     133        for (int j = 0; j < generationZero.Count(); j++) {
     134          var individual = generationZero.ElementAt(j);
     135          if (i == 0) brokenColumnNames.Add(((Permutation)individual.Data).ToString());
     136          brokenInteritanceSchemaOccurrenceMatrix[i, j] = CalculateBrokenInheritanceOccurrences(entry.Key, individual).ToString();
     137        }
     138      }
     139      brokenInteritanceSchemaOccurrenceMatrix.RowNames = brokenRowNames;
     140      brokenInteritanceSchemaOccurrenceMatrix.ColumnNames = brokenColumnNames;
     141      Results.Add(new Result(BrokenInteritanceSchemaOccurrenceName, brokenInteritanceSchemaOccurrenceMatrix));
     142
     143      var unbrokenInteritanceSchemaOccurrenceMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, generationZero.Count());
     144      IList<string> unbrokenRowNames = new List<string>();
     145      IList<string> unbrokenColumnNames = new List<string>();
     146      for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
     147        var entry = sortedSchemataMatrix.ElementAt(i);
     148        unbrokenRowNames.Add(new Permutation(PermutationTypes.RelativeUndirected, entry.Key).ToString());
     149        for (int j = 0; j < generationZero.Count(); j++) {
     150          var individual = generationZero.ElementAt(j);
     151          if (i == 0) unbrokenColumnNames.Add(((Permutation)individual.Data).ToString());
     152          unbrokenInteritanceSchemaOccurrenceMatrix[i, j] = CalculateUnbrokenInheritanceOccurrences(entry.Key, individual).ToString();
     153        }
     154      }
     155      unbrokenInteritanceSchemaOccurrenceMatrix.RowNames = unbrokenRowNames;
     156      unbrokenInteritanceSchemaOccurrenceMatrix.ColumnNames = brokenColumnNames;
     157      Results.Add(new Result(UnbrokenInteritanceSchemaOccurrenceName, unbrokenInteritanceSchemaOccurrenceMatrix));
    123158
    124159      return base.Apply();
     160    }
     161
     162    private int CalculateBrokenInheritanceOccurrences(int[] schema, GenealogyGraphNode individual) {
     163      // TODO
     164      return -1;
     165    }
     166
     167    private int CalculateUnbrokenInheritanceOccurrences(int[] schema, GenealogyGraphNode individual) {
     168      var filteredDescendants = individual.Descendants().Where(x => IsMatch(schema, ((Permutation)x.Data).ToArray()));
     169      return filteredDescendants.Any() ? (int)filteredDescendants.Max(x => x.Rank) : 0;
    125170    }
    126171
     
    130175        if (IsSubtour(schema, (Permutation)individual.Data)) occurrences++;
    131176      return occurrences;
    132     }
    133 
    134     private int ExploreBrokenInteritanceSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {
    135       // TODO
    136       return 0;
    137     }
    138 
    139     private int ExploreUnbrokenInteritanceSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {
    140       // TODO
    141       return 0;
    142177    }
    143178
     
    171206    }
    172207
     208    // attention: brute force matching
     209    // should be replaced with KMP, BM or RK
     210    private bool IsMatch(int[] pattern, int[] tour) {
     211      bool match = false;
     212      for (int i = 0; i <= tour.Length - pattern.Length && !match; i++) {
     213        for (int j = 0; j < pattern.Length && (match = pattern[j] == tour[i + j] || pattern[j] == -1); j++) ;
     214      }
     215      return match;
     216    }
     217
     218    private IList<int[]> GenerateWildcardSchemata(int[] schema) {
     219      string formatString = "{0:d" + schema.Length + "}";
     220      var combs = new List<bool[]>();
     221      string comb = string.Empty;
     222
     223      // create all desired wildcard combinations
     224      int i = 0;
     225      int wildcardCount = 0; // first wildcard combination contains no wildcards
     226      while (wildcardCount < schema.Length) {
     227        if (wildcardCount > schema.Length / 8 && wildcardCount < schema.Length / 2)
     228          combs.Add(comb.Select(x => x == '1').ToArray()); // '1' becomes true which indicates a wildcard
     229        wildcardCount = (comb = string.Format(formatString, Convert.ToString(++i, 2))).Count(x => x == '1');
     230      }
     231
     232      // create all desired wildcard instances
     233      var wildcardSchemata = new List<int[]>();
     234      foreach (var c in combs) {
     235        wildcardSchemata.Add(schema.Select((x, index) => c[index] ? -1 : x).ToArray()); // -1 is a wildcard in the schema
     236      }
     237
     238      return wildcardSchemata;
     239    }
     240
     241
    173242    private class SchemaEqualityComparer : EqualityComparer<int[]> {
    174243      public override bool Equals(int[] x, int[] y) {
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/GenealogyAnalyzer.cs

    r8161 r8216  
    202202          foreach (var parent in parents) {
    203203            if (GlobalTraceMap.ContainsKey(parent)) {
    204               // handle cycle
     204              // handle mutation
    205205              double quality = Evaluate(parent);
    206206              var node = new GenealogyGraphNode(parent) { Quality = quality, Label = label, Rank = generation - 0.5 };
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/HeuristicLab.Analysis.AlgorithmBehavior-3.3.csproj

    r8191 r8216  
    7575  <ItemGroup>
    7676    <Compile Include="Analyzers\BuildingBlockAnalyzer.cs" />
     77    <Compile Include="Analyzers\BrokenInteritanceSchemaAnalyzer.cs" />
     78    <Compile Include="Analyzers\SchemaAnalyzer.cs" />
    7779    <Compile Include="Analyzers\GenealogyAnalyzer.cs" />
    7880    <Compile Include="GenealogyGraph.cs" />
Note: See TracChangeset for help on using the changeset viewer.