Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/17 01:15:25 (7 years ago)
Author:
pkimmesw
Message:

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs

    r14834 r14875  
    44  using System.Linq;
    55
    6   using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
    7   using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
    8   using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
     6  using Attributes;
     7  using Data.Pool;
     8  using Stack;
    99
    1010  public static class ExpressionTable {
    11     public static readonly IDictionary<string, Expression> StatelessExpressionTable;
    12     public static readonly IDictionary<string, Func<Expression>> StatefulExpressionFactory;
    13     public static readonly IDictionary<StackTypes, IList<string>> StackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>();
    14     public static readonly IDictionary<int, string> IndexToNameTable = new Dictionary<int, string>();
    15     public static readonly IDictionary<Type, string> TypeToNameTable = new Dictionary<Type, string>();
    16     public static readonly IDictionary<Type, PushExpressionAttribute> TypeToAttributeTable = new Dictionary<Type, PushExpressionAttribute>();
    17     public static readonly IDictionary<StackTypes, IList<string>> StackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>();
    18     public static readonly string[] ExpressionNames;
    19 
    20     public static readonly Type[] StatelessExpressionTypes;
    21     public static readonly Type[] StatefulExpressionTypes;
    22 
    23     public static readonly int Count;
     11    public static readonly IReadOnlyDictionary<string, Expression> StatelessExpressionTable;
     12    public static readonly IReadOnlyDictionary<string, Func<Expression>> StatefulExpressionFactory;
     13    public static readonly IReadOnlyList<string> ExpressionNames;
     14    public static readonly int ExpressionCount;
     15
     16    private static readonly Dictionary<int, string> indexToNameTable = new Dictionary<int, string>();
     17    private static readonly Dictionary<Type, string> typeToNameTable = new Dictionary<Type, string>();
     18    private static readonly Dictionary<string, Type> nameToTypeTable = new Dictionary<string, Type>();
     19    private static readonly Dictionary<StackTypes, IList<string>> stackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>();
     20    private static readonly Dictionary<StackTypes, IList<string>> stackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>();
     21    private static readonly Dictionary<Type, PushExpressionAttribute> typeToAttributeTable = new Dictionary<Type, PushExpressionAttribute>();
     22
     23    public static readonly IReadOnlyList<Type> StatelessExpressionTypes;
     24    public static readonly IReadOnlyList<Type> StatefulExpressionTypes;
    2425
    2526    static ExpressionTable() {
     
    3132      ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefulExpressionFactory.Keys).ToArray();
    3233
    33       Count = StatelessExpressionTable.Count + StatefulExpressionFactory.Count;
    34     }
    35 
    36     public static int StatelessCount
    37     {
    38       get
    39       {
    40         return StatelessExpressionTable.Count;
    41       }
    42     }
    43 
    44     public static int StatefullCount
    45     {
    46       get
    47       {
    48         return StatefulExpressionFactory.Count;
    49       }
    50     }
     34      ExpressionCount = StatelessExpressionTable.Count + StatefulExpressionFactory.Count;
     35    }
     36
     37    public static IReadOnlyDictionary<int, string> IndexToNameTable { get { return indexToNameTable; } }
     38    public static IReadOnlyDictionary<Type, string> TypeToNameTable { get { return typeToNameTable; } }
     39    public static IReadOnlyDictionary<string, Type> NameToTypeTable { get { return nameToTypeTable; } }
     40    public static IReadOnlyDictionary<StackTypes, IList<string>> StackTypeToNamesTable { get { return stackTypeToNamesTable; } }
     41    public static IReadOnlyDictionary<StackTypes, IList<string>> StackDependencyToNamesTable { get { return stackDependencyToNamesTable; } }
     42    public static IReadOnlyDictionary<Type, PushExpressionAttribute> TypeToAttributeTable { get { return typeToAttributeTable; } }
    5143
    5244    private static Dictionary<string, Expression> GetStatelessExpressionTable() {
     
    5749        var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
    5850
    59         TypeToAttributeTable.Add(type, attribute);
     51        typeToAttributeTable.Add(type, attribute);
    6052        dictionary.Add(attribute.ExpressionName, expression);
    61         IndexToNameTable.Add(IndexToNameTable.Keys.Count, attribute.ExpressionName);
    62         TypeToNameTable.Add(type, attribute.ExpressionName);
    63 
    64         if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) {
    65           StackTypeToNamesTable.Add(attribute.StackType, new List<string>());
    66         }
    67 
    68         StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
     53        indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName);
     54        typeToNameTable.Add(type, attribute.ExpressionName);
     55        nameToTypeTable.Add(attribute.ExpressionName, type);
     56
     57        if (!stackTypeToNamesTable.ContainsKey(attribute.StackType)) {
     58          stackTypeToNamesTable.Add(attribute.StackType, new List<string>());
     59        }
     60
     61        stackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
    6962
    7063        var dependencies = attribute.StackType | attribute.AdditionalStackDependencies;
    71         if (!StackDependencyToNamesTable.ContainsKey(dependencies)) {
    72           StackDependencyToNamesTable.Add(dependencies, new List<string>());
    73         }
    74 
    75         StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
     64        if (!stackDependencyToNamesTable.ContainsKey(dependencies)) {
     65          stackDependencyToNamesTable.Add(dependencies, new List<string>());
     66        }
     67
     68        stackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
    7669      }
    7770
     
    9083        var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
    9184
    92         TypeToAttributeTable.Add(type, attribute);
     85        typeToAttributeTable.Add(type, attribute);
    9386        dictionary.Add(attribute.ExpressionName, creator);
    94         IndexToNameTable.Add(IndexToNameTable.Keys.Count, attribute.ExpressionName);
    95         TypeToNameTable.Add(type, attribute.ExpressionName);
    96 
    97         if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) {
    98           StackTypeToNamesTable.Add(attribute.StackType, new List<string>());
    99         }
    100 
    101         StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
     87        indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName);
     88        typeToNameTable.Add(type, attribute.ExpressionName);
     89        nameToTypeTable.Add(attribute.ExpressionName, type);
     90
     91        if (!stackTypeToNamesTable.ContainsKey(attribute.StackType)) {
     92          stackTypeToNamesTable.Add(attribute.StackType, new List<string>());
     93        }
     94
     95        stackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
    10296
    10397        var dependencies = attribute.StackType | attribute.AdditionalStackDependencies;
    104         if (!StackDependencyToNamesTable.ContainsKey(dependencies)) {
    105           StackDependencyToNamesTable.Add(dependencies, new List<string>());
    106         }
    107 
    108         StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
     98        if (!stackDependencyToNamesTable.ContainsKey(dependencies)) {
     99          stackDependencyToNamesTable.Add(dependencies, new List<string>());
     100        }
     101
     102        stackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
    109103      }
    110104
     
    146140
    147141    public static IReadOnlyList<string> GetExpressionsByStackTypes(StackTypes allowedTypes) {
    148       return StackDependencyToNamesTable
     142      return stackDependencyToNamesTable
    149143          .Where(entry => (entry.Key & ~allowedTypes) == StackTypes.None)
    150144          .SelectMany(entry => entry.Value)
     
    163157
    164158    public static Expression GetExpression(int index) {
    165       return GetExpression(IndexToNameTable[index]);
     159      return GetExpression(indexToNameTable[index]);
     160    }
     161
     162    public static Expression GetExpression(Type type) {
     163      var name = TypeToNameTable[type];
     164      return GetExpression(name);
    166165    }
    167166
     
    177176
    178177    public static Expression GetStatelessExpression<T>() where T : StatelessExpression {
    179       return GetStatelessExpression(TypeToNameTable[typeof(T)]);
     178      return GetStatelessExpression(typeToNameTable[typeof(T)]);
    180179    }
    181180
     
    198197
    199198    public static Expression GetStatefulExpression<T>() {
    200       return GetStatefulExpression(TypeToNameTable[typeof(T)]);
     199      return GetStatefulExpression(typeToNameTable[typeof(T)]);
    201200    }
    202201
Note: See TracChangeset for help on using the changeset viewer.