Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/14/11 10:57:27 (13 years ago)
Author:
mkommend
Message:

#1479: Updated grammar editor and changed related classes.

Location:
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r6377 r6409  
    270270                                              where s.InitialFrequency > 0.0
    271271                                              select node.Grammar.GetMaximumExpressionLength(s)).Max();
    272         if (aggregatedLongestExpressionLength < targetLength) minArity = i + 1;
     272        if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1;
    273273        else break;
    274274      }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs

    r6387 r6409  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Core;
    2425
     
    3334    int MaximumArity { get; }
    3435
     36    IEnumerable<ISymbol> Flatten();
     37
    3538    event EventHandler Changed;
    3639  }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6403 r6409  
    131131    #region protected grammar manipulation methods
    132132    protected void AddSymbol(ISymbol symbol) {
    133       AddSymbolToDictionaries(symbol);
    134       var groupSymbol = symbol as GroupSymbol;
    135       if (groupSymbol != null) {
    136         foreach (ISymbol s in groupSymbol.Flatten())
    137           AddSymbolToDictionaries(s);
    138       }
     133      foreach (ISymbol s in symbol.Flatten())
     134        AddSymbolToDictionaries(s);
    139135
    140136      ClearCaches();
     
    148144
    149145    protected void RemoveSymbol(ISymbol symbol) {
    150       RemoveSymbolFromDictionaries(symbol);
    151       var groupSymbol = symbol as GroupSymbol;
    152       if (groupSymbol != null) {
    153         foreach (ISymbol s in groupSymbol.Flatten())
    154           RemoveSymbolFromDictionaries(s);
    155       }
     146      foreach (ISymbol s in symbol.Flatten())
     147        RemoveSymbolFromDictionaries(s);
    156148
    157149      foreach (GroupSymbol group in symbols.Values.OfType<GroupSymbol>())
     
    188180
    189181    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
    190       List<ISymbol> parents;
    191       List<ISymbol> childs;
    192182      bool changed = false;
    193183
    194       var parentGroup = parent as GroupSymbol;
    195       if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList();
    196       else parents = new List<ISymbol>() { parent };
    197       var childGroup = child as GroupSymbol;
    198       if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList();
    199       else childs = new List<ISymbol>() { child };
    200 
    201       foreach (ISymbol p in parents) {
    202         foreach (ISymbol c in childs) {
    203           changed |= AddAllowedChildSymbolToDictionaries(p, c);
    204         }
    205       }
     184      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
     185        changed |= AddAllowedChildSymbolToDictionaries(p, child);
    206186
    207187      if (changed) {
     
    229209
    230210    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    231       List<ISymbol> parents;
    232       List<ISymbol> childs;
    233211      bool changed = false;
    234212
    235       var parentGroup = parent as GroupSymbol;
    236       if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList();
    237       else parents = new List<ISymbol>() { parent };
    238       var childGroup = child as GroupSymbol;
    239       if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList();
    240       else childs = new List<ISymbol>() { child };
    241 
    242       foreach (ISymbol p in parents) {
    243         foreach (ISymbol c in childs) {
    244           changed |= AddAllowedChildSymbolToDictionaries(p, c, argumentIndex);
    245         }
    246       }
     213      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
     214        changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex);
    247215
    248216      if (changed) {
     
    255223    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) {
    256224      List<string> childSymbols;
    257       if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols))
    258         if (childSymbols.Contains(child.Name)) return false;
     225      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     226        childSymbols = new List<string>();
     227        allowedChildSymbols.Add(parent.Name, childSymbols);
     228      }
     229      if (childSymbols.Contains(child.Name)) return false;
     230
    259231
    260232      var key = Tuple.Create(parent.Name, argumentIndex);
     
    263235        allowedChildSymbolsPerIndex.Add(key, childSymbols);
    264236      }
     237
    265238      if (childSymbols.Contains(child.Name)) return false;
    266239
     
    314287
    315288    protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
    316       var groupSymbol = symbol as GroupSymbol;
    317       if (groupSymbol != null) {
    318         var symbols = groupSymbol.Flatten().Where(s => !(s is GroupSymbol));
    319         if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
    320         if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
    321         foreach (ISymbol s in symbols)
    322           SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
    323       } else {
    324         if (symbol.MinimumArity > minimumSubtreeCount) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
    325         if (symbol.MaximumArity < maximumSubtreeCount) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
    326         SetSubTreeCountInDictionaries(symbol, minimumSubtreeCount, maximumSubtreeCount);
    327       }
     289      var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
     290      if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     291      if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     292
     293      foreach (ISymbol s in symbols)
     294        SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
    328295
    329296      ClearCaches();
     
    355322
    356323      List<string> temp;
    357       if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
    358         return temp.Contains(child.Name);
     324      if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
     325        if (temp.Contains(child.Name)) return true;
     326        if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
     327      }
    359328      return false;
    360329    }
     
    362331    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    363332      if (!child.Enabled) return false;
     333      if (IsAllowedChildSymbol(parent, child)) return true;
    364334
    365335      List<string> temp;
    366       if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
     336      var key = Tuple.Create(parent.Name, argumentIndex);
     337      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
    367338        if (temp.Contains(child.Name)) return true;
    368 
    369       var key = Tuple.Create(parent.Name, argumentIndex);
    370       if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp))
    371         return temp.Contains(child.Name);
     339        if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
     340      }
    372341      return false;
    373342    }
     
    375344    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    376345      List<string> childs;
    377       if (allowedChildSymbols.TryGetValue(parent.Name, out childs))
    378         return childs.Select(s => GetSymbol(s)).Where(s => s.Enabled);
    379       return Enumerable.Empty<ISymbol>();
     346      if (!allowedChildSymbols.TryGetValue(parent.Name, out childs))
     347        return Enumerable.Empty<ISymbol>();
     348
     349      return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled);
    380350    }
    381351
     
    452422    }
    453423
    454 
    455424    #region events
    456425    private void RegisterSymbolEvents(ISymbol symbol) {
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r6379 r6409  
    8181    }
    8282    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    83       return grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent));
     83      var symbols = grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent));
     84      return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled && !(s is GroupSymbol));
    8485    }
    8586    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
    86       return grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex));
     87      var symbols = grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex));
     88      return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled && !(s is GroupSymbol));
    8789    }
    8890
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs

    r6387 r6409  
    2727
    2828namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     29  [StorableClass]
    2930  public sealed class GroupSymbol : Symbol {
    3031    private const int minimumArity = 0;
     
    4546    public IEnumerable<ISymbol> Symbols {
    4647      get { return symbols; }
     48      private set { symbols = new ObservableSet<ISymbol>(value); }
    4749    }
    4850
     
    7577    }
    7678
    77     public IEnumerable<ISymbol> Flatten() {
    78       return symbols.Union(symbols.OfType<GroupSymbol>().SelectMany(g => g.Flatten()));
     79    public override IEnumerable<ISymbol> Flatten() {
     80      return base.Flatten().Union(symbols.SelectMany(s => s.Flatten()));
    7981    }
    8082  }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs

    r6387 r6409  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    9697    [StorableHook(HookType.AfterDeserialization)]
    9798    private void AfterDeserialization() {
    98       if (initialFrequency.IsAlmost(0.0)) enabled = false;
     99      // BackwardsCompatibility3.3
     100      #region Backwards compatible code, remove with 3.4
     101      if (initialFrequency.IsAlmost(0.0) && !(this is GroupSymbol)) enabled = false;
     102      #endregion
     103
    99104    }
    100105
    101106    public virtual ISymbolicExpressionTreeNode CreateTreeNode() {
    102107      return new SymbolicExpressionTreeNode(this);
     108    }
     109
     110    public virtual IEnumerable<ISymbol> Flatten() {
     111      return new List<ISymbol>() { this };
    103112    }
    104113
Note: See TracChangeset for help on using the changeset viewer.