Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/06/18 16:54:14 (6 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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);
Note: See TracChangeset for help on using the changeset viewer.