Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/19/11 13:52:12 (13 years ago)
Author:
mkommend
Message:

#1532:

  • Enabled renaming of symbols
  • Fixed cloning of grammers
  • Added readonly attribute in grammars to lock grammars during the algorithm run
  • Removed useless clone method in cloner
  • Changed CheckedItemCollectionViews to enable scrolling during the locked state
Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs

    r5809 r6233  
    8383          subtree.Grammar.SetSubtreeCount(invokeSymbol, duplicatedDefunBranch.NumberOfArguments, duplicatedDefunBranch.NumberOfArguments);
    8484
    85           foreach (Symbol symbol in subtree.Grammar.Symbols) {
     85          foreach (ISymbol symbol in subtree.Grammar.Symbols) {
    8686            if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol))
    8787              subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol);
     
    9393          }
    9494
    95           foreach (Symbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))
     95          foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))
    9696            if (symbol != invokeSymbol) //avoid duplicate entry invokesymbol / invokesymbol
    9797              subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol);
    9898          for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++) {
    99             foreach (Symbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)))
     99            foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)))
    100100              subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol, i);
    101101          }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r6009 r6233  
    3838    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
    3939    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
     40    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
    4041    #region Parameter Properties
    4142    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     
    4748    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
    4849      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
     50    }
     51    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
     52      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
    4953    }
    5054    #endregion
     
    5761    }
    5862    public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
    59       get { return SymbolicExpressionTreeGrammarParameter.ActualValue; }
     63      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
    6064    }
    6165    #endregion
     
    6973      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    7074      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     75      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
    7176    }
    7277
     
    7479      return new ProbabilisticTreeCreator(this, cloner);
    7580    }
     81    [StorableHook(HookType.AfterDeserialization)]
     82    private void AfterDeserialization() {
     83      if (!Parameters.ContainsKey(ClonedSymbolicExpressionTreeGrammarParameterName))
     84        Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
     85    }
     86
     87    public override IOperation Apply() {
     88      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
     89        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
     90        IScope globalScope = ExecutionContext.Scope;
     91        while (globalScope.Parent != null)
     92          globalScope = globalScope.Parent;
     93
     94        globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
     95      }
     96      return base.Apply();
     97    }
    7698
    7799    protected override ISymbolicExpressionTree Create(IRandom random) {
    78100      return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
    79 
    80101    }
    81102
     
    165186                                select s)
    166187                               .ToList();
     188          if (allowedSymbols.Count == 0) return false;
    167189          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
    168190          var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs

    r5809 r6233  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Parameters;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    5554    }
    5655
    57     public sealed override IOperation Apply() {
     56    public override IOperation Apply() {
    5857      SymbolicExpressionTree = Create(Random);
    5958      return base.Apply();
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammar.cs

    r5809 r6233  
    2121
    2222
     23using System;
     24using HeuristicLab.Core;
    2325namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    24   public interface ISymbolicExpressionGrammar : ISymbolicExpressionGrammarBase {
     26  public interface ISymbolicExpressionGrammar : ISymbolicExpressionGrammarBase, IStatefulItem {
    2527    ISymbol ProgramRootSymbol { get; }
    2628    ISymbol StartSymbol { get; }
     
    3032    int MinimumFunctionArguments { get; set; }
    3133    int MaximumFunctionArguments { get; set; }
     34
     35    bool ReadOnly { get; set; }
     36    event EventHandler ReadOnlyChanged;
    3237  }
    3338}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs

    r5809 r6233  
    2020#endregion
    2121
    22 using System.Linq;
     22using System;
    2323using HeuristicLab.Common;
     24using HeuristicLab.Core;
    2425using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2526
     
    2829  public abstract class SymbolicExpressionGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionGrammar {
    2930    #region fields & properties
     31    [Storable(DefaultValue = false)]
     32    private bool readOnly;
     33    public bool ReadOnly {
     34      get { return readOnly; }
     35      set {
     36        if (readOnly != value) {
     37          readOnly = value;
     38          OnReadOnlyChanged();
     39        }
     40      }
     41    }
     42
    3043    [Storable]
    3144    private int minimumFunctionDefinitions;
     
    102115    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
    103116      : base(original, cloner) {
    104       programRootSymbol = (ProgramRootSymbol)cloner.Clone(original.programRootSymbol);
    105       startSymbol = (StartSymbol)cloner.Clone(original.StartSymbol);
    106       defunSymbol = (Defun)cloner.Clone(original.defunSymbol);
    107       symbols = original.symbols
    108         .ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
     117      programRootSymbol = cloner.Clone(original.programRootSymbol);
     118      startSymbol = cloner.Clone(original.StartSymbol);
     119      defunSymbol = cloner.Clone(original.defunSymbol);
     120
    109121      maximumFunctionArguments = original.maximumFunctionArguments;
    110122      minimumFunctionArguments = original.minimumFunctionArguments;
     
    140152      }
    141153    }
     154
     155    public event EventHandler ReadOnlyChanged;
     156    protected virtual void OnReadOnlyChanged() {
     157      var handler = ReadOnlyChanged;
     158      if (handler != null)
     159        handler(this, EventArgs.Empty);
     160    }
     161
     162    #region IStatefulItem
     163    void IStatefulItem.InitializeState() { }
     164    void IStatefulItem.ClearState() {
     165      ReadOnly = false;
     166    }
     167    #endregion
    142168  }
    143169}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6009 r6233  
    8181      cachedMinExpressionDepth = new Dictionary<string, int>();
    8282    }
     83    [StorableHook(HookType.AfterDeserialization)]
     84    private void AfterDeserialization() {
     85      foreach (ISymbol symbol in symbols.Values)
     86        RegisterSymbolEvents(symbol);
     87    }
     88
    8389    protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
    8490      : base(original, cloner) {
     
    8793      cachedMinExpressionDepth = new Dictionary<string, int>();
    8894
     95
     96      symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
    8997      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
    90       symbols = new Dictionary<string, ISymbol>(original.symbols);
    9198
    9299      allowedChildSymbols = new Dictionary<string, List<string>>();
     
    97104      foreach (var element in original.allowedChildSymbolsPerIndex)
    98105        allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
     106
     107      foreach (ISymbol symbol in symbols.Values)
     108        RegisterSymbolEvents(symbol);
    99109    }
    100110
     
    114124    protected void AddSymbol(ISymbol symbol) {
    115125      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
     126      RegisterSymbolEvents(symbol);
    116127      symbols.Add(symbol.Name, symbol);
    117128      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     129      ClearCaches();
     130    }
     131
     132    private void RegisterSymbolEvents(ISymbol symbol) {
     133      symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     134      symbol.NameChanged += new EventHandler(Symbol_NameChanged);
     135    }
     136    private void DeregisterSymbolEvents(ISymbol symbol) {
     137      symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     138      symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
     139    }
     140
     141    private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
     142      if (symbols.ContainsKey(e.Value)) e.Cancel = true;
     143    }
     144    private void Symbol_NameChanged(object sender, EventArgs e) {
     145      ISymbol symbol = (ISymbol)sender;
     146      string oldName = symbols.Where(x => x.Value == symbol).First().Key;
     147      string newName = symbol.Name;
     148
     149      symbols.Remove(oldName);
     150      symbols.Add(newName, symbol);
     151
     152      var subtreeCount = symbolSubtreeCount[oldName];
     153      symbolSubtreeCount.Remove(oldName);
     154      symbolSubtreeCount.Add(newName, subtreeCount);
     155
     156      List<string> allowedChilds;
     157      if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
     158        allowedChildSymbols.Remove(oldName);
     159        allowedChildSymbols.Add(newName, allowedChilds);
     160      }
     161
     162      for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
     163        if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
     164          allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
     165          allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
     166        }
     167      }
     168
     169      foreach (var parent in Symbols) {
     170        if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
     171          if (allowedChilds.Remove(oldName))
     172            allowedChilds.Add(newName);
     173
     174        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     175          if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
     176            if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
     177        }
     178      }
     179
    118180      ClearCaches();
    119181    }
     
    126188      symbolSubtreeCount.Remove(symbol.Name);
    127189
    128 
    129190      foreach (var parent in Symbols) {
    130191        List<string> allowedChilds;
     
    137198        }
    138199      }
     200      DeregisterSymbolEvents(symbol);
    139201      ClearCaches();
    140202    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r5809 r6233  
    3636    }
    3737    public override IDeepCloneable Clone(Cloner cloner) {
     38      foreach (ISymbol symbol in base.Symbols)
     39        cloner.RegisterClonedObject(symbol, symbol);
    3840      return new SymbolicExpressionTreeGrammar(this, cloner);
    3941    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTerminalNode.cs

    r5809 r6233  
    4545    protected SymbolicExpressionTreeTerminalNode() : base() { }
    4646
    47     protected SymbolicExpressionTreeTerminalNode(Symbol symbol)
     47    protected SymbolicExpressionTreeTerminalNode(ISymbol symbol)
    4848      : base() {
    4949      // symbols are reused
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTopLevelNode.cs

    r5809 r6233  
    4242    }
    4343    public SymbolicExpressionTreeTopLevelNode() : base() { }
    44     public SymbolicExpressionTreeTopLevelNode(Symbol symbol) : base(symbol) { }
     44    public SymbolicExpressionTreeTopLevelNode(ISymbol symbol) : base(symbol) { }
    4545
    4646
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Argument.cs

    r5809 r6233  
    3838    }
    3939
    40     public override bool CanChangeDescription {
    41       get { return false; }
    42     }
    43 
    4440    [StorableConstructor]
    4541    private Argument(bool deserializing) : base(deserializing) { }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunction.cs

    r5809 r6233  
    2020#endregion
    2121
    22 using System;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
     
    3837    public string FunctionName {
    3938      get { return functionName; }
    40       set {
    41         if (value == null) throw new ArgumentNullException();
    42         functionName = value;
    43       }
    4439    }
    4540
     
    5348    public InvokeFunction(string functionName)
    5449      : base("Invoke: " + functionName, InvokeFunction.InvokeFunctionDescription) {
    55       this.FunctionName = functionName;
     50      this.functionName = functionName;
    5651    }
    5752
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs

    r5809 r6233  
    4343    }
    4444    public override bool CanChangeName {
    45       get { return false; }
     45      get { return !(this is IReadOnlySymbol); }
    4646    }
    4747    public override bool CanChangeDescription {
     
    5656      initialFrequency = original.initialFrequency;
    5757    }
    58     protected Symbol()
    59       : base() {
    60       initialFrequency = 1.0;
    61     }
    6258
    6359    protected Symbol(string name, string description)
     
    6561      initialFrequency = 1.0;
    6662    }
    67 
    6863
    6964    public virtual ISymbolicExpressionTreeNode CreateTreeNode() {
Note: See TracChangeset for help on using the changeset viewer.