Changeset 12290 for branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization
- Timestamp:
- 04/07/15 14:31:06 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization-gkr
- Files:
-
- 7 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.csproj
r11981 r12290 50 50 </ItemGroup> 51 51 <ItemGroup> 52 <Compile Include="PartialExpressionInterpreter.cs" /> 52 53 <Compile Include="ExpressionInterpreter.cs" /> 53 54 <Compile Include="Feature.cs" /> … … 55 56 <Compile Include="Interfaces\IProblem.cs" /> 56 57 <Compile Include="Interfaces\ISymbolicExpressionTreeProblem.cs" /> 58 <Compile Include="Problems\PrimePolynomialProblem.cs" /> 57 59 <Compile Include="Problems\PermutationProblem.cs" /> 58 60 <Compile Include="Problems\EvenParityProblem.cs" /> -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs
r12099 r12290 68 68 } 69 69 70 public IEnumerable<Feature> GetFeatures(string phrase) { 71 throw new NotImplementedException(); 70 public IEnumerable<Feature> GetFeatures(string phrase) 71 { 72 return new[] {new Feature(phrase, 1.0)}; 72 73 } 73 74 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/FindPhrasesProblem.cs
r12099 r12290 159 159 } 160 160 161 public IEnumerable<Feature> GetFeatures(string phrase) { 162 throw new NotImplementedException(); 161 public IEnumerable<Feature> GetFeatures(string phrase) 162 { 163 return new Feature[] {new Feature(phrase, 1.0),}; 163 164 } 164 165 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs
r12099 r12290 10 10 // counts the number of times a pair of symbols occurs in a sentence 11 11 public class RoyalPairProblem : ISymbolicExpressionTreeProblem { 12 private const string grammarString = @"13 G(S):14 S -> a | aS | b | bS15 ";16 17 private const string hlGrammarString = @"18 G(S):19 S -> a | b | SS20 ";21 12 22 13 private readonly IGrammar grammar; 14 private readonly int numTerminals; 23 15 public string Name { get { return "RoyalPair"; } } 24 25 public RoyalPairProblem() { 26 this.grammar = new Grammar(grammarString); 27 this.TreeBasedGPGrammar = new Grammar(hlGrammarString); 28 // TODO: allow configuration of the number of symbols 16 17 public RoyalPairProblem(int numTerminals = 2) { 18 this.numTerminals = numTerminals; 19 20 var sentenceSymbol = 'S'; 21 var terminalSymbols = Enumerable.Range(0, numTerminals).Select(off => (char)((byte)'a' + off)).ToArray(); 22 var nonTerminalSymbols = new char[] { sentenceSymbol }; 23 24 { 25 // create grammar 26 // S -> a..z | aS .. zS 27 var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString())) 28 .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString()))); 29 30 this.grammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules); 31 } 32 { 33 // create grammar for tree-based GP 34 // S -> a..z | SS 35 var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString())) 36 .Concat(new Tuple<char, string>[] { Tuple.Create(sentenceSymbol, sentenceSymbol.ToString() + sentenceSymbol) }); 37 38 this.TreeBasedGPGrammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules); 39 } 40 41 29 42 } 30 43 … … 49 62 50 63 public IEnumerable<Feature> GetFeatures(string phrase) { 51 throw new NotImplementedException(); 64 if (phrase.Length <= 1) 65 yield return new Feature("$$", 1.0); 66 else if (phrase.Length == 2) 67 yield return new Feature(phrase, 1.0); 68 else if (phrase.EndsWith("S")) // second to last symbol 69 yield return new Feature(phrase.Substring(phrase.Length - 3, 2), 1.0); 70 else // last symbol 71 yield return new Feature(phrase.Substring(phrase.Length - 2, 2), 1.0); 72 52 73 } 74 53 75 public IGrammar TreeBasedGPGrammar { get; private set; } 54 76 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs
r12099 r12290 3 3 using System.Diagnostics; 4 4 using System.Linq; 5 using System.Runtime.InteropServices; 5 6 using System.Text; 6 7 using System.Text.RegularExpressions; … … 148 149 } 149 150 150 public IEnumerable<Feature> GetFeatures(string phrase) { 151 throw new NotImplementedException(); 151 public IEnumerable<Feature> GetFeatures(string phrase) 152 { 153 return new Feature[] {new Feature(phrase, 1.0)}; 152 154 } 153 155 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs
r12099 r12290 138 138 139 139 public IEnumerable<Feature> GetFeatures(string phrase) { 140 phrase = CanonicalRepresentation(phrase);141 var isTerminal = grammar.IsTerminal(phrase);142 143 yield return new Feature(isTerminal + ToString(), 1.0);144 145 yield return new Feature("$" + (phrase.Length > 0 ? phrase[0] : ' '), 1.0);146 if (!isTerminal) {147 for (int i = 4; i < phrase.Length; i++) {148 if (!grammar.IsTerminal(phrase[i])) {149 yield return new Feature(phrase[i - 4].ToString() + phrase[i - 3].ToString() + phrase[i - 2] + phrase[i - 1], 1.0);150 break;151 }152 }153 }154 155 140 yield return new Feature(phrase, 1.0); 141 //var ant = new Ant(false); 142 //int p = 0; 143 //Run(ant, phrase.Replace('A', '.'), ref p, true); 144 //yield return new Feature(ant.PosX + "x" + ant.PosY + "-" + ant.Heading, 1.0); 156 145 } 157 146 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
r12099 r12290 19 19 // V -> a .. j 20 20 // "; 21 //private const string grammarString = @" 22 //G(E): 23 //E -> a | b | c | d | e | f | g | h | i | j | a+E | b+E | c+E | d+E | e+E | f+E | g+E | h+E | i+E | j+E | a*E | b*E | c*E | d*E | e*E | f*E | g*E | h*E | i*E | j*E 24 //"; 21 25 private const string grammarString = @" 22 26 G(E): 23 E -> a | b | c | d | e | f | g | h | i | j | a+E | b+E | c+E | d+E | e+E | f+E | g+E | h+E | i+E | j+E | a*E | b*E | c*E | d*E | e*E | f*E | g*E | h*E | i*E | j*E 27 E -> a | b | c | d | e | f | g | h | i | j | a+E | b+E | c+E | d+E | e+E | f+E | g+E | h+E | i+E | j+E | a*E | b*E | c*E | d*E | e*E | f*E | g*E | h*E | i*E | j*E 24 28 "; 25 29 … … 148 152 } 149 153 154 private double[] varIds = new double[] { }; 155 150 156 // splits the phrase into terms and creates (sparse) term-occurrance features 151 157 public IEnumerable<Feature> GetFeatures(string phrase) { 152 var canonicalTerms = new HashSet<string>(); 153 foreach (string t in phrase.Split('+')) { 154 canonicalTerms.Add(CanonicalTerm(t)); 155 } 156 return canonicalTerms.Select(entry => new Feature(entry, 1.0)) 157 .Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 158 // var canonicalTerms = new HashSet<string>(); 159 // foreach (string t in phrase.Split('+')) { 160 // canonicalTerms.Add(CanonicalTerm(t)); 161 // } 162 // return canonicalTerms.Select(entry => new Feature(entry, 1.0)) 163 // .Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 164 165 var partialInterpreter = new PartialExpressionInterpreter(); 166 var vars = new double[] { 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, }; 167 var s = partialInterpreter.Interpret(phrase, vars); 168 //if (s.Any()) 169 // return new Feature[] { new Feature(s.Pop().ToString(), 1.0), }; 170 //else 171 // return new Feature[] { new Feature("$", 1.0), }; 172 return new Feature[] { new Feature(string.Join(",", s), 1.0) }; 158 173 } 159 174
Note: See TracChangeset
for help on using the changeset viewer.