Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SymbolicRegressionPoly10Problem.cs @ 11770

Last change on this file since 11770 was 11770, checked in by gkronber, 9 years ago

#2283: worked on generic sequential search alg with bandit policy as parameter

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Net;
5using System.Security;
6using System.Security.AccessControl;
7using System.Text;
8using HeuristicLab.Common;
9
10namespace HeuristicLab.Problems.GrammaticalOptimization {
11  public class SymbolicRegressionPoly10Problem : IProblem {
12    //    private const string grammarString = @"
13    //    G(E):
14    //    E -> V | V+E | V-E | V*E | (E)
15    //    V -> a .. j
16    //    ";
17    private const string grammarString = @"
18    G(E):
19    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
20    ";
21
22
23    private readonly IGrammar grammar;
24
25    private readonly int N;
26    private readonly double[][] x;
27    private readonly double[] y;
28
29    public SymbolicRegressionPoly10Problem() {
30      this.grammar = new Grammar(grammarString);
31
32      this.N = 500;
33      this.x = new double[N][];
34      this.y = new double[N];
35
36      GenerateData();
37    }
38
39    private void GenerateData() {
40      // generate data with fixed seed to make sure that data is always the same
41      var rand = new Random(31415);
42      for (int i = 0; i < N; i++) {
43        x[i] = new double[10];
44        for (int j = 0; j < 10; j++) {
45          x[i][j] = rand.NextDouble() * 2 - 1;
46        }
47        // poly-10 no noise
48        /* a*b + c*d + e*f + a*g*i + c*f*j */
49        y[i] = x[i][0] * x[i][1] +
50               x[i][2] * x[i][3] +
51               x[i][4] * x[i][5] +
52               x[i][0] * x[i][6] * x[i][8] +
53               x[i][2] * x[i][5] * x[i][9];
54      }
55    }
56
57    public double BestKnownQuality(int maxLen) {
58      // for now only an upper bound is returned, ideally we have an R² of 1.0
59      // the optimal R² can only be reached for sentences of at least 23 symbols
60      return 1.0;
61    }
62
63    public IGrammar Grammar {
64      get { return grammar; }
65    }
66
67    public double Evaluate(string sentence) {
68      var interpreter = new ExpressionInterpreter();
69      return HeuristicLab.Common.Extensions.RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i])).ToArray());
70    }
71
72
73
74    // right now only + and * is supported
75    private Dictionary<string, string> cache = new Dictionary<string, string>();
76    public string CanonicalRepresentation(string phrase) {
77      string res;
78      if (!cache.TryGetValue(phrase, out res)) {
79        var terms = phrase.Split('+').Select(t => t.Replace("*", ""));
80        var terminalTerms = terms.Where(t => t.All(ch => grammar.IsTerminal(ch)));
81        var nonTerminalTerms = terms.Where(t => t.Any(ch => grammar.IsNonTerminal(ch)));
82
83        res = string.Join("+", terminalTerms.Select(term => CanonicalTerm(term)).OrderBy(term => term).Concat(nonTerminalTerms.Select(term => CanonicalTerm(term))));
84        cache[phrase] = res;
85      }
86      return res;
87    }
88
89    private string CanonicalTerm(string term) {
90      return string.Join("", term.OrderByDescending(ch => (byte)ch)); // we want to have the up-case characters last
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.