Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/12/21 09:41:38 (3 years ago)
Author:
dpiringe
Message:

#3136

  • added a simple way of evaluation (using r2 evaluator)
  • added a simple analyzing logic for "Best Tree"
  • added a connection to SubFunction in SubFunctionTreeNode
File:
1 edited

Legend:

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

    r18065 r18066  
    2424    private const string GrammarParameterName = "Grammar";
    2525    private const string StructureTemplateParameterName = "Structure Template";
    26     private const string MainTreeResultParameterName = "Main Tree";
    2726    #endregion
    2827
     
    3231    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
    3332    public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
    34     public IResultParameter<ISymbolicExpressionTree> MainTreeResultParameter => (IResultParameter<ISymbolicExpressionTree>)Parameters[MainTreeResultParameterName];
    3533    #endregion
    3634
     
    6159    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
    6260
    63     public override bool Maximization => false;
     61    public override bool Maximization => true;
    6462    #endregion
    6563
     
    7876
    7977      var structureTemplate = new StructureTemplate();
    80       structureTemplate.Changed += (sender, args) => {
    81         foreach (var e in Encoding.Encodings.ToArray())
    82           Encoding.Remove(e);
     78      structureTemplate.Changed += OnTemplateChanged;
    8379
    84         foreach(var sf in structureTemplate.SubFunctions.Values) {
    85           Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth));
    86         }
    87       };
    88 
    89       Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, problemData));
     80      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData));
    9081      Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate));
    9182      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
    92       Parameters.Add(new ResultParameter<ISymbolicExpressionTree>(MainTreeResultParameterName, ""));
    93       MainTreeResultParameter.DefaultValue = new SymbolicExpressionTree();
     83
     84      structureTemplate.Template = "f(x)*f(y)+5";
    9485    }
    9586
     
    10596    #endregion
    10697
    107     public override double Evaluate(Individual individual, IRandom random) {
    108       var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
    109       var subFunctionDict = StructureTemplate.SubFunctions;
    110          
    111       // build main tree
    112       foreach (var n in templateTree.IterateNodesPrefix()) {
    113         if(n is SubFunctionTreeNode subFunctionTreeNode) {
    114           if(subFunctionDict.TryGetValue(subFunctionTreeNode, out SubFunction subFunction)) {
    115             var subFunctionTree = individual.SymbolicExpressionTree(subFunction.Name);
    116             var parent = n.Parent;
    117             // remove all subtrees
     98    private void OnTemplateChanged(object sender, EventArgs args) {
     99      foreach (var e in Encoding.Encodings.ToArray())
     100        Encoding.Remove(e);
    118101
    119             var subtreeCount = parent.SubtreeCount;
    120             for (int idx = 0; idx < subtreeCount; ++idx)
    121               parent.RemoveSubtree(idx);
     102      foreach (var sf in StructureTemplate.SubFunctions.Values) {
     103        Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth));
     104      }
     105    }
    122106
    123             // add new tree
    124             parent.AddSubtree(subFunctionTree.Root);
    125           }
     107    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     108      base.Analyze(individuals, qualities, results, random);
     109
     110      int bestIdx = 0;
     111      double bestQuality = Maximization ? double.MinValue : double.MaxValue;
     112      for(int idx = 0; idx < qualities.Length; ++idx) {
     113        if((Maximization && qualities[idx] > bestQuality) ||
     114          (!Maximization && qualities[idx] < bestQuality)) {
     115          bestQuality = qualities[idx];
     116          bestIdx = idx;
    126117        }
    127118      }
    128119
    129       MainTreeResultParameter.ActualValue = templateTree;
     120      if (results.TryGetValue("Best Tree", out IResult result))
     121        result.Value = BuildTree(individuals[bestIdx]);
     122      else
     123        results.Add(new Result("Best Tree", BuildTree(individuals[bestIdx])));
     124
    130125      /*
    131       foreach (var kvp in individual.Values) {
    132         if(kvp.Value is SymbolicExpressionTree tree) {
    133           foreach(var n in tree.IterateNodesPrefix()) {
    134             if(n.Symbol is Variable v) {
    135               var t = v.VariableNames;
    136             }
    137           }
     126      if (results.TryGetValue("Tree", out IResult result)) {
     127        var list = result.Value as ItemList<ISymbolicExpressionTree>;
     128        list.Clear();
     129        list.AddRange(individuals.Select(x => (BuildTree(x))));
     130      } else
     131        results.Add(new Result("Tree", new ItemList<ISymbolicExpressionTree>(individuals.Select(x => (BuildTree(x))))));
     132      */
     133    }
     134
     135    public override double Evaluate(Individual individual, IRandom random) {
     136      var tree = BuildTree(individual);
     137      var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
     138      var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable);
     139      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     140        interpreter, tree,
     141        estimationInterval.LowerBound, estimationInterval.UpperBound,
     142        ProblemData, ProblemData.TrainingIndices, false);
     143     
     144      return quality;
     145    }
     146
     147    private ISymbolicExpressionTree BuildTree(Individual individual) {
     148      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
     149
     150      // build main tree
     151      foreach (var n in templateTree.IterateNodesPrefix()) {
     152        if (n.Symbol is SubFunctionSymbol) {
     153          var subFunctionTreeNode = n as SubFunctionTreeNode;
     154          var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.SubFunction.Name);
     155          var parent = n.Parent;
     156
     157          // remove SubFunctionTreeNode
     158          parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));
     159
     160          // add new tree
     161          var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
     162                                            .GetSubtree(0); // Offset
     163          parent.AddSubtree(subTree);
    138164        }
    139165      }
    140       */
    141       return 0.0;
     166      return templateTree;
    142167    }
    143168
Note: See TracChangeset for help on using the changeset viewer.