Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/04/21 16:58:33 (3 years ago)
Author:
dpiringe
Message:

#3136

  • added new parameters
  • added the builded tree into the scope, this allows operators to use the final tree
  • added new operators
File:
1 edited

Legend:

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

    r18075 r18076  
    2424    private const string StructureTemplateParameterName = "Structure Template";
    2525    private const string InterpreterParameterName = "Interpreter";
    26 
    27     private const string StructureTemplateDescriptionText =
     26    private const string EstimationLimitsParameterName = "EstimationLimits";
     27    private const string BestTrainingSolutionParameterName = "Best Training Solution";
     28
     29    private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
     30
     31    private const string StructureTemplateDescriptionText =
    2832      "Enter your expression as string in infix format into the empty input field.\n" +
    2933      "By checking the \"Apply Linear Scaling\" checkbox you can add the relevant scaling terms to your expression.\n" +
     
    3640    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
    3741    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName];
     42    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter => (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName];
     43    public IResultParameter<ISymbolicRegressionSolution> BestTrainingSolutionParameter => (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestTrainingSolutionParameterName];
    3844    #endregion
    3945
    4046    #region Properties
    41     public IRegressionProblemData ProblemData { 
    42       get => ProblemDataParameter.Value; 
     47    public IRegressionProblemData ProblemData {
     48      get => ProblemDataParameter.Value;
    4349      set {
    4450        ProblemDataParameter.Value = value;
     
    5460    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
    5561
     62    public DoubleLimit EstimationLimits => EstimationLimitsParameter.Value;
     63
    5664    public override bool Maximization => true;
    5765    #endregion
     
    6472    public StructuredSymbolicRegressionSingleObjectiveProblem() {
    6573      var problemData = new ShapeConstrainedRegressionProblemData();
     74      var targetInterval = problemData.VariableRanges.GetInterval(problemData.TargetVariable);
     75      var estimationWidth = targetInterval.Width * 10;
     76
    6677
    6778      var structureTemplate = new StructureTemplate();
     
    6980
    7081      Parameters.Add(new ValueParameter<IRegressionProblemData>(
    71         ProblemDataParameterName, 
     82        ProblemDataParameterName,
    7283        problemData));
    73 
    7484      Parameters.Add(new FixedValueParameter<StructureTemplate>(
    75         StructureTemplateParameterName, 
    76         StructureTemplateDescriptionText, 
     85        StructureTemplateParameterName,
     86        StructureTemplateDescriptionText,
    7787        structureTemplate));
    78 
    7988      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(
    80         InterpreterParameterName,
    81         new SymbolicDataAnalysisExpressionTreeInterpreter())
    82         { Hidden = true });
     89        InterpreterParameterName,
     90        new SymbolicDataAnalysisExpressionTreeInterpreter()) { Hidden = true });
     91      Parameters.Add(new FixedValueParameter<DoubleLimit>(
     92        EstimationLimitsParameterName,
     93        new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth)));
     94      Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, ""));
    8395
    8496      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
    85     }
    86 
    87     public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original,
    88       Cloner cloner) : base(original, cloner){ }
     97
     98      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
     99      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
     100      //TODO change to value lookup
     101      //Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
     102      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
     103
     104    }
     105
     106    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original,
     107      Cloner cloner) : base(original, cloner) { }
    89108
    90109    [StorableConstructor]
     
    114133        if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
    115134          Encoding.Add(new SymbolicExpressionTreeEncoding(
    116             f.Name, 
    117             f.Grammar, 
    118             f.MaximumSymbolicExpressionTreeLength, 
     135            f.Name,
     136            f.Grammar,
     137            f.MaximumSymbolicExpressionTreeLength,
    119138            f.MaximumSymbolicExpressionTreeDepth));
    120139      }
     
    124143      base.Analyze(individuals, qualities, results, random);
    125144
    126       int bestIdx = 0;
    127       double bestQuality = Maximization ? double.MinValue : double.MaxValue;
    128       for(int idx = 0; idx < qualities.Length; ++idx) {
    129         if((Maximization && qualities[idx] > bestQuality) ||
    130           (!Maximization && qualities[idx] < bestQuality)) {
    131           bestQuality = qualities[idx];
    132           bestIdx = idx;
    133         }
    134       }
    135 
    136       if (results.TryGetValue("Best Tree", out IResult result)) {
    137         var tree = BuildTree(individuals[bestIdx]);
    138         if (StructureTemplate.ApplyLinearScaling)
    139           AdjustLinearScalingParams(tree, Interpreter);
    140         result.Value = tree;
    141       }
    142       else {
    143         var tree = BuildTree(individuals[bestIdx]);
    144         if (StructureTemplate.ApplyLinearScaling)
    145           AdjustLinearScalingParams(tree, Interpreter);
    146         results.Add(new Result("Best Tree", tree));
    147       }
    148     }
     145      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
     146      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     147
     148      if (!results.ContainsKey(BestTrainingSolutionParameter.ActualName)) {
     149        results.Add(new Result(BestTrainingSolutionParameter.ActualName, typeof(SymbolicRegressionSolution)));
     150      }
     151
     152      var tree = (ISymbolicExpressionTree)best[SymbolicExpressionTreeName];
     153
     154      var model = new SymbolicRegressionModel(ProblemData.TargetVariable, tree, Interpreter);
     155      var solution = model.CreateRegressionSolution(ProblemData);
     156
     157      results[BestTrainingSolutionParameter.ActualName].Value = solution;
     158    }
     159
    149160
    150161    public override double Evaluate(Individual individual, IRandom random) {
     
    152163
    153164      if (StructureTemplate.ApplyLinearScaling)
    154         AdjustLinearScalingParams(tree, Interpreter);
    155       var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable);
     165        AdjustLinearScalingParams(ProblemData, tree, Interpreter);
     166
     167      individual[SymbolicExpressionTreeName] = tree;
     168
    156169      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
    157         Interpreter, tree, 
    158         estimationInterval.LowerBound, estimationInterval.UpperBound,
     170        Interpreter, tree,
     171        EstimationLimits.Lower, EstimationLimits.Upper,
    159172        ProblemData, ProblemData.TrainingIndices, false);
    160      
     173
    161174      return quality;
    162175    }
    163176
    164     private void AdjustLinearScalingParams(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
     177    private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
    165178      var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
    166179      var scalingNode = offsetNode.Subtrees.Where(x => !(x is ConstantTreeNode)).First();
     
    169182      var scalingConstantNode = (ConstantTreeNode)scalingNode.Subtrees.Where(x => x is ConstantTreeNode).First();
    170183
    171       var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, ProblemData.TrainingIndices);
    172       var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
     184      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
     185      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
    173186
    174187      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
    175       if(error == OnlineCalculatorError.None) {
     188      if (error == OnlineCalculatorError.None) {
    176189        offsetConstantNode.Value = a;
    177190        scalingConstantNode.Value = b;
     
    186199
    187200      // build main tree
    188       foreach (var n in templateTree.IterateNodesPrefix()) {
    189         if (n.Symbol is SubFunctionSymbol) {
    190           var subFunctionTreeNode = n as SubFunctionTreeNode;
    191           var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
    192 
    193           // add new tree
    194           var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
    195                                             .GetSubtree(0); // Offset
    196           subFunctionTreeNode.AddSubtree(subTree);
    197         }
     201      foreach (var subFunctionTreeNode in templateTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
     202        var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
     203
     204        // add new tree
     205        var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
     206                                          .GetSubtree(0); // Offset
     207        subTree = (ISymbolicExpressionTreeNode)subTree.Clone();
     208        subFunctionTreeNode.AddSubtree(subTree);
     209
    198210      }
    199211      return templateTree;
Note: See TracChangeset for help on using the changeset viewer.