Changeset 11799 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/RoyalSequenceProblem.cs
- Timestamp:
- 01/19/15 20:09:12 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/RoyalSequenceProblem.cs
r11792 r11799 31 31 this.correctReward = correctReward; 32 32 this.incorrectReward = incorrectReward; 33 var sentenceSymbol = 'S';33 const char sentenceSymbol = 'S'; 34 34 var terminalSymbols = Enumerable.Range(0, alphabetSize).Select(off => (char)((byte)'a' + off)).ToArray(); 35 var nonTerminalSymbols = new char[] { 'S'};36 var rules = terminalSymbols.Select(t => Tuple.Create( 'S', t.ToString()))37 .Concat(terminalSymbols.Select(t => Tuple.Create( 'S', t + "S")));35 var nonTerminalSymbols = new char[] { sentenceSymbol }; 36 var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString())) 37 .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString()))); 38 38 //var rules = terminalSymbols.Select(t => Tuple.Create('S', t + "S")) 39 39 // .Concat(terminalSymbols.Select(t => Tuple.Create('S', t.ToString()))); … … 64 64 // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow! 65 65 Debug.Assert(sentence.Any(c => grammar.IsTerminal(c))); 66 // as long as only correct symbols are found we increase the reward by +167 // on the first incorrect symbol we return68 66 var reward = 0.0; 69 67 for (int i = 0; i < Math.Min(sentence.Length, sequenceLen); i++) { … … 71 69 reward += correctReward; 72 70 } else { 73 // alternativelyreduce reward by number of remaining symbols71 // reduce reward by number of remaining symbols 74 72 return Math.Max(0.0, reward + incorrectReward * (sentence.Length - i)); 75 // stop on first incorrect symbol and return reward76 //return reward;77 73 } 78 74 } … … 80 76 } 81 77 78 // in each position there could be multiple correct and incorrect symbols 82 79 public string CanonicalRepresentation(string terminalPhrase) { 83 throw new NotImplementedException(); 84 return terminalPhrase; 80 var sb = new StringBuilder(); 81 for (int i = 0; i < terminalPhrase.Length; i++) { 82 if (optimalSymbolsForPos[i].Contains(terminalPhrase[i])) { 83 sb.Append(optimalSymbolsForPos[i].First()); // all symbols in the set are equivalent 84 } else { 85 sb.Append(terminalPhrase[i]); 86 } 87 } 88 return sb.ToString(); 85 89 } 86 90 }
Note: See TracChangeset
for help on using the changeset viewer.