Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6296 for branches


Ignore:
Timestamp:
05/26/11 12:28:43 (14 years ago)
Author:
mkommend
Message:

#1479: Updated grammars and added GroupSymbol.

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

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs

    r6284 r6296  
    2727    ISymbolicExpressionTreeNode CreateTreeNode();
    2828    double InitialFrequency { get; set; }
     29    bool Enabled { get; set; }
    2930    bool Fixed { get; set; }
    3031
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6284 r6296  
    3636  [StorableClass]
    3737  public abstract class SymbolicExpressionGrammarBase : NamedItem, ISymbolicExpressionGrammarBase {
     38
    3839    #region properties for separation between implementation and persistence
    3940    [Storable(Name = "Symbols")]
     
    6263    #endregion
    6364
     65    protected bool suppressEvents;
    6466    protected Dictionary<string, ISymbol> symbols;
    6567    protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
     
    8082      cachedMaxExpressionLength = new Dictionary<string, int>();
    8183      cachedMinExpressionDepth = new Dictionary<string, int>();
     84
     85      suppressEvents = false;
    8286    }
    8387    [StorableHook(HookType.AfterDeserialization)]
     
    106110      foreach (ISymbol symbol in symbols.Values)
    107111        RegisterSymbolEvents(symbol);
     112
     113      suppressEvents = false;
    108114    }
    109115
     
    118124      allowedChildSymbols = new Dictionary<string, List<string>>();
    119125      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
     126
     127      suppressEvents = false;
    120128    }
    121129
    122130    #region protected grammar manipulation methods
    123131    protected void AddSymbol(ISymbol symbol) {
    124       if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
    125       RegisterSymbolEvents(symbol);
    126132      symbols.Add(symbol.Name, symbol);
    127133      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     134      RegisterSymbolEvents(symbol);
     135
     136      var groupSymbol = symbol as GroupSymbol;
     137      if (groupSymbol != null) AddSymbol(groupSymbol);
     138
    128139      ClearCaches();
    129140      OnChanged();
    130141    }
    131142
    132     private void RegisterSymbolEvents(ISymbol symbol) {
    133       symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    134       symbol.NameChanged += new EventHandler(Symbol_NameChanged);
    135       symbol.Changed += new EventHandler(Symbol_Changed);
    136     }
    137     private void DeregisterSymbolEvents(ISymbol symbol) {
    138       symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    139       symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
    140       symbol.Changed -= new EventHandler(Symbol_Changed);
    141     }
    142 
    143     private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
    144       if (symbols.ContainsKey(e.Value)) e.Cancel = true;
    145     }
    146     private void Symbol_NameChanged(object sender, EventArgs e) {
    147       ISymbol symbol = (ISymbol)sender;
    148       string oldName = symbols.Where(x => x.Value == symbol).First().Key;
    149       string newName = symbol.Name;
    150 
    151       symbols.Remove(oldName);
    152       symbols.Add(newName, symbol);
    153 
    154       var subtreeCount = symbolSubtreeCount[oldName];
    155       symbolSubtreeCount.Remove(oldName);
    156       symbolSubtreeCount.Add(newName, subtreeCount);
    157 
    158       List<string> allowedChilds;
    159       if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
    160         allowedChildSymbols.Remove(oldName);
    161         allowedChildSymbols.Add(newName, allowedChilds);
    162       }
    163 
    164       for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
    165         if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
    166           allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
    167           allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
    168         }
    169       }
    170 
    171       foreach (var parent in Symbols) {
    172         if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
    173           if (allowedChilds.Remove(oldName))
    174             allowedChilds.Add(newName);
    175 
    176         for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
    177           if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
    178             if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
    179         }
    180       }
    181 
    182       ClearCaches();
    183       OnChanged();
    184     }
    185 
    186     private void Symbol_Changed(object sende, EventArgs e) {
    187       ClearCaches();
    188       OnChanged();
     143    private void AddSymbol(GroupSymbol groupSymbol) {
     144      foreach (ISymbol symbol in groupSymbol.Symbols) {
     145        symbols.Add(symbol.Name, symbol);
     146        symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     147        RegisterSymbolEvents(symbol);
     148
     149        var childGroup = symbol as GroupSymbol;
     150        if (childGroup != null) AddSymbol(childGroup);
     151      }
    189152    }
    190153
     
    218181
    219182    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
     183      suppressEvents = true;
     184      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++)
     185        RemoveAllowedChildSymbol(parent, child, argumentIndex);
     186      suppressEvents = false;
     187
    220188      List<string> childSymbols;
    221189      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     
    223191        allowedChildSymbols.Add(parent.Name, childSymbols);
    224192      }
    225       if (childSymbols.Contains(child.Name)) throw new ArgumentException();
    226       childSymbols.Add(child.Name);
    227       ClearCaches();
    228       OnChanged();
     193
     194      if (!childSymbols.Contains(child.Name)) {
     195        childSymbols.Add(child.Name);
     196        ClearCaches();
     197        OnChanged();
     198      }
    229199    }
    230200
    231201    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     202      List<string> childSymbols;
     203      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols))
     204        if (childSymbols.Contains(child.Name)) return;
     205
    232206      var key = Tuple.Create(parent.Name, argumentIndex);
    233       List<string> childSymbols;
    234207      if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
    235208        childSymbols = new List<string>();
     
    237210      }
    238211
    239       if (IsAllowedChildSymbol(parent, child)) throw new ArgumentException();
    240       if (childSymbols.Contains(child.Name)) throw new ArgumentException();
    241       childSymbols.Add(child.Name);
    242       ClearCaches();
    243       OnChanged();
     212      if (!childSymbols.Contains(child.Name)) {
     213        childSymbols.Add(child.Name);
     214        ClearCaches();
     215        OnChanged();
     216      }
    244217    }
    245218
    246219    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
     220      bool changed = false;
    247221      List<string> childSymbols;
    248222      if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
    249         if (allowedChildSymbols[parent.Name].Remove(child.Name)) {
    250           ClearCaches();
    251           OnChanged();
     223        if (allowedChildSymbols[parent.Name].Remove(child.Name)) changed = true;
     224      }
     225
     226      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) {
     227        var key = Tuple.Create(parent.Name, argumentIndex);
     228        if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
     229          if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) changed = true;
    252230        }
    253231      }
     232
     233      if (changed) {
     234        ClearCaches();
     235        OnChanged();
     236      }
    254237    }
    255238
    256239    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     240      bool changed = false;
     241
     242      List<string> childSymbols;
     243      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     244        if (childSymbols.Remove(child.Name)) {
     245          suppressEvents = true;
     246          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     247            if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i);
     248          }
     249          suppressEvents = false;
     250          changed = true;
     251        }
     252      }
     253
    257254      var key = Tuple.Create(parent.Name, argumentIndex);
    258       List<string> childSymbols;
    259255      if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
    260         if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) {
    261           ClearCaches();
    262           OnChanged();
    263         }
     256        if (allowedChildSymbolsPerIndex[key].Remove(child.Name))
     257          changed = true;
     258      }
     259
     260      if (changed) {
     261        ClearCaches();
     262        OnChanged();
    264263      }
    265264    }
     
    277276    #endregion
    278277
    279     #region ISymbolicExpressionGrammarBase Members
    280278    public virtual IEnumerable<ISymbol> Symbols {
    281279      get { return symbols.Values; }
    282280    }
    283281    public virtual IEnumerable<ISymbol> AllowedSymbols {
    284       get { return Symbols.Where(s => !s.InitialFrequency.IsAlmost(0.0)); }
     282      get { return Symbols.Where(s => s.Enabled); }
    285283    }
    286284    public virtual bool ContainsSymbol(ISymbol symbol) {
     
    289287
    290288    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
     289      if (!child.Enabled) return false;
     290
    291291      List<string> temp;
    292292      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
     
    296296
    297297    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     298      if (!child.Enabled) return false;
     299
    298300      List<string> temp;
    299301      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
     
    307309
    308310    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    309       return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s;
     311      List<string> childs;
     312      if (allowedChildSymbols.TryGetValue(parent.Name, out childs))
     313        return childs.Select(s => GetSymbol(s)).Where(s => s.Enabled);
     314      return Enumerable.Empty<ISymbol>();
    310315    }
    311316
     
    320325        result = result.Union(temp);
    321326
    322       return result.Select(x => GetSymbol(x)).Where(s => !s.InitialFrequency.IsAlmost(0.0));
     327      return result.Select(x => GetSymbol(x)).Where(s => s.Enabled);
    323328    }
    324329
     
    342347        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    343348        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    344                                               let minForSlot = (long)(from s in AllowedSymbols
    345                                                                       where IsAllowedChildSymbol(symbol, s, argIndex)
     349                                              let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    346350                                                                      select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
    347351                                              select minForSlot).DefaultIfEmpty(0).Sum();
     
    359363        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    360364        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
    361                                   let maxForSlot = (long)(from s in AllowedSymbols
    362                                                           where IsAllowedChildSymbol(symbol, s, argIndex)
     365                                  let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    363366                                                          select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
    364367                                  select maxForSlot).DefaultIfEmpty(0).Sum();
     
    375378        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
    376379        long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    377                              let minForSlot = (long)(from s in AllowedSymbols
    378                                                      where IsAllowedChildSymbol(symbol, s, argIndex)
     380                             let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    379381                                                     select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
    380382                             select minForSlot).DefaultIfEmpty(0).Max();
     
    385387    }
    386388
     389
     390    #region events
     391    private void RegisterSymbolEvents(ISymbol symbol) {
     392      symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     393      symbol.NameChanged += new EventHandler(Symbol_NameChanged);
     394      symbol.Changed += new EventHandler(Symbol_Changed);
     395    }
     396    private void DeregisterSymbolEvents(ISymbol symbol) {
     397      symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     398      symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
     399      symbol.Changed -= new EventHandler(Symbol_Changed);
     400    }
     401
     402    private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
     403      if (symbols.ContainsKey(e.Value)) e.Cancel = true;
     404    }
     405    private void Symbol_NameChanged(object sender, EventArgs e) {
     406      ISymbol symbol = (ISymbol)sender;
     407      string oldName = symbols.Where(x => x.Value == symbol).First().Key;
     408      string newName = symbol.Name;
     409
     410      symbols.Remove(oldName);
     411      symbols.Add(newName, symbol);
     412
     413      var subtreeCount = symbolSubtreeCount[oldName];
     414      symbolSubtreeCount.Remove(oldName);
     415      symbolSubtreeCount.Add(newName, subtreeCount);
     416
     417      List<string> allowedChilds;
     418      if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
     419        allowedChildSymbols.Remove(oldName);
     420        allowedChildSymbols.Add(newName, allowedChilds);
     421      }
     422
     423      for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
     424        if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
     425          allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
     426          allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
     427        }
     428      }
     429
     430      foreach (var parent in Symbols) {
     431        if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
     432          if (allowedChilds.Remove(oldName))
     433            allowedChilds.Add(newName);
     434
     435        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     436          if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
     437            if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
     438        }
     439      }
     440
     441      ClearCaches();
     442      OnChanged();
     443    }
     444
     445    private void Symbol_Changed(object sende, EventArgs e) {
     446      ClearCaches();
     447      OnChanged();
     448    }
     449
    387450    public event EventHandler Changed;
    388451    protected virtual void OnChanged() {
     452      if (suppressEvents) return;
    389453      var handler = Changed;
    390454      if (handler != null) Changed(this, EventArgs.Empty);
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs

    r6285 r6296  
    3232    public IEnumerable<ISymbol> Symbols {
    3333      get { return symbols; }
    34       private set { symbols = new List<ISymbol>(value); }
     34    }
     35
     36    public override bool Enabled {
     37      get { return base.Enabled; }
     38      set {
     39        if (value != Enabled) {
     40          base.Enabled = value;
     41          symbols.ForEach(s => s.Enabled = value);
     42        }
     43      }
    3544    }
    3645
     
    3948    private GroupSymbol(GroupSymbol original, Cloner cloner)
    4049      : base(original, cloner) {
     50      symbols = original.Symbols.Select(s => cloner.Clone(s)).ToList();
    4151    }
    4252    public override IDeepCloneable Clone(Cloner cloner) {
     
    4454    }
    4555
    46     public GroupSymbol(string name) : this(name, "A symbol to group other symbols.") { }
    47     public GroupSymbol(string name, string description) : this(name, description, Enumerable.Empty<ISymbol>()) { }
    48     public GroupSymbol(string name, string description, IEnumerable<ISymbol> symbols)
    49       : base(name, description) {
     56    public GroupSymbol(string name, IEnumerable<ISymbol> symbols)
     57      : base(name, "A symbol which groups other symbols") {
    5058      this.symbols = new List<ISymbol>(symbols);
     59      InitialFrequency = 0.0;
    5160    }
    5261  }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs

    r6284 r6296  
    3838        if (value != initialFrequency) {
    3939          initialFrequency = value;
     40          if (initialFrequency.IsAlmost(0.0)) enabled = false;
     41          OnChanged(EventArgs.Empty);
     42        }
     43      }
     44    }
     45
     46    [Storable(DefaultValue = true)]
     47    private bool enabled;
     48    public virtual bool Enabled {
     49      get { return enabled; }
     50      set {
     51        if (value != enabled) {
     52          enabled = value;
    4053          OnChanged(EventArgs.Empty);
    4154        }
     
    6881      : base(original, cloner) {
    6982      initialFrequency = original.initialFrequency;
     83      enabled = original.enabled;
    7084      @fixed = original.@fixed;
    7185    }
     
    7488      : base(name, description) {
    7589      initialFrequency = 1.0;
     90      enabled = true;
    7691      @fixed = false;
     92    }
     93
     94    [StorableHook(HookType.AfterDeserialization)]
     95    private void AfterDeserialization() {
     96      if (initialFrequency.IsAlmost(0.0)) enabled = false;
    7797    }
    7898
     
    81101    }
    82102
    83     #region events
    84103    public event EventHandler Changed;
    85104    protected virtual void OnChanged(EventArgs e) {
     
    88107        handlers(this, e);
    89108    }
    90     #endregion
    91109  }
    92110}
Note: See TracChangeset for help on using the changeset viewer.