using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Net; using System.Security; using System.Security.AccessControl; using System.Text; using HeuristicLab.Common; namespace HeuristicLab.Problems.GrammaticalOptimization { public class SymbolicRegressionPoly10Problem : IProblem { // private const string grammarString = @" // G(E): // E -> V | V+E | V-E | V*E | (E) // V -> a .. j // "; private const string grammarString = @" G(E): 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 "; private readonly IGrammar grammar; private readonly int N; private readonly double[][] x; private readonly double[] y; public SymbolicRegressionPoly10Problem() { this.grammar = new Grammar(grammarString); this.N = 500; this.x = new double[N][]; this.y = new double[N]; GenerateData(); } private void GenerateData() { // generate data with fixed seed to make sure that data is always the same var rand = new Random(31415); for (int i = 0; i < N; i++) { x[i] = new double[10]; for (int j = 0; j < 10; j++) { x[i][j] = rand.NextDouble() * 2 - 1; } // poly-10 no noise /* a*b + c*d + e*f + a*g*i + c*f*j */ y[i] = x[i][0] * x[i][1] + x[i][2] * x[i][3] + x[i][4] * x[i][5] + x[i][0] * x[i][6] * x[i][8] + x[i][2] * x[i][5] * x[i][9]; } } public double BestKnownQuality(int maxLen) { // for now only an upper bound is returned, ideally we have an R² of 1.0 // the optimal R² can only be reached for sentences of at least 23 symbols return 1.0; } public IGrammar Grammar { get { return grammar; } } public double Evaluate(string sentence) { var interpreter = new ExpressionInterpreter(); return HeuristicLab.Common.Extensions.RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i])).ToArray()); } // most-recently-used caching (with limited capacity) for canonical representations MostRecentlyUsedCache canonicalPhraseCache = new MostRecentlyUsedCache(100000); // right now only + and * is supported public string CanonicalRepresentation(string phrase) { string canonicalPhrase; if (!canonicalPhraseCache.TryGetValue(phrase, out canonicalPhrase)) { var terms = phrase.Split('+'); var canonicalTerms = new SortedSet(); // only the last term might contain a NT-symbol. make sure this term is added at the end for (int i = 0; i < terms.Length - 1; i++) { canonicalTerms.Add(CanonicalTerm(terms[i])); } var sb = new StringBuilder(phrase.Length); foreach (var t in canonicalTerms) sb.Append(t).Append('+'); sb.Append(CanonicalTerm(terms[terms.Length - 1])); canonicalPhrase = sb.ToString(); canonicalPhraseCache.Add(phrase, canonicalPhrase); } return canonicalPhrase; } // cache the canonical form of terms for performance reasons private Dictionary canonicalTermDictionary = new Dictionary(); private string CanonicalTerm(string term) { string canonicalTerm; if (!canonicalTermDictionary.TryGetValue(term, out canonicalTerm)) { // add var chars = term.ToCharArray(); Array.Sort(chars); var sb = new StringBuilder(chars.Length); // we want to have the up-case characters last for (int i = chars.Length - 1; i > 0; i--) { if (chars[i] != '*') { sb.Append(chars[i]); if (chars[i - 1] != '*') sb.Append('*'); } } if (chars[0] != '*') sb.Append(chars[0]); // last term canonicalTerm = sb.ToString(); canonicalTermDictionary.Add(term, canonicalTerm); } return canonicalTerm; } // splits the phrase into terms and creates (sparse) term-occurrance features public IEnumerable GetFeatures(string phrase) { var canonicalTerms = new HashSet(); foreach (string t in phrase.Split('+')) { canonicalTerms.Add(CanonicalTerm(t)); } return canonicalTerms.Select(entry => new Feature(entry, 1.0)).Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); } } }