Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/02/10 11:38:04 (14 years ago)
Author:
mkommend
Message:

changed symbols and grammars to be more efficient in respect to cloning, construction and deserialization (ticket #1073)

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs

    r3541 r3993  
    6363      }
    6464      set {
    65         allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
     65        allowedChildSymbols = new Dictionary<string, List<List<string>>>();
    6666        foreach (var pair in value) {
    67           allowedChildSymbols[pair.Key] = new List<HashSet<string>>();
     67          allowedChildSymbols[pair.Key] = new List<List<string>>();
    6868          foreach (var entry in pair.Value) {
    69             var hashSet = new HashSet<string>();
     69            var hashSet = new List<string>();
    7070            foreach (string child in entry) {
    7171              hashSet.Add(child);
     
    8585    private Dictionary<string, int> minSubTreeCount;
    8686    private Dictionary<string, int> maxSubTreeCount;
    87     private Dictionary<string, List<HashSet<string>>> allowedChildSymbols;
     87    private Dictionary<string, List<List<string>>> allowedChildSymbols;
    8888    private Dictionary<string, Symbol> allSymbols;
    8989    [Storable]
     
    9292    public DefaultSymbolicExpressionGrammar()
    9393      : base() {
    94       Clear();
    95     }
    96 
    97     public void Clear() {
    9894      minSubTreeCount = new Dictionary<string, int>();
    9995      maxSubTreeCount = new Dictionary<string, int>();
    100       allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
     96      allowedChildSymbols = new Dictionary<string, List<List<string>>>();
    10197      allSymbols = new Dictionary<string, Symbol>();
     98
    10299      cachedMinExpressionLength = new Dictionary<string, int>();
    103100      cachedMaxExpressionLength = new Dictionary<string, int>();
     
    110107    }
    111108
     109    //copy constructor for cloning
     110    protected DefaultSymbolicExpressionGrammar(DefaultSymbolicExpressionGrammar copy) :base() {
     111      this.minSubTreeCount = new Dictionary<string, int>(copy.minSubTreeCount);
     112      this.maxSubTreeCount = new Dictionary<string, int>(copy.maxSubTreeCount);
     113     
     114      this.startSymbol = copy.startSymbol;
     115      this.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
     116      foreach (var entry in copy.allowedChildSymbols) {
     117        this.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
     118        foreach (var set in entry.Value) {
     119          this.allowedChildSymbols[entry.Key].Add(new List<string>(set));
     120        }
     121      }
     122      this.allSymbols = new Dictionary<string, Symbol>(copy.allSymbols);
     123
     124      cachedMinExpressionLength = new Dictionary<string, int>();
     125      cachedMaxExpressionLength = new Dictionary<string, int>();
     126      cachedMinExpressionDepth = new Dictionary<string, int>();
     127    }
     128
     129    [StorableConstructor]
     130    protected DefaultSymbolicExpressionGrammar(bool deserializing)
     131      : base(deserializing) {
     132      cachedMinExpressionLength = new Dictionary<string, int>();
     133      cachedMaxExpressionLength = new Dictionary<string, int>();
     134      cachedMinExpressionDepth = new Dictionary<string, int>();
     135    }
     136
     137    public void Clear() {
     138      minSubTreeCount.Clear();
     139      maxSubTreeCount.Clear();
     140      allowedChildSymbols.Clear();
     141      allSymbols.Clear();
     142
     143      cachedMaxExpressionLength.Clear();
     144      cachedMinExpressionLength.Clear();
     145      cachedMinExpressionDepth.Clear();
     146
     147      startSymbol = new StartSymbol();
     148      AddSymbol(startSymbol);
     149      SetMinSubtreeCount(startSymbol, 1);
     150      SetMaxSubtreeCount(startSymbol, 1);
     151    }
     152
    112153    #region ISymbolicExpressionGrammar Members
    113154
     
    117158    }
    118159
    119 
    120160    public void AddSymbol(Symbol symbol) {
    121161      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
    122162      allSymbols.Add(symbol.Name, symbol);
    123       allowedChildSymbols[symbol.Name] = new List<HashSet<string>>();
     163      allowedChildSymbols[symbol.Name] = new List<List<string>>();
    124164      ClearCaches();
    125165    }
     
    211251      maxSubTreeCount[symbol.Name] = nSubTrees;
    212252      while (allowedChildSymbols[symbol.Name].Count <= nSubTrees)
    213         allowedChildSymbols[symbol.Name].Add(new HashSet<string>());
     253        allowedChildSymbols[symbol.Name].Add(new List<string>());
    214254      while (allowedChildSymbols[symbol.Name].Count > nSubTrees) {
    215255        allowedChildSymbols[symbol.Name].RemoveAt(allowedChildSymbols[symbol.Name].Count - 1);
     
    243283
    244284    public override IDeepCloneable Clone(Cloner cloner) {
    245       DefaultSymbolicExpressionGrammar clone = (DefaultSymbolicExpressionGrammar)base.Clone(cloner);
    246       clone.maxSubTreeCount = new Dictionary<string, int>(maxSubTreeCount);
    247       clone.minSubTreeCount = new Dictionary<string, int>(minSubTreeCount);
    248       clone.startSymbol = startSymbol;
    249       clone.allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
    250       foreach (var entry in allowedChildSymbols) {
    251         clone.allowedChildSymbols[entry.Key] = new List<HashSet<string>>();
    252         foreach (var set in entry.Value) {
    253           clone.allowedChildSymbols[entry.Key].Add(new HashSet<string>(set));
    254         }
    255       }
    256       clone.allSymbols = new Dictionary<string, Symbol>(allSymbols);
     285      DefaultSymbolicExpressionGrammar clone = new DefaultSymbolicExpressionGrammar(this);
     286      cloner.RegisterClonedObject(this, clone);
    257287      return clone;
    258288    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs

    r3990 r3993  
    7272    private Defun defunSymbol;
    7373
    74     public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
     74
    7575
    7676    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
     
    7979      maxFunctionDefinitions = 3;
    8080      Initialize(mainBranchGrammar);
     81    }
     82
     83    //copy constructor for cloning
     84    protected GlobalSymbolicExpressionGrammar(GlobalSymbolicExpressionGrammar copy)
     85      : base(copy) {
     86      this.maxFunctionArguments = copy.maxFunctionArguments;
     87      this.minFunctionArguments = copy.minFunctionArguments;
     88      this.maxFunctionDefinitions = copy.maxFunctionDefinitions;
     89      this.minFunctionDefinitions = copy.minFunctionDefinitions;
     90    }
     91
     92    [StorableConstructor]
     93    protected GlobalSymbolicExpressionGrammar(bool deserializing)
     94      : base(deserializing) {
    8195    }
    8296
     
    142156
    143157    public override IDeepCloneable Clone(Cloner cloner) {
    144       GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
    145       clone.defunSymbol = defunSymbol;
    146       clone.maxFunctionArguments = maxFunctionArguments;
    147       clone.maxFunctionDefinitions = maxFunctionDefinitions;
    148       clone.minFunctionArguments = minFunctionArguments;
    149       clone.minFunctionDefinitions = minFunctionDefinitions;
     158      GlobalSymbolicExpressionGrammar clone = new GlobalSymbolicExpressionGrammar(this);
     159      cloner.RegisterClonedObject(this, clone);
    150160      return clone;
    151161    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Argument.cs

    r3824 r3993  
    2828  /// </summary>
    2929  [StorableClass]
    30   [Item("Argument", "Symbol that represents a function argument.")]
     30  [Item(Argument.ArgumentName, Argument.ArgumentDescription)]
    3131  public sealed class Argument : ReadOnlySymbol {
     32    public const string ArgumentName = "Argument";
     33    public const string ArgumentDescription = "Symbol that represents a function argument.";
    3234    [Storable]
    3335    private int argumentIndex;
     
    3638    }
    3739
     40    public override bool CanChangeDescription {
     41      get { return false; }
     42    }
     43
     44    [StorableConstructor]
    3845    private Argument() : base() { }
    3946
    4047    public Argument(int argumentIndex)
    41       : base() {
     48      : base("ARG" + argumentIndex, Argument.ArgumentDescription) {
    4249      this.argumentIndex = argumentIndex;
    4350      this.name = "ARG" + argumentIndex;
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Defun.cs

    r3983 r3993  
    2828  /// </summary>
    2929  [StorableClass]
    30   [Item("Defun", "Symbol that represents a function defining node.")]
     30  [Item(Defun.DefunName, Defun.DefunDescription)]
    3131  public sealed class Defun : ReadOnlySymbol {
    32     public Defun() : base() { }
     32    public const string DefunName = "Defun";
     33    public const string DefunDescription = "Symbol that represents a function defining node.";
     34
     35    public Defun() : base(Defun.DefunName, Defun.DefunDescription) { }
     36    [StorableConstructor]
     37    protected Defun(bool deserializing) : base(deserializing) { }
    3338
    3439    public override SymbolicExpressionTreeNode CreateTreeNode() {
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunction.cs

    r3824 r3993  
    2929  /// </summary>
    3030  [StorableClass]
    31   [Item("InvokeFunction", "Symbol that the invokation of another function.")]
     31  [Item(InvokeFunction.InvokeFunctionName, InvokeFunction.InvokeFunctionDescription)]
    3232  public sealed class InvokeFunction : ReadOnlySymbol {
     33    public const string InvokeFunctionName = "InvokeFunction";
     34    public const string InvokeFunctionDescription = "Symbol that the invokation of another function.";
    3335    public override bool CanChangeName {
    3436      get {
     
    4648    }
    4749
    48     private InvokeFunction() : base() { }
    49 
     50    [StorableConstructor]
     51    private InvokeFunction(bool deserializing) : base(deserializing) { }
    5052    public InvokeFunction(string functionName)
    51       : base() {
     53      : base("Invoke: " + functionName, InvokeFunction.InvokeFunctionDescription) {
    5254      this.FunctionName = functionName;
    53       this.name = "Invoke: " + functionName;
    5455    }
    5556
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/ProgramRootSymbol.cs

    r3824 r3993  
    2525namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
    2626  [StorableClass]
    27   [Item("ProgramRootSymbol", "Special symbol that represents the program root node of a symbolic expression tree.")]
     27  [Item(ProgramRootSymbol.ProgramRootSymbolName, ProgramRootSymbol.ProgramRootSymbolDescription)]
    2828  public sealed class ProgramRootSymbol : ReadOnlySymbol {
     29    public const string ProgramRootSymbolName = "ProgramRootSymbol";
     30    public const string ProgramRootSymbolDescription = "Special symbol that represents the program root node of a symbolic expression tree.";
     31
     32    public ProgramRootSymbol() : base(ProgramRootSymbol.ProgramRootSymbolName, ProgramRootSymbol.ProgramRootSymbolDescription) { }
     33    [StorableConstructor]
     34    private ProgramRootSymbol(bool deserializing) : base(deserializing) { }
     35
    2936    public override SymbolicExpressionTreeNode CreateTreeNode() {
    3037      return new SymbolicExpressionTreeTopLevelNode(this);
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/ReadOnlySymbol.cs

    r3824 r3993  
    5050    }
    5151
    52     protected ReadOnlySymbol()
    53       : base() {
    54       this.name = ItemName;
    55       this.description = ItemDescription;
    56       initialFrequency = 1.0;
    57     }
     52    protected ReadOnlySymbol() : base() { }
     53    protected ReadOnlySymbol(string name, string description) : base(name, description) { }
     54    [StorableConstructor]
     55    protected ReadOnlySymbol(bool deserializing) : base(deserializing) { }
    5856  }
    5957}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Symbol.cs

    r3824 r3993  
    5454    protected Symbol()
    5555      : base() {
    56       this.name = ItemName;
    57       this.description = ItemDescription;
    5856      initialFrequency = 1.0;
    5957    }
     58
     59    protected Symbol(string name, string description)
     60      : base(name, description) {
     61      initialFrequency = 1.0;
     62    }
     63
     64    [StorableConstructor]
     65    protected Symbol(bool deserializing) : base(deserializing) { }
    6066
    6167    public virtual SymbolicExpressionTreeNode CreateTreeNode() {
Note: See TracChangeset for help on using the changeset viewer.