Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5695


Ignore:
Timestamp:
03/15/11 17:03:08 (14 years ago)
Author:
mkommend
Message:

#1418: Implemented persistence of grammars. Currently it does not work, because the Tuple type is not serializable (see #1442).

Location:
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs

    r5691 r5695  
    4949    public int MinimumFunctionArguments {
    5050      get { return minimumFunctionArguments; }
    51       set {
    52         minimumFunctionArguments = value;
    53       }
     51      set { minimumFunctionArguments = value; }
    5452    }
    5553    [Storable]
     
    5755    public int MaximumFunctionArguments {
    5856      get { return maximumFunctionArguments; }
    59       set {
    60         maximumFunctionArguments = value;
    61       }
     57      set { maximumFunctionArguments = value; }
    6258    }
    6359
     
    7066      get { return ProgramRootSymbol; }
    7167    }
     68    [Storable(Name = "ProgramRootSymbol")]
     69    private ISymbol StorableProgramRootSymbol {
     70      get { return programRootSymbol; }
     71      set { programRootSymbol = (ProgramRootSymbol)value; }
     72    }
    7273
    73     [Storable]
    7474    private StartSymbol startSymbol;
    7575    public StartSymbol StartSymbol {
     
    7979      get { return StartSymbol; }
    8080    }
     81    [Storable(Name = "StartSymbol")]
     82    private ISymbol StorableStartSymbol {
     83      get { return startSymbol; }
     84      set { startSymbol = (StartSymbol)value; }
     85    }
    8186
    8287    [Storable]
     
    8489    protected Defun DefunSymbol {
    8590      get { return defunSymbol; }
     91    }
     92    [Storable(Name = "DefunSymbol")]
     93    private ISymbol StorableDefunSymbol {
     94      get { return defunSymbol; }
     95      set { defunSymbol = (Defun)value; }
    8696    }
    8797    #endregion
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r5688 r5695  
    3737  public abstract class SymbolicExpressionGrammarBase : NamedItem, ISymbolicExpressionGrammarBase {
    3838    #region properties for separation between implementation and persistence
    39 
    40     //TODO implement storable properties;
    41 
     39    [Storable(Name = "Symbols")]
     40    private IEnumerable<ISymbol> StorableSymbols {
     41      get { return symbols.Values.ToArray(); }
     42      set { symbols = value.ToDictionary(sym => sym.Name); }
     43    }
     44
     45    [Storable(Name = "SymbolSubtreeCount")]
     46    private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount {
     47      get { return symbolSubtreeCount.Select(x => new KeyValuePair<ISymbol, Tuple<int, int>>(GetSymbol(x.Key), x.Value)).ToArray(); }
     48      set { symbolSubtreeCount = value.ToDictionary(x => x.Key.Name, x => x.Value); }
     49    }
     50
     51    [Storable(Name = "AllowedChildSymbols")]
     52    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
     53      get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(y => GetSymbol(y)))).ToArray(); ; }
     54      set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }
     55    }
     56
     57    [Storable(Name = "AllowedChildSymbolsPerIndex")]
     58    private IEnumerable<KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>> StorableAllowedChildSymbolsPerIndex {
     59      get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(y => GetSymbol(y)))).ToArray(); }
     60      set { allowedChildSymbolsPerIndex = value.ToDictionary(x => Tuple.Create(x.Key.Item1.Name, x.Key.Item2), x => x.Value.Select(y => y.Name).ToList()); }
     61    }
    4262    #endregion
    4363
    4464    protected Dictionary<string, ISymbol> symbols;
    45     protected Dictionary<string, Tuple<int, int>> symbolSubTreeCount;
     65    protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
    4666    protected Dictionary<string, List<string>> allowedChildSymbols;
    4767    protected Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;
     
    6787      cachedMinExpressionDepth = new Dictionary<string, int>();
    6888
    69       symbolSubTreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubTreeCount);
     89      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
    7090      symbols = new Dictionary<string, ISymbol>(original.symbols);
    7191
     
    86106
    87107      symbols = new Dictionary<string, ISymbol>();
    88       symbolSubTreeCount = new Dictionary<string, Tuple<int, int>>();
     108      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
    89109      allowedChildSymbols = new Dictionary<string, List<string>>();
    90110      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
     
    95115      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
    96116      symbols.Add(symbol.Name, symbol);
    97       symbolSubTreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     117      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
    98118      ClearCaches();
    99119    }
     
    104124      for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++)
    105125        allowedChildSymbolsPerIndex.Remove(Tuple.Create(symbol.Name, i));
    106       symbolSubTreeCount.Remove(symbol.Name);
     126      symbolSubtreeCount.Remove(symbol.Name);
    107127
    108128
     
    165185      }
    166186
    167       symbolSubTreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
     187      symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
    168188      ClearCaches();
    169189    }
     
    217237
    218238    public virtual int GetMinimumSubtreeCount(ISymbol symbol) {
    219       return symbolSubTreeCount[symbol.Name].Item1;
     239      return symbolSubtreeCount[symbol.Name].Item1;
    220240    }
    221241    public virtual int GetMaximumSubtreeCount(ISymbol symbol) {
    222       return symbolSubTreeCount[symbol.Name].Item2;
     242      return symbolSubtreeCount[symbol.Name].Item2;
    223243    }
    224244
Note: See TracChangeset for help on using the changeset viewer.