Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/12/17 14:11:43 (7 years ago)
Author:
pkimmesw
Message:

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfiguration.cs

    r15017 r15032  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration {
    2   using System;
    32  using System.Collections.Generic;
    4   using System.Linq;
    53  using Base.Erc;
    64  using Common;
    7   using Core;
    8   using Expressions;
    95  using Persistence.Default.CompositeSerializers.Storable;
    10   using Stack;
    116
    127  [StorableClass]
    13   public class PushConfiguration : ParameterizedNamedItem, IReadOnlyPushConfiguration, IEnabledExpressionsConfiguration {
     8  public class PushConfiguration : PushConfigurationBase, IReadOnlyPushConfiguration {
    149    public PushConfiguration() {
    1510      Name = "Push Configuration";
    16 
    17       enabledExpressions = ExpressionTable.ExpressionNames.ToList();
    18       enabledStacks = new Dictionary<StackTypes, bool>();
    1911
    2012      ErcOptions = new ErcOptions();
     
    3123      MaxParenthesesClose = 4;
    3224      ParenthesesCloseBiasLevel = 1;
    33 
    34       InitEnabledStacks();
    3525    }
    3626
    37     private void InitEnabledStacks(bool state = true) {
    38       foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
    39         if (!enabledStacks.ContainsKey(type) && type != StackTypes.None)
    40           enabledStacks.Add(type, state);
    41       }
    42     }
    4327
    4428    [StorableConstructor]
     
    4832
    4933    public PushConfiguration(PushConfiguration origin, Cloner cloner) : base(origin, cloner) {
    50       enabledExpressions = origin.EnabledExpressions.ToList();
    51       enabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);
    5234
    5335      ErcOptions = cloner.Clone(origin.ErcOptions);
     
    6648    }
    6749
    68     [StorableHook(HookType.AfterDeserialization)]
    69     // ReSharper disable once UnusedMember.Local
    70     private void AfterDeserialization() {
    71       // Ensures that all types added after last serialization are available in enabledStacks
    72       InitEnabledStacks(false);
    73     }
    74 
    7550    [Storable]
    7651    public string FloatStringFormat { get; set; }
    7752
    78     [Storable]
    79     private readonly Dictionary<StackTypes, bool> enabledStacks;
    8053
    81     public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } }
    82 
    83     public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
    84 
    85     [Storable]
    86     private readonly List<string> enabledExpressions;
    87 
    88     public IList<string> EnabledExpressions
    89     {
    90       get { return enabledExpressions; }
    91       set
    92       {
    93         var removedExpressions = enabledExpressions.ToArray();
    94         enabledExpressions.Clear();
    95         enabledExpressions.AddRange(value);
    96 
    97         if (EnabledExpressionsChanged != null) {
    98           EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));
    99         }
    100       }
    101     }
    10254    IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions { get { return enabledExpressions; } }
    10355
     
    174126    public int MaxVectorLength { get; set; }
    175127
    176 
    177     public void SetEnabledStacks(StackTypes types) {
    178       // Disable all
    179       EnabledExpressions.Clear();
    180 
    181       foreach (StackTypes type in Enum.GetValues(types.GetType())) {
    182         if (type == StackTypes.None) continue;
    183         enabledStacks[type] = false;
    184       }
    185 
    186       foreach (var pair in ExpressionTable.StackDependencyToNamesTable) {
    187         if (types.HasFlag(pair.Key)) {
    188           foreach (var name in pair.Value) {
    189             EnableExpression(name, true);
    190           }
    191         }
    192       }
    193     }
    194 
    195     public void EnableExpressionOfStack(StackTypes types) {
    196       EnableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
    197     }
    198 
    199     public void EnableExpressionDependentOnStack(StackTypes types) {
    200       var names = ExpressionTable.StackDependencyToNamesTable[types].Where(
    201         name => {
    202           var type = ExpressionTable.NameToTypeTable[name];
    203           var attribute = ExpressionTable.TypeToAttributeTable[type];
    204 
    205           return (attribute.StackType | attribute.AdditionalStackDependencies)
    206             .ToEnumerable()
    207             .All(st => enabledStacks.ContainsKey(st) && enabledStacks[st]);
    208         });
    209 
    210       EnableExpressions(names);
    211     }
    212 
    213     private void EnableExpressions(IEnumerable<string> names) {
    214       foreach (var name in names.Except(EnabledExpressions))
    215         EnabledExpressions.Add(name);
    216 
    217       if (EnabledExpressionsChanged != null) {
    218         EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(names, new string[0]));
    219       }
    220     }
    221 
    222     public void DisableExpressionsOfStack(StackTypes types) {
    223       DisableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
    224     }
    225 
    226     public void DisableExpressionsDependentOnStack(StackTypes types) {
    227       var names = ExpressionTable.StackDependencyToNamesTable
    228           .Where(pair => pair.Key.HasFlag(types))
    229           .SelectMany(pair => pair.Value);
    230 
    231       DisableExpressions(names);
    232     }
    233 
    234     private void DisableExpressions(IEnumerable<string> names) {
    235       foreach (var name in names.Intersect(EnabledExpressions))
    236         EnabledExpressions.Remove(name);
    237 
    238       if (EnabledExpressionsChanged != null) {
    239         EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names));
    240       }
    241     }
    242 
    243     public void EnableExpression(string name, bool enableStackIfDisabled = false) {
    244       if (EnabledExpressions.Contains(name)) return;
    245 
    246       EnabledExpressions.Add(name);
    247 
    248       if (enableStackIfDisabled) {
    249         var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
    250         var attribute = ExpressionTable.TypeToAttributeTable[type];
    251         enabledStacks[attribute.StackType] = true;
    252       }
    253 
    254       if (EnabledExpressionsChanged != null) {
    255         EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
    256           new[] { name },
    257           new string[0]));
    258       }
    259     }
    260 
    261     public void DisableExpression(string name, bool disableStackIfEnabled = false) {
    262       if (!EnabledExpressions.Contains(name)) return;
    263 
    264       EnabledExpressions.Remove(name);
    265 
    266       if (disableStackIfEnabled) {
    267         var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
    268         var attribute = ExpressionTable.TypeToAttributeTable[type];
    269         enabledStacks[attribute.StackType] = false;
    270       }
    271 
    272       if (EnabledExpressionsChanged != null) {
    273         EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
    274           new string[0],
    275           new[] { name }));
    276       }
    277     }
    278 
    279     public void EnableExpression<T>(bool enableStackIfDisabled = false) where T : Expression {
    280       var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
    281       EnableExpression(attribute.ExpressionName, enableStackIfDisabled);
    282     }
    283 
    284     public void DisableExpression<T>(bool disableStackIfEnabled = false) where T : Expression {
    285       var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
    286       DisableExpression(attribute.ExpressionName, disableStackIfEnabled);
    287     }
    288 
    289     public void SetExpression(string name, bool state, bool cascadeForStack = false) {
    290       if (state) EnableExpression(name, cascadeForStack);
    291       else DisableExpression(name, cascadeForStack);
    292     }
    293 
    294     public void SetExpression<T>(bool state, bool cascadeForStack = false) where T : Expression {
    295       if (state) EnableExpression<T>(cascadeForStack);
    296       else DisableExpression<T>(cascadeForStack);
    297     }
    298 
    299     public void EnableStack(StackTypes type, bool enableExpressions = true, bool enableDependencies = true) {
    300       enabledStacks[type] = true;
    301 
    302       if (enableExpressions) EnableExpressionOfStack(type);
    303       if (enableDependencies) EnableExpressionDependentOnStack(type);
    304     }
    305 
    306     public void DisableStack(StackTypes type, bool disableExpressions = true, bool disableDependencies = true) {
    307       enabledStacks[type] = false;
    308 
    309       if (disableExpressions) DisableExpressionsOfStack(type);
    310       if (disableDependencies) DisableExpressionsDependentOnStack(type);
    311     }
    312 
    313     public void SetStack(StackTypes type, bool state, bool setExpressions = true, bool setDependencies = true) {
    314       if (state) EnableStack(type, setExpressions, setDependencies);
    315       else DisableStack(type, setExpressions, setDependencies);
    316     }
    317 
    318128    public override IDeepCloneable Clone(Cloner cloner) {
    319129      return new PushConfiguration(this, cloner);
Note: See TracChangeset for help on using the changeset viewer.