Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/07/15 14:31:06 (10 years ago)
Author:
gkronber
Message:

#2283 created a new branch to separate development from aballeit

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  
    5050  </ItemGroup>
    5151  <ItemGroup>
     52    <Compile Include="PartialExpressionInterpreter.cs" />
    5253    <Compile Include="ExpressionInterpreter.cs" />
    5354    <Compile Include="Feature.cs" />
     
    5556    <Compile Include="Interfaces\IProblem.cs" />
    5657    <Compile Include="Interfaces\ISymbolicExpressionTreeProblem.cs" />
     58    <Compile Include="Problems\PrimePolynomialProblem.cs" />
    5759    <Compile Include="Problems\PermutationProblem.cs" />
    5860    <Compile Include="Problems\EvenParityProblem.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs

    r12099 r12290  
    6868    }
    6969
    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)};
    7273    }
    7374
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/FindPhrasesProblem.cs

    r12099 r12290  
    159159    }
    160160
    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),};
    163164    }
    164165
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs

    r12099 r12290  
    1010  // counts the number of times a pair of symbols occurs in a sentence
    1111  public class RoyalPairProblem : ISymbolicExpressionTreeProblem {
    12     private const string grammarString = @"
    13 G(S):
    14 S -> a | aS | b | bS
    15 ";
    16 
    17     private const string hlGrammarString = @"
    18 G(S):
    19 S -> a | b | SS
    20 ";
    2112
    2213    private readonly IGrammar grammar;
     14    private readonly int numTerminals;
    2315    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
    2942    }
    3043
     
    4962
    5063    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
    5273    }
     74
    5375    public IGrammar TreeBasedGPGrammar { get; private set; }
    5476    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs

    r12099 r12290  
    33using System.Diagnostics;
    44using System.Linq;
     5using System.Runtime.InteropServices;
    56using System.Text;
    67using System.Text.RegularExpressions;
     
    148149    }
    149150
    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)};
    152154    }
    153155
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs

    r12099 r12290  
    138138
    139139    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    
    155140      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);
    156145    }
    157146
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs

    r12099 r12290  
    1919    //    V -> a .. j
    2020    //    ";
     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    //";
    2125    private const string grammarString = @"
    2226    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  
    2428    ";
    2529
     
    148152    }
    149153
     154    private double[] varIds = new double[] { };
     155
    150156    // splits the phrase into terms and creates (sparse) term-occurrance features
    151157    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) };
    158173    }
    159174
Note: See TracChangeset for help on using the changeset viewer.