# source:branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SymbolicRegressionPoly10Problem.cs@11745

Last change on this file since 11745 was 11745, checked in by gkronber, 7 years ago

#2283: worked on contextual MCTS

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