Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/15 16:32:26 (9 years ago)
Author:
gkronber
Message:

#2283 implemented bridge to HL (solve grammatical optimization problem instances with StandardGP and OffspringSelectionGP)

File:
1 edited

Legend:

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

    r11832 r11846  
    33using System.Collections.Generic;
    44using System.Collections.Specialized;
     5using System.Diagnostics;
    56using System.Linq;
    67using System.Net;
     
    910using System.Text;
    1011using HeuristicLab.Common;
     12using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    1113
    1214namespace HeuristicLab.Problems.GrammaticalOptimization {
    13   public class SymbolicRegressionPoly10Problem : IProblem {
     15  public class SymbolicRegressionPoly10Problem : ISymbolicExpressionTreeProblem {
    1416    //    private const string grammarString = @"
    1517    //    G(E):
     
    2224    ";
    2325
     26    // for tree-based GP in HL we need a different grammar for the same language
     27    // E = expr, S = sum, P = product
     28    private const string hlGrammarString = @"
     29    G(E):
     30    E -> S | P | a | b | c | d | e | f | g | h | i | j
     31    S -> EE | EEE
     32    P -> EE | EEE
     33    ";
     34    // mininmal tree for the optimal expr (40 nodes)
     35    // E S
     36    //     E S
     37    //         E P
     38    //           E a E b
     39    //         E P
     40    //           E c E d
     41    //         E P
     42    //           E e E f
     43    //     E S
     44    //         E P
     45    //           E a E g E i
     46    //         E P
     47    //           E c E f E j
     48
     49    private SymbolicExpressionGrammar symbExprGrammar;
     50    public ISymbolicExpressionGrammar SymbolicExpressionGrammar {
     51      get {
     52        return symbExprGrammar;
     53      }
     54    }
    2455
    2556    private readonly IGrammar grammar;
     
    3162    public SymbolicRegressionPoly10Problem() {
    3263      this.grammar = new Grammar(grammarString);
     64      this.symbExprGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    3365
    3466      this.N = 500;
     
    71103      return HeuristicLab.Common.Extensions.RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i])).ToArray());
    72104    }
    73 
    74105
    75106
     
    130161    }
    131162
     163    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     164      var sb = new StringBuilder();
     165
     166      TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb);
     167
     168      return sb.ToString();
     169    }
     170
     171    private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) {
     172      if (treeNode.SubtreeCount == 0) {
     173        // terminal
     174        sb.Append(treeNode.Symbol.Name);
     175      } else {
     176        string op = string.Empty;
     177        switch (treeNode.Symbol.Name) {
     178          case "S": op = "+"; break;
     179          case "P": op = "*"; break;
     180          default: {
     181              Debug.Assert(treeNode.SubtreeCount == 1);
     182              break;
     183            }
     184        }
     185        // nonterminal
     186        if (op == "+") sb.Append("(");
     187        TreeToSentence(treeNode.Subtrees.First(), sb);
     188        foreach (var subTree in treeNode.Subtrees.Skip(1)) {
     189          sb.Append(op);
     190          TreeToSentence(subTree, sb);
     191        }
     192        if (op == "+") sb.Append(")");
     193      }
     194    }
    132195  }
    133196}
Note: See TracChangeset for help on using the changeset viewer.