Changeset 15828
- Timestamp:
- 03/07/18 12:52:16 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15827 r15828 166 166 167 167 // ADDITION 168 if ( ReferenceEquals(currentSymbol, Addition)) {168 if (currentSymbol == Addition) { 169 169 var uniqueChildHashes = new HashSet<THashType>(); 170 170 171 171 // First subtree 172 if ( ReferenceEquals(parseStack.Peek(), Addition)) {172 if (parseStack.Peek() == Addition) { 173 173 uniqueChildHashes.UnionWith(GetSubtreeHashes(parseStack, aggregateHashes)); 174 174 } else { … … 177 177 } 178 178 // Second subtree 179 if ( ReferenceEquals(parseStack.Peek(), Addition)) {179 if (parseStack.Peek() == Addition) { 180 180 uniqueChildHashes.UnionWith(GetSubtreeHashes(parseStack, aggregateHashes)); 181 181 } else { … … 190 190 191 191 // MULTIPLICATION 192 if ( ReferenceEquals(currentSymbol, Multiplication)) {192 if (currentSymbol == Multiplication) { 193 193 var childHashes = new List<THashType>(); 194 194 195 195 // First subtree 196 if ( ReferenceEquals(parseStack.Peek(), Multiplication)) {196 if (parseStack.Peek() == Multiplication) { 197 197 childHashes.AddRange(GetSubtreeHashes(parseStack, aggregateHashes)); 198 198 } else { … … 200 200 } 201 201 // Second subtree 202 if ( ReferenceEquals(parseStack.Peek(), Multiplication)) {202 if (parseStack.Peek() == Multiplication) { 203 203 childHashes.AddRange(GetSubtreeHashes(parseStack, aggregateHashes)); 204 204 } else { … … 231 231 232 232 // LOG, EXP, SIN, INV 233 if ( ReferenceEquals(currentSymbol, Log) || ReferenceEquals(currentSymbol, Exp)||234 ReferenceEquals(currentSymbol, Sin) || ReferenceEquals(currentSymbol, Cos)||235 ReferenceEquals(currentSymbol, Inv)) {233 if (currentSymbol == Log || currentSymbol == Exp || 234 currentSymbol == Sin || currentSymbol == Cos || 235 currentSymbol == Inv) { 236 236 return new[] { aggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack, aggregateHashes)) }; 237 237 } … … 244 244 var hashesArray = hashes.ToArray(); 245 245 246 if (( ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) && hashesArray.Count()<= 1) {246 if ((operatorSym == Addition || operatorSym == Multiplication) && hashesArray.Length <= 1) { 247 247 return hashesArray[0]; 248 248 } … … 251 251 } 252 252 253 return $"[{ hashesArray.Aggregate(operatorSym.StringRepresentation, (result, ti) => string.Concat(result, " ° ", ti))}]"; // TODO: use string join instead of string.Concat253 return $"[{operatorSym.StringRepresentation} ° {string.Join(" ° ", hashesArray)}]"; 254 254 } 255 255 256 256 private int AggregateIntHashes(Symbol operatorSym, int[] hashes) { 257 257 int start; 258 if ((ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) && 259 hashes.Length <= 1) { 258 if ((operatorSym == Addition || operatorSym == Multiplication) && hashes.Length <= 1) { 260 259 start = 0; 261 260 … … 302 301 ISymbolicExpressionTreeNode parsedSubTree = null; 303 302 304 if ( ReferenceEquals(currentSymbol, Addition)) {303 if (currentSymbol == Addition) { 305 304 parsedSubTree = addSy.CreateTreeNode(); 306 305 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // left part 307 306 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // right part 308 307 309 } else if ( ReferenceEquals(currentSymbol, Multiplication)) {308 } else if (currentSymbol == Multiplication) { 310 309 parsedSubTree = mulSy.CreateTreeNode(); 311 310 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // left part 312 311 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // right part 313 312 314 } else if ( ReferenceEquals(currentSymbol, Log)) {313 } else if (currentSymbol == Log) { 315 314 parsedSubTree = logSy.CreateTreeNode(); 316 315 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 317 316 318 } else if ( ReferenceEquals(currentSymbol, Exp)) {317 } else if (currentSymbol == Exp) { 319 318 parsedSubTree = expSy.CreateTreeNode(); 320 319 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 321 320 322 } else if ( ReferenceEquals(currentSymbol, Sin)) {321 } else if (currentSymbol == Sin) { 323 322 parsedSubTree = sinSy.CreateTreeNode(); 324 323 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 325 324 326 } else if ( ReferenceEquals(currentSymbol, Cos)) {325 } else if (currentSymbol == Cos) { 327 326 parsedSubTree = cosSy.CreateTreeNode(); 328 327 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 329 328 330 } else if ( ReferenceEquals(currentSymbol, Inv)) {329 } else if (currentSymbol == Inv) { 331 330 parsedSubTree = divSy.CreateTreeNode(); 332 331 ConstantTreeNode dividend = (ConstantTreeNode)constSy.CreateTreeNode(); -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs
r15806 r15828 5 5 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration { 6 6 7 // TODO: struct not useful here because you are boxing the structs with a new call below 8 public struct StoredSymbolString { 7 public class StoredSymbolString { 9 8 public readonly int Hash; 10 9 public readonly SymbolString SymbolString; … … 80 79 SymbolString result = storedValues[hash]; 81 80 storedValues.Remove(hash); 82 return new StoredSymbolString(hash, result); // TODO: new for structs creates a box anyway81 return new StoredSymbolString(hash, result); 83 82 } 84 83 85 84 public void Store(int hash, SymbolString s) { 86 storeInternal .Invoke(hash); // TODO: explicit .Invoke() not necessary, just storeInternal(hash) is ok85 storeInternal(hash); 87 86 storedValues[hash] = s; 88 87 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
r15812 r15828 1 using System.Collections.Generic; 1 using System; 2 using System.Collections.Generic; 2 3 3 4 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { 4 5 5 public abstract class Symbol { 6 public abstract class Symbol : IEquatable<Symbol> { 7 private readonly int stringRepresentationHash; 8 6 9 public string StringRepresentation { get; } 7 10 8 11 protected Symbol(string representation) { 9 12 StringRepresentation = representation; 13 stringRepresentationHash = representation.GetHashCode(); 10 14 } 11 15 … … 13 17 return StringRepresentation; 14 18 } 19 20 #region IEquatable 21 public static bool operator ==(Symbol s1, Symbol s2) { 22 return !ReferenceEquals(null, s1) && !ReferenceEquals(null, s2) && (ReferenceEquals(s1, s2) 23 || s1.Equals(s2)); 24 } 25 26 public static bool operator !=(Symbol s1, Symbol s2) { 27 return !(s1 == s2); 28 } 29 30 public bool Equals(Symbol other) { 31 if (ReferenceEquals(null, other)) return false; 32 if (ReferenceEquals(this, other)) return true; 33 return string.Equals(StringRepresentation, other.StringRepresentation); 34 } 35 36 public override bool Equals(object obj) { 37 if (ReferenceEquals(null, obj)) return false; 38 if (ReferenceEquals(this, obj)) return true; 39 if (obj.GetType() != this.GetType()) return false; 40 return Equals((Symbol)obj); 41 } 42 43 public override int GetHashCode() { 44 return stringRepresentationHash; 45 } 46 #endregion 15 47 } 16 48
Note: See TracChangeset
for help on using the changeset viewer.