Changeset 18095


Ignore:
Timestamp:
11/25/21 15:32:01 (6 days ago)
Author:
dpiringe
Message:

#3136

  • added a Evaluate method, which uses the static method Calculate and evaluates a ISymbolicExpressionTree without the need of an ExecutionContext
    • implemented this new method in all single objective SymReg evaluators
Location:
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r18084 r18095  
    267267      <Private>False</Private>
    268268    </ProjectReference>
     269    <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
     270      <Project>{f4539fb6-4708-40c9-be64-0a1390aea197}</Project>
     271      <Name>HeuristicLab.Random-3.3</Name>
     272    </ProjectReference>
    269273  </ItemGroup>
    270274  <ItemGroup>
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Interfaces/ISymbolicRegressionSingleObjectiveEvaluator.cs

    r17180 r18095  
    1 using HEAL.Attic;
     1using System.Collections.Generic;
     2using System.Linq;
     3using HEAL.Attic;
     4using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25#region License Information
    36/* HeuristicLab
     
    2427  [StorableType("5dd2601a-b884-48c0-85bc-bc1f437187a3")]
    2528  public interface ISymbolicRegressionSingleObjectiveEvaluator : ISymbolicRegressionEvaluator, ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> {
     29    double Evaluate(
     30      IRegressionProblemData problemData,
     31      ISymbolicExpressionTree tree,
     32      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     33      IEnumerable<int> rows = null,
     34      bool applyLinearScaling = true,
     35      double lowerEstimationLimit = double.MinValue,
     36      double upperEstimationLimit = double.MaxValue);
    2637  }
    2738}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NMSESingleObjectiveConstraintsEvaluator.cs

    r17958 r18095  
    233233      return nmse;
    234234    }
     235
     236    public override double Evaluate(IRegressionProblemData problemData,
     237      ISymbolicExpressionTree solution,
     238      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     239      IEnumerable<int> rows = null,
     240      bool applyLinearScaling = true,
     241      double lowerEstimationLimit = double.MinValue,
     242      double upperEstimationLimit = double.MaxValue) {
     243
     244      if (OptimizeParameters) {
     245        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
     246          interpreter,
     247          solution,
     248          problemData,
     249          rows,
     250          false,
     251          ConstantOptimizationIterations,
     252          true,
     253          lowerEstimationLimit,
     254          upperEstimationLimit);
     255      } else {
     256        if (applyLinearScaling) {
     257          var rootNode = new ProgramRootSymbol().CreateTreeNode();
     258          var startNode = new StartSymbol().CreateTreeNode();
     259          var offset = solution.Root.GetSubtree(0) //Start
     260                                    .GetSubtree(0); //Offset
     261          var scaling = offset.GetSubtree(0);
     262
     263          //Check if tree contains offset and scaling nodes
     264          if (!(offset.Symbol is Addition) || !(scaling.Symbol is Multiplication))
     265            throw new ArgumentException($"{ItemName} can only be used with LinearScalingGrammar.");
     266
     267          var t = (ISymbolicExpressionTreeNode)scaling.GetSubtree(0).Clone();
     268          rootNode.AddSubtree(startNode);
     269          startNode.AddSubtree(t);
     270          var newTree = new SymbolicExpressionTree(rootNode);
     271
     272          //calculate alpha and beta for scaling
     273          var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
     274
     275          var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     276          OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta,
     277            out var errorState);
     278
     279          if (errorState == OnlineCalculatorError.None) {
     280            //Set alpha and beta to the scaling nodes from ia grammar
     281            var offsetParameter = offset.GetSubtree(1) as ConstantTreeNode;
     282            offsetParameter.Value = alpha;
     283            var scalingParameter = scaling.GetSubtree(1) as ConstantTreeNode;
     284            scalingParameter.Value = beta;
     285          }
     286        } // else: alpha and beta are evolved
     287      }
     288      return Calculate(
     289        interpreter,
     290        solution,
     291        lowerEstimationLimit,
     292        upperEstimationLimit,
     293        problemData,
     294        rows ?? problemData.TrainingIndices,
     295        BoundsEstimator,
     296        UseSoftConstraints,
     297        PenalityFactor);
     298    }
    235299  }
    236300}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r17944 r18095  
    3030using HeuristicLab.Optimization;
    3131using HeuristicLab.Parameters;
     32using HeuristicLab.Random;
    3233
    3334namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    175176
    176177      return base.InstrumentedApply();
     178    }
     179
     180    public override double Evaluate(IRegressionProblemData problemData,
     181      ISymbolicExpressionTree solution,
     182      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     183      IEnumerable<int> rows = null,
     184      bool applyLinearScaling = true,
     185      double lowerEstimationLimit = double.MinValue,
     186      double upperEstimationLimit = double.MaxValue) {
     187
     188
     189      var random = RandomParameter?.Value ?? new MersenneTwister((uint)DateTime.Now.Millisecond);
     190      double quality;
     191
     192      var propability = random.NextDouble();
     193      if (propability < ConstantOptimizationProbability.Value) {
     194        var counter = new EvaluationsCounter();
     195        quality = OptimizeConstants(
     196          interpreter,
     197          solution,
     198          problemData,
     199          rows ?? problemData.TrainingIndices,
     200          applyLinearScaling,
     201          ConstantOptimizationIterations.Value,
     202          updateVariableWeights: UpdateVariableWeights,
     203          lowerEstimationLimit: lowerEstimationLimit,
     204          upperEstimationLimit: upperEstimationLimit,
     205          updateConstantsInTree: UpdateConstantsInTree,
     206          counter: counter);
     207
     208        if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
     209          var evaluationRows = GenerateRowsToEvaluate();
     210          quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     211            interpreter,
     212            solution,
     213            lowerEstimationLimit,
     214            upperEstimationLimit,
     215            problemData,
     216            evaluationRows,
     217            applyLinearScaling);
     218        }
     219
     220      } else {
     221        var evaluationRows = GenerateRowsToEvaluate();
     222        quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     223          interpreter,
     224          solution,
     225          lowerEstimationLimit,
     226          upperEstimationLimit,
     227          problemData,
     228          evaluationRows,
     229          applyLinearScaling);
     230      }
     231      return quality;
    177232    }
    178233
     
    311366        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
    312367        if (constantTreeNode != null) {
    313           if (constantTreeNode.Parent.Symbol is Power 
     368          if (constantTreeNode.Parent.Symbol is Power
    314369              && constantTreeNode.Parent.GetSubtree(1) == constantTreeNode) continue; // exponents in powers are not optimizated (see TreeToAutoDiffTermConverter)
    315370          constantTreeNode.Value = constants[i++];
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionLogResidualEvaluator.cs

    r17180 r18095  
    9090      return mlr;
    9191    }
     92
     93    public override double Evaluate(IRegressionProblemData problemData,
     94      ISymbolicExpressionTree solution,
     95      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     96      IEnumerable<int> rows = null,
     97      bool applyLinearScaling = true,
     98      double lowerEstimationLimit = double.MinValue,
     99      double upperEstimationLimit = double.MaxValue) {
     100      return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices);
     101    }
    92102  }
    93103}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionMeanRelativeErrorEvaluator.cs

    r17180 r18095  
    8383      return mre;
    8484    }
     85
     86    public override double Evaluate(IRegressionProblemData problemData,
     87      ISymbolicExpressionTree solution,
     88      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     89      IEnumerable<int> rows = null,
     90      bool applyLinearScaling = true,
     91      double lowerEstimationLimit = double.MinValue,
     92      double upperEstimationLimit = double.MaxValue) {
     93      return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices);
     94    }
    8595  }
    8696}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs

    r17180 r18095  
    2222using HeuristicLab.Common;
    2323using HEAL.Attic;
     24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     25using System.Collections.Generic;
     26
    2427namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2528  [StorableType("7EB90F03-4385-474F-BDE7-3B133E8FEAAB")]
     
    2831    protected SymbolicRegressionSingleObjectiveEvaluator(StorableConstructorFlag _) : base(_) { }
    2932    protected SymbolicRegressionSingleObjectiveEvaluator(SymbolicRegressionSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
    30     protected SymbolicRegressionSingleObjectiveEvaluator(): base() {}   
     33    protected SymbolicRegressionSingleObjectiveEvaluator(): base() {}
     34    public abstract double Evaluate(
     35      IRegressionProblemData problemData,
     36      ISymbolicExpressionTree tree,
     37      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     38      IEnumerable<int> rows = null,
     39      bool applyLinearScaling = true,
     40      double lowerEstimationLimit = double.MinValue,
     41      double upperEstimationLimit = double.MaxValue);
    3142  }
    3243}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs

    r17180 r18095  
    8484      return mse;
    8585    }
     86
     87    public override double Evaluate(IRegressionProblemData problemData,
     88      ISymbolicExpressionTree solution,
     89      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     90      IEnumerable<int> rows = null,
     91      bool applyLinearScaling = true,
     92      double lowerEstimationLimit = double.MinValue,
     93      double upperEstimationLimit = double.MaxValue) {
     94      return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling);
     95    }
    8696  }
    8797}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs

    r17180 r18095  
    8484      return mse;
    8585    }
     86
     87    public override double Evaluate(IRegressionProblemData problemData,
     88      ISymbolicExpressionTree solution,
     89      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     90      IEnumerable<int> rows = null,
     91      bool applyLinearScaling = true,
     92      double lowerEstimationLimit = double.MinValue,
     93      double upperEstimationLimit = double.MaxValue) {
     94      return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling);
     95    }
    8696  }
    8797}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs

    r17180 r18095  
    8484      return mse;
    8585    }
     86
     87    public override double Evaluate(IRegressionProblemData problemData,
     88      ISymbolicExpressionTree solution,
     89      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     90      IEnumerable<int> rows = null,
     91      bool applyLinearScaling = true,
     92      double lowerEstimationLimit = double.MinValue,
     93      double upperEstimationLimit = double.MaxValue) {
     94      return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling);
     95    }
    8696  }
    8797}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs

    r17180 r18095  
    8686      return r2;
    8787    }
     88
     89    public override double Evaluate(IRegressionProblemData problemData,
     90      ISymbolicExpressionTree solution,
     91      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     92      IEnumerable<int> rows = null,
     93      bool applyLinearScaling = true,
     94      double lowerEstimationLimit = double.MinValue,
     95      double upperEstimationLimit = double.MaxValue) {
     96      return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling);
     97    }
    8898  }
    8999}
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs

    r18084 r18095  
    3131
    3232    private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
     33    private const string VariableName = "Variable";
    3334
    3435    private const string StructureTemplateDescriptionText =
     
    9495
    9596      Parameters.Add(new ConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator>(
    96         TreeEvaluatorParameterName, 
     97        TreeEvaluatorParameterName,
    9798        evaluators,
    9899        evaluators.First()));
     
    114115      Parameters.Add(new FixedValueParameter<DoubleLimit>(
    115116        EstimationLimitsParameterName,
    116         new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth)));
    117       EstimationLimitsParameter.Hidden = true;
    118 
    119       Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, ""));
    120       this.BestTrainingSolutionParameter.Hidden = true;
     117        new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth)) { Hidden = true });
     118
     119      Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "") { Hidden = true });
    121120
    122121      this.EvaluatorParameter.Hidden = true;
    123 
    124      
    125 
     122     
    126123      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
    127124      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
     
    175172      base.Analyze(individuals, qualities, results, random);
    176173
    177       var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    178       var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     174      var best = GetBestIndividual(individuals, qualities).Item1;
    179175
    180176      if (!results.ContainsKey(BestTrainingSolutionParameter.ActualName)) {
     
    199195      individual[SymbolicExpressionTreeName] = tree;
    200196
    201       //TreeEvaluatorParameter.Value.EstimationLimitsParameter.ActualValue = EstimationLimits;
    202       //TreeEvaluatorParameter.Value.EstimationLimitsParameter.Value = EstimationLimits;
    203       //var quality = TreeEvaluatorParameter.Value.Evaluate(new ExecutionContext(null, this, new Scope("Test")), tree, ProblemData, ProblemData.TrainingIndices);
    204 
    205       var quality = double.MaxValue;
    206       var evaluatorGUID = TreeEvaluatorParameter.Value.GetType().GUID;
    207 
    208       // TODO: use Evaluate method instead of static Calculate -> a fake ExecutionContext is needed
    209       if (evaluatorGUID == typeof(NMSESingleObjectiveConstraintsEvaluator).GUID) {
    210         quality = NMSESingleObjectiveConstraintsEvaluator.Calculate(
    211         Interpreter, tree,
    212         EstimationLimits.Lower, EstimationLimits.Upper,
    213         ProblemData, ProblemData.TrainingIndices, new IntervalArithBoundsEstimator());
    214       } else if (evaluatorGUID == typeof(SymbolicRegressionLogResidualEvaluator).GUID) {
    215         quality = SymbolicRegressionLogResidualEvaluator.Calculate(
    216         Interpreter, tree,
    217         EstimationLimits.Lower, EstimationLimits.Upper,
    218         ProblemData, ProblemData.TrainingIndices);
    219       } else if (evaluatorGUID == typeof(SymbolicRegressionMeanRelativeErrorEvaluator).GUID) {
    220         quality = SymbolicRegressionMeanRelativeErrorEvaluator.Calculate(
    221         Interpreter, tree,
    222         EstimationLimits.Lower, EstimationLimits.Upper,
    223         ProblemData, ProblemData.TrainingIndices);
    224       } else if (evaluatorGUID == typeof(SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator).GUID) {
    225         quality = SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.Calculate(
    226         Interpreter, tree,
    227         EstimationLimits.Lower, EstimationLimits.Upper,
    228         ProblemData, ProblemData.TrainingIndices, false);
    229       } else if (evaluatorGUID == typeof(SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator).GUID) {
    230         quality = SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.Calculate(
    231         Interpreter, tree,
    232         EstimationLimits.Lower, EstimationLimits.Upper,
    233         ProblemData, ProblemData.TrainingIndices, false);
    234       } else { // SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator
    235         quality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(
    236         Interpreter, tree,
    237         EstimationLimits.Lower, EstimationLimits.Upper,
    238         ProblemData, ProblemData.TrainingIndices, false);
    239       }
    240    
    241       return quality;
     197      return TreeEvaluatorParameter.Value.Evaluate(
     198        ProblemData,
     199        tree,
     200        Interpreter,
     201        ProblemData.TrainingIndices,
     202        StructureTemplate.ApplyLinearScaling,
     203        EstimationLimits.Lower,
     204        EstimationLimits.Upper);
    242205    }
    243206
     
    280243
    281244    private void SetupVariables(SubFunction subFunction) {
    282       var varSym = (Variable)subFunction.Grammar.GetSymbol("Variable");
     245      var varSym = (Variable)subFunction.Grammar.GetSymbol(VariableName);
    283246      if (varSym == null) {
    284247        varSym = new Variable();
Note: See TracChangeset for help on using the changeset viewer.