Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/17 01:11:18 (7 years ago)
Author:
pkimmesw
Message:

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

File:
1 edited

Legend:

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

    r14746 r14777  
    1010  public static class ExpressionTable {
    1111    public static readonly IDictionary<string, Expression> StatelessExpressionTable;
    12     public static readonly IDictionary<string, Func<Expression>> StatefullExpressionFactory;
    13     public static readonly IDictionary<StackType, IList<string>> StackTypeToNamesTable = new Dictionary<StackType, IList<string>>();
     12    public static readonly IDictionary<string, Func<Expression>> StatefulExpressionFactory;
     13    public static readonly IDictionary<StackTypes, IList<string>> StackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>();
    1414    public static readonly IDictionary<int, string> IndexToNameTable = new Dictionary<int, string>();
    1515    public static readonly IDictionary<Type, string> TypeToNameTable = new Dictionary<Type, string>();
     16    public static readonly IDictionary<StackTypes, IList<string>> StackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>();
    1617    public static readonly string[] ExpressionNames;
    1718
     19    public static readonly Type[] StatelessExpressionTypes;
     20    public static readonly Type[] StatefulExpressionTypes;
     21
    1822    public static readonly int Count;
    1923
    2024    static ExpressionTable() {
     25      StatelessExpressionTypes = GetExpressionTypes(typeof(StatelessExpression)).ToArray();
     26      StatefulExpressionTypes = GetExpressionTypes(typeof(StatefulExpression<>)).ToArray();
     27
    2128      StatelessExpressionTable = GetStatelessExpressionTable();
    22       StatefullExpressionFactory = GetStatefullExpressionFactory();
    23       ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefullExpressionFactory.Keys).ToArray();
    24 
    25       Count = StatelessExpressionTable.Count + StatefullExpressionFactory.Count;
     29      StatefulExpressionFactory = GetStatefulExpressionFactory();
     30      ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefulExpressionFactory.Keys).ToArray();
     31
     32      Count = StatelessExpressionTable.Count + StatefulExpressionFactory.Count;
    2633    }
    2734
     
    3845      get
    3946      {
    40         return StatefullExpressionFactory.Count;
     47        return StatefulExpressionFactory.Count;
    4148      }
    4249    }
     
    4451    private static Dictionary<string, Expression> GetStatelessExpressionTable() {
    4552      var dictionary = new Dictionary<string, Expression>();
    46       var expressionTypes = GetExpressionTypes(typeof(StatelessExpression));
    47 
    48       foreach (var type in expressionTypes) {
     53
     54      foreach (var type in StatelessExpressionTypes) {
    4955        var expression = Activator.CreateInstance(type) as Expression;
    5056        var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
     
    5460        TypeToNameTable.Add(type, attribute.ExpressionName);
    5561
    56         if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) {
    57           StackTypeToNamesTable.Add(attribute.StackType, new List<string>());
    58         }
    59 
    60         StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
     62        if (!StackTypeToNamesTable.ContainsKey(attribute.StackTypes)) {
     63          StackTypeToNamesTable.Add(attribute.StackTypes, new List<string>());
     64        }
     65
     66        StackTypeToNamesTable[attribute.StackTypes].Add(attribute.ExpressionName);
     67
     68        var dependencies = attribute.StackTypes | attribute.AdditionalStackDependencies;
     69        if (!StackDependencyToNamesTable.ContainsKey(dependencies)) {
     70          StackDependencyToNamesTable.Add(dependencies, new List<string>());
     71        }
     72
     73        StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
    6174      }
    6275
     
    6477    }
    6578
    66     private static Dictionary<string, Func<Expression>> GetStatefullExpressionFactory() {
     79    private static Dictionary<string, Func<Expression>> GetStatefulExpressionFactory() {
    6780      var dictionary = new Dictionary<string, Func<Expression>>();
    68       var statefullExpressionType = typeof(StatefulExpression<>);
    69       var expressionTypes = GetExpressionTypes(statefullExpressionType);
    70 
    71       foreach (var type in expressionTypes) {
     81
     82      foreach (var type in StatefulExpressionTypes) {
    7283        // Make a NewExpression that calls the ctor
    7384        var newExp = System.Linq.Expressions.Expression.New(type);
     
    8192        TypeToNameTable.Add(type, attribute.ExpressionName);
    8293
    83         if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) {
    84           StackTypeToNamesTable.Add(attribute.StackType, new List<string>());
    85         }
    86 
    87         StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
     94        if (!StackTypeToNamesTable.ContainsKey(attribute.StackTypes)) {
     95          StackTypeToNamesTable.Add(attribute.StackTypes, new List<string>());
     96        }
     97
     98        StackTypeToNamesTable[attribute.StackTypes].Add(attribute.ExpressionName);
     99
     100        var dependencies = attribute.StackTypes | attribute.AdditionalStackDependencies;
     101        if (!StackDependencyToNamesTable.ContainsKey(dependencies)) {
     102          StackDependencyToNamesTable.Add(dependencies, new List<string>());
     103        }
     104
     105        StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
    88106      }
    89107
     
    124142    }
    125143
     144    public static IReadOnlyList<string> GetEnabledExpressionsByStackTypes(StackTypes allowedTypes) {
     145      return StackDependencyToNamesTable
     146          .Where(entry => (entry.Key & ~allowedTypes) == StackTypes.None)
     147          .SelectMany(entry => entry.Value)
     148          .ToArray();
     149    }
     150
    126151    public static PushProgram GetProgram(IManagedPool<PushProgram> pool, int[] index) {
    127152      var expressions = new Expression[index.Length];
     
    142167
    143168      if (!TryGetStatelessExpression(name, out expression) &&
    144           !TryGetStatefullExpression(name, out expression))
     169          !TryGetStatefulExpression(name, out expression))
    145170        throw new InvalidOperationException(string.Format("Expression with name {0} not found", name));
    146171
     
    153178
    154179    public static Expression GetStatelessExpression(string name) {
    155       Expression expression;
    156       if (StatelessExpressionTable.TryGetValue(name, out expression))
    157         return expression;
     180      if (StatelessExpressionTable.ContainsKey(name))
     181        return StatelessExpressionTable[name];
    158182      throw new NotSupportedException("Expression not supported: " + name);
    159183    }
    160184
    161185    public static bool TryGetStatelessExpression(string name, out Expression expression) {
    162       return StatelessExpressionTable.TryGetValue(name, out expression);
    163     }
    164 
    165     public static Expression GetStatefullExpression<T>() {
    166       return GetStatefullExpression(TypeToNameTable[typeof(T)]);
    167     }
    168 
    169     public static Expression GetStatefullExpression(string name) {
     186      if (!StatelessExpressionTable.ContainsKey(name)) {
     187        expression = null;
     188        return false;
     189      }
     190
     191      expression = StatelessExpressionTable[name];
     192
     193      return true;
     194    }
     195
     196    public static Expression GetStatefulExpression<T>() {
     197      return GetStatefulExpression(TypeToNameTable[typeof(T)]);
     198    }
     199
     200    public static Expression GetStatefulExpression(string name) {
    170201      Func<Expression> creator;
    171       if (StatefullExpressionFactory.TryGetValue(name, out creator))
     202      if (StatefulExpressionFactory.TryGetValue(name, out creator))
    172203        return creator();
    173204      throw new NotSupportedException("Expression not supported: " + name);
    174205    }
    175206
    176     public static bool TryGetStatefullExpression(string name, out Expression expression) {
    177       Func<Expression> creator;
    178 
    179       if (StatefullExpressionFactory.TryGetValue(name, out creator)) {
    180         expression = creator();
     207    public static bool TryGetStatefulExpression(string name, out Expression expression) {
     208      if (StatefulExpressionFactory.ContainsKey(name)) {
     209        expression = StatefulExpressionFactory[name]();
    181210        return true;
    182211      }
Note: See TracChangeset for help on using the changeset viewer.