Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/13/22 16:28:29 (3 years ago)
Author:
mkommend
Message:

#3136:

  • Extracted parameter optimization into dedicated helper utility.
  • Implemented evaluation in the structured SymReg problem directly.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs

    r18191 r18192  
    2929using HeuristicLab.Optimization;
    3030using HeuristicLab.Parameters;
    31 using HeuristicLab.PluginInfrastructure;
    3231using HeuristicLab.Problems.Instances;
    3332using HeuristicLab.Problems.Instances.DataAnalysis;
     
    4039
    4140    #region Constants
    42     private const string TreeEvaluatorParameterName = "TreeEvaluator";
    4341    private const string ProblemDataParameterName = "ProblemData";
    4442    private const string StructureTemplateParameterName = "Structure Template";
     
    6058
    6159    #region Parameters
    62     public IConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator> TreeEvaluatorParameter => (IConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator>)Parameters[TreeEvaluatorParameterName];
    6360    public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
    6461    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
     
    8077      }
    8178    }
    82 
    83     public SymbolicRegressionSingleObjectiveEvaluator TreeEvaluator => TreeEvaluatorParameter.Value;
    8479
    8580    public StructureTemplate StructureTemplate => StructureTemplateParameter.Value;
     
    117112
    118113      var structureTemplate = new StructureTemplate();
    119 
    120       var evaluators = new ItemSet<SymbolicRegressionSingleObjectiveEvaluator>(
    121         ApplicationManager.Manager.GetInstances<SymbolicRegressionSingleObjectiveEvaluator>()
    122         .Where(x => x.Maximization == Maximization));
    123 
    124       Parameters.Add(new ConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator>(
    125         TreeEvaluatorParameterName,
    126         evaluators,
    127         evaluators.First()));
    128114
    129115      Parameters.Add(new ValueParameter<IRegressionProblemData>(
     
    202188      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
    203189      ApplyLinearScalingParameter.Value.ValueChanged += (o, e) => StructureTemplate.ApplyLinearScaling = ApplyLinearScaling;
    204       OptimizeParametersParameter.Value.ValueChanged += (o, e) => {
    205         if (OptimizeParameters) ApplyLinearScaling = true;
    206       };
     190      //OptimizeParametersParameter.Value.ValueChanged += (o, e) => {
     191      //  if (OptimizeParameters) ApplyLinearScaling = true;
     192      //};
    207193
    208194    }
     
    267253        throw new ArgumentException("No structure template defined!");
    268254
     255      //create tree where all functions have been resolved (integrated)
    269256      var tree = BuildTree(templateTree, individual);
    270 
    271       // NMSEConstraintsEvaluator sets linear scaling terms itself
    272       if (ApplyLinearScaling && !(TreeEvaluator is NMSESingleObjectiveConstraintsEvaluator)) {
     257      individual[SymbolicExpressionTreeName] = tree;
     258
     259      if (OptimizeParameters) {
     260        ParameterOptimization.OptimizeTreeParameters(ProblemData, tree, interpreter: Interpreter);
     261      } else if (ApplyLinearScaling) {
    273262        LinearScaling.AdjustLinearScalingParams(ProblemData, tree, Interpreter);
    274263      }
    275264
    276       individual[SymbolicExpressionTreeName] = tree;
    277 
    278       return TreeEvaluator.Evaluate(
    279         tree, ProblemData,
    280         ProblemData.TrainingIndices,
    281         Interpreter,
    282         StructureTemplate.ApplyLinearScaling,
    283         EstimationLimits.Lower,
    284         EstimationLimits.Upper);
     265      //calculate NMSE
     266      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, ProblemData.TrainingIndices);
     267      var boundedEstimatedValues = estimatedValues.LimitToRange(EstimationLimits.Lower, EstimationLimits.Upper);
     268      var targetValues = ProblemData.TargetVariableTrainingValues;
     269      var nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out var errorState);
     270      if (errorState != OnlineCalculatorError.None)
     271        nmse = 1.0;
     272
     273      //evaluate constraints
     274      var constraints = Enumerable.Empty<ShapeConstraint>();
     275      if (ProblemData is ShapeConstrainedRegressionProblemData scProbData)
     276        constraints = scProbData.ShapeConstraints.EnabledConstraints;
     277      if (constraints.Any()) {
     278        var boundsEstimator = new IntervalArithBoundsEstimator();
     279        var constraintViolations = IntervalUtil.GetConstraintViolations(constraints, boundsEstimator, ProblemData.VariableRanges, tree);
     280
     281        // infinite/NaN constraints
     282        if (constraintViolations.Any(x => double.IsNaN(x) || double.IsInfinity(x)))
     283          nmse = 1.0;
     284
     285        if (constraintViolations.Any(x => x > 0.0))
     286          nmse = 1.0;
     287      }
     288
     289      return nmse;
    285290    }
    286291
Note: See TracChangeset for help on using the changeset viewer.