Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/27/10 11:44:36 (15 years ago)
Author:
gkronber
Message:

Fixed bugs in persistence of grammars.#937 (Data types and operators for symbolic expression tree encoding)

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

Legend:

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

    r3462 r3541  
    4040  [Item("DefaultSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
    4141  public class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionGrammar {
    42     [Storable]
     42
     43    #region properties for separation between implementation and persistence
     44    [Storable]
     45    private IEnumerable<KeyValuePair<string, int>> MinSubTreeCount {
     46      get { return minSubTreeCount.AsEnumerable(); }
     47      set { minSubTreeCount = value.ToDictionary(x => x.Key, x => x.Value); }
     48    }
     49
     50    [Storable]
     51    private IEnumerable<KeyValuePair<string, int>> MaxSubTreeCount {
     52      get { return maxSubTreeCount.AsEnumerable(); }
     53      set { maxSubTreeCount = value.ToDictionary(x => x.Key, x => x.Value); }
     54    }
     55
     56    [Storable]
     57    private IEnumerable<KeyValuePair<string, IEnumerable<IEnumerable<string>>>> AllowedChildSymbols {
     58      get {
     59        return (from parentEntry in allowedChildSymbols
     60                let setEnumeration = parentEntry.Value.Select(set => set.AsEnumerable()).ToList()
     61                select new KeyValuePair<string, IEnumerable<IEnumerable<string>>>(parentEntry.Key, setEnumeration))
     62                .ToList();
     63      }
     64      set {
     65        allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
     66        foreach (var pair in value) {
     67          allowedChildSymbols[pair.Key] = new List<HashSet<string>>();
     68          foreach (var entry in pair.Value) {
     69            var hashSet = new HashSet<string>();
     70            foreach (string child in entry) {
     71              hashSet.Add(child);
     72            }
     73            allowedChildSymbols[pair.Key].Add(hashSet);
     74          }
     75        }
     76      }
     77    }
     78    [Storable]
     79    private IEnumerable<KeyValuePair<string, Symbol>> AllSymbols {
     80      get { return allSymbols.AsEnumerable(); }
     81      set { allSymbols = value.ToDictionary(x => x.Key, x => x.Value); }
     82    }
     83    #endregion
     84
    4385    private Dictionary<string, int> minSubTreeCount;
    44     [Storable]
    4586    private Dictionary<string, int> maxSubTreeCount;
    46     [Storable]
    4787    private Dictionary<string, List<HashSet<string>>> allowedChildSymbols;
    48     [Storable]
    4988    private Dictionary<string, Symbol> allSymbols;
     89    [Storable]
     90    private Symbol startSymbol;
    5091
    5192    public DefaultSymbolicExpressionGrammar()
    5293      : base() {
    53       Reset();
    54     }
    55 
    56     private void Initialize() {
    57       startSymbol = new StartSymbol();
    58       AddSymbol(startSymbol);
    59       SetMinSubtreeCount(startSymbol, 1);
    60       SetMaxSubtreeCount(startSymbol, 1);
    61     }
    62 
    63     #region ISymbolicExpressionGrammar Members
    64 
    65     private Symbol startSymbol;
    66     public Symbol StartSymbol {
    67       get { return startSymbol; }
    68       set { startSymbol = value; }
    69     }
    70 
    71     protected void Reset() {
     94      Clear();
     95    }
     96
     97    public void Clear() {
    7298      minSubTreeCount = new Dictionary<string, int>();
    7399      maxSubTreeCount = new Dictionary<string, int>();
     
    77103      cachedMaxExpressionLength = new Dictionary<string, int>();
    78104      cachedMinExpressionDepth = new Dictionary<string, int>();
    79       Initialize();
    80     }
     105
     106      startSymbol = new StartSymbol();
     107      AddSymbol(startSymbol);
     108      SetMinSubtreeCount(startSymbol, 1);
     109      SetMaxSubtreeCount(startSymbol, 1);
     110    }
     111
     112    #region ISymbolicExpressionGrammar Members
     113
     114    public Symbol StartSymbol {
     115      get { return startSymbol; }
     116      set { startSymbol = value; }
     117    }
     118
    81119
    82120    public void AddSymbol(Symbol symbol) {
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs

    r3484 r3541  
    4040      set {
    4141        minFunctionDefinitions = value;
    42         Reset();
     42        Initialize();
    4343      }
    4444    }
     
    4949      set {
    5050        maxFunctionDefinitions = value;
    51         Reset();
     51        Initialize();
    5252      }
    5353    }
     
    5858      set {
    5959        minFunctionArguments = value;
    60         Reset();
     60        Initialize();
    6161      }
    6262    }
     
    6767      set {
    6868        maxFunctionArguments = value;
    69         Reset();
     69        Initialize();
    7070      }
    7171    }
     
    8383    }
    8484
    85     [StorableHook(HookType.AfterDeserialization)]
    86     private void AfterDeserializationHook() {
    87       Reset();
    88     }
     85    private void Initialize() {
     86      base.Clear();
    8987
    90     private new void Reset() {
    91       base.Reset();
    92       Initialize();
    93     }
    94 
    95     private void Initialize() {
    9688      // remove the start symbol of the default grammar
    9789      RemoveSymbol(StartSymbol);
     
    10799      SetMaxSubtreeCount(defunSymbol, 1);
    108100
    109       // copy symbols from mainBranchGrammar
    110       foreach (var symb in mainBranchGrammar.Symbols) {
    111         AddSymbol(symb);
    112         SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
    113         SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
    114       }
     101      if (mainBranchGrammar != null) {
     102        // copy symbols from mainBranchGrammar
     103        foreach (var symb in mainBranchGrammar.Symbols) {
     104          AddSymbol(symb);
     105          SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
     106          SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
     107        }
    115108
    116       // the start symbol of the mainBranchGrammar is allowed as the result producing branch
    117       SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
     109        // the start symbol of the mainBranchGrammar is allowed as the result producing branch
     110        SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
    118111
    119       // ADF branches maxFunctionDefinitions
    120       for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
    121         SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
    122       }
     112        // ADF branches maxFunctionDefinitions
     113        for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
     114          SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
     115        }
    123116
    124       // copy syntax constraints from mainBranchGrammar
    125       foreach (var parent in mainBranchGrammar.Symbols) {
    126         for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
    127           foreach (var child in mainBranchGrammar.Symbols) {
    128             if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
    129               SetAllowedChild(parent, child, i);
     117        // copy syntax constraints from mainBranchGrammar
     118        foreach (var parent in mainBranchGrammar.Symbols) {
     119          for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
     120            foreach (var child in mainBranchGrammar.Symbols) {
     121              if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
     122                SetAllowedChild(parent, child, i);
     123              }
    130124            }
    131125          }
    132126        }
    133       }
    134127
    135       // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
    136       foreach (var symb in mainBranchGrammar.Symbols) {
    137         if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
    138           SetAllowedChild(defunSymbol, symb, 0);
     128        // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
     129        foreach (var symb in mainBranchGrammar.Symbols) {
     130          if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
     131            SetAllowedChild(defunSymbol, symb, 0);
     132        }
    139133      }
    140134    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs

    r3486 r3541  
    3636  public class SymbolicExpressionTreeNode : ICloneable {
    3737    [Storable]
    38     private List<SymbolicExpressionTreeNode> subTrees;
     38    private IList<SymbolicExpressionTreeNode> subTrees;
    3939    [Storable]
    4040    private Symbol symbol;
Note: See TracChangeset for help on using the changeset viewer.