Changeset 16053


Ignore:
Timestamp:
08/06/18 16:54:14 (16 months ago)
Author:
bburlacu
Message:

#2886: Refactor RSquaredEvaluator as a standalone ParameterizedNamedItem which is a parameter of the algorithm. Implement BestSolutionAnalyzer analyzer for quality statistics. Add license headers where missing.

Location:
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration
Files:
2 added
1 deleted
10 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/IGrammarEnumerationAnalyzer.cs

    r15821 r16053  
    1 using HeuristicLab.Core;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using HeuristicLab.Core;
    223
    324namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    425  public interface IGrammarEnumerationAnalyzer : IItem {
    5     void Register(GrammarEnumerationAlgorithm g);
     26    void Register(GrammarEnumerationAlgorithm algorithm);
    627    void Deregister(GrammarEnumerationAlgorithm algorithm);
    728  }
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SearchGraphVisualizer.cs

    r16026 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Diagnostics;
    324using System.IO;
    4 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    525using HeuristicLab.Common;
    626using HeuristicLab.Core;
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SentenceLogger.cs

    r16026 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Diagnostics;
    324using System.IO;
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r16026 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Diagnostics;
    425using System.Linq;
    5 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    626using HeuristicLab.Common;
    727using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    234254    }
    235255
    236     // returns the maximum achievable sentence length below the maximum complexity
     256    /// <summary>
     257    /// returns the maximum achievable sentence length below the maximum complexity
     258    /// TODO: figure out a way to calculate this accurately
     259    /// </summary>
     260    /// <param name="maxComplexity"></param>
     261    /// <returns>Maximum sentence length according to the grammar and the maximum complexity</returns>
    237262    public int GetMaxSentenceLength(int maxComplexity) {
    238263      SymbolList s = new SymbolList(StartSymbol);
     
    253278    }
    254279
    255     public double EvaluatePhrase(SymbolList s, IRegressionProblemData problemData, bool optimizeConstants, int iterations) {
     280    public double EvaluatePhrase(SymbolList s, IRegressionProblemData problemData, bool optimizeConstants, int iterations, bool applyLinearScaling) {
    256281      SymbolicExpressionTree tree = ParseSymbolicExpressionTree(s);
    257282
    258       return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants, iterations);
     283      return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants, iterations, applyLinearScaling);
    259284    }
    260285
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r16026 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
    425using System.Threading;
    5 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    626using HeuristicLab.Collections;
    727using HeuristicLab.Common;
     
    3151    private readonly string ExpansionsPerSecondName = "Expansions per second";
    3252
    33     private readonly string OptimizeConstantsParameterName = "Optimize Constants";
    34     private readonly string ConstantOptimizationIterationsParameterName = "Constant Optimization Iterations";
     53    private readonly string EvaluatorParameterName = "Evaluator";
     54
    3555    private readonly string ErrorWeightParameterName = "Error Weight";
    3656    private readonly string SearchDataStructureParameterName = "Search Data Structure";
     
    4767    public override bool SupportsPause { get { return true; } }
    4868
    49     protected IFixedValueParameter<BoolValue> OptimizeConstantsParameter {
    50       get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
    51     }
    52 
    53     public bool OptimizeConstants {
    54       get { return OptimizeConstantsParameter.Value.Value; }
    55       set { OptimizeConstantsParameter.Value.Value = value; }
    56     }
    57 
    58     public int ConstantOptimizationIterations {
    59       get { return ConstantOptimizationIterationsParameter.Value.Value; }
    60       set { ConstantOptimizationIterationsParameter.Value.Value = value; }
    61     }
    62 
    63     public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
    64       get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
     69    public IFixedValueParameter<RSquaredEvaluator> EvaluatorParameter {
     70      get { return (IFixedValueParameter<RSquaredEvaluator>)Parameters[EvaluatorParameterName]; }
     71    }
     72
     73    public RSquaredEvaluator Evaluator {
     74      get { return EvaluatorParameter.Value; }
    6575    }
    6676
     
    102112    public int SearchDataStructureSize {
    103113      get { return SearchDataStructureSizeParameter.Value.Value; }
     114      set { SearchDataStructureSizeParameter.Value.Value = value; }
    104115    }
    105116
     
    189200
    190201    public GrammarEnumerationAlgorithm() {
    191       Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(false)));
    192202      Parameters.Add(new FixedValueParameter<DoubleValue>(ErrorWeightParameterName, "Defines, how much weight is put on a phrase's r² value when priorizing phrases during search.", new DoubleValue(0.8)));
    193203      Parameters.Add(new FixedValueParameter<IntValue>(MaxComplexityParameterName, "The maximum number of variable symbols in a sentence.", new IntValue(12)));
     
    195205      Parameters.Add(new FixedValueParameter<IntValue>(SearchDataStructureSizeParameterName, "The size of the search data structure.", new IntValue((int)1e5)));
    196206      Parameters.Add(new FixedValueParameter<EnumValue<StorageType>>(SearchDataStructureParameterName, new EnumValue<StorageType>(StorageType.SortedSet)));
    197       Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, new IntValue(10)));
     207      Parameters.Add(new FixedValueParameter<RSquaredEvaluator>(EvaluatorParameterName, new RSquaredEvaluator()));
    198208
    199209      SearchDataStructureParameter.Value.ValueChanged += (o, e) => Prepare();
     
    203213        new SearchGraphVisualizer(),
    204214        new SentenceLogger(),
    205         new RSquaredEvaluator()
     215        new BestSolutionAnalyzer()
    206216      };
    207217
     
    215225        Analyzers.SetItemCheckedState(analyzer, false);
    216226      }
    217       Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true);
     227      Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is BestSolutionAnalyzer), true);
    218228
    219229      var grammarSymbols = Enum.GetValues(typeof(GrammarRule))
     
    275285      MaxSentenceLength = Grammar.GetMaxSentenceLength(MaxComplexity);
    276286      var errorWeight = ErrorWeight;
    277       var optimizeConstants = OptimizeConstants; // cache value to avoid parameter lookup
    278       var iterations = ConstantOptimizationIterations;
     287      var evaluator = EvaluatorParameter.Value;
     288      var problemData = Problem.ProblemData;
     289
    279290      // main search loop
    280291      while (OpenPhrases.Count > 0) {
     
    317328
    318329            // Is the best solution found? (only if RSquaredEvaluator is activated)
    319             if (Results.ContainsKey(RSquaredEvaluator.BestTrainingQualityResultName)) {
    320               double r2 = ((DoubleValue)Results[RSquaredEvaluator.BestTrainingQualityResultName].Value).Value;
    321               if (r2.IsAlmost(1.0)) {
    322                 UpdateView(force: true);
    323                 return;
    324               }
    325             }
     330            //if (Results.ContainsKey(RSquaredEvaluator.BestTrainingQualityResultName)) {
     331            //  double r2 = ((DoubleValue)Results[RSquaredEvaluator.BestTrainingQualityResultName].Value).Value;
     332            //  if (r2.IsAlmost(1.0)) {
     333            //    UpdateView(force: true);
     334            //    return;
     335            //  }
     336            //}
    326337
    327338            if (!DistinctSentencesComplexity.ContainsKey(phraseHash) || DistinctSentencesComplexity[phraseHash] > newPhraseComplexity) {
     
    334345
    335346          } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) {
    336 
    337             bool isCompleteSentence = IsCompleteSentence(newPhrase);
    338             double r2 = isCompleteSentence ? Grammar.EvaluatePhrase(newPhrase, Problem.ProblemData, optimizeConstants, iterations) : fetchedSearchNode.R2;
     347            double r2 = IsCompleteSentence(newPhrase) ? evaluator.Evaluate(problemData, Grammar, newPhrase) : fetchedSearchNode.R2;
    339348            double phrasePriority = GetPriority(newPhrase, r2);
    340349
     
    389398      var model = new SymbolicRegressionModel(Problem.ProblemData.TargetVariable, tree, interpreter);
    390399
     400      var iterations = EvaluatorParameter.Value.ConstantOptimizationIterations;
     401      var applyLinearScaling = EvaluatorParameter.Value.ApplyLinearScaling;
     402
    391403      SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
    392404        interpreter,
     
    394406        Problem.ProblemData,
    395407        Problem.ProblemData.TrainingIndices,
    396         applyLinearScaling: true,
    397         maxIterations: ConstantOptimizationIterations,
     408        applyLinearScaling: applyLinearScaling,
     409        maxIterations: iterations,
    398410        updateVariableWeights: false,
    399411        updateConstantsInTree: true);
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/LruCache.cs

    r15981 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections;
    324using System.Collections.Generic;
     
    627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    728
    8 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
     29namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    930  /// <summary>
    1031  /// A least-recently-used cache stored like a dictionary.
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/RSquaredEvaluator.cs

    r16052 r16053  
    1 using System;
    2 using System.Diagnostics;
    3 using HeuristicLab.Analysis;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
    422using HeuristicLab.Common;
    523using HeuristicLab.Core;
    624using HeuristicLab.Data;
    725using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     26using HeuristicLab.Parameters;
    827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    928using HeuristicLab.Problems.DataAnalysis;
     
    1433  [Item("RSquaredEvaluator", "")]
    1534  [StorableClass]
    16   public class RSquaredEvaluator : Item, IGrammarEnumerationAnalyzer {
    17     public static readonly string BestTrainingQualityResultName = "Best R² (Training)";
    18     public static readonly string BestTestQualityResultName = "Best R² (Test)";
    19     public static readonly string BestTrainingModelResultName = "Best model (Training)";
    20     public static readonly string BestTrainingSolutionResultName = "Best solution (Training)";
    21     public static readonly string BestComplexityResultName = "Best solution complexity";
    22     public static readonly string BestSolutions = "Best solutions";
     35  public class RSquaredEvaluator : ParameterizedNamedItem, IGrammarEnumerationEvaluator {
     36    private readonly string OptimizeConstantsParameterName = "Optimize Constants";
     37    private readonly string ApplyLinearScalingParameterName = "Apply Linear Scaling";
     38    private readonly string ConstantOptimizationIterationsParameterName = "Constant Optimization Iterations";
     39
     40    #region parameter properties
     41    public IFixedValueParameter<BoolValue> OptimizeConstantsParameter {
     42      get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
     43    }
     44
     45    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
     46      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     47    }
     48
     49    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
     50      get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
     51    }
     52
     53    public bool OptimizeConstants {
     54      get { return OptimizeConstantsParameter.Value.Value; }
     55      set { OptimizeConstantsParameter.Value.Value = value; }
     56    }
     57
     58    public bool ApplyLinearScaling {
     59      get { return ApplyLinearScalingParameter.Value.Value; }
     60      set { ApplyLinearScalingParameter.Value.Value = value; }
     61    }
     62
     63    public int ConstantOptimizationIterations {
     64      get { return ConstantOptimizationIterationsParameter.Value.Value; }
     65      set { ConstantOptimizationIterationsParameter.Value.Value = value; }
     66    }
     67    #endregion
    2368
    2469    private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    2570
    26     public RSquaredEvaluator() { }
     71    public RSquaredEvaluator() {
     72      Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(false)));
     73      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Apply linear scaling on the tree model during evaluation.", new BoolValue(false)));
     74      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, new IntValue(10)));
     75    }
    2776
    2877    [StorableConstructor]
     
    3685    }
    3786
    38     public void Register(GrammarEnumerationAlgorithm algorithm) {
    39       algorithm.Started += OnStarted;
    40       algorithm.Stopped += OnStopped;
    41       algorithm.DistinctSentenceGenerated += AlgorithmOnDistinctSentenceGenerated;
     87    public double Evaluate(IRegressionProblemData problemData, Grammar grammar, SymbolList sentence) {
     88      var tree = grammar.ParseSymbolicExpressionTree(sentence);
     89      return Evaluate(problemData, tree, OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling);
    4290    }
    4391
    44     public void Deregister(GrammarEnumerationAlgorithm algorithm) {
    45       algorithm.Started -= OnStarted;
    46       algorithm.Stopped -= OnStopped;
    47       algorithm.DistinctSentenceGenerated -= AlgorithmOnDistinctSentenceGenerated;
     92    public double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree) {
     93      return Evaluate(problemData, tree, OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling);
    4894    }
    4995
    50     private void AlgorithmOnDistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
    51       GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender;
    52       EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase, algorithm.OptimizeConstants, algorithm.ConstantOptimizationIterations);
    53     }
    54 
    55     private void OnStarted(object sender, EventArgs eventArgs) {
    56       GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender;
    57 
    58       algorithm.BestTrainingSentence = null;
    59     }
    60 
    61     private void OnStopped(object sender, EventArgs eventArgs) { }
    62 
    63     private T GetValue<T>(IItem value) where T : struct {
    64       var v = value as ValueTypeValue<T>;
    65       if (v == null)
    66         throw new ArgumentException(string.Format("Item is not of type {0}", typeof(ValueTypeValue<T>)));
    67       return v.Value;
    68     }
    69 
    70     private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolList sentence, bool optimizeConstants, int maxIterations) {
    71       var results = algorithm.Results;
    72       var grammar = algorithm.Grammar;
    73       var problemData = algorithm.Problem.ProblemData;
    74 
    75       SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(sentence);
    76       Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
    77 
    78       double r2 = Evaluate(problemData, tree, optimizeConstants, maxIterations);
    79       double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0;
    80       if (r2 < bestR2)
    81         return;
    82 
    83       var bestComplexity = results.ContainsKey(BestComplexityResultName) ? GetValue<int>(results[BestComplexityResultName].Value) : int.MaxValue;
    84       var complexity = sentence.Complexity;
    85 
    86       if (algorithm.BestTrainingSentence == null || r2 > bestR2 || (r2.IsAlmost(bestR2) && complexity < bestComplexity)) {
    87         algorithm.BestTrainingSentence = sentence;
    88 
    89         var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, expressionTreeLinearInterpreter);
    90         model.Scale(problemData);
    91         var bestSolution = model.CreateRegressionSolution(problemData);
    92 
    93         results.AddOrUpdateResult(BestTrainingQualityResultName, new DoubleValue(bestSolution.TrainingRSquared));
    94         results.AddOrUpdateResult(BestTestQualityResultName, new DoubleValue(bestSolution.TestRSquared));
    95         results.AddOrUpdateResult(BestTrainingModelResultName, bestSolution.Model);
    96         results.AddOrUpdateResult(BestTrainingSolutionResultName, bestSolution);
    97         results.AddOrUpdateResult(BestComplexityResultName, new IntValue(complexity));
    98 
    99         // record best sentence quality & length
    100         DataTable dt;
    101         if (!results.ContainsKey(BestSolutions)) {
    102           var names = new[] { "Quality", "Relative Length", "Complexity", "Timestamp" };
    103           dt = new DataTable();
    104           foreach (var name in names) {
    105             dt.Rows.Add(new DataRow(name) { VisualProperties = { StartIndexZero = true } });
    106           }
    107           results.AddOrUpdateResult(BestSolutions, dt);
    108         }
    109         dt = (DataTable)results[BestSolutions].Value;
    110         dt.Rows["Quality"].Values.Add(r2);
    111         dt.Rows["Relative Length"].Values.Add((double)sentence.Count);
    112         dt.Rows["Complexity"].Values.Add(complexity);
    113         dt.Rows["Timestamp"].Values.Add(algorithm.ExecutionTime.TotalMilliseconds / 1000d);
    114       }
    115     }
    116 
    117     public static double Evaluate(IRegressionProblemData problemData, SymbolicExpressionTree tree, bool optimizeConstants = true, int maxIterations = 10) {
     96    public static double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree, bool optimizeConstants = true, int maxIterations = 10, bool applyLinearScaling = false) {
    11897      double r2;
    11998
     
    125104          problemData,
    126105          problemData.TrainingIndices,
    127           applyLinearScaling: true,
     106          applyLinearScaling: applyLinearScaling,
    128107          maxIterations: maxIterations,
    129108          updateVariableWeights: false,
     
    143122          problemData,
    144123          problemData.TrainingIndices,
    145           applyLinearScaling: true);
     124          applyLinearScaling: applyLinearScaling);
    146125      }
    147126      return double.IsNaN(r2) ? 0.0 : r2;
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs

    r16026 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections;
    324using System.Collections.Generic;
     
    627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    728
    8 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
     29namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    930  [StorableClass]
    1031  public class SearchNode : DeepCloneable {
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs

    r16026 r16053  
    1 using System;
    2 using System.Collections;
    3 using System.Collections.Generic;
    4 using System.Diagnostics;
    5 using System.Linq;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    623using HeuristicLab.Common;
    724using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    103120  }
    104121
    105  
     122
    106123}
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs

    r16026 r16053  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Diagnostics;
     
    627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    728
    8 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
     29namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    930  [StorableClass]
    1031  public abstract class Hasher<THashType> : DeepCloneable {
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.csproj

    r16026 r16053  
    116116  </ItemGroup>
    117117  <ItemGroup>
     118    <Compile Include="Analysis\BestSolutionAnalyzer.cs" />
    118119    <Compile Include="Analysis\IGrammarEnumerationAnalyzer.cs" />
    119     <Compile Include="Analysis\RSquaredEvaluator.cs" />
     120    <Compile Include="Analysis\IGrammarEnumerationEvaluator.cs" />
     121    <Compile Include="GrammarEnumeration\RSquaredEvaluator.cs" />
    120122    <Compile Include="Analysis\SentenceLogger.cs" />
    121123    <Compile Include="Analysis\SearchGraphVisualizer.cs" />
Note: See TracChangeset for help on using the changeset viewer.