Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283: worked on grammatical optimization problem solvers (simple MCTS done)

File size: 3.0 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 | j | a+E | b+E | c+E | d+E | e+E | f+E | g+E | h+E | j+E | a*E | b*E | c*E | d*E | e*E | f*E | g*E | h*E | j*E
19    ";
20
21
22    private readonly IGrammar grammar;
23    private readonly ExpressionInterpreter interpreter;
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      this.interpreter = new ExpressionInterpreter();
32
33      this.N = 500;
34      this.x = new double[N][];
35      this.y = new double[N];
36
37      GenerateData();
38    }
39
40    private void GenerateData() {
41      // generate data with fixed seed to make sure that data is always the same
42      var rand = new Random(31415);
43      for (int i = 0; i < N; i++) {
44        x[i] = new double[10];
45        for (int j = 0; j < 10; j++) {
46          x[i][j] = rand.NextDouble() * 2 - 1;
47        }
48        // poly-10 no noise
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 GetBestKnownQuality(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      return RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i])).ToArray());
69    }
70
71    private double RSq(IEnumerable<double> xs, IEnumerable<double> ys) {
72      // two pass implementation, but we don't care
73      var meanX = xs.Average();
74      var meanY = ys.Average();
75
76      var s = 0.0;
77      var ssX = 0.0;
78      var ssY = 0.0;
79      foreach (var p in xs.Zip(ys, (x, y) => new { x, y })) {
80        s += (p.x - meanX) * (p.y - meanY);
81        ssX += Math.Pow(p.x - meanX, 2);
82        ssY += Math.Pow(p.y - meanY, 2);
83      }
84
85      if (s.IsAlmost(0)) return 0;
86      if (ssX.IsAlmost(0) || ssY.IsAlmost(0)) return 0;
87      return s * s / (ssX * ssY);
88    }
89
90
91    // right now only + and * is supported
92    public string Hash(string terminalPhrase) {
93      var terms = terminalPhrase.Split('+');
94      return string.Join("+", terms.Select(term => string.Join("", term.Replace("*", "").OrderBy(ch => ch)))
95        .OrderBy(term => term));
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.