- Timestamp:
- 09/22/18 21:57:59 (6 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
r16053 r16176 26 26 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { 27 27 [StorableClass] 28 public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {28 public abstract class Symbol : DeepCloneable, IEquatable<Symbol>, IComparable<Symbol> { 29 29 [Storable] 30 30 private readonly int stringRepresentationHash; … … 78 78 return stringRepresentationHash; 79 79 } 80 81 public int CompareTo(Symbol other) { 82 var v1 = this as VariableTerminalSymbol; 83 var v2 = other as VariableTerminalSymbol; 84 85 if ((v1 == null && v2 == null) || (v1 != null && v2 != null)) 86 return this.StringRepresentation.CompareTo(other.StringRepresentation); 87 return v1 == null ? 1 : -1; 88 } 80 89 #endregion 81 90 } … … 119 128 protected NonterminalSymbol(bool deserializing) : base(deserializing) { } 120 129 } 121 122 123 130 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs
r16053 r16176 28 28 29 29 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { 30 30 31 [StorableClass] 31 32 public abstract class Hasher<THashType> : DeepCloneable { 33 protected struct HashItem : IComparable<HashItem>, IEquatable<HashItem> { 34 public THashType Value { get; private set; } 35 public Symbol Symbol { get; private set; } 36 37 public int CompareTo(HashItem other) { 38 var res = Symbol.CompareTo(other.Symbol); 39 return res == 0 ? Comparer<THashType>.Default.Compare(Value, other.Value) : res; 40 } 41 42 private HashItem(THashType h, Symbol s) { 43 Value = h; 44 Symbol = s; 45 } 46 47 public static HashItem Create(THashType h, Symbol s) { 48 return new HashItem(h, s); 49 } 50 51 public bool Equals(HashItem other) { 52 return Symbol.Equals(other.Symbol) && Value.Equals(other.Value); 53 } 54 } 32 55 protected Hasher(Grammar grammar) { 33 56 Grammar = grammar; … … 41 64 protected Grammar Grammar { get; private set; } 42 65 43 protected abstract THashType AggregateHashes(Symbol operatorSym, IList<THashType> hashes);66 protected abstract HashItem AggregateHashes(Symbol operatorSym, IList<HashItem> hashes); 44 67 45 68 public int CalcHashCode(SymbolList sentence) { … … 49 72 50 73 Symbol peek = parseStack.Peek(); 51 return AggregateHashes(peek, GetSubtreeHashes(parseStack)). GetHashCode();74 return AggregateHashes(peek, GetSubtreeHashes(parseStack)).Value.GetHashCode(); 52 75 } 53 76 … … 55 78 protected Hasher(bool deserializing) { } 56 79 57 private IList< THashType> GetSubtreeHashes(Stack<Symbol> parseStack) {80 private IList<HashItem> GetSubtreeHashes(Stack<Symbol> parseStack) { 58 81 Symbol currentSymbol = parseStack.Pop(); 59 82 … … 72 95 currentSymbol == Grammar.Sin || currentSymbol == Grammar.Inv) { 73 96 // Directly aggregate the single subtree 74 return new[] { AggregateHashes( parseStack.Peek(), GetSubtreeHashes(parseStack)) };97 return new[] { AggregateHashes(currentSymbol, GetSubtreeHashes(parseStack)) }; 75 98 } 76 99 77 100 // var, const or nonterminal symbol 78 return new[] { AggregateHashes(currentSymbol, new THashType[0]) };79 } 80 81 private void AddAdditionChildHashes(HashSet< THashType> hashSet, Stack<Symbol> symbolStack) {101 return new[] { AggregateHashes(currentSymbol, new HashItem[0]) }; 102 } 103 104 private void AddAdditionChildHashes(HashSet<HashItem> hashSet, Stack<Symbol> symbolStack) { 82 105 var peek = symbolStack.Peek(); 83 106 var hashes = GetSubtreeHashes(symbolStack); // will pop the stack … … 89 112 } 90 113 91 private IList< THashType> GetAdditionSubtreeHashes(Stack<Symbol> parseStack) {92 var uniqueChildHashes = new HashSet< THashType>();114 private IList<HashItem> GetAdditionSubtreeHashes(Stack<Symbol> parseStack) { 115 var uniqueChildHashes = new HashSet<HashItem>(); 93 116 94 117 AddAdditionChildHashes(uniqueChildHashes, parseStack); // first child … … 100 123 } 101 124 102 private void AddMultiplicationChildHashes(List< THashType> hashList, Stack<Symbol> symbolStack) {125 private void AddMultiplicationChildHashes(List<HashItem> hashList, Stack<Symbol> symbolStack) { 103 126 var peek = symbolStack.Peek(); 104 127 var hashes = GetSubtreeHashes(symbolStack); … … 109 132 } 110 133 111 p ublic IList<THashType> GetMultiplicationSubtreeHashes(Stack<Symbol> parseStack) {112 var childHashes = new List< THashType>();134 private IList<HashItem> GetMultiplicationSubtreeHashes(Stack<Symbol> parseStack) { 135 var childHashes = new List<HashItem>(); 113 136 114 137 AddMultiplicationChildHashes(childHashes, parseStack); // first child … … 159 182 protected IntHasher(bool deserializing) : base(deserializing) { } 160 183 161 protected override int AggregateHashes(Symbol operatorSym, IList<int> hashes) {184 protected override HashItem AggregateHashes(Symbol operatorSym, IList<HashItem> hashes) { 162 185 int start; 163 186 if ((operatorSym == Grammar.Addition || operatorSym == Grammar.Multiplication) && hashes.Count <= 1) { … … 165 188 166 189 } else if (operatorSym is NonterminalSymbol || operatorSym is VariableTerminalSymbol) { 167 return operatorSym.StringRepresentation.GetHashCode();190 return HashItem.Create(operatorSym.StringRepresentation.GetHashCode(), operatorSym); 168 191 169 192 } else { … … 172 195 173 196 for (int i = 0; i < hashes.Count; i++) { 174 start = ((start << 5) + start) ^ hashes[i] ;175 } 176 return start;197 start = ((start << 5) + start) ^ hashes[i].Value; 198 } 199 return HashItem.Create(start, operatorSym); 177 200 } 178 201 } … … 191 214 protected StringHasher(bool deserializing) : base(deserializing) { } 192 215 193 protected override string AggregateHashes(Symbol operatorSym, IList<string> hashes) {216 protected override HashItem AggregateHashes(Symbol operatorSym, IList<HashItem> hashes) { 194 217 var hashesArray = hashes.ToArray(); 195 218 … … 198 221 } 199 222 if (operatorSym is NonterminalSymbol || operatorSym is VariableTerminalSymbol) { 200 return operatorSym.StringRepresentation;201 } 202 203 return $"[{operatorSym.StringRepresentation} ° {string.Join(" ° ", hashesArray)}]";223 return HashItem.Create(operatorSym.StringRepresentation, operatorSym); 224 } 225 226 return HashItem.Create($"[{operatorSym.StringRepresentation} ° {string.Join(" ° ", hashesArray.Select(x => x.Value))}]", operatorSym); 204 227 } 205 228 }
Note: See TracChangeset
for help on using the changeset viewer.