Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/10/15 16:12:08 (9 years ago)
Author:
gkronber
Message:

#2283: experiments with q-learning

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/PartialExpressionInterpreter.cs

    r12291 r12298  
    22using System.Collections.Generic;
    33using System.Linq;
     4using System.Security.Policy;
    45using HeuristicLab.Common;
    56
     
    910    private string sentence;
    1011    private int syIdx;
     12    private HashSet<double> intermediateValues = new HashSet<double>();
    1113    private Stack<double> stack = new Stack<double>();
    12     private Stack<char> opStack = new Stack<char>();
    1314    // interprets sentences from L(G(Expr)):
    1415    // Expr -> Term { ('+' | '-' | '^' ) Term }
     
    2223    // The constant symbols '0' .. '9' are treated as ERC indices
    2324
    24     public Stack<double> Interpret(string sentence, double[] vars) {
     25    public IEnumerable<double> Interpret(string sentence, double[] vars) {
    2526      return Interpret(sentence, vars, emptyErc);
    2627    }
    2728
    28     public Stack<double> Interpret(string sentence, double[] vars, double[] erc) {
     29    public IEnumerable<double> Interpret(string sentence, double[] vars, double[] erc) {
    2930      InitLex(sentence);
    30       stack.Clear(); opStack.Clear();
     31      intermediateValues.Clear();
     32      stack.Clear();
    3133      Expr(vars, erc);
    32       return new Stack<double>(stack);
     34      return intermediateValues;
    3335    }
    3436
     
    5860        if (curSy == '+') {
    5961          NewSy();
    60           if (!Term(d, erc)) { stack.Push(-1.0); return false; }
     62          if (!Term(d, erc)) { return false; }
    6163          stack.Push(stack.Pop() + stack.Pop());
     64          intermediateValues.Add(stack.Peek());
    6265        } else if (curSy == '-') {
    6366          NewSy();
    64           if (!Term(d, erc)) { stack.Push(-2.0); return false; return false; }
     67          if (!Term(d, erc)) { return false; }
    6568          stack.Push(-stack.Pop() + stack.Pop());
     69          intermediateValues.Add(stack.Peek());
    6670        } else {
    6771          NewSy();
    68           if (!Term(d, erc)) { stack.Push(-3.0); return false; }
     72          if (!Term(d, erc)) { return false; }
    6973          var e = stack.Pop();
    7074          var r = stack.Pop();
    7175          stack.Push(Not(r) * e + r * Not(e)); // xor = (!x AND y) OR (x AND !y)
     76          intermediateValues.Add(stack.Peek());
    7277        }
    7378        curSy = CurSy();
     
    8287        if (curSy == '*') {
    8388          NewSy();
    84           if (!Fact(d, erc)) { stack.Push(-4.0); return false; }
     89          if (!Fact(d, erc)) { return false; }
    8590          stack.Push(stack.Pop() * stack.Pop());
     91          intermediateValues.Add(stack.Peek());
    8692        } else {
    8793          NewSy();
    88           if (!Fact(d, erc)) { stack.Push(-5.0); return false; }
     94          if (!Fact(d, erc)) { return false; }
    8995          var nom = stack.Pop();
    9096          var r = stack.Pop();
    9197          if (HeuristicLab.Common.Extensions.IsAlmost(nom, 0.0)) nom = 1.0;
    9298          stack.Push(r / nom);
     99          intermediateValues.Add(stack.Peek());
    93100        }
    94101        curSy = CurSy();
     
    100107      var curSy = CurSy();
    101108      if (curSy == '!') {
    102         NewSy();
    103         if (!Expr(d, erc)) { stack.Push(-7.0); return false; }
    104         stack.Push(Not(stack.Pop()));
     109        //NewSy();
     110        //if (!Expr(d, erc)) { stack.Push(-7.0); return false; }
     111        //stack.Push(Not(stack.Pop()));
    105112      } else if (curSy == '(') {
    106         NewSy();
    107         if (!Expr(d, erc)) { stack.Push(-8.0); return false; }
    108         if (CurSy() != ')') throw new ArgumentException();
    109         NewSy();
     113        //NewSy();
     114        //if (!Expr(d, erc)) { stack.Push(-8.0); return false; }
     115        //if (CurSy() != ')') throw new ArgumentException();
     116        //NewSy();
    110117      } else if (curSy >= 'a' && curSy <= 'z') {
    111118        int o = (byte)curSy - (byte)'a';
     
    113120        if (o < 0 || o >= d.Length) throw new ArgumentException();
    114121        stack.Push(d[o]);
     122        intermediateValues.Add(stack.Peek());
    115123        NewSy();
    116124      } else if (curSy == '/') {
    117125        // /-symbol is used in the expressionextender to represent inverse (1/x).
    118126        // this is necessary because we also use symbols 0..9 as indices for ERCs
    119         NewSy();
    120         if (!Fact(d, erc)) { stack.Push(-9.0); return false; }
    121         stack.Push(1.0 / stack.Pop());
     127        //NewSy();
     128        //if (!Fact(d, erc)) { stack.Push(-9.0); return false; }
     129        //stack.Push(1.0 / stack.Pop());
    122130      } else if (curSy >= '0' && curSy <= '9') {
    123131        int o = (byte)curSy - (byte)'0';
     
    125133        if (o < 0 || o >= 10) throw new ArgumentException();
    126134        stack.Push(erc[o]);
     135        intermediateValues.Add(stack.Peek());
    127136        NewSy();
    128137      } else {
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs

    r12295 r12298  
    156156    // splits the phrase into terms and creates (sparse) term-occurrance features
    157157    public IEnumerable<Feature> GetFeatures(string phrase) {
    158       // var canonicalTerms = new HashSet<string>();
    159       // foreach (string t in phrase.Split('+')) {
    160       //   canonicalTerms.Add(CanonicalTerm(t));
     158      //if (phrase.EndsWith("E")) phrase = phrase.TrimEnd('*', '+', 'E');
     159      //yield return new Feature("$$$", 1.0); // const
     160      //var canonicalTerms = new HashSet<string>();
     161      //foreach (string t in phrase.Split('+')) {
     162      //  canonicalTerms.Add(CanonicalTerm(t));
     163      //}
     164      //return canonicalTerms.Select(entry => new Feature(entry, 1.0));
     165      //.Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) });
     166
     167
     168      if (phrase.EndsWith("E")) phrase = phrase.TrimEnd('*', '+', 'E');
     169      //var len = 5;
     170      //var start = Math.Max(0, phrase.Length - len);
     171      //var end = Math.Min(phrase.Length, start + len);
     172      //string f = phrase.Substring(start, end - start);
     173      //yield return new Feature(f, 1.0);
     174      //
     175
     176      var terms = phrase.Split('+');
     177      foreach (var t in terms.Distinct()) yield return new Feature(t, 1.0);
     178
     179      for (int i = 0; i < terms.Length; i++) {
     180        for (int j = i + 1; j < terms.Length; j++) {
     181          yield return new Feature(terms[i] + " " + terms[j], 1.0);
     182        }
     183      }
     184
     185      // var substrings = new HashSet<string>();
     186      // for (int start = 0; start <= phrase.Length - 2; start += 2) {
     187      //   var s = phrase.Substring(start, 3);
     188      //   substrings.Add(s);
    161189      // }
    162       // return canonicalTerms.Select(entry => new Feature(entry, 1.0))
    163       //   .Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) });
    164 
    165       return new Feature[] { new Feature(phrase, 1.0), };
    166 
    167       // var partialInterpreter = new PartialExpressionInterpreter();
    168       // var vars = new double[] { 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, };
    169       // var s = partialInterpreter.Interpret(phrase, vars);
    170       // //if (s.Any())
    171       // //  return new Feature[] { new Feature(s.Pop().ToString(), 1.0), };
    172       // //else
    173       // //  return new Feature[] { new Feature("$", 1.0), };
    174       // return new Feature[] { new Feature(string.Join(",", s), 1.0) };
     190      //
     191      // var list = new List<string>(substrings);
     192      //
     193      // for (int i = 0; i < list.Count; i++) {
     194      //   yield return new Feature(list[i], 1.0);
     195      //   //for (int j = i+1; j < list.Count; j++) {
     196      //   //  yield return new Feature(list[i] + " " + list[j], 1.0);
     197      //   //}
     198      // }
     199
     200      //
     201      // for (int len = 1; len <= phrase.Length; len += 2) {
     202      //   var start = Math.Max(0, phrase.Length - len);
     203      //   var end = Math.Min(phrase.Length, start + len);
     204      //   string f = phrase.Substring(start, end - start);
     205      //   yield return new Feature(f, 1.0);
     206      //
     207      // }
     208
     209      //var partialInterpreter = new PartialExpressionInterpreter();
     210      //var vars = new double[] { 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, };
     211      //var s = partialInterpreter.Interpret(phrase, vars);
     212      ////if (s.Any())
     213      ////  return new Feature[] { new Feature(s.Pop().ToString(), 1.0), };
     214      ////else
     215      ////  return new Feature[] { new Feature("$", 1.0), };
     216      //return s.Select(f => new Feature(f.ToString(), 1.0));
    175217    }
    176218
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/SentenceSetStatistics.cs

    r11865 r12298  
    5050    public override string ToString() {
    5151      return
    52         string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} first {6,7:F5} {7,20} last {8,7:F5} {9,20}",
     52        string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} last {6,7:F5} {7,20}",
    5353      NumberOfSentences, AverageQuality,
    5454      BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, bestKnownQuality) ? 1.0 : 0.0,
    5555      BestSentenceIndex, TrimToSize(BestSentence, 30),
    56       FirstSentenceQuality, TrimToSize(FirstSentence, 20),
    5756      LastSentenceQuality, TrimToSize(LastSentence, 20)
     57      //LastSentenceQuality, TrimToSize(LastSentence, 20)
    5858     );
    5959    }
Note: See TracChangeset for help on using the changeset viewer.