Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/27/15 16:34:34 (9 years ago)
Author:
gkronber
Message:

linear value function approximation and good results for poly-10 benchmark

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs

    r11803 r11832  
    5050    }
    5151
    52     public string CanonicalRepresentation(string terminalPhrase) {
     52    public string CanonicalRepresentation(string phrase) {
    5353      throw new NotImplementedException();
    54       return terminalPhrase;
     54      return phrase;
     55    }
     56
     57    public IEnumerable<Feature> GetFeatures(string phrase)
     58    {
     59      throw new NotImplementedException();
    5560    }
    5661  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/ExpressionInterpreter.cs

    r11803 r11832  
    33using System.Diagnostics.Eventing.Reader;
    44using System.Linq;
     5using System.Runtime.CompilerServices;
    56using System.Runtime.Remoting.Messaging;
    67using System.Text;
     
    1011namespace HeuristicLab.Problems.GrammaticalOptimization {
    1112  public class ExpressionInterpreter {
     13    private static readonly double[] emptyErc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
     14
    1215    private string sentence;
    1316    private int syIdx;
     
    1518    // Expr -> Term { ('+' | '-' | '^' ) Term }
    1619    // Term -> Fact { ('*' | '/') Fact }
    17     // Fact -> '!' Expr | '(' Expr ')' | Var
     20    // Fact -> '!' Expr | '(' Expr ')' | Var | const
    1821    // Var -> 'a'..'z'
     22    // const -> '0' .. '9'
     23
     24    // constants are Koza-style ephemeral random constants (ERC). for now we only allow up to 10 constants.
     25    // The constant symbols '0' .. '9' are treated as ERC indices
    1926
    2027    // for boolean problems the symbol * representes the AND operator, the symbol + represents the OR operator, ! = NOT, ^ = XOR
    2128
    2229    public bool Interpret(string sentence, bool[] vars) {
     30      return Interpret(sentence, vars, emptyErc);
     31    }
     32
     33    public bool Interpret(string sentence, bool[] vars, double[] erc) {
    2334      InitLex(sentence);
    2435      var d = new double[vars.Length];
    2536      for (int i = 0; i < vars.Length; i++) d[i] = vars[i] ? 1.0 : 0.0;
    26       return !(Expr(d).IsAlmost(0));
     37      return !(Expr(d, erc).IsAlmost(0));
    2738    }
    2839
    2940    public double Interpret(string sentence, double[] vars) {
     41      return Interpret(sentence, vars, emptyErc);
     42    }
     43
     44    public double Interpret(string sentence, double[] vars, double[] erc) {
    3045      InitLex(sentence);
    31       return Expr(vars);
     46      return Expr(vars, erc);
    3247    }
    3348
     
    5166    }
    5267
    53     private double Expr(double[] d) {
     68    private double Expr(double[] d, double[] erc) {
    5469      var r = 0.0;
    55       r = Term(d);
     70      r = Term(d, erc);
    5671      var curSy = CurSy();
    5772      while (curSy == '+' || curSy == '-' || curSy == '^') {
    5873        if (curSy == '+') {
    5974          NewSy();
    60           r += Expr(d);
     75          r += Expr(d, erc);
    6176        } else if (curSy == '-') {
    6277          NewSy();
    63           r -= Expr(d);
     78          r -= Expr(d, erc);
    6479        } else {
    6580          NewSy();
    66           var e = Expr(d);
     81          var e = Expr(d, erc);
    6782          r = Not(r) * e + r * Not(e); // xor = (!x AND y) OR (x AND !y)
    6883        }
     
    7287    }
    7388
    74     private double Term(double[] d) {
     89    private double Term(double[] d, double[] erc) {
    7590      var r = 0.0;
    76       r = Fact(d);
     91      r = Fact(d, erc);
    7792      var curSy = CurSy();
    7893      while (curSy == '*' || curSy == '/') {
    7994        if (curSy == '*') {
    8095          NewSy();
    81           r *= Term(d);
     96          r *= Term(d, erc);
    8297        } else {
    8398          NewSy();
    84           r /= Term(d);
     99          r /= Term(d, erc);
    85100        }
    86101        curSy = CurSy();
     
    89104    }
    90105
    91     private double Fact(double[] d) {
     106    private double Fact(double[] d, double[] erc) {
    92107      double r = 0.0;
    93108      var curSy = CurSy();
    94109      if (curSy == '!') {
    95110        NewSy();
    96         r = Not(Expr(d));
     111        r = Not(Expr(d, erc));
    97112      } else if (curSy == '(') {
    98113        NewSy();
    99         r = Expr(d);
     114        r = Expr(d, erc);
    100115        if (CurSy() != ')') throw new ArgumentException();
    101116        NewSy();
    102       } else /* if (curSy >= 'a' && curSy <= 'z') */ {
     117      } else if (curSy >= 'a' && curSy <= 'z') {
    103118        int o = (byte)curSy - (byte)'a';
    104119        //int o = Convert.ToByte(CurSy()) - Convert.ToByte('a');
    105120        if (o < 0 || o >= d.Length) throw new ArgumentException();
    106121        r = d[o];
     122        NewSy();
     123      } else /* if (curSy >= '0' && curSy <= '9') */ {
     124        int o = (byte)curSy - (byte)'0';
     125        //int o = Convert.ToByte(CurSy()) - Convert.ToByte('a');
     126        if (o < 0 || o >= 10) throw new ArgumentException();
     127        r = erc[o];
    107128        NewSy();
    108129      }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/FindPhrasesProblem.cs

    r11803 r11832  
    129129    }
    130130
    131     public string CanonicalRepresentation(string terminalPhrase) {
     131    public string CanonicalRepresentation(string phrase) {
    132132      // as the ordering of phrases does not matter we can reorder the phrases
    133133      // and remove duplicates
    134       var numPhrases = terminalPhrase.Length / phraseLen;
     134      var numPhrases = phrase.Length / phraseLen;
    135135      var phrases = new SortedSet<string>();
    136136      for (int phraseIdx = 0; phraseIdx < numPhrases; phraseIdx++) {
    137137        var sentenceIdx = phraseIdx * phraseLen;
    138         var phrase = terminalPhrase.Substring(sentenceIdx, phraseLen);
    139         phrase = CanonicalPhrase(phrase);
    140         if (!phrases.Contains(phrase)) phrases.Add(phrase);
     138        var subphrase = phrase.Substring(sentenceIdx, phraseLen);
     139        subphrase = CanonicalPhrase(subphrase);
     140        if (!phrases.Contains(subphrase)) phrases.Add(subphrase);
    141141      }
    142       var remainder = terminalPhrase.Substring(numPhrases * phraseLen, terminalPhrase.Length - (numPhrases * phraseLen));
     142      var remainder = phrase.Substring(numPhrases * phraseLen, phrase.Length - (numPhrases * phraseLen));
    143143      remainder = CanonicalPhrase(remainder);
    144144      if (!phrases.Contains(remainder)) phrases.Add(remainder);
     
    146146      return string.Join("", phrases);
    147147    }
     148
     149    public IEnumerable<Feature> GetFeatures(string phrase)
     150    {
     151      throw new NotImplementedException();
     152    }
     153
     154    public override string ToString() {
     155      return string.Format("\"FindPhrasesProblem {0} {1} {2} {3:F2} {4} {5:F2} {6}\"", numPhrases, phraseLen,
     156        optimalPhrases.Count, correctReward, decoyPhrases.Count, decoyReward, phrasesAsSets);
     157    }
    148158  }
    149159}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/HardPalindromeProblem.cs

    r11803 r11832  
    3939    }
    4040
    41     public string CanonicalRepresentation(string terminalPhrase) {
    42       return terminalPhrase;
     41    public string CanonicalRepresentation(string phrase) {
     42      return phrase;
     43    }
     44
     45    public IEnumerable<Feature> GetFeatures(string phrase)
     46    {
     47      throw new NotImplementedException();
    4348    }
    4449  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs

    r11803 r11832  
    8080    }
    8181
    82     public string CanonicalRepresentation(string terminalPhrase) {
    83       return terminalPhrase;
     82    public string CanonicalRepresentation(string phrase) {
     83      return phrase;
     84    }
     85
     86    public IEnumerable<Feature> GetFeatures(string phrase)
     87    {
     88      throw new NotImplementedException();
    8489    }
    8590  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs

    r11803 r11832  
    3535    }
    3636
    37     public string CanonicalRepresentation(string terminalPhrase) {
     37    public string CanonicalRepresentation(string phrase) {
     38      //throw new NotImplementedException();
     39      return phrase;
     40    }
     41
     42    public IEnumerable<Feature> GetFeatures(string phrase)
     43    {
    3844      throw new NotImplementedException();
    39       return terminalPhrase;
    4045    }
    4146  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs

    r11806 r11832  
    113113    }
    114114
    115     public string CanonicalRepresentation(string terminalPhrase) {
     115    public string CanonicalRepresentation(string phrase) {
    116116      if (phrasesAsSets) {
    117117        var sb = new StringBuilder();
    118         var numPhrases = terminalPhrase.Length / phraseLen;
     118        var numPhrases = phrase.Length / phraseLen;
    119119        for (int phraseIdx = 0; phraseIdx < numPhrases; phraseIdx++) {
    120120          var sentenceIdx = phraseIdx * phraseLen;
    121           var phrase = terminalPhrase.Substring(sentenceIdx, phraseLen);
    122           phrase = CanonicalPhrase(phrase);
    123           sb.Append(phrase);
     121          var subphrase = phrase.Substring(sentenceIdx, phraseLen);
     122          subphrase = CanonicalPhrase(subphrase);
     123          sb.Append(subphrase);
    124124        }
    125125
    126         var remainder = terminalPhrase.Substring(numPhrases * phraseLen, terminalPhrase.Length - (numPhrases * phraseLen));
     126        var remainder = phrase.Substring(numPhrases * phraseLen, phrase.Length - (numPhrases * phraseLen));
    127127        remainder = CanonicalPhrase(remainder);
    128128        sb.Append(remainder);
     
    130130        return sb.ToString();
    131131      } else
    132         return terminalPhrase;
     132        return phrase;
     133    }
     134
     135    public IEnumerable<Feature> GetFeatures(string phrase)
     136    {
     137      throw new NotImplementedException();
    133138    }
    134139  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalRoadProblem.cs

    r11803 r11832  
    2929      throw new NotImplementedException();
    3030    }
    31     public string CanonicalRepresentation(string terminalPhrase) {
    32       return terminalPhrase;
     31    public string CanonicalRepresentation(string phrase) {
     32      return phrase;
    3333    }
    3434
     35    public IEnumerable<Feature> GetFeatures(string phrase)
     36    {
     37      throw new NotImplementedException();
     38    }
    3539  }
    3640}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSequenceProblem.cs

    r11803 r11832  
    7777
    7878    // in each position there could be multiple correct and incorrect symbols
    79     public string CanonicalRepresentation(string terminalPhrase) {
     79    public string CanonicalRepresentation(string phrase) {
    8080      var sb = new StringBuilder();
    81       for (int i = 0; i < terminalPhrase.Length; i++) {
    82         if (optimalSymbolsForPos[i].Contains(terminalPhrase[i])) {
     81      for (int i = 0; i < phrase.Length; i++) {
     82        if (optimalSymbolsForPos[i].Contains(phrase[i])) {
    8383          sb.Append(optimalSymbolsForPos[i].First()); // all symbols in the set are equivalent
    8484        } else {
    85           sb.Append(terminalPhrase[i]);
     85          sb.Append(phrase[i]);
    8686        }
    8787      }
    8888      return sb.ToString();
    8989    }
     90
     91    public IEnumerable<Feature> GetFeatures(string phrase)
     92    {
     93      throw new NotImplementedException();
     94    }
    9095  }
    9196}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSymbolProblem.cs

    r11806 r11832  
    3434      return regex.Matches(sentence).Count;
    3535    }
    36     public string CanonicalRepresentation(string terminalPhrase) {
     36    public string CanonicalRepresentation(string phrase) {
    3737      throw new NotImplementedException();
    38       return terminalPhrase;
     38      return phrase;
    3939    }
    4040
     41    public IEnumerable<Feature> GetFeatures(string phrase)
     42    {
     43      throw new NotImplementedException();
     44    }
    4145  }
    4246}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalTreeProblem.cs

    r11803 r11832  
    2929      throw new NotImplementedException();
    3030    }
    31     public string CanonicalRepresentation(string terminalPhrase) {
     31    public string CanonicalRepresentation(string phrase) {
    3232      throw new NotImplementedException();
    33       return terminalPhrase;
     33      return phrase;
    3434    }
    3535
     36    public IEnumerable<Feature> GetFeatures(string phrase)
     37    {
     38      throw new NotImplementedException();
     39    }
    3640  }
    3741}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs

    r11806 r11832  
    102102    }
    103103
    104     public string CanonicalRepresentation(string terminalPhrase) {
    105       var sb = new StringBuilder(terminalPhrase);
    106       string canonicalPhrase = terminalPhrase;
     104    public string CanonicalRepresentation(string phrase) {
     105      var sb = new StringBuilder(phrase);
     106      string canonicalPhrase = phrase;
    107107      string oldPhrase;
    108108      do {
     
    112112      } while (canonicalPhrase != oldPhrase);
    113113      return sb.ToString();
     114    }
     115
     116    public IEnumerable<Feature> GetFeatures(string phrase) {
     117      return Enumerable.Repeat(new Feature(CanonicalRepresentation(phrase), 1.0), 1);
    114118    }
    115119  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs

    r11806 r11832  
    120120      return canonicalTerm;
    121121    }
     122
     123    // splits the phrase into terms and creates (sparse) term-occurrance features
     124    public IEnumerable<Feature> GetFeatures(string phrase) {
     125      var canonicalTerms = new HashSet<string>();
     126      foreach (string t in phrase.Split('+')) {
     127        canonicalTerms.Add(CanonicalTerm(t));
     128      }
     129      return canonicalTerms.Select(entry => new Feature(entry, 1.0)).Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) });
     130    }
     131
    122132  }
    123133}
Note: See TracChangeset for help on using the changeset viewer.