Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/25/21 15:32:01 (3 years 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
File:
1 edited

Legend:

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