Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/06/18 13:18:31 (7 years ago)
Author:
lkammere
Message:

#2886: Add simple data analysis tests and further informations about algorithm run.

Location:
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15722 r15723  
    5959
    6060
    61       #region Production ruless
     61      #region Production rules
     62      // order of production is important, since they are accessed via index
     63      // in memoization.
    6264      StartSymbol = Expr;
    6365
     
    8991    }
    9092
     93    /*
     94    #region Memoize subtrees
     95
     96    public void MemoizeSubtrees(SymbolString sentence) {
     97      Stack<TerminalSymbol> parseStack = new Stack<TerminalSymbol>(sentence.OfType<TerminalSymbol>());
     98
     99      // Parse root symbol "+"
     100      MemoizeSubtreeExpression(parseStack);
     101    }
     102
     103    private SymbolString MemoizeSubtreeExpression(Stack<TerminalSymbol> parseStack) {
     104      SymbolString subtree = new SymbolString();
     105
     106      if (ReferenceEquals(parseStack.Peek(), Addition)) {
     107        subtree.Add(parseStack.Pop());
     108        subtree.InsertRange(0, MemoizeSubtreeExpression(parseStack));
     109        subtree.InsertRange(0, MemoizeSubtreeTerm(parseStack));
     110
     111        Expr.Alternatives[0].GeneratedSentences.Add(subtree);
     112      } else {
     113        subtree.InsertRange(0, MemoizeSubtreeTerm(parseStack));
     114
     115        Expr.Alternatives[1].GeneratedSentences.Add(subtree);
     116      }
     117
     118      return subtree;
     119    }
     120
     121    private SymbolString MemoizeSubtreeTerm(Stack<TerminalSymbol> parseStack) {
     122      SymbolString subtree = new SymbolString();
     123
     124      if (ReferenceEquals(parseStack.Peek(), Multiplication)) {
     125        subtree.Add(parseStack.Pop());
     126        subtree.InsertRange(0, MemoizeSubtreeTerm(parseStack));
     127        subtree.InsertRange(0, MemoizeSubtreeFactor(parseStack));
     128
     129        Term.Alternatives[0].GeneratedSentences.Add(subtree);
     130      } else {
     131        subtree.InsertRange(0, MemoizeSubtreeFactor(parseStack));
     132
     133        Term.Alternatives[1].GeneratedSentences.Add(subtree);
     134      }
     135
     136      return subtree;
     137    }
     138
     139    private SymbolString MemoizeSubtreeFactor(Stack<TerminalSymbol> parseStack) {
     140      SymbolString subtree = new SymbolString(MemoizeSubtreeVar(parseStack));
     141
     142      Factor.Alternatives[0].GeneratedSentences.Add(subtree);
     143      return subtree;
     144    }
     145
     146    private SymbolString MemoizeSubtreeVar(Stack<TerminalSymbol> parseStack) {
     147      SymbolString subtree = new SymbolString(parseStack.Pop().ToEnumerable());
     148
     149      // ... not really
     150      //Var.Alternatives[0].GeneratedSentences.Add(subtree);
     151      return subtree;
     152    }
     153
     154
     155    #endregion
     156    */
     157   
     158    #region Hashing
    91159    public int CalcHashCode(SymbolString sentence) {
    92160      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
     
    99167    }
    100168
    101     #region Hashing
    102169    private int[] GetSubtreeHashes(TerminalSymbol currentSymbol, Stack<TerminalSymbol> parseStack) {
    103170      List<int> childHashes = null;
     
    166233      return childHashes.ToArray();
    167234    }
    168 
    169235
    170236    private int AggregateHashes(IEnumerable<int> hashes) {
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15722 r15723  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Collections.ObjectModel;
    4 using System.Diagnostics;
     1using System.Collections.Generic;
    52using System.Linq;
    63using System.Threading;
     
    2320  public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> {
    2421    private readonly string BestTrainingSolution = "Best solution (training)";
     22    private readonly string BestTrainingSolutionString = "Best solution string (training)";
    2523    private readonly string BestTrainingSolutionQuality = "Best solution quality (training)";
    2624    private readonly string BestTestSolution = "Best solution (test)";
     25    private readonly string BestTestSolutionString = "Best solution string (test)";
    2726    private readonly string BestTestSolutionQuality = "Best solution quality (test)";
    2827
    2928    private readonly string MaxTreeSizeParameterName = "Max. Tree Nodes";
    3029    private readonly string GuiUpdateIntervalParameterName = "GUI Update Interval";
     30    private readonly string UseMemoizationParameterName = "Use Memoization?";
    3131
    3232
    3333    #region properties
    34     public IValueParameter<IntValue> MaxTreeSizeParameter {
     34    protected IValueParameter<IntValue> MaxTreeSizeParameter {
    3535      get { return (IValueParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
    3636    }
    37 
    3837    public int MaxTreeSize {
    3938      get { return MaxTreeSizeParameter.Value.Value; }
     39      set { MaxTreeSizeParameter.Value.Value = value; }
    4040    }
    4141
    42     public IValueParameter<IntValue> GuiUpdateIntervalParameter {
    43       get { return (IValueParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
     42    protected IValueParameter<IntValue> GuiUpdateIntervalParameter {
     43      get { return (IValueParameter<IntValue>)Parameters[GuiUpdateIntervalParameterName]; }
     44    }
     45    public int GuiUpdateInterval {
     46      get { return GuiUpdateIntervalParameter.Value.Value; }
     47      set { GuiUpdateIntervalParameter.Value.Value = value; }
    4448    }
    4549
    46     public int GuiUpdateInterval {
    47       get { return GuiUpdateIntervalParameter.Value.Value; }
     50    protected IValueParameter<BoolValue> UseMemoizationParameter {
     51      get { return (IValueParameter<BoolValue>)Parameters[UseMemoizationParameterName]; }
     52    }
     53    public bool UseMemoization {
     54      get { return UseMemoizationParameter.Value.Value; }
     55      set { UseMemoizationParameter.Value.Value = value; }
    4856    }
    4957
     
    5967
    6068    public GrammarEnumerationAlgorithm() {
    61       Problem = new RegressionProblem();
    6269
    63       Parameters.Add(new ValueParameter<IntValue>(MaxTreeSizeParameterName, "The number of clusters.", new IntValue(4)));
     70      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
     71      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
     72
     73      Problem = new RegressionProblem() {
     74        ProblemData = regProblem
     75      };
     76
     77      Parameters.Add(new ValueParameter<IntValue>(MaxTreeSizeParameterName, "The number of clusters.", new IntValue(6)));
    6478      Parameters.Add(new ValueParameter<IntValue>(GuiUpdateIntervalParameterName, "Number of generated sentences, until GUI is refreshed.", new IntValue(4000)));
     79      Parameters.Add(new ValueParameter<BoolValue>(UseMemoizationParameterName, "Should already subtrees be reused within a run.", new BoolValue(true)));
    6580    }
    6681
     
    7287      List<SymbolString> allGenerated = new List<SymbolString>();
    7388      List<SymbolString> distinctGenerated = new List<SymbolString>();
     89
     90      int expansions = 0;
     91
    7492      HashSet<int> evaluatedHashes = new HashSet<int>();
    7593
     
    87105          allGenerated.Add(currSymbolString);
    88106
    89           if (evaluatedHashes.Add(grammar.CalcHashCode(currSymbolString))) {
    90             EvaluateSentence(currSymbolString);
    91             distinctGenerated.Add(currSymbolString);
    92           }
     107          //if (evaluatedHashes.Add(grammar.CalcHashCode(currSymbolString))) {
     108          EvaluateSentence(currSymbolString);
     109          //distinctGenerated.Add(currSymbolString);
     110          //}
    93111
    94           UpdateView(allGenerated, distinctGenerated);
     112          UpdateView(allGenerated, distinctGenerated, expansions);
    95113
    96114        } else {
     
    111129      }
    112130
     131      UpdateView(allGenerated, distinctGenerated, expansions, force: true);
     132
    113133      StringArray sentences = new StringArray(allGenerated.Select(r => r.ToString()).ToArray());
    114134      Results.Add(new Result("All generated sentences", sentences));
     
    118138
    119139
    120     private void UpdateView(List<SymbolString> allGenerated, List<SymbolString> distinctGenerated) {
     140    private void UpdateView(List<SymbolString> allGenerated, List<SymbolString> distinctGenerated, int expansions, bool force = false) {
    121141      int generatedSolutions = allGenerated.Count;
    122142      int distinctSolutions = distinctGenerated.Count;
    123143
    124       if (generatedSolutions % GuiUpdateInterval == 0) {
     144      if (force || generatedSolutions % GuiUpdateInterval == 0) {
    125145        Results.AddOrUpdateResult("Generated Solutions", new IntValue(generatedSolutions));
    126         Results.Add(new Result("Distinct Solutions", new IntValue(distinctSolutions)));
     146        Results.AddOrUpdateResult("Distinct Solutions", new IntValue(distinctSolutions));
    127147
    128148        DoubleValue averageTreeLength = new DoubleValue(allGenerated.Select(r => r.Count).Average());
    129         Results.Add(new Result("Average Tree Length of Solutions", averageTreeLength));
     149        Results.AddOrUpdateResult("Average Tree Length of Solutions", averageTreeLength);
     150
     151        IntValue expansionsValue = new IntValue(expansions);
     152        Results.AddOrUpdateResult("Expansions", expansionsValue);
    130153      }
    131154    }
     
    146169
    147170        Results.Add(new Result(BestTrainingSolution, newSolution));
     171        Results.Add(new Result(BestTrainingSolutionString, new StringValue(symbolString.ToString()).AsReadOnly()));
    148172        Results.Add(new Result(BestTrainingSolutionQuality, new DoubleValue(newSolution.TrainingRSquared).AsReadOnly()));
    149173        Results.Add(new Result(BestTestSolution, newSolution));
     174        Results.Add(new Result(BestTestSolutionString, new StringValue(symbolString.ToString()).AsReadOnly()));
    150175        Results.Add(new Result(BestTestSolutionQuality, new DoubleValue(newSolution.TestRSquared).AsReadOnly()));
    151176
     
    154179        if (currBestTrainingSolution.TrainingRSquared < newSolution.TrainingRSquared) {
    155180          currBestTrainingSolutionResult.Value = newSolution;
     181          Results.AddOrUpdateResult(BestTrainingSolutionString, new StringValue(symbolString.ToString()));
    156182          Results.AddOrUpdateResult(BestTrainingSolutionQuality, new DoubleValue(newSolution.TrainingRSquared).AsReadOnly());
    157183        }
     
    160186        if (currBestTestSolution.TestRSquared < newSolution.TestRSquared) {
    161187          currBestTestSolutionResult.Value = newSolution;
     188          Results.AddOrUpdateResult(BestTestSolutionString, new StringValue(symbolString.ToString()));
    162189          Results.AddOrUpdateResult(BestTestSolutionQuality, new DoubleValue(newSolution.TestRSquared).AsReadOnly());
    163190        }
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Sentence.cs

    r15712 r15723  
    77namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
    88  public class SymbolString : List<Symbol> {
     9
     10    public SymbolString() { }
    911
    1012    public SymbolString(IEnumerable<Symbol> symbols) : base(symbols) { }
     
    2022    }
    2123
    22     public override string ToString()
    23     {
     24    public override string ToString() {
    2425      return string.Join(" ", this);
    2526    }
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs

    r15714 r15723  
    11using System.Collections.Generic;
    22using System.Linq;
     3using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    34
    45namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
Note: See TracChangeset for help on using the changeset viewer.